3
3
; ; Author: Charl Botha
4
4
; ; Maintainer: Andrew Christianson
5
5
; ; Version: 0.1.0
6
- ; ; Package-Requires: (lsp-mode cl )
6
+ ; ; Package-Requires: (cl-lib lsp-mode projectile python )
7
7
; ; Homepage: https://git.sr.ht/~kristjansson/lsp-python-ms
8
8
; ; Keywords: lsp python
9
9
29
29
; ; from https://vxlabs.com/2018/11/19/configuring-emacs-lsp-mode-and-microsofts-visual-studio-code-python-language-server/
30
30
31
31
; ;; Code:
32
- (require 'cl )
32
+ (require 'cl-lib )
33
+ (require 'lsp-mode )
34
+ (require 'projectile )
35
+ (require 'python )
36
+
37
+ ; ; forward declare variable
38
+ (defvar lsp-render-markdown-markup-content )
33
39
34
40
(defvar lsp-python-ms-dir nil
35
- " Path to langeuage server directory containing Microsoft.Python.LanguageServer.dll" )
41
+ " Path to language server directory.
42
+
43
+ This is the directory containing Microsoft.Python.LanguageServer.dll." )
36
44
37
45
(defvar lsp-python-ms-dotnet nil
38
- " Full path to dotnet executable. You only need to set this if dotnet is not on your path." )
46
+ " Full path to dotnet executable.
47
+
48
+ You only need to set this if dotnet is not on your path." )
39
49
40
50
(defvar lsp-python-ms-executable
41
51
(cond
42
52
((executable-find " Microsoft.Python.LanguageServer" ))
43
53
((executable-find " Microsoft.Python.LanguageServer.exe" ))
44
54
(t nil ))
45
- " Path to Microsoft.Python.LanguageServer.exe" )
55
+ " Path to Microsoft.Python.LanguageServer.exe. " )
46
56
47
57
; ; it's crucial that we send the correct Python version to MS PYLS,
48
58
; ; else it returns no docs in many cases furthermore, we send the
49
59
; ; current Python's (can be virtualenv) sys.path as searchPaths
50
60
51
61
(defun lsp-python-ms--get-python-ver-and-syspath (workspace-root )
52
- " Return list with pyver-string and json-encoded list of python
53
- search paths."
62
+ " Return list with pyver-string and list of python search paths.
63
+
64
+ The WORKSPACE-ROOT will be prepended to the list of python search
65
+ paths and then the entire list will be json-encoded."
54
66
(let ((python (executable-find python-shell-interpreter))
55
67
(init " from __future__ import print_function; import sys; import json;" )
56
68
(ver " print(\" %s.%s\" % (sys.version_info[0], sys.version_info[1]));" )
57
69
(sp (concat " sys.path.insert(0, '" workspace-root " '); print(json.dumps(sys.path))" )))
58
70
(with-temp-buffer
59
71
(call-process python nil t nil " -c" (concat init ver sp))
60
- (subseq (split-string (buffer-string ) " \n " ) 0 2 ))))
72
+ (cl- subseq (split-string (buffer-string ) " \n " ) 0 2 ))))
61
73
62
74
; ; I based most of this on the vs.code implementation:
63
75
; ; https://github.com/Microsoft/vscode-python/blob/master/src/client/activation/languageServer/languageServer.ts#L219
64
76
; ; (it still took quite a while to get right, but here we are!)
65
77
(defun lsp-python-ms--extra-init-params (&optional workspace )
78
+ " Return extra initialization params.
79
+
80
+ Optionally add the WORKSPACE to the python search list."
66
81
(let ((workspace-root (if workspace (lsp--workspace-root workspace) (pwd ))))
67
- (destructuring-bind (pyver pysyspath)
82
+ (cl- destructuring-bind (pyver pysyspath)
68
83
(lsp-python-ms--get-python-ver-and-syspath workspace-root)
69
84
`(:interpreter
70
85
(:properties (:InterpreterPath ,(executable-find python-shell-interpreter)
@@ -80,14 +95,6 @@ search paths."
80
95
:maxDocumentationTextLength 0 )
81
96
:searchPaths ,(json-read-from-string pysyspath)))))
82
97
83
- (defun lsp-python-ms--client-initialized (client )
84
- " Callback for client initialized."
85
- (lsp-client-on-notification client " python/languageServerStarted" 'lsp-python-ms--language-server-started ))
86
-
87
- (defun lsp-python-ms--language-server-started (workspace params )
88
- " Callback for server started initialized."
89
- (message " [MS Python language server started] " ))
90
-
91
98
(defun lsp-python-ms--workspace-root ()
92
99
" Get the root using ffip or projectile, or just return `default-directory' ."
93
100
(cond
@@ -107,14 +114,15 @@ search paths."
107
114
(setq rx (concat rx " \\ |\r " )))
108
115
(replace-regexp-in-string rx " " str)))
109
116
117
+ (defun lsp-python-ms--language-server-started-callback (workspace _params )
118
+ " Handle the python/languageServerStarted message.
110
119
111
- (defun lsp-python-ms--language-server-started-callback (workspace params )
112
- " Handle the python/languageServerStarted message"
120
+ WORKSPACE is just used for logging and _PARAMS is unused."
113
121
(lsp-workspace-status " ::Started" workspace)
114
122
(message " Python language server started " ))
115
123
116
124
(defun lsp-python-ms--client-initialized (client )
117
- " Callback to register and configure the client after it's initialized"
125
+ " Callback to register and configure the CLIENT after it's initialized. "
118
126
(lsp-client-on-notification client " python/languageServerStarted" 'lsp-python-ms--language-server-started-callback ))
119
127
120
128
; ; this gets called when we do lsp-describe-thing-at-point
@@ -132,14 +140,22 @@ search paths."
132
140
:filter-return #'lsp-python-ms--filter-nbsp )
133
141
134
142
(defun lsp-python-ms--command-string ()
135
- " Return the command that starts the server."
143
+ " Return the command to start the server."
136
144
(if lsp-python-ms-executable
137
145
lsp-python-ms-executable
138
146
(list (lsp-python-ms--find-dotnet)
139
147
(concat lsp-python-ms-dir " Microsoft.Python.LanguageServer.dll" ))))
140
148
141
- ; ;; Old lsp-mode
142
- (unless (fboundp 'lsp-register-client )
149
+ (if (fboundp 'lsp-register-client )
150
+ ; ; New lsp-mode
151
+ (lsp-register-client
152
+ (make-lsp-client
153
+ :new-connection (lsp-stdio-connection 'lsp-python-ms--command-string )
154
+ :major-modes '(python-mode )
155
+ :server-id 'mspyls
156
+ :initialization-options 'lsp-python-ms--extra-init-params
157
+ :notification-handlers (lsp-ht (" python/languageServerStarted" 'lsp-python-ms--language-server-started-callback ))))
158
+ ; ; Old lsp-mode
143
159
(lsp-define-stdio-client
144
160
lsp-python " python"
145
161
#'lsp-python-ms--workspace-root
@@ -148,18 +164,6 @@ search paths."
148
164
:extra-init-params #'lsp-python-ms--extra-init-params
149
165
:initialize #'lsp-python-ms--client-initialized ))
150
166
151
- ; ;; New lsp-mode
152
- (when (fboundp 'lsp-register-client )
153
- (lsp-register-client
154
- (make-lsp-client
155
- :new-connection (lsp-stdio-connection 'lsp-python-ms--command-string )
156
- :major-modes '(python-mode )
157
- :server-id 'mspyls
158
- :initialization-options 'lsp-python-ms--extra-init-params
159
- :notification-handlers (lsp-ht (" python/languageServerStarted" 'lsp-python-ms--language-server-started ))
160
- )))
161
-
162
-
163
167
(provide 'lsp-python-ms )
164
168
165
169
; ;; lsp-python-ms.el ends here
0 commit comments