lsp-graphql.el (3263B)
1 ;;; lsp-graphql.el --- lsp client for graphql -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2021 Binbin Ye 4 5 ;; Author: Binbin Ye 6 ;; Keywords: lsp, graphql 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 ;; Support for running graphql lsp. Support multiple server running at the same time when editing tsx/jsx. 24 25 ;;; Code: 26 27 (require 'lsp-mode) 28 29 (lsp-dependency 'graphql-language-service-cli 30 '(:system "graphql-lsp") 31 '(:npm :package "graphql-language-service-cli" 32 :path "graphql-lsp")) 33 34 35 (defgroup lsp-graphql nil 36 "LSP support for the GraphQL, using the graphql-language-service-cli as language server." 37 :link '(url-link "https://github.com/graphql/graphiql/tree/main/packages/graphql-language-service-cli#readme") 38 :group 'lsp-mode) 39 40 (defcustom lsp-clients-graphql-server-args '("server" "--method=stream") 41 "CLI arguments for graphql language server." 42 :type '(repeat string) 43 :risky t 44 :group 'lsp-graphql) 45 46 (defcustom lsp-graphql-target-file-extensions '("ts" "js" "jsx" "tsx" "vue" "graphql" "graphqls" "gql") 47 "List of target file extensions for the GraphQL language server." 48 :type '(repeat string) 49 :group 'lsp-graphql) 50 51 (defun lsp-graphql-activate-p (filename &optional _) 52 "Check if the GraphQL language server should be enabled based on FILENAME." 53 (let ((target-extensions (mapconcat 'identity lsp-graphql-target-file-extensions "\\|"))) 54 (or (string-match-p (format "\\.%s\\'" target-extensions) filename) 55 (and (derived-mode-p 'js-mode 'js2-mode 'typescript-mode 'typescript-ts-mode) 56 (not (derived-mode-p 'json-mode)))))) 57 58 59 (lsp-register-client 60 (make-lsp-client :new-connection (lsp-stdio-connection (lambda() 61 (cons (lsp-package-path 'graphql-language-service-cli) 62 lsp-clients-graphql-server-args))) 63 :major-modes '(graphql-mode) 64 :language-id "graphql" 65 :server-id 'graphql-lsp 66 :priority -3 67 :add-on? t 68 :multi-root t 69 :activation-fn 'lsp-graphql-activate-p 70 :download-server-fn (lambda (_client callback error-callback _update?) 71 (lsp-package-ensure 72 'graphql-language-service-cli 73 callback 74 error-callback)))) 75 76 (lsp-consistency-check lsp-graphql) 77 78 (provide 'lsp-graphql) 79 ;;; lsp-graphql.el ends here