Skip to content

Commit 1ddd70a

Browse files
authored
feat: add TypeSpec tsp-server support (#4604)
* Add lsp-typespec client for TypeSpec TypeSpec (https://github.com/microsoft/typespec) is a language for defining cloud service APIs and shapes. TypeSpec is a highly extensible language with primitives that can describe API shapes common among REST, OpenAPI, gRPC, and other protocols. * update CHANGELOG and add doc entry * update tsp-server link * Add missing quote for server-url value
1 parent a49b386 commit 1ddd70a

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

CHANGELOG.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Add support for buf CLI ([[https://github.com/bufbuild/buf/releases/tag/v1.43.0][beta]])
2424
* Add fennel support
2525
* Add support for [[https://github.com/nextflow-io/language-server][Nextflow]]
26+
* Add TypeSpec support
2627

2728
** 9.0.0
2829
* Add language server config for QML (Qt Modeling Language) using qmlls.

clients/lsp-typespec.el

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+

docs/lsp-clients.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,15 @@
496496
"lsp-install-server": "jsts-ls",
497497
"debugger": "Yes (Firefox/Chrome)"
498498
},
499+
{
500+
"name": "typespec",
501+
"full-name": "TypeSpec",
502+
"server-name": "tsp server",
503+
"server-url": "https://github.com/microsoft/typespec/tree/main/packages/compiler",
504+
"installation": "npm i -g @typespec/compiler",
505+
"lsp-install-server": "@typespec/compiler",
506+
"debugger": "Yes (Firefox/Chrome)"
507+
},
499508
{
500509
"name": "deno",
501510
"common-group-name": "javascript",

lsp-mode.el

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ As defined by the Language Server Protocol 3.16."
189189
lsp-ruby-syntax-tree lsp-ruff lsp-rust lsp-semgrep lsp-shader
190190
lsp-solargraph lsp-solidity lsp-sonarlint lsp-sorbet lsp-sourcekit
191191
lsp-sql lsp-sqls lsp-steep lsp-svelte lsp-tailwindcss lsp-terraform
192-
lsp-tex lsp-tilt lsp-toml lsp-trunk lsp-ttcn3 lsp-typeprof lsp-v
192+
lsp-tex lsp-tilt lsp-toml lsp-trunk lsp-ttcn3 lsp-typeprof lsp-typespec lsp-v
193193
lsp-vala lsp-verilog lsp-vetur lsp-vhdl lsp-vimscript lsp-volar lsp-wgsl
194194
lsp-xml lsp-yaml lsp-yang lsp-zig)
195195
"List of the clients to be automatically required."
@@ -796,6 +796,7 @@ Changes take effect only when a new session is started."
796796
("\\.svelte$" . "svelte")
797797
("\\.toml\\'" . "toml")
798798
("\\.ts$" . "typescript")
799+
("\\.tsp$" . "typespec")
799800
("\\.tsx$" . "typescriptreact")
800801
("\\.ttcn3$" . "ttcn3")
801802
("\\.vue$" . "vue")
@@ -891,6 +892,7 @@ Changes take effect only when a new session is started."
891892
(js-ts-mode . "javascript")
892893
(typescript-mode . "typescript")
893894
(typescript-ts-mode . "typescript")
895+
(typespec-mode . "typespec")
894896
(tsx-ts-mode . "typescriptreact")
895897
(svelte-mode . "svelte")
896898
(fsharp-mode . "fsharp")

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ nav:
174174
- TOML: page/lsp-toml.md
175175
- Trunk: page/lsp-trunk.md
176176
- TTCN3: page/lsp-ttcn3.md
177+
- TypeSpec: page/lsp-typespec.md
177178
- V: page/lsp-v.md
178179
- Vala: page/lsp-vala.md
179180
- Verilog/SystemVerilog (hdl-checker): page/lsp-verilog.md

0 commit comments

Comments
 (0)