Skip to content

Commit 910341f

Browse files
authored
feat: Add ts-query (#4647)
* feat: Add ts-query * Add docs and autoload * wip: test settings * fix download url * fix: compile error
1 parent 8e03634 commit 910341f

File tree

5 files changed

+144
-1
lines changed

5 files changed

+144
-1
lines changed

CHANGELOG.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Add fennel support
2525
* Add support for [[https://github.com/nextflow-io/language-server][Nextflow]]
2626
* Add TypeSpec support
27+
* Add Tree-sitter query support
2728

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

clients/lsp-ts-query.el

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
;;; lsp-ts-query.el --- LSP client for tree-sitter query -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2024 emacs-lsp maintainers
4+
5+
;; This file is not part of GNU Emacs.
6+
7+
;; This program is free software: you can redistribute it and/or modify
8+
;; it under the terms of the GNU General Public License as published by
9+
;; the Free Software Foundation, either version 3 of the License, or
10+
;; (at your option) any later version.
11+
12+
;; This program is distributed in the hope that it will be useful,
13+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
;; GNU General Public License for more details.
16+
17+
;; You should have received a copy of the GNU General Public License
18+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
;;; Commentary:
21+
;;
22+
;; LSP client for tree-sitter query.
23+
;;
24+
25+
;;; Code:
26+
27+
(require 'lsp-mode)
28+
29+
(declare-function tree-sitter-langs--bin-dir "ext:tree-sitter-langs-build.el")
30+
31+
(defgroup lsp-ts-query nil
32+
"LSP client for tree-sitter query."
33+
:group 'lsp-mode
34+
:link '(url-link "https://github.com/ribru17/ts_query_ls"))
35+
36+
(defcustom lsp-ts-query-executable "ts_query_ls"
37+
"The ts-query-ls executable to use.
38+
39+
Leave as just the executable name to use the default behavior of finding the
40+
executable with variable `exec-path'."
41+
:group 'lsp-ts-query
42+
:type 'string)
43+
44+
(defcustom lsp-ts-query-parser-install-directories
45+
(cl-remove-if #'nil
46+
(vector (expand-file-name (locate-user-emacs-file "tree-sitter"))
47+
(and (featurep 'tree-sitter-langs)
48+
(tree-sitter-langs--bin-dir))))
49+
"Where to look for parsers, of the form <lang>.(so|dll|dylib) or
50+
tree-sitter-<lang>.wasm."
51+
:group 'lsp-ts-query
52+
:type '(vector string))
53+
54+
(defcustom lsp-ts-query-language-retrieval-patterns nil
55+
"A list of patterns to aid the LSP in finding a language, given a file
56+
path. Patterns must have one capture group which represents the language
57+
name. Ordered from highest to lowest precedence."
58+
:group 'lsp-ts-query
59+
:type '(vector string))
60+
61+
;;
62+
;;; Installation
63+
64+
(defcustom lsp-ts-query-server-store-path
65+
(expand-file-name "ts-query/" lsp-server-install-dir)
66+
"The path to the file in which ts-query will be stored."
67+
:type 'file
68+
:group 'lsp-ts-query)
69+
70+
(defconst lsp-ts-query--download-url-format
71+
"https://github.com/ribru17/ts_query_ls/releases/latest/download/ts_query_ls-%s-%s.%s"
72+
"Format to the download url link.")
73+
74+
(defun lsp-ts-query--url ()
75+
"Return Url points to the ts-query' zip/tar file."
76+
(let* ((x86 (string-prefix-p "x86_64" system-configuration))
77+
(arch (if x86 "x86_64" "aarch64")))
78+
(cl-case system-type
79+
((cygwin windows-nt ms-dos)
80+
(format lsp-ts-query--download-url-format arch "pc-windows-msvc" "zip"))
81+
(darwin
82+
(format lsp-ts-query--download-url-format arch "apple-darwin" "tar.gz"))
83+
(gnu/linux
84+
(format lsp-ts-query--download-url-format arch "unknown-linux-gnu" "tar.gz")))))
85+
86+
(defun lsp-ts-query--stored-executable ()
87+
"Return the stored ts-query executable.
88+
89+
This is differ from the variable `lsp-ts-query-executable'; this is local storage
90+
and not the global storage."
91+
(f-join lsp-ts-query-server-store-path
92+
(pcase system-type ('windows-nt "ts_query_ls.exe") (_ "ts_query_ls"))))
93+
94+
(lsp-register-custom-settings
95+
'(("ts_query_ls.settings.parser_install_directories" lsp-ts-query-parser-install-directories)
96+
("ts_query_ls.settings.language_retrieval_patterns" lsp-ts-query-language-retrieval-patterns)))
97+
98+
(lsp-dependency
99+
'ts-query-ls
100+
'(:system "ts_query_ls")
101+
`(:download :url ,(lsp-ts-query--url)
102+
:decompress ,(pcase system-type ('windows-nt :zip) (_ :targz))
103+
:store-path ,(f-join lsp-ts-query-server-store-path "temp")
104+
:set-executable? t)
105+
`(:system ,(lsp-ts-query--stored-executable)))
106+
107+
(defun lsp-ts-query--check-enabled (file-name _mode)
108+
"Check if the the language server should be enabled in this buffer
109+
or FILE-NAME."
110+
(let ((ext (file-name-extension file-name)))
111+
(when (and (string-match-p "/queries/" file-name)
112+
(equal "scm" ext))
113+
t)))
114+
115+
(lsp-register-client
116+
(make-lsp-client
117+
:new-connection (lsp-stdio-connection
118+
(lambda () (or (executable-find lsp-ts-query-executable)
119+
(lsp-ts-query--stored-executable)))
120+
(lambda ()
121+
(or (executable-find lsp-ts-query-executable)
122+
(file-executable-p (lsp-ts-query--stored-executable)))))
123+
:activation-fn #'lsp-ts-query--check-enabled
124+
:priority -1
125+
:add-on? t
126+
:server-id 'ts-query-ls
127+
:download-server-fn
128+
(lambda (_client callback error-callback _update?)
129+
(lsp-package-ensure 'ts-query-ls callback error-callback))))
130+
131+
(lsp-consistency-check lsp-ts-query)
132+
133+
(provide 'lsp-ts-query)
134+
;;; lsp-ts-query.el ends here

docs/lsp-clients.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,13 @@
11881188
"installation-url": "https://docs.trunk.io/check",
11891189
"debugger": "Not available"
11901190
},
1191+
{
1192+
"name": "ts-query",
1193+
"full-name": "Tree-sitter Query",
1194+
"server-name": "ts-query-lsp",
1195+
"server-url": "https://github.com/ribru17/ts_query_ls",
1196+
"debugger": "Not available"
1197+
},
11911198
{
11921199
"name": "typeprof",
11931200
"full-name": "Ruby (TypeProf)",

lsp-mode.el

Lines changed: 1 addition & 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-typespec lsp-v
192+
lsp-tex lsp-tilt lsp-toml lsp-trunk lsp-ts-query 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."

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ nav:
173173
- TeX, LaTeX, etc (texlab, external): page/lsp-latex.md
174174
- Tilt: page/lsp-tilt.md
175175
- TOML: page/lsp-toml.md
176+
- Tree-sitter Query: page/lsp-ts-query.md
176177
- Trunk: page/lsp-trunk.md
177178
- TTCN3: page/lsp-ttcn3.md
178179
- TypeSpec: page/lsp-typespec.md

0 commit comments

Comments
 (0)