|
| 1 | +;;; lsp-typespec.el --- Typespec Client settings -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2024 [email protected] |
| 4 | + |
| 5 | +;; Author: Jeremy Meng <[email protected]> |
| 6 | +;; Keywords: languages,tools |
| 7 | + |
| 8 | +;; This program is free software; you can redistribute it and/or modify |
| 9 | +;; it under the terms of the GNU General Public License as published by |
| 10 | +;; the Free Software Foundation, either version 3 of the License, or |
| 11 | +;; (at your option) any later version. |
| 12 | + |
| 13 | +;; This program is distributed in the hope that it will be useful, |
| 14 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +;; GNU General Public License for more details. |
| 17 | + |
| 18 | +;; You should have received a copy of the GNU General Public License |
| 19 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +;;; Commentary: |
| 22 | + |
| 23 | +;; lsp-typespec client |
| 24 | + |
| 25 | +;;; Code: |
| 26 | + |
| 27 | +(require 'lsp-mode) |
| 28 | +(require 'lsp-semantic-tokens) |
| 29 | + |
| 30 | +(defgroup lsp-typespec nil |
| 31 | + "LSP support for Typespec." |
| 32 | + :link '(url-link "https://github.com/microsoft/typespec/blob/9c95ccda8c84c7c6afa24b2f4b21cf1ecbe680dd/packages/compiler/cmd/tsp-server.js") |
| 33 | + :group 'lsp-mode |
| 34 | + :tag "Lsp Typespec") |
| 35 | + |
| 36 | +(defcustom lsp-typespec-custom-server-command nil |
| 37 | + "The typespec-lisp server command." |
| 38 | + :group 'lsp-typespec |
| 39 | + :risky t |
| 40 | + :type '(repeat string)) |
| 41 | + |
| 42 | +(lsp-dependency |
| 43 | + 'typespec-lsp |
| 44 | + '(:npm :package "@typespec/compiler" |
| 45 | + :path "tsp-server") |
| 46 | + '(:system "tsp-server")) |
| 47 | + |
| 48 | +(defun lsp-typespec--server-executable-path () |
| 49 | + "Return the typespec-lsp server command." |
| 50 | + (or |
| 51 | + (when-let ((workspace-folder (lsp-find-session-folder (lsp-session) default-directory))) |
| 52 | + (let ((tsp-server-local-path (f-join workspace-folder "node_modules" ".bin" |
| 53 | + (if (eq system-type 'windows-nt) "tsp-server.cmd" "tsp-server")))) |
| 54 | + (when (f-exists? tsp-server-local-path) |
| 55 | + tsp-server-local-path))) |
| 56 | + (executable-find "tsp-server") |
| 57 | + (lsp-package-path 'tsp-server) |
| 58 | + "tsp-server")) |
| 59 | + |
| 60 | +(lsp-register-client |
| 61 | + (make-lsp-client |
| 62 | + :semantic-tokens-faces-overrides '(:types (("docCommentTag" . font-lock-keyword-face) |
| 63 | + ("event" . default))) |
| 64 | + :new-connection (lsp-stdio-connection `(,(lsp-typespec--server-executable-path) "--stdio")) |
| 65 | + :activation-fn (lsp-activate-on "typespec") |
| 66 | + :major-modes '(typespec-mode) |
| 67 | + :server-id 'typespec-lsp)) |
| 68 | + |
| 69 | +(lsp-consistency-check lsp-typespec) |
| 70 | + |
| 71 | +(defun lsp-typespec-semantic-tokens-refresh (&rest _) |
| 72 | + "Force refresh semantic tokens." |
| 73 | + (when-let ((workspace (and lsp-semantic-tokens-enable |
| 74 | + (lsp-find-workspace 'typespec-lsp (buffer-file-name))))) |
| 75 | + (--each (lsp--workspace-buffers workspace) |
| 76 | + (when (lsp-buffer-live-p it) |
| 77 | + (lsp-with-current-buffer it |
| 78 | + (lsp-semantic-tokens--enable)))))) |
| 79 | + |
| 80 | +(with-eval-after-load 'typespec |
| 81 | + (when lsp-semantic-tokens-enable |
| 82 | + ;; refresh tokens |
| 83 | + (add-hook 'typespec-mode-hook #'lsp-typespec-semantic-tokens-refresh))) |
| 84 | + |
| 85 | +(provide 'lsp-typespec) |
| 86 | +;;; lsp-typespec.el ends here |
| 87 | + |
0 commit comments