config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

lsp-yaml.el (9856B)


      1 ;;; lsp-yaml.el --- LSP YAML server integration        -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2019  Aya Igarashi
      4 
      5 ;; Author: Aya Igarashi <ladiclexxx@gmail.com>
      6 ;; Keywords:
      7 
      8 ;; This program is free software; you can redistribute it and/or modify
      9 ;; it under the terms of the GNU General Public License as published by
     10 ;; the Free Software Foundation, either version 3 of the License, or
     11 ;; (at your option) any later version.
     12 
     13 ;; This program is distributed in the hope that it will be useful,
     14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 ;; GNU General Public License for more details.
     17 
     18 ;; You should have received a copy of the GNU General Public License
     19 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     20 
     21 ;;; Commentary:
     22 
     23 ;;
     24 
     25 ;;; Code:
     26 
     27 (require 'lsp-mode)
     28 (require 'dash)
     29 
     30 (defgroup lsp-yaml nil
     31   "LSP support for YAML, using yaml-language-server."
     32   :group 'lsp-mode
     33   :link '(url-link "https://github.com/redhat-developer/yaml-language-server")
     34   :package-version '(lsp-mode . "6.2"))
     35 
     36 (defcustom lsp-yaml-format-enable t
     37   "Enable/disable default YAML formatter."
     38   :type 'boolean
     39   :group 'lsp-yaml
     40   :package-version '(lsp-mode . "6.2"))
     41 
     42 (defcustom lsp-yaml-single-quote nil
     43   "Use single quote instead of double quotes."
     44   :type 'boolean
     45   :group 'lsp-yaml
     46   :package-version '(lsp-mode . "6.2"))
     47 
     48 (defcustom lsp-yaml-bracket-spacing t
     49   "Print spaces between brackets in objects."
     50   :type 'boolean
     51   :group 'lsp-yaml
     52   :package-version '(lsp-mode . "6.2"))
     53 
     54 (defcustom lsp-yaml-prose-wrap "preserve"
     55   "Options for prose-wrap.
     56    Always: wrap prose if it exceeds the print width.
     57    Never: never wrap the prose.
     58    Preserve: wrap prose as-is."
     59   :type '(choice
     60           (const "always")
     61           (const "never")
     62           (const "preserve"))
     63   :group 'lsp-yaml
     64   :package-version '(lsp-mode . "6.2"))
     65 
     66 (defcustom lsp-yaml-print-width 80
     67   "Specify the line length that the printer will wrap on."
     68   :type 'number
     69   :group 'lsp-yaml
     70   :package-version '(lsp-mode . "6.2"))
     71 
     72 (defcustom lsp-yaml-validate t
     73   "Enable/disable validation feature."
     74   :type 'boolean
     75   :group 'lsp-yaml
     76   :package-version '(lsp-mode . "6.2"))
     77 
     78 (defcustom lsp-yaml-hover t
     79   "Enable/disable hover feature."
     80   :type 'boolean
     81   :group 'lsp-yaml
     82   :package-version '(lsp-mode . "6.2"))
     83 
     84 (defcustom lsp-yaml-completion t
     85   "Enable/disable completion feature."
     86   :type 'boolean
     87   :group 'lsp-yaml
     88   :package-version '(lsp-mode . "6.2"))
     89 
     90 (defcustom lsp-yaml-schemas '()
     91   "Associate schemas to YAML files in a glob pattern."
     92   :type '(alist :key-type (symbol :tag "schema") :value-type (lsp-string-vector :tag "files (glob)"))
     93   :group 'lsp-yaml
     94   :package-version '(lsp-mode . "6.2"))
     95 
     96 (defcustom lsp-yaml-schema-store-enable t
     97   "Enable/disable JSON Schema store. When set to true, available YAML
     98    schemas will be automatically pulled from the store."
     99   :type 'boolean
    100   :group 'lsp-yaml
    101   :package-version '(lsp-mode . "6.2"))
    102 
    103 (defcustom lsp-yaml-custom-tags nil
    104   "Custom tags for the parser to use."
    105   :type '(lsp-repeatable-vector string)
    106   :group 'lsp-yaml
    107   :package-version '(lsp-mode . "6.2"))
    108 
    109 (defcustom lsp-yaml-schema-store-uri "https://www.schemastore.org/api/json/catalog.json"
    110   "URL of schema store catalog to use."
    111   :type 'string
    112   :group 'lsp-yaml)
    113 
    114 (defcustom lsp-yaml-schema-store-local-db (expand-file-name
    115                                            (locate-user-emacs-file
    116                                             (f-join ".cache" "lsp" "lsp-yaml-schemas.json")))
    117   "Cached database of schema store."
    118   :type 'file
    119   :group 'lsp-yaml)
    120 
    121 (defcustom lsp-yaml-max-items-computed 5000
    122   "The maximum number of outline symbols and folding regions computed.
    123 Limited for performance reasons."
    124   :type 'number
    125   :group 'lsp-yaml
    126   :package-version '(lsp-mode . "8.0.0"))
    127 
    128 
    129 (defvar lsp-yaml--schema-store-schemas-alist nil
    130   "A list of schemas fetched from schema stores.")
    131 
    132 (lsp-register-custom-settings
    133  '(("yaml.format.enable" lsp-yaml-format-enable t)
    134    ("yaml.format.singleQuote" lsp-yaml-single-quote t)
    135    ("yaml.format.bracketSpacing" lsp-yaml-bracket-spacing)
    136    ("yaml.format.proseWrap" lsp-yaml-prose-wrap)
    137    ("yaml.format.printWidth" lsp-yaml-print-width)
    138    ("yaml.validate" lsp-yaml-validate t)
    139    ("yaml.hover" lsp-yaml-hover t)
    140    ("yaml.completion" lsp-yaml-completion t)
    141    ("yaml.schemas" lsp-yaml-schemas)
    142    ("yaml.schemaStore.enable" lsp-yaml-schema-store-enable t)
    143    ("yaml.schemaStore.url" lsp-yaml-schema-store-uri)
    144    ("yaml.customTags" lsp-yaml-custom-tags)
    145    ("yaml.maxItemsComputed" lsp-yaml-max-items-computed)))
    146 
    147 (defcustom lsp-yaml-server-command '("yaml-language-server" "--stdio")
    148   "Command to start yaml-languageserver."
    149   :type '(repeat string)
    150   :group 'lsp-yaml
    151   :package-version '(lsp-mode . "6.2"))
    152 
    153 (lsp-dependency 'yaml-language-server
    154                 '(:system "yaml-language-server")
    155                 '(:npm :package "yaml-language-server"
    156                        :path "yaml-language-server"))
    157 
    158 (lsp-register-client
    159  (make-lsp-client :new-connection (lsp-stdio-connection
    160                                    (lambda ()
    161                                      `(,(or (executable-find (cl-first lsp-yaml-server-command))
    162                                             (lsp-package-path 'yaml-language-server))
    163                                        ,@(cl-rest lsp-yaml-server-command))))
    164                   :activation-fn (lsp-activate-on "yaml")
    165                   :priority 0
    166                   :server-id 'yamlls
    167                   :initialized-fn (lambda (workspace)
    168                                     (with-lsp-workspace workspace
    169                                       (lsp--set-configuration
    170                                        (lsp-configuration-section "yaml"))))
    171                   :download-server-fn (lambda (_client callback error-callback _update?)
    172                                         (lsp-package-ensure 'yaml-language-server
    173                                                             callback error-callback))))
    174 
    175 (defcustom lsp-yaml-schema-extensions '(((name . "Kubernetes v1.30.3")
    176                                           (description . "Kubernetes v1.30.3 manifest schema definition")
    177                                           (url . "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.30.3-standalone-strict/all.json")
    178                                           (fileMatch . ["*-k8s.yaml" "*-k8s.yml"])))
    179   "User defined schemas that extend default schema store.
    180 Used in `lsp-yaml--get-supported-schemas' to supplement schemas provided by
    181 `lsp-yaml-schema-store-uri'."
    182   :type  'list
    183   :group 'lsp-yaml
    184   :package-version '(lsp-mode . "9.0.1"))
    185 
    186 (defun lsp-yaml-download-schema-store-db (&optional force-downloading)
    187   "Download remote schema store at `lsp-yaml-schema-store-uri' into local cache.
    188 Set FORCE-DOWNLOADING to non-nil to force re-download the database."
    189   (interactive "P")
    190   (when (or force-downloading (not (file-exists-p lsp-yaml-schema-store-local-db)))
    191     (unless (file-directory-p (file-name-directory lsp-yaml-schema-store-local-db))
    192       (mkdir (file-name-directory lsp-yaml-schema-store-local-db) t))
    193     (url-copy-file lsp-yaml-schema-store-uri lsp-yaml-schema-store-local-db force-downloading)))
    194 
    195 (defun lsp-yaml--get-supported-schemas ()
    196   "Get out the list of supported schemas."
    197   (when (and lsp-yaml-schema-store-enable
    198              (not lsp-yaml--schema-store-schemas-alist))
    199     (lsp-yaml-download-schema-store-db)
    200     (setq lsp-yaml--schema-store-schemas-alist
    201           (alist-get 'schemas (json-read-file lsp-yaml-schema-store-local-db))))
    202   (seq-concatenate 'list lsp-yaml-schema-extensions lsp-yaml--schema-store-schemas-alist))
    203 
    204 (defun lsp-yaml-set-buffer-schema (uri-string)
    205   "Set yaml schema for the current buffer to URI-STRING."
    206   (interactive "MURI: ")
    207   (let* ((uri (intern uri-string))
    208          (workspace-path (file-relative-name
    209                           (lsp--uri-to-path (lsp--buffer-uri))
    210                           (lsp-workspace-root (lsp--buffer-uri))))
    211          (glob (concat "/" workspace-path))
    212          (current-config (assoc uri lsp-yaml-schemas))
    213          (current-patterns (and current-config (cdr current-config))))
    214     (if current-config
    215         (or (member glob (append current-patterns nil))
    216             (setq lsp-yaml-schemas
    217                   (cl-acons uri
    218                             (vconcat (vector glob) current-patterns)
    219                             (assq-delete-all uri
    220                                              (mapcar (lambda (x) (lsp-yaml--remove-glob x glob))
    221                                                      lsp-yaml-schemas)))))
    222       (setq lsp-yaml-schemas
    223             (cl-acons uri (vector glob) (mapcar (lambda (x) (lsp-yaml--remove-glob x glob))
    224                                                 lsp-yaml-schemas))))
    225     (lsp--set-configuration (lsp-configuration-section "yaml"))))
    226 
    227 (defun lsp-yaml-select-buffer-schema ()
    228   "Select schema for the current buffer based on the list of supported schemas."
    229   (interactive)
    230   (let* ((schema (lsp--completing-read "Select buffer schema: "
    231                                        (lsp-yaml--get-supported-schemas)
    232                                        (lambda (schema)
    233                                          (format "%s: %s" (alist-get 'name schema)(alist-get 'description schema)))
    234                                        nil t))
    235          (uri (alist-get 'url schema)))
    236     (lsp-yaml-set-buffer-schema uri)))
    237 
    238 (defun lsp-yaml--remove-glob (mapping glob)
    239   (let ((patterns (cdr mapping)))
    240     (cons (car mapping)
    241           (vconcat (-filter (lambda (p) (not (equal p glob)))
    242                             (append patterns nil)) nil))))
    243 
    244 (lsp-consistency-check lsp-yaml)
    245 
    246 (provide 'lsp-yaml)
    247 ;;; lsp-yaml.el ends here