-
-
Notifications
You must be signed in to change notification settings - Fork 868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature request] support org-mode source code blocks #377
Comments
IMO this integration must go in org-mode similar to the other language adapters. The |
I did some research as per @stardiviner comment at #409 . Here it is how you can do it for java. This code requires (defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :file-name)))
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :file-name) lsp--path-to-uri))
(lsp)
;; other lsp-java specific stuff that you usually run with the major mode, company mode and so on.
) Then in the org mode file you should specify the root of the project. Note that you have to do the java specific setup as per guide in lsp-java. Configure the file-name so lsp-mode could create workspace for it.
After that you can go into the src block and press |
@yyoncho After tried your method, I still can't get completion. But it does not report error on lsp workspace. Here is the value of
|
Did you enable all the modes you need as per Here it is the full code: (defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :file-name)))
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :file-name) lsp--path-to-uri))
(lsp-java-enable)
(push 'company-lsp company-backends)
(lsp-ui-mode t)
(flycheck-mode t)
(company-mode t)
(lsp-ui-flycheck-enable t)) |
Yes, I use (defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :file-name)))
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :file-name) lsp--path-to-uri))
(lsp-javacomp-enable)
(lsp-ui-mode t)
;; other lsp-java specific stuff that you usually run with the major mode, company mode and so on.
(company-mode t)) I think
|
I get this error when I open source block:
It reminds "time out", so I wait for a while, about 1 minute, the opened source block buffer still can't get completion. |
I guss the root path is wrong (I guess from the result). It is |
@stardiviner Sorry, I do not know how javacomp work I can help you only with |
@yyoncho I tried
|
Can you delete ~/.emacs.d/eclipse.jdt.ls folder and retry? You have 2 versions of the server. Also, to make sure that |
When I open the source block, the minibuffer shows following messages, I checked out from
How can I dig deeper? |
When I open the source block again:
|
This line means that you have started something that is not related to
The path looks incorrect - the maven src folder is `/src/main/java/{project-namespace}. You may download the following test project: https://github.com/yyoncho/dap-mode/tree/master/features/fixtures/test-project to start with. |
I got another error:
I guess this is because timeout of lsp-java server. And I see a warning message before open the source block buffer:
(defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :file-name)))
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :file-name) lsp--path-to-uri))
;; (lsp-javacomp-enable)
(lsp-java-enable)
(lsp-ui-mode t)
;; other lsp-java specific stuff that you usually run with the major mode, company mode and so on.
(company-mode t))
(use-package lsp-java
:ensure t
:init (add-hook 'java-mode-hook #'lsp-java-enable)
:config
;; set the projects that are going to be imported into the workspace.
(setq lsp-java--workspace-folders (list "~/IdeaProjects/JavaTest/"
"~/Documents/learning/Java/test-project/"))) Upper timeout issue seems is random. Later try times:
|
Can you remove "~/IdeaProjects/JavaTest/" just to isolate the problem, set |
I removed "~/IdeaProjects/JavaTest/". And I'm curious what caused this error: |
@stardiviner You may now play with the Idea project. I suspect that for some reason(e. g. your PC is slow or was under load) JDT server was unable to parse the projects in 10 seconds time and the initialization has failed and after that LSP was mode is broken. @twlz0ne did you had a chance to play with the solution that works for @stardiviner ? |
@yyoncho Yes, my small notebook is slow on hardware performance. But I'm increasing Emacs performance on other places like disable some packages. Then I test now, it initialize lsp-java need about 8 seconds now. |
@stardiviner yeah, so I have guessed it right. There is a bug to make that initialize function async which will solve that problem. |
My configuration: (defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :file-name)))
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :file-name) lsp--path-to-uri))
(setq lsp-response-timeout 50)
(my/enable-lsp-java))
(defun my/lsp-java-enable ()
(message ">>>>>>>>>> (my/lsp-java-enable) <<<<<<<<<<")
(set (make-local-variable 'company-backends)
'(;; company-lsp
company-capf))
(lsp-java-enable))
(setq lsp-java-server-install-dir "~/.emacs.d/eclipse.jdt.ls/server/")
(setq lsp-java--workspace-folders
(list ;; "~/IdeaProjects/JavaTest/"
"~/Documents/learning/Java/test-project/"))
(add-hook 'java-mode-hook #'my/lsp-java-enable)
|
can you do Also, here it is the full function to enable autocompletion, I am not sure company-capf could handle lsp-java. (defun org-babel-edit-prep:java (babel-info)
(setq-local buffer-file-name (->> babel-info caddr (alist-get :file-name)))
(setq-local lsp-buffer-uri (->> babel-info caddr (alist-get :file-name) lsp--path-to-uri))
(lsp-java-enable)
(push 'company-lsp company-backends)
(lsp-ui-mode t)
(flycheck-mode t)
(company-mode t)
(lsp-ui-flycheck-enable t)) |
Great! @twlz0ne @stardiviner you may consider contributing a wiki page so this info is not lost. I am making the bug as closed. |
Ok, I will take time to contributing a wiki page. |
After check out Wiki Pages https://github.com/emacs-lsp/lsp-mode/wiki, it can't online add wiki page using an online editor. Also I don't know how to fork the wiki page repo. |
@stardiviner thanks, only the repo owner could change the wiki permissions: @vibhavp can you make the wiki public so people could contribute: https://help.github.com/articles/changing-access-permissions-for-wikis/ ? |
Seems this workaround doesn't work now. I got this error. Debugger entered--Lisp error: (wrong-type-argument arrayp nil)
file-truename(nil)
(url-hexify-string (file-truename (alist-get :file-name (car (cdr (cdr babel-info))))) url-path-allowed-chars)
(concat lsp--uri-file-prefix (url-hexify-string (file-truename (alist-get :file-name (car (cdr (cdr babel-info))))) url-path-allowed-chars))
(set (make-local-variable 'lsp-buffer-uri) (concat lsp--uri-file-prefix (url-hexify-string (file-truename (alist-get :file-name (car (cdr (cdr babel-info))))) url-path-allowed-chars)))
org-babel-edit-prep:python(("python" "import sys\nprint(sys.path)" ((:results . "output replace") (:exports . "code") (:tangle . "no") (:hlines . "no") (:noweb . "no") (:cache . "no") (:session . "none")) "" nil 3848 "(ref:%s)"))
org-edit-src-code()
org-edit-special(nil)
funcall-interactively(org-edit-special nil)
call-interactively(org-edit-special nil nil)
command-execute(org-edit-special) @twlz0ne Would you please let me know how to get it work in Python block? Thanks. |
I guess you are missing the configuration in org buffer.
|
@yyoncho Yes, you are right. I added My workaround: (defun org-babel-edit-prep:python (babel-info)
(setq-local lsp-buffer-uri (lsp--path-to-uri
(or (->> babel-info caddr (alist-get :file-name))
(buffer-file-name))))
(lsp-python-enable)) |
I wouldn't say it is a workaround but it is a normal org-mode adapter. Flycheck works fine with lsp-java. Note that when you open a file xxxx directly would be the same as when you are editing it in org-mode since the line |
I tried this config: (use-package lsp-python
:ensure t
:commands lsp-python-enable
:init
(add-hook 'python-mode-hook #'lsp-mode)
(add-hook 'python-mode-hook #'lsp-python-enable)
(defun org-babel-edit-prep:python (babel-info)
"Prepare buffer local environment for Org source block Java."
(if-let* ((lang (car babel-info))
(ext (cdr (assoc lang org-babel-tangle-lang-exts)))
;; detect the header argument :lsp-file exist, if not, use default
;; "/tmp/tmp.EXT".
(lsp-file (or (->> babel-info
caddr
(alist-get :file))
(buffer-file-name)
(format "/tmp/tmp.%s" ext)))
(lsp-file-url (lsp--path-to-uri lsp-file)))
(progn
(setq-local buffer-file-name lsp-file)
(setq-local lsp-buffer-uri lsp-file-url)
(if lsp-mode (lsp-python-enable))
(message "lsp-mode workspace file setup for source block done!"))))) Test: import os
import sys
print("hello, world!")
print(sys.path)
pri Upper python source block still report error when I open source block to try get code completion:
Maybe my config is wrong, maybe somewhere is still wrong. Why |
@stardiviner If you are using the latest (lsp-define-stdio-client lsp-python "python"
(lsp-make-traverser #'(lambda (dir)
(if lsp-python-use-init-for-project-root
(not (directory-files dir nil "__init__.py"))
(directory-files
dir
nil
"setup.py\\|Pipfile\\|setup.cfg\\|tox.ini"))))
nil
:command-fn 'lsp-python--ls-command)
(defun org-babel-edit-prep:python (babel-info)
"Prepare the local buffer environment for Org source block."
(let ((lsp-file (or (->> babel-info caddr (alist-get :file))
buffer-file-name)))
(setq-local buffer-file-name lsp-file)
(setq-local lsp-buffer-uri (lsp--path-to-uri buffer-file-name))
(lsp-python-enable))) |
This line is wrong. When you enable lsp-mode this will prevent lsp--cur-workspace initialization.
The same - change it to |
I removed those lines. still can't get code completion. This lsp is really hard to configure. I would prefer to stay on other code completion packages until this lsp-lang are enough great to use. Huuuuu...... :( |
@stardiviner you make monitor #462 and #376 . |
@stardiviner (use-package lsp-python
:ensure t
:commands lsp-python-enable
:hook (python-mode . lsp-python-enable)
:config
(defun org-babel-edit-prep:python (babel-info)
"Prepare the local buffer environment for Org source block."
(let ((lsp-file (or (->> babel-info caddr (alist-get :file))
buffer-file-name)))
(setq-local buffer-file-name lsp-file)
(setq-local lsp-buffer-uri (lsp--path-to-uri buffer-file-name))
(lsp-python-enable)))
;; FIXME: https://github.com/emacs-lsp/lsp-python/issues/28
(lsp-define-stdio-client lsp-python "python"
(lsp-make-traverser #'(lambda (dir)
(if lsp-python-use-init-for-project-root
(not (directory-files dir nil "__init__.py"))
(directory-files
dir
nil
"setup.py\\|Pipfile\\|setup.cfg\\|tox.ini"))))
nil
:command-fn 'lsp-python--ls-command)) |
@seagle0128 Thank you for sharing. Added into my init files. |
@twlz0ne @stardiviner @seagle0128 #655 might be interesting for you. |
is this post merged ? |
It will be great if can use lsp when editing source code block in temporary buffer
*Org Src FILENAME [ LANG ]*
for org-mode.The text was updated successfully, but these errors were encountered: