-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlsp-volar.el
138 lines (123 loc) · 6.55 KB
/
lsp-volar.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-volar.el --- A lsp-mode client for Vue3 -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021 JadeStrong
;;
;; Author: JadeStrong <https://github.com/jadestrong>
;; Maintainer: JadeStrong <jadestrong@163.com>
;; Created: November 08, 2021
;; Modified: November 08, 2021
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/jadestrong/lsp-volar
;; Package-Requires: ((emacs "25.1"))
;;
;; This file is not part of GNU Emacs.
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; provide the connection to lsp-mode and volar language server
;;
;;; Code:
(require 'lsp-mode)
(require 'json)
(defgroup lsp-volar nil
"Lsp support for vue3."
:group 'lsp-volar
:link '(url-link "https://github.com/johnsoncodehk/volar")
:package-version '(lsp-mode . "8.0.1"))
(defcustom lsp-volar-take-over-mode t
"Enable Take Over Mode."
:type 'boolean
:group 'lsp-volar
:package-version '(lsp-mode . "8.0.1"))
(defcustom lsp-volar-activate-file ".volarrc"
"A file with a custom name placed in WORKSPACE-ROOT is used to force enable
volar when there is no package.json in the WORKSPACE-ROOT."
:type 'string
:group 'lsp-volar
:package-version '(lsp-mode . "8.0.1"))
(defconst lsp-volar--is-windows (memq system-type '(cygwin windows-nt ms-dos)))
(defun lsp-volar-get-typescript-server-path ()
"Get tsserver.js file path."
(if-let ((package-path (lsp-package-path 'typescript))
(system-server-path (apply #'f-join (if lsp-volar--is-windows
(append (cl-subseq (f-split (file-truename (lsp-package-path 'typescript))) 0 -1) '("node_modules" "typescript" "lib"))
(append (cl-subseq (f-split (file-truename (lsp-package-path 'typescript))) 0 -2) '("lib")))))
(is-exist (f-file-p system-server-path)))
system-server-path
(progn (lsp--error "[lsp-volar] Typescript is not detected correctly. Please ensure the npm package typescript is installed in your project or system (npm install -g typescript), otherwise open an issue") "")))
(lsp-dependency 'typescript
'(:system "tsserver")
'(:npm :package "typescript"
:path "tsserver"))
(lsp-dependency 'volar-language-server
'(:system "vue-language-server")
'(:npm :package "@volar/vue-language-server" :path "vue-language-server"))
(lsp-register-custom-settings
'(("typescript.tsdk" (lambda () (if-let ((project-root (lsp-workspace-root))
(server-path (f-join project-root "node_modules/typescript/lib"))
(is-exist (file-exists-p server-path)))
server-path
(lsp-volar-get-typescript-server-path))) t)))
(defun lsp-volar--vue-project-p (workspace-root)
"Check if the `Vue' package is present in the package.json file
in the WORKSPACE-ROOT."
(if-let ((package-json (f-join workspace-root "package.json"))
(exist (f-file-p package-json))
(config (json-read-file package-json))
(dependencies (alist-get 'dependencies config)))
(alist-get 'vue dependencies)
nil))
(defun lsp-volar--activate-p (filename &optional _)
"Check if the volar-language-server should be enabled base on FILENAME."
(if lsp-volar-take-over-mode
(and (or
(and (lsp-workspace-root) (lsp-volar--vue-project-p (lsp-workspace-root)))
(and (lsp-workspace-root) lsp-volar-activate-file (f-file-p (f-join (lsp-workspace-root) lsp-volar-activate-file))))
(or (or (string-match-p "\\.mjs\\|\\.[jt]sx?\\'" filename)
(and (derived-mode-p 'js-mode 'typescript-mode)
(not (derived-mode-p 'json-mode))))
(string= (file-name-extension filename) "vue")))
(string= (file-name-extension filename) "vue")))
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection
(lambda ()
`(,(lsp-package-path 'volar-language-server) "--stdio")))
:activation-fn 'lsp-volar--activate-p
:priority 0
:multi-root nil
:server-id 'vue-semantic-server
:initialization-options (lambda () (ht-merge (lsp-configuration-section "typescript")
(ht ("serverMode" 0)
("diagnosticMode" 1)
("textDocumentSync" 2))))
:initialized-fn (lambda (workspace)
(with-lsp-workspace workspace
(lsp--server-register-capability
(lsp-make-registration
:id "random-id"
:method "workspace/didChangeWatchedFiles"
:register-options? (lsp-make-did-change-watched-files-registration-options
:watchers
`[,(lsp-make-file-system-watcher :glob-pattern "**/*.js")
,(lsp-make-file-system-watcher :glob-pattern "**/*.ts")
,(lsp-make-file-system-watcher :glob-pattern "**/*.vue")
,(lsp-make-file-system-watcher :glob-pattern "**/*.jsx")
,(lsp-make-file-system-watcher :glob-pattern "**/*.tsx")
,(lsp-make-file-system-watcher :glob-pattern "**/*.json")])))))
:download-server-fn (lambda (_client callback error-callback _update?)
(lsp-package-ensure 'volar-language-server
callback error-callback))))
(provide 'lsp-volar)
;;; lsp-volar.el ends here