manateelazycat / lsp-bridge

A blazingly fast LSP client for Emacs
GNU General Public License v3.0
1.42k stars 205 forks source link

Cannot start "deno lsp" #933

Closed finalclass closed 5 months ago

finalclass commented 5 months ago

I have this minimal init.el file (I stripped everything to be sure that I'm not doing anything wrong):

(use-package markdown-mode :ensure t)
(use-package yasnippet :ensure t)

(add-to-list 'load-path "/home/sel/.emacs.d/lsp-bridge")

(require 'yasnippet)
(yas-global-mode 1)

(require 'lsp-bridge)
(global-lsp-bridge-mode)

(use-package typescript-mode :ensure t)

(setq lsp-bridge-get-single-lang-server-by-project
      (lambda (project-path file-path)
    "deno"))

With this configuration the only language server should be "deno" however when I open any *.ts file the regular typescript server is being used.

Moreover when I add some debug statements ((message "TEST")) in the lambda function I don't see it in *Messages*. I tried with lsp-bridge-get-multi-lang-server-by-project also but it does not seam to do anything.

manateelazycat commented 5 months ago
(setq lsp-bridge-get-multi-lang-server-by-project
      (lambda (project-path filepath)
        ;; If typescript file include deno.land url, then use Deno LSP server.
        (save-excursion
          (when (string-equal (file-name-extension filepath) "ts")
            (dolist (buf (buffer-list))
              (when (string-equal (buffer-file-name buf) filepath)
                (with-current-buffer buf
                  (goto-char (point-min))
                  (when (search-forward-regexp (regexp-quote "from \"https://deno.land") nil t)
                    (return "deno")))))))))
finalclass commented 5 months ago

This does not work for me because I use deno.jsonc for imports (as almost everyone that uses deno). Nevertheless, why my lambda function is not being called?

finalclass commented 5 months ago

Moreover the method you provided will work only if you have deno.land imports. On any other file in a deno project it will fail. I'm able to tweak it properly however from my observation it's never called.

manateelazycat commented 5 months ago

please upload test code, I will research it

finalclass commented 5 months ago

image

main.ts

import { sum } from "./math.ts";

console.log(sum(2, 2));

math.ts

export function sum(a:number, b:number) : number {
    return a + b;
}

init.el

(use-package markdown-mode :ensure t)
(use-package yasnippet :ensure t)

(add-to-list 'load-path "/home/sel/.emacs.d/lsp-bridge")

(require 'yasnippet)
(yas-global-mode 1)

(require 'lsp-bridge)
(global-lsp-bridge-mode)

(use-package typescript-mode :ensure t)

(setq lsp-bridge-get-lang-server-by-project
      (lambda (project-path file-path)
    "deno"))

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages '(typescript-mode yasnippet markdown-mode))
 '(safe-local-variable-values '((typescript-indent-offset . 2))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
~$ emacs --version
GNU Emacs 29.2
Copyright (C) 2024 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
~$ uname -a
Linux rog 5.15.154-1-MANJARO #1 SMP PREEMPT Wed Apr 10 19:10:26 UTC 2024 x86_64 GNU/Linux
~$ deno --version
deno 1.39.1 (release, x86_64-unknown-linux-gnu)
v8 12.0.267.8
typescript 5.3.3
manateelazycat commented 5 months ago
;; lsp-bridge first try `lsp-bridge--get-multi-lang-server-func', then try `lsp-bridge--get-single-lang-server-func'
;; So we need remove `ts' and `tsx' setting from default value of lsp-bridge-multi-lang-server-extension-list.
(setq lsp-bridge-multi-lang-server-extension-list
      (cl-remove-if (lambda (item)
                      (equal (car item) '("ts" "tsx")))
                    lsp-bridge-multi-lang-server-extension-list))

;; Last we customize `lsp-bridge-get-single-lang-server-by-project' to return `deno' lsp server name.
;; I recommand you write some code to compare project-path or file-path, return `deno' only if match target path.
(setq lsp-bridge-get-single-lang-server-by-project
      (lambda (project-path file-path)
        "deno"))
finalclass commented 4 months ago

I'm sorry for writing so late, but I was on vacation. I've tested the solution, and it works great. Thanks a lot!

Since when I write JS I always use deno, the lsp-bridge-get-single-lang-server-by-project function is very simple for me:

;; use deno for ts and tsx, for anything else  use defaults
(setq lsp-bridge-get-single-lang-server-by-project
      (lambda (project-path file-path)
    (when (or (string-suffix-p ".ts" file-path)
          (string-suffix-p ".tsx" file-path))
      "deno")))