Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 7fafe2d

Browse files
Merge pull request #6 from seanfarley/smf/lint
linting cleanup and fix for new lsp-mode refactor
2 parents 62a9a84 + 7cf421b commit 7fafe2d

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

Cask

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(source gnu)
2+
(source melpa)
3+
4+
(package-file "lsp-python-ms.el")
5+
6+
(files "*.el")
7+
8+
(development
9+
(depends-on "lsp-mode")
10+
(depends-on "projectile"))

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ lsp-mode client leveraging microsoft's [[https://github.com/Microsoft/python-lan
3737
"~/python-language-server/output/bin/Release/osx-x64/publish/Microsoft.Python.LanguageServer"))
3838
#+END_SRC
3939

40+
For developement, you might find it useful to run =cask install=.
4041
* Credit
4142

4243
All credit to [[https://cpbotha.net][cpbotha]] on [[https://vxlabs.com/2018/11/19/configuring-emacs-lsp-mode-and-microsofts-visual-studio-code-python-language-server/][vxlabs]]! This just tidies and packages his

lsp-python-ms.el

+39-35
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
;; Author: Charl Botha
44
;; Maintainer: Andrew Christianson
55
;; Version: 0.1.0
6-
;; Package-Requires: (lsp-mode cl)
6+
;; Package-Requires: (cl-lib lsp-mode projectile python)
77
;; Homepage: https://git.sr.ht/~kristjansson/lsp-python-ms
88
;; Keywords: lsp python
99

@@ -29,42 +29,57 @@
2929
;; from https://vxlabs.com/2018/11/19/configuring-emacs-lsp-mode-and-microsofts-visual-studio-code-python-language-server/
3030

3131
;;; 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)
3339

3440
(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.")
3644

3745
(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.")
3949

4050
(defvar lsp-python-ms-executable
4151
(cond
4252
((executable-find "Microsoft.Python.LanguageServer"))
4353
((executable-find "Microsoft.Python.LanguageServer.exe"))
4454
(t nil))
45-
"Path to Microsoft.Python.LanguageServer.exe")
55+
"Path to Microsoft.Python.LanguageServer.exe.")
4656

4757
;; it's crucial that we send the correct Python version to MS PYLS,
4858
;; else it returns no docs in many cases furthermore, we send the
4959
;; current Python's (can be virtualenv) sys.path as searchPaths
5060

5161
(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."
5466
(let ((python (executable-find python-shell-interpreter))
5567
(init "from __future__ import print_function; import sys; import json;")
5668
(ver "print(\"%s.%s\" % (sys.version_info[0], sys.version_info[1]));")
5769
(sp (concat "sys.path.insert(0, '" workspace-root "'); print(json.dumps(sys.path))")))
5870
(with-temp-buffer
5971
(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))))
6173

6274
;; I based most of this on the vs.code implementation:
6375
;; https://github.com/Microsoft/vscode-python/blob/master/src/client/activation/languageServer/languageServer.ts#L219
6476
;; (it still took quite a while to get right, but here we are!)
6577
(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."
6681
(let ((workspace-root (if workspace (lsp--workspace-root workspace) (pwd))))
67-
(destructuring-bind (pyver pysyspath)
82+
(cl-destructuring-bind (pyver pysyspath)
6883
(lsp-python-ms--get-python-ver-and-syspath workspace-root)
6984
`(:interpreter
7085
(:properties (:InterpreterPath ,(executable-find python-shell-interpreter)
@@ -80,14 +95,6 @@ search paths."
8095
:maxDocumentationTextLength 0)
8196
:searchPaths ,(json-read-from-string pysyspath)))))
8297

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-
9198
(defun lsp-python-ms--workspace-root ()
9299
"Get the root using ffip or projectile, or just return `default-directory'."
93100
(cond
@@ -107,14 +114,15 @@ search paths."
107114
(setq rx (concat rx "\\|\r")))
108115
(replace-regexp-in-string rx " " str)))
109116

117+
(defun lsp-python-ms--language-server-started-callback (workspace _params)
118+
"Handle the python/languageServerStarted message.
110119
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."
113121
 (lsp-workspace-status "::Started" workspace)
114122
 (message "Python language server started"))
115123

116124
(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."
118126
 (lsp-client-on-notification client "python/languageServerStarted" 'lsp-python-ms--language-server-started-callback))
119127

120128
;; this gets called when we do lsp-describe-thing-at-point
@@ -132,14 +140,22 @@ search paths."
132140
:filter-return #'lsp-python-ms--filter-nbsp)
133141

134142
(defun lsp-python-ms--command-string ()
135-
"Return the command that starts the server."
143+
"Return the command to start the server."
136144
(if lsp-python-ms-executable
137145
lsp-python-ms-executable
138146
(list (lsp-python-ms--find-dotnet)
139147
(concat lsp-python-ms-dir "Microsoft.Python.LanguageServer.dll"))))
140148

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
143159
(lsp-define-stdio-client
144160
lsp-python "python"
145161
#'lsp-python-ms--workspace-root
@@ -148,18 +164,6 @@ search paths."
148164
:extra-init-params #'lsp-python-ms--extra-init-params
149165
:initialize #'lsp-python-ms--client-initialized))
150166

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-
163167
(provide 'lsp-python-ms)
164168

165169
;;; lsp-python-ms.el ends here

0 commit comments

Comments
 (0)