lsp-mode
lsp-mode copied to clipboard
Documentation for using lsp-mode in temp org-src-mode buffers
I am trying to figure out how to get lsp-mode to work in org-src-mode buffers and I'm not having any luck. I think it would be great if there was some documentation explaining how to get that working. It was suggested to me in the lsp-mode discord that a recipe for this did exist at some point in the past, so perhaps it's just a matter of finding it?
Thanks for all your hard work, lsp-mode is a fantastic project, and I'm really enjoying the org-mode support!
c/c @yyoncho
+1 Very much needed this. Though there is a workaround provided by @yyoncho. If lsp-mode enables in org-src-mode temp buffers so the power of ORG and LSP can be used together.
+1 Would really like this.
I wonder if having it in the org-src-mode buffers is really necessary for you: I have used a mix of lsp and org by defining the :tangle property on the src block and calling lsp-org. That provides autocompletion, compilation errors, etcc.. , but you need to narrow to region yourself. Maybe I can make up a function that narrows to the src-block area? Still it would be mandatory to define the :tangle attribute, which means that the source block needs to refer to an existing file.
I will take a look.
So the following is supposed to work:
Org-mode file:
#+BEGIN_SRC java :tangle java-project/src/test/java/temp/AppTest.java
package temp;
import org.junit.Test;
public class AppTest {
@Test
public void testA() {
System.out.println("Entering testA...");
App.demo();
fo();
}
private void fo() {
System.out.println("Foo called.");
}
private void foo2() {
System.out.println("Foo called.");
}
@Test
public void testB() {
System.out.println("Entering testB...\n");
fo();
}
}
and put the following lines in your config:
(defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :tangle)))
(lsp))
Note the suffix of the function should match the language you want to use lsp-mode
for.
C-c C-c on code block returns error "can't compile java block without a classname" which sounds wrong for java junit tests and I don't see anything new (e.g., autocompletion) on the C-c ' narrowed buffer or the standard code block
C-c C-c
is org-mode function and it has nothing to do with lsp-mode.
don't see anything new (e.g., autocompletion) on the C-c ' narrowed buffer or the standard code block
With that much information, I cannot help. Check if the server is running, check if lsp-mode is active in that buffer, etc.
Ok, will check
I have been trying to get sourcekit-lsp working in Orc Src buffer for remote files over tramp.
I can confirm that opening the file over tramp directly works with the remote lsp server.
However, in an Org Src buffer I get the following in my lsp-log buffer
Command "sourcekit-lsp" is not present on the path.
Here is my config
(defun org-babel-edit-prep:swift (babel-info)
"BABEL-INFO.
https://github.com/emacs-lsp/lsp-mode/issues/2842#issuecomment-870807018"
(setq-local buffer-file-name (->> babel-info caddr (alist-get :tangle)))
(lsp))
(add-hook 'swift-mode-hook
(lambda()
(setq tramp-remote-path (list "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"))
(lsp-register-client
(make-lsp-client :new-connection (lsp-tramp-connection "sourcekit-lsp")
:major-modes '(swift-mode)
:remote? t
:server-id 'sourcekit-remote))
(lsp)))
I have been trying to step through the debugger to see if I can spot a difference between when it works when accessing the file directly vs when using an Org Src buffer with no luck.
(setq-local buffer-file-name (->> babel-info caddr (alist-get :tangle)))
Maybe this is making the file local and at the same time, you have remote registration.
Check if file-remote-p
returns t
It seems to be setting the buffer-file-name correctly. I even tried running revert-buffer
on the Org Src buffer and the correct remote path shows up in the Revert buffer from file ...
prompt. However, the error persists even with revert buffer using the correct remote path. But if I use C-x C-f
and enter the remote path the lsp server starts up no problem. There is something different about the Org Src buffer but I just can't figure it out.
Also, was not sure where/how you wanted me to run file-remote-p
but after the Org Src buffer loads I ran it as M-: (file-remote-p (buffer-file-name))
and it did return the remote part of the file path. ie. /ssh::user@host
There are 2 functions you should check: lsp--server-binary-present? and lsp--supports-buffer? You may use (gethash 'CLIENT-ID lsp-clients) to get the client.
SOLVED!
I had to also set the default-directory
in the org-babel-edit-prep:swift
Solution:
(defun org-babel-edit-prep:swift (babel-info)
"Setup for lsp-mode in Org Src buffer using BABEL-INFO."
(setq-local default-directory (->> babel-info caddr (alist-get :dir)))
(setq-local buffer-file-name (->> babel-info caddr (alist-get :tangle)))
(lsp))
Cause of the issue:
The Command ... is not present
log messages can be found in this function below of ${HOME}/.emacs.d/elpa/lsp-mode-20220223.639/lsp-mode.el
starting at line 6893
:
(defun lsp-server-present? (final-command)
"Check whether FINAL-COMMAND is present."
;; executable-find only gained support for remote checks after 27 release
(or (and (cond
((not (file-remote-p default-directory))
(executable-find (cl-first final-command)))
((version<= "27.0" emacs-version)
(with-no-warnings (executable-find (cl-first final-command) (file-remote-p default-directory))))
(t))
(prog1 t
(lsp-log "Command \"%s\" is present on the path." (s-join " " final-command))))
(ignore (lsp-log "Command \"%s\" is not present on the path." (s-join " " final-command)))))
The key line (not (file-remote-p default-directory))
is where I realized it was using the default-directory of my local org file.
Note:
You will need to set both the :tangle
and the :dir
org babel header argument for my solution to work.
Example org src block:
#+header: :tangle /ssh:user@host:/path/to/file
#+header: :dir /ssh:user@host:/path/to/remote/directory
#+begin_src <YOUR_LANGUAGE>
...
#+end_src
Thank you very much @yyoncho for steering me to this solutions