company-lsp
company-lsp copied to clipboard
How can I use lsp completion without lsp--cur-workspace?
I want to get code completion in Org Mode source block. But lsp-mode/company-lsp requires lsp--cur-workspace
. Is it possible to code completion without it?
company-lsp doesn't work if you cannot run language server on the code.
IMO there can be solution to this - create a temporary buffer for the source block and run language server over the temporary file, pipe the result to the original buffer. However, this is beyond the scope of company-lsp. Maybe open a feature request to lsp-mode?
This is already fixed in lsp-mode https://github.com/emacs-lsp/lsp-mode/issues/377#issuecomment-428039281
This is fixed in lsp-java
. I try some other clients like lsp-python
, lsp-javascript-typescript
and lsp-php
.
- [x] lsp-python works fine
- [ ] lsp-javascript-typescript reports error:
Warning (lsp-mode): Couldn’t find project root, using the current directory as the root.
Don't know how to set lsp-mode project root. - [ ] lsp-php does not provide the completion. did not raise any error up.
To setup lsp-javascript-typescript you should have package.json in the root(similar to the java project where you have pom.xml somewhere in the root). Here it is the relevant code https://github.com/emacs-lsp/lsp-javascript/blob/ab62826962887e82f0bc968817be4fc89a6953e4/lsp-typescript.el#L54 . You should first setup the language server without org-mode and then configure org-mode to point to one of the files in the project.
I decide to write a small package specially for handling this. I found Org Mode has a built-in header argument :file
. I might use it in package. @yyoncho WDYT?
Sounds good to me. Make sure you do a reddit post after it is ready.
The lsp--cur-workspace
still is nil
when I setup like this:
#+begin_src emacs-lisp :cache no
(defun org-babel-edit-prep:js (babel-info)
"Prepare buffer local environment for Org source block Java."
(setq-local buffer-file-name (->> babel-info
caddr
(alist-get :file)))
(setq-local lsp-buffer-uri (->> babel-info
caddr
(alist-get :file)
lsp--path-to-uri))
(lsp-javascript-typescript-enable)
)
#+end_src
#+begin_src js :file "~/Documents/learning/Web/JavaScript/react-demo/index.js"
#+end_src
(use-package lsp-javascript-typescript
:ensure t
:ensure-system-package (javascript-typescript-stdio . "npm i -g javascript-typescript-langserver")
:init (require 'lsp-javascript-typescript)
(add-hook 'js-mode-hook #'lsp-mode)
(add-hook 'js-mode-hook #'lsp-javascript-typescript-enable)
(add-hook 'js2-mode-hook #'lsp-mode)
(add-hook 'js2-mode-hook #'lsp-javascript-typescript-enable)
(add-hook 'js3-mode-hook #'lsp-mode)
(add-hook 'js3-mode-hook #'lsp-javascript-typescript-enable) ;; for js3-mode support
(add-hook 'typescript-mode-hook #'lsp-javascript-typescript-enable)
(add-hook 'rjsx-mode #'lsp-javascript-typescript-enable) ;; for rjsx-mode support
;; Org Babel source block support
;; TODO:
)
This bug is no longer relevant.