Skip to content

Use true async and add code actions prefix. - #5

Merged
jcs090218 merged 8 commits into
emacs-sideline:masterfrom
DevelopmentCool2449:master
Mar 22, 2024
Merged

Use true async and add code actions prefix.#5
jcs090218 merged 8 commits into
emacs-sideline:masterfrom
DevelopmentCool2449:master

Conversation

@DevelopmentCool2449

Copy link
Copy Markdown
Contributor

Based on joaotavora comments (#3).
This adds true async and code action prefix from sideline-lsp.

Comment thread sideline-eglot.el Outdated
@DevelopmentCool2449
DevelopmentCool2449 marked this pull request as ready for review March 20, 2024 22:39
@DevelopmentCool2449 DevelopmentCool2449 changed the title (Draft) Use true async and add code actions prefix. Use true async and add code actions prefix. Mar 20, 2024
@jcs090218

Copy link
Copy Markdown
Member

I've tried your branch but the code action doesn't seem to work. 🤔

@DevelopmentCool2449

Copy link
Copy Markdown
Contributor Author

I've tried your branch but the code action doesn't seem to work. 🤔

It's now fixed.

Comment thread sideline-eglot.el Outdated
(advice-remove 'y-or-n-p #'always)))

(defvar-local sideline-eglot--ht-candidates nil
(defvar sideline-eglot--ht-candidates nil

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work with all buffers since this is no longer a local variable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it on 2 buffers with different language server and it works well for me, it hasn't given me any incovenience.

@DevelopmentCool2449 DevelopmentCool2449 Mar 21, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also this is the only way i found to make this works because async json apparently works in a temp buffer.

@jcs090218

Copy link
Copy Markdown
Member

This is the working patch for me.

diff --git a/sideline-eglot.el b/sideline-eglot.el
index 2d803ef..8266d99 100644
--- a/sideline-eglot.el
+++ b/sideline-eglot.el
@@ -60,39 +60,38 @@ This can be used to insert, for example, an unicode character: 💡"
      ,@body
      (advice-remove 'y-or-n-p #'always)))
 
-(defvar sideline-eglot--ht-candidates nil
+(defvar-local sideline-eglot--ht-candidates nil
   "Holds candidates.")
 
-(defvar sideline-eglot--callback)
-
 (defun sideline-eglot--async-candidates (callback &rest _)
   "Request eglot's CALLBACK candidates."
-  (setq sideline-eglot--callback callback)
-  (jsonrpc-async-request
-   (eglot-current-server)
-   :textDocument/codeAction
-   (list :textDocument (eglot--TextDocumentIdentifier)
-         :range (list :start (eglot--pos-to-lsp-position (point))
-                      :end (eglot--pos-to-lsp-position nil))
-         :context
-         `(:diagnostics
-           [,@(cl-loop for diag in (flymake-diagnostics (point) nil)
-                       when (cdr (assoc 'eglot-lsp-diag
-                                        (eglot--diag-data diag)))
-                       collect it)]))
-   :success-fn
-   (lambda (resp)
-     (let ((actions (append resp nil)))
-       (if sideline-eglot--ht-candidates
-           (ht-clear sideline-eglot--ht-candidates)
-         (setq sideline-eglot--ht-candidates (ht-create)))
-       (dolist (row actions)
-         (ht-set sideline-eglot--ht-candidates
-                 (concat sideline-eglot-code-actions-prefix
-                         (cl-getf row :title))
-                 row))
-       (funcall sideline-eglot--callback (ht-keys sideline-eglot--ht-candidates))))
-   :deferred :textDocument/codeAction))
+  (let ((buffer (current-buffer)))
+    (jsonrpc-async-request
+     (eglot-current-server)
+     :textDocument/codeAction
+     (list :textDocument (eglot--TextDocumentIdentifier)
+           :range (list :start (eglot--pos-to-lsp-position (point))
+                        :end (eglot--pos-to-lsp-position nil))
+           :context
+           `(:diagnostics
+             [,@(cl-loop for diag in (flymake-diagnostics (point) nil)
+                         when (cdr (assoc 'eglot-lsp-diag
+                                          (eglot--diag-data diag)))
+                         collect it)]))
+     :success-fn
+     (lambda (resp)
+       (sideline--with-buffer buffer
+         (let ((actions (append resp nil)))
+           (if sideline-eglot--ht-candidates
+               (ht-clear sideline-eglot--ht-candidates)
+             (setq sideline-eglot--ht-candidates (ht-create)))
+           (dolist (row actions)
+             (ht-set sideline-eglot--ht-candidates
+                     (concat sideline-eglot-code-actions-prefix
+                             (cl-getf row :title))
+                     row))
+           (funcall callback (ht-keys sideline-eglot--ht-candidates)))))
+     :deferred :textDocument/codeAction)))
 
 ;;;###autoload
 (defun sideline-eglot (command)
@@ -110,9 +109,9 @@ Argument COMMAND is required in sideline backend."
             (command (cl-getf matching-code-action :command))
             (server (eglot-current-server)))
          (sideline-eglot--inhibit-timeout
-          (eglot-execute-command server
-                                 (cl-getf command :command)
-                                 (cl-getf command :arguments))))))))
+           (eglot-execute-command server
+                                  (cl-getf command :command)
+                                  (cl-getf command :arguments))))))))
 
 (provide 'sideline-eglot)
 ;;; sideline-eglot.el ends here

Comment thread sideline-eglot.el Outdated
(concat sideline-eglot-code-actions-prefix
(cl-getf row :title))
row))
(funcall sideline-eglot--callback (ht-keys sideline-eglot--ht-candidates))))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the lambda, you will lose buffer local variables sideline-eglot--callback and sideline-eglot--ht-candidates since you are inside the *temp* buffer.

@DevelopmentCool2449

Copy link
Copy Markdown
Contributor Author
sideline-eglot--ht-candidates

Thanks! it works to me too, i'm updating this patch.

@jcs090218
jcs090218 merged commit b9118fa into emacs-sideline:master Mar 22, 2024
@jcs090218

Copy link
Copy Markdown
Member

Thank you!

@jcs090218 jcs090218 mentioned this pull request Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants