Skip to content

Commit

Permalink
support ruff-lsp and ruff for flycheck
Browse files Browse the repository at this point in the history
  • Loading branch information
wyuenho committed Jun 8, 2024
1 parent 494a61a commit d832a08
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
26 changes: 22 additions & 4 deletions pet.el
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ Selects a virtualenv in the follow order:
(defvar flycheck-python-mypy-executable)
(defvar flycheck-python-pyright-executable)
(defvar flycheck-python-pycompile-executable)
(defvar flycheck-python-ruff-executable)

(defun pet-flycheck-python-pylint-find-pylintrc ()
"Polyfill `flycheck-pylintrc'.
Expand Down Expand Up @@ -717,15 +718,17 @@ default otherwise."
(setq-local flycheck-python-mypy-executable (pet-executable-find "mypy"))
(setq-local flycheck-python-mypy-python-executable (pet-executable-find "python"))
(setq-local flycheck-python-pyright-executable (pet-executable-find "pyright"))
(setq-local flycheck-python-pycompile-executable python-shell-interpreter)))
(setq-local flycheck-python-pycompile-executable python-shell-interpreter)
(setq-local flycheck-python-ruff-executable (pet-executable-find "ruff"))))
(kill-local-variable 'flycheck-python-mypy-config)
(kill-local-variable 'flycheck-pylintrc)
(kill-local-variable 'flycheck-python-flake8-executable)
(kill-local-variable 'flycheck-python-pylint-executable)
(kill-local-variable 'flycheck-python-mypy-executable)
(kill-local-variable 'flycheck-python-mypy-python-executable)
(kill-local-variable 'flycheck-python-pyright-executable)
(kill-local-variable 'flycheck-python-pycompile-executable)))
(kill-local-variable 'flycheck-python-pycompile-executable)
(kill-local-variable 'flycheck-python-ruff-executable)))

;;;###autoload
(defun pet-flycheck-setup ()
Expand All @@ -745,7 +748,8 @@ default otherwise."
(kill-local-variable 'flycheck-python-mypy-executable)
(kill-local-variable 'flycheck-python-mypy-python-executable)
(kill-local-variable 'flycheck-python-pyright-executable)
(kill-local-variable 'flycheck-python-pycompile-executable))
(kill-local-variable 'flycheck-python-pycompile-executable)
(kill-local-variable 'flycheck-python-ruff-executable))



Expand All @@ -762,7 +766,7 @@ default otherwise."
FN is `eglot--executable-find', ARGS is the arguments to
`eglot--executable-find'."
(pcase-let ((`(,command . ,_) args))
(if (member command '("pylsp" "pyls" "pyright-langserver" "jedi-language-server"))
(if (member command '("pylsp" "pyls" "pyright-langserver" "jedi-language-server" "ruff-lsp"))
(pet-executable-find command)
(apply fn args))))

Expand Down Expand Up @@ -811,6 +815,12 @@ COMMAND is the name of the Python language server command."
:workspace
(:environmentPath
,(pet-executable-find "python")))))
((string-match-p "ruff-lsp" command)
`(:settings
(:interpreter
,(pet-executable-find "python")
:path
,(pet-executable-find "ruff"))))
(t nil)))

(defalias 'pet--proper-list-p 'proper-list-p)
Expand Down Expand Up @@ -903,6 +913,8 @@ FN is `eglot--guess-contact', ARGS is the arguments to
(defvar lsp-pylsp-plugins-jedi-environment)
(defvar lsp-pyright-python-executable-cmd)
(defvar lsp-pyright-venv-path)
(defvar lsp-ruff-lsp-ruff-path)
(defvar lsp-ruff-lsp-python-path)
(defvar dap-python-executable)
(defvar python-pytest-executable)
(defvar python-black-command)
Expand All @@ -927,6 +939,8 @@ buffer local values."
(setq-local lsp-pylsp-plugins-jedi-environment python-shell-virtualenv-root)
(setq-local lsp-pyright-venv-path python-shell-virtualenv-root)
(setq-local lsp-pyright-python-executable-cmd python-shell-interpreter)
(setq-local lsp-ruff-lsp-ruff-path (pet-executable-find "ruff"))
(setq-local lsp-ruff-lsp-python-path python-shell-interpreter)
(setq-local dap-python-executable python-shell-interpreter)
(setq-local python-pytest-executable (pet-executable-find "pytest"))
(setq-local python-black-command (pet-executable-find "black"))
Expand All @@ -950,6 +964,8 @@ buffer local values."
(kill-local-variable 'lsp-pylsp-plugins-jedi-environment)
(kill-local-variable 'lsp-pyright-venv-path)
(kill-local-variable 'lsp-pyright-python-executable-cmd)
(kill-local-variable 'lsp-ruff-lsp-ruff-path)
(kill-local-variable 'lsp-ruff-lsp-python-path)
(kill-local-variable 'dap-python-executable)
(kill-local-variable 'python-pytest-executable)
(kill-local-variable 'python-black-command)
Expand Down Expand Up @@ -996,6 +1012,8 @@ has assigned to."
lsp-pylsp-plugins-jedi-environment
lsp-pyright-python-executable-cmd
lsp-pyright-venv-path
lsp-ruff-lsp-ruff-path
lsp-ruff-lsp-python-path
dap-python-executable
python-pytest-executable
python-black-command
Expand Down
61 changes: 47 additions & 14 deletions test/pet-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,8 @@
("pylint" "/home/user/project/.venv/bin/pylint")
("mypy" "/home/user/project/.venv/bin/mypy")
("python" "/home/user/project/.venv/bin/python")
("pyright" "/home/user/project/.venv/bin/pyright"))))
("pyright" "/home/user/project/.venv/bin/pyright")
("ruff" "/home/user/project/.venv/bin/ruff"))))
(spy-on 'derived-mode-p :and-return-value t)
(pet-flycheck-toggle-local-vars)
(expect flycheck-python-mypy-config :to-equal `("mypy.ini" ".mypy.ini" "pyproject.toml" "setup.cfg" "/home/user/.config/mypy/config"))
Expand All @@ -900,7 +901,8 @@
(expect flycheck-python-mypy-executable :to-equal "/home/user/project/.venv/bin/mypy")
(expect flycheck-python-mypy-python-executable :to-equal "/home/user/project/.venv/bin/python")
(expect flycheck-python-pyright-executable :to-equal "/home/user/project/.venv/bin/pyright")
(expect flycheck-python-pycompile-executable :to-equal python-shell-interpreter))
(expect flycheck-python-pycompile-executable :to-equal python-shell-interpreter)
(expect flycheck-python-ruff-executable :to-equal "/home/user/project/.venv/bin/ruff"))

(it "should reset `flycheck' Python checkers variables to default when `flycheck-mode' is nil"
(spy-on 'pet-flycheck-python-pylint-find-pylintrc :and-return-value "/etc/pylintrc")
Expand All @@ -910,7 +912,8 @@
("pylint" "/home/user/project/.venv/bin/pylint")
("mypy" "/home/user/project/.venv/bin/mypy")
("python" "/home/user/project/.venv/bin/python")
("pyright" "/home/user/project/.venv/bin/pyright"))))
("pyright" "/home/user/project/.venv/bin/pyright")
("ruff" "/home/user/project/.venv/bin/ruff"))))
(spy-on 'derived-mode-p :and-return-value t)
(pet-flycheck-toggle-local-vars)
(setq-local flycheck-mode nil)
Expand All @@ -923,6 +926,7 @@
(expect (local-variable-p 'flycheck-python-mypy-python-executable) :to-be nil)
(expect (local-variable-p 'flycheck-python-pyright-executable) :to-be nil)
(expect (local-variable-p 'flycheck-python-pycompile-executable) :to-be nil)
(expect (local-variable-p 'flycheck-python-ruff-executable) :to-be nil)

(kill-local-variable 'flycheck-mode)))

Expand Down Expand Up @@ -965,7 +969,8 @@
(expect (local-variable-p 'flycheck-python-pylint-executable) :not :to-be-truthy)
(expect (local-variable-p 'flycheck-python-mypy-executable) :not :to-be-truthy)
(expect (local-variable-p 'flycheck-python-pyright-executable) :not :to-be-truthy)
(expect (local-variable-p 'flycheck-python-pycompile-executable) :not :to-be-truthy)))
(expect (local-variable-p 'flycheck-python-pycompile-executable) :not :to-be-truthy)
(expect (local-variable-p 'flycheck-python-ruff-executable) :not :to-be-truthy)))

(describe "pet-eglot--executable-find-advice"
(it "should delegate to `pet-executable-find' for Python LSP servers"
Expand All @@ -988,6 +993,10 @@
(expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "jedi-language-server")
(expect 'eglot--executable-find :not :to-have-been-called)

(expect (pet-eglot--executable-find-advice 'eglot--executable-find "ruff-lsp") :to-equal "ruff-lsp")
(expect (spy-context-return-value (spy-calls-most-recent 'pet-executable-find)) :to-equal "ruff-lsp")
(expect 'eglot--executable-find :not :to-have-been-called)

(expect (pet-eglot--executable-find-advice 'eglot--executable-find "sh" "-c") :to-equal "sh -c")
(expect 'eglot--executable-find :to-have-been-called-with "sh" "-c")))

Expand Down Expand Up @@ -1162,11 +1171,11 @@
(spy-on 'pet-executable-find :and-call-fake
(lambda (command)
(assoc-default command
'(("flake8" . "/usr/bin/flake8")
("pylint" . "/usr/bin/pylint")
("python" . "/usr/bin/python")
'(("flake8" . "/usr/bin/flake8")
("pylint" . "/usr/bin/pylint")
("python" . "/usr/bin/python")
("jedi-language-server" . "/home/user/.local/bin/jedi-language-server")
("ruff" . "/usr/bin/ruff"))))))
("ruff" . "/usr/bin/ruff"))))))

(it "should return eglot initialization options for pylsp"
(expect (pet-lookup-eglot-server-initialization-options "/home/user/.local/bin/pylsp") :to-equal
Expand Down Expand Up @@ -1215,7 +1224,15 @@
"/home/user/.local/bin/jedi-language-server")
:workspace
(:environmentPath
"/usr/bin/python"))))))
"/usr/bin/python")))))

(it "should return eglot initialization options for ruff-lsp"
(expect (pet-lookup-eglot-server-initialization-options "ruff-lsp") :to-equal
'(:settings
(:interpreter
"/usr/bin/python"
:path
"/usr/bin/ruff")))))

(describe "pet-merge-eglot-initialization-options"
(it "should deeply merge 2 plists"
Expand Down Expand Up @@ -1256,12 +1273,15 @@
(kill-local-variable 'lsp-pylsp-plugins-jedi-environment)
(kill-local-variable 'lsp-pyright-venv-path)
(kill-local-variable 'lsp-pyright-python-executable-cmd)
(kill-local-variable 'lsp-ruff-lsp-ruff-path)
(kill-local-variable 'lsp-ruff-lsp-python-path)
(kill-local-variable 'dap-python-executable)
(kill-local-variable 'python-pytest-executable)
(kill-local-variable 'python-black-command)
(kill-local-variable 'python-isort-command)
(kill-local-variable 'blacken-executable)
(kill-local-variable 'yapfify-executable))
(kill-local-variable 'yapfify-executable)
(kill-local-variable 'ruff-format-command))

(it "should set up all buffer local variables for supported packages"
(spy-on 'pet-executable-find :and-call-fake
Expand All @@ -1279,7 +1299,8 @@
"/usr/bin/isort")
("yapf"
"/usr/bin/yapf")
)))
("ruff"
"/usr/bin/ruff"))))
(spy-on 'pet-virtualenv-root :and-return-value "/home/user/project/.venv/")
(spy-on 'pet-flycheck-setup)

Expand All @@ -1294,12 +1315,15 @@
(expect (local-variable-p 'lsp-pylsp-plugins-jedi-environment) :to-be-truthy)
(expect (local-variable-p 'lsp-pyright-venv-path) :to-be-truthy)
(expect (local-variable-p 'lsp-pyright-python-executable-cmd) :to-be-truthy)
(expect (local-variable-p 'lsp-ruff-lsp-ruff-path) :to-be-truthy)
(expect (local-variable-p 'lsp-ruff-lsp-python-path) :to-be-truthy)
(expect (local-variable-p 'dap-python-executable) :to-be-truthy)
(expect (local-variable-p 'python-pytest-executable) :to-be-truthy)
(expect (local-variable-p 'python-black-command) :to-be-truthy)
(expect (local-variable-p 'python-isort-command) :to-be-truthy)
(expect (local-variable-p 'blacken-executable) :to-be-truthy)
(expect (local-variable-p 'yapfify-executable) :to-be-truthy)
(expect (local-variable-p 'ruff-format-command) :to-be-truthy)

(expect python-shell-interpreter :to-equal "/usr/bin/python")
(expect python-shell-virtualenv-root :to-equal "/home/user/project/.venv/")
Expand All @@ -1308,12 +1332,15 @@
(expect lsp-pylsp-plugins-jedi-environment :to-equal "/home/user/project/.venv/")
(expect lsp-pyright-venv-path :to-equal "/home/user/project/.venv/")
(expect lsp-pyright-python-executable-cmd :to-equal "/usr/bin/python")
(expect lsp-ruff-lsp-ruff-path :to-equal "/usr/bin/ruff")
(expect lsp-ruff-lsp-python-path :to-equal "/usr/bin/python")
(expect dap-python-executable :to-equal "/usr/bin/python")
(expect python-pytest-executable :to-equal "/usr/bin/pytest")
(expect python-black-command :to-equal "/usr/bin/black")
(expect python-isort-command :to-equal "/usr/bin/isort")
(expect blacken-executable :to-equal "/usr/bin/black")
(expect yapfify-executable :to-equal "/usr/bin/yapf")))
(expect yapfify-executable :to-equal "/usr/bin/yapf")
(expect ruff-format-command :to-equal "/usr/bin/ruff")))

(describe "pet-buffer-local-vars-teardown"
(after-each
Expand All @@ -1324,12 +1351,15 @@
(kill-local-variable 'lsp-pylsp-plugins-jedi-environment)
(kill-local-variable 'lsp-pyright-venv-path)
(kill-local-variable 'lsp-pyright-python-executable-cmd)
(kill-local-variable 'lsp-ruff-lsp-ruff-path)
(kill-local-variable 'lsp-ruff-lsp-python-path)
(kill-local-variable 'dap-python-executable)
(kill-local-variable 'python-pytest-executable)
(kill-local-variable 'python-black-command)
(kill-local-variable 'python-isort-command)
(kill-local-variable 'blacken-executable)
(kill-local-variable 'yapfify-executable))
(kill-local-variable 'yapfify-executable)
(kill-local-variable 'ruff-format-command))

(it "should reset all buffer local variables for supported packages to default"
(spy-on 'pet-flycheck-teardown)
Expand All @@ -1344,12 +1374,15 @@
(expect (local-variable-p 'lsp-pylsp-plugins-jedi-environment) :not :to-be-truthy)
(expect (local-variable-p 'lsp-pyright-venv-path) :not :to-be-truthy)
(expect (local-variable-p 'lsp-pyright-python-executable-cmd) :not :to-be-truthy)
(expect (local-variable-p 'lsp-ruff-lsp-ruff-path) :not :to-be-truthy)
(expect (local-variable-p 'lsp-ruff-lsp-python-path) :not :to-be-truthy)
(expect (local-variable-p 'dap-python-executable) :not :to-be-truthy)
(expect (local-variable-p 'python-pytest-executable) :not :to-be-truthy)
(expect (local-variable-p 'python-black-command) :not :to-be-truthy)
(expect (local-variable-p 'python-isort-command) :not :to-be-truthy)
(expect (local-variable-p 'blacken-executable) :not :to-be-truthy)
(expect (local-variable-p 'yapfify-executable) :not :to-be-truthy)))
(expect (local-variable-p 'yapfify-executable) :not :to-be-truthy)
(expect (local-variable-p 'ruff-format-command) :not :to-be-truthy)))

(describe "pet-verify-setup"
:var ((old-default-directory default-directory)
Expand Down

0 comments on commit d832a08

Please sign in to comment.