|
| 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 |
0 commit comments