forked from emacs-lsp/lsp-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-json.el
138 lines (115 loc) · 5.35 KB
/
lsp-json.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
;;; lsp-json.el --- vscode-json-languageserver integration -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Kien Nguyen
;; Author: kien.n.quang at gmail.com
;; Keywords: lsp
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'lsp-mode)
(require 'ht)
(require 'url)
(require 'url-util)
(defgroup lsp-json nil
"LSP support for JSON, using vscode-json-languageserver."
:group 'lsp-mode
:link '(url-link "https://github.com/vscode-langservers/vscode-json-languageserver")
:package-version '(lsp-mode . "6.3"))
(defcustom lsp-json-schemas nil
"Associate schemas to JSON files in the current project"
:type '(repeat alist)
:group 'lsp-json
:package-version '(lsp-mode . "6.3"))
(defcustom lsp-json-format-enable t
"Enable/disable default JSON formatter"
:type 'boolean
:group 'lsp-json
:package-version '(lsp-mode . "6.3"))
(defcustom lsp-http-proxy nil
"The URL of the proxy server to use when fetching schema."
:type 'string
:group 'lsp-json
:package-version '(lsp-mode . "6.3"))
(defcustom lsp-http-proxyStrictSSL t
"The URL of the proxy server to use when fetching schema."
:type 'boolean
:group 'lsp-json
:package-version '(lsp-mode . "6.3"))
(lsp-register-custom-settings
'(("json.format.enable" lsp-json-format-enable t)
("json.schemas" lsp-json-schemas)
("http.proxy" lsp-http-proxy)
("http.proxyStrictSSL" lsp-http-proxyStrictSSL)))
(defvar lsp-json--extra-init-params
`(:handledSchemaProtocols ["file" "http" "https"]))
(defvar lsp-json--major-modes '(json-mode jsonc-mode))
(defvar lsp-json--schema-associations
(ht ("/*.css-data.json" ["https://raw.githubusercontent.com/Microsoft/vscode-css-languageservice/master/docs/customData.schema.json"])
("/package.json" ["http://json.schemastore.org/package"])
("/*.html-data.json" ["https://raw.githubusercontent.com/Microsoft/vscode-html-languageservice/master/docs/customData.schema.json"])
("/*.schema.json" ["http://json-schema.org/draft-07/schema#"])
("/bower.json" ["http://json.schemastore.org/bower"])
("/composer.json" ["http://json.schemastore.org/composer"])
("/tsconfig.json" ["http://json.schemastore.org/tsconfig"])
("/tsconfig.*.json" ["http://json.schemastore.org/tsconfig"])
("/typings.json" ["http://json.schemastore.org/typings"])
("/.bowerrc" ["http://json.schemastore.org/bowerrc"])
("/.babelrc" ["http://json.schemastore.org/babelrc"])
("/.babelrc.json" ["http://json.schemastore.org/babelrc"])
("/babel.config.json" ["http://json.schemastore.org/babelrc"])
("/jsconfig.json" ["http://json.schemastore.org/jsconfig"])
("/jsconfig.*.json" ["http://json.schemastore.org/jsconfig"])
("/project.json" ["http://json.schemastore.org/project"])
("/omnisharp.json" ["http://json.schemastore.org/omnisharp"]))
"Default json schemas.")
(defun lsp-json--get-content (_workspace uri callback)
"Get content from URI."
(ignore-errors
(url-retrieve uri (lambda (_status callback)
(goto-char (point-min))
(re-search-forward "\n\n" nil 'noerror)
(funcall
callback
(decode-coding-string (buffer-substring (point) (point-max))
'utf-8-unix)))
(list callback))))
(lsp-dependency 'vscode-json-languageserver
'(:system "vscode-json-languageserver")
'(:npm :package "vscode-json-languageserver"
:path "vscode-json-languageserver"))
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection (lambda ()
(list (lsp-package-path 'vscode-json-languageserver)
"--stdio")))
:major-modes lsp-json--major-modes
:server-id 'json-ls
:priority -1
:multi-root t
:completion-in-comments? t
:initialization-options lsp-json--extra-init-params
:async-request-handlers (ht ("vscode/content" #'lsp-json--get-content))
:initialized-fn (lambda (w)
(with-lsp-workspace w
(lsp--set-configuration (lsp-configuration-section "json"))
(lsp-notify "json/schemaAssociations" lsp-json--schema-associations)))
:download-server-fn (lambda (_client callback error-callback _update?)
(lsp-package-ensure 'vscode-json-languageserver callback error-callback))))
;; Compatibility
(with-eval-after-load 'company-lsp
(advice-add 'company-tng--supress-post-completion
:after-while
(lambda (&rest _)
(not (memq major-mode lsp-json--major-modes)))
'((name . --force-post-completion-for-json))))
(provide 'lsp-json)
;;; lsp-json.el ends here