company-lsp icon indicating copy to clipboard operation
company-lsp copied to clipboard

How can I use lsp completion without lsp--cur-workspace?

Open stardiviner opened this issue 5 years ago • 8 comments

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?

stardiviner avatar Oct 02 '18 09:10 stardiviner

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?

tigersoldier avatar Oct 11 '18 16:10 tigersoldier

This is already fixed in lsp-mode https://github.com/emacs-lsp/lsp-mode/issues/377#issuecomment-428039281

yyoncho avatar Oct 11 '18 16:10 yyoncho

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.

stardiviner avatar Oct 23 '18 01:10 stardiviner

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.

yyoncho avatar Oct 23 '18 04:10 yyoncho

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?

stardiviner avatar Oct 23 '18 06:10 stardiviner

Sounds good to me. Make sure you do a reddit post after it is ready.

yyoncho avatar Oct 23 '18 06:10 yyoncho

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:
  )

stardiviner avatar Oct 23 '18 07:10 stardiviner

This bug is no longer relevant.

yyoncho avatar Mar 14 '19 06:03 yyoncho