config

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

lsp-json.el (5223B)


      1 ;;; lsp-json.el ---  vscode-json-languageserver integration -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2019  Kien Nguyen
      4 
      5 ;; Author: kien.n.quang at gmail.com
      6 ;; Keywords: lsp
      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 'ht)
     29 (require 'url)
     30 (require 'url-util)
     31 
     32 (defgroup lsp-json nil
     33   "LSP support for JSON, using vscode's built-in language server."
     34   :group 'lsp-mode
     35   :link '(url-link "https://github.com/microsoft/vscode/tree/main/extensions/json-language-features/server")
     36   :package-version '(lsp-mode . "6.3"))
     37 
     38 (defcustom lsp-json-schemas nil
     39   "Associate schemas to JSON files in the current project example can be found
     40 here, https://github.com/emacs-lsp/lsp-mode/issues/3368#issuecomment-1049635155."
     41   :type '(repeat alist)
     42   :group 'lsp-json
     43   :package-version '(lsp-mode . "6.3"))
     44 
     45 (defcustom lsp-http-proxy nil
     46   "The URL of the proxy server to use when fetching schema."
     47   :type 'string
     48   :group 'lsp-json
     49   :package-version '(lsp-mode . "6.3"))
     50 
     51 (defcustom lsp-http-proxyStrictSSL t
     52   "The URL of the proxy server to use when fetching schema."
     53   :type 'boolean
     54   :group 'lsp-json
     55   :package-version '(lsp-mode . "6.3"))
     56 
     57 (lsp-register-custom-settings
     58  '(("json.schemas" lsp-json-schemas)
     59    ("http.proxy" lsp-http-proxy)
     60    ("http.proxyStrictSSL" lsp-http-proxyStrictSSL)))
     61 
     62 (defvar lsp-json--extra-init-params
     63   `( :provideFormatter t
     64      :handledSchemaProtocols ["file" "http" "https"]))
     65 
     66 (defvar lsp-json--schema-associations
     67   `( :/*.css-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-css-languageservice/master/docs/customData.schema.json"]
     68      :/package.json ["http://json.schemastore.org/package"]
     69      :/*.html-data.json ["https://raw.githubusercontent.com/Microsoft/vscode-html-languageservice/master/docs/customData.schema.json"]
     70      :/*.schema.json ["http://json-schema.org/draft-07/schema#"]
     71      :/bower.json ["http://json.schemastore.org/bower"]
     72      :/composer.json ["http://json.schemastore.org/composer"]
     73      :/tsconfig.json ["http://json.schemastore.org/tsconfig"]
     74      :/tsconfig.*.json ["http://json.schemastore.org/tsconfig"]
     75      :/typings.json ["http://json.schemastore.org/typings"]
     76      :/.bowerrc ["http://json.schemastore.org/bowerrc"]
     77      :/.babelrc ["http://json.schemastore.org/babelrc"]
     78      :/.babelrc.json ["http://json.schemastore.org/babelrc"]
     79      :/babel.config.json ["http://json.schemastore.org/babelrc"]
     80      :/jsconfig.json ["http://json.schemastore.org/jsconfig"]
     81      :/jsconfig.*.json ["http://json.schemastore.org/jsconfig"]
     82      :/project.json ["http://json.schemastore.org/project"]
     83      :/omnisharp.json ["http://json.schemastore.org/omnisharp"]
     84      :/.eslintrc.json ["http://json.schemastore.org/eslintrc"]
     85      :/.eslintrc ["http://json.schemastore.org/eslintrc"])
     86   "Default json schemas.")
     87 
     88 (defun lsp-json--get-content (_workspace uri callback)
     89   "Get content from URI."
     90   (ignore-errors
     91     (url-retrieve uri
     92                   (lambda (_status callback)
     93                     (goto-char (point-min))
     94                     (re-search-forward "\n\n" nil 'noerror)
     95                     (funcall
     96                      callback
     97                      (decode-coding-string (buffer-substring (point) (point-max))
     98                                            'utf-8-unix)))
     99                   (list callback))))
    100 
    101 (lsp-dependency 'vscode-json-languageserver
    102                 '(:system "vscode-json-language-server")
    103                 ;; The binary shipped from microsoft/vscode
    104                 '(:system "vscode-json-languageserver")
    105                 '(:npm :package "vscode-langservers-extracted"
    106                        :path "vscode-json-language-server"))
    107 
    108 (lsp-register-client
    109  (make-lsp-client
    110   :new-connection
    111   (lsp-stdio-connection
    112    (lambda () (list (lsp-package-path 'vscode-json-languageserver) "--stdio")))
    113   :activation-fn (lsp-activate-on "json" "jsonc")
    114   :server-id 'json-ls
    115   :priority 0
    116   :multi-root t
    117   :completion-in-comments? t
    118   :initialization-options lsp-json--extra-init-params
    119   :async-request-handlers (ht ("vscode/content" #'lsp-json--get-content))
    120   :initialized-fn
    121   (lambda (w)
    122     (with-lsp-workspace w
    123       (lsp--set-configuration
    124        (ht-merge (lsp-configuration-section "json")
    125                  (lsp-configuration-section "http")))
    126       (lsp-notify "json/schemaAssociations" lsp-json--schema-associations)))
    127   :download-server-fn
    128   (lambda (_client callback error-callback _update?)
    129     (lsp-package-ensure 'vscode-json-languageserver callback error-callback))))
    130 
    131 (lsp-consistency-check lsp-json)
    132 
    133 (provide 'lsp-json)
    134 ;;; lsp-json.el ends here