Skip to content

Commit

Permalink
fix: lsp--find-workspaces-for workspace/executeCommand respects the t…
Browse files Browse the repository at this point in the history
…arget command

Calling

    (lsp--find-workspaces-for '(:method "workspace/executeCommand"
                                :params (:command "arbitrary-command")))

would return *any* workspace that had the :executeCommandProvider capability
registerd.

This would result in a given command being forwarded for **all active
workspaces**. Some servers (such as ruff) would raise a KeyError for unknown
commands -- causing the minibuffer to, sometimes, show an error.

This change introduces a new :check-message callback that can be used in
lsp-method-requirements -- this callback receives the workspace and the
message to be sent.

Ths entry for workspace/executeCommand has been updated to check if the target
workspace can execute the required command.
  • Loading branch information
kassick committed Jul 11, 2024
1 parent 567990b commit 1602f19
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions lsp-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,13 @@ directory")
("textDocument/typeDefinition" :capability :typeDefinitionProvider)
("textDocument/typeHierarchy" :capability :typeHierarchyProvider)
("textDocument/diagnostic" :capability :diagnosticProvider)
("workspace/executeCommand" :capability :executeCommandProvider)
("workspace/executeCommand"
:capability :executeCommandProvider
:check-message (lambda (workspace msg)
(-let* (((&plist :method :params) msg)
((&plist :command) params))
(with-lsp-workspace workspace
(lsp-can-execute-command? command)))))
("workspace/symbol" :capability :workspaceSymbolProvider))

"Map methods to requirements.
Expand Down Expand Up @@ -6640,14 +6646,15 @@ REFERENCES? t when METHOD returns references."
(evil-set-command-property 'lsp-find-references :jump t)
(evil-set-command-property 'lsp-find-type-definition :jump t))

(defun lsp--workspace-method-supported? (check-command method capability workspace)
(defun lsp--workspace-method-supported? (check-command method check-message msg capability workspace)
(with-lsp-workspace workspace
(if check-command
(funcall check-command workspace)
(or
(when capability (lsp--capability capability))
(lsp--registered-capability method)
(and (not capability) (not check-command))))))
(cond
(check-command (funcall check-command workspace))
(check-message (funcall check-message workspace msg))
(t (or
(when capability (lsp--capability capability))
(lsp--registered-capability method)
(and (not capability) (not check-command)))))))

(defun lsp-disable-method-for-server (method server-id)
"Disable METHOD for SERVER-ID."
Expand All @@ -6662,20 +6669,23 @@ REFERENCES? t when METHOD returns references."
(eq server-id))
(lsp--workspace-method-supported? check-command
method
nil
nil
capability
workspace))))))
(alist-get method lsp-method-requirements nil nil 'string=)))

(defun lsp--find-workspaces-for (msg-or-method)
"Find all workspaces in the current project that can handle MSG."
(let ((method (if (stringp msg-or-method)
msg-or-method
(plist-get msg-or-method :method))))

(-let (((method . msg) (if (stringp msg-or-method)
(cons msg-or-method `(:method ,msg-or-method))
(cons (plist-get msg-or-method :method) msg-or-method))))
(-if-let (reqs (cdr (assoc method lsp-method-requirements)))
(-let (((&plist :capability :check-command) reqs))
(-let (((&plist :capability :check-command :check-message) reqs))
(-filter
(-partial #'lsp--workspace-method-supported?
check-command method capability)
check-command method check-message msg capability)
(lsp-workspaces)))
(lsp-workspaces))))

Expand Down

0 comments on commit 1602f19

Please sign in to comment.