config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

lsp-trunk.el (3000B)


      1 ;;; lsp-trunk.el --- trunk support -*- lexical-binding: t; -*-
      2 ;;
      3 ;; Copyright (C) 2024 Trunk Technologies, Inc.
      4 ;;
      5 ;; Author: Tyler Jang <tyler@trunk.io>
      6 ;; Keywords: trunk, lsp, meta-linter
      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 ;; This file is not part of GNU Emacs.
     21 ;;
     22 ;;; Commentary:
     23 ;;
     24 ;;  Trunk support for lsp-mode
     25 ;;
     26 ;;; Code:
     27 
     28 (require 'lsp-mode)
     29 
     30 (defgroup lsp-trunk nil
     31   "LSP support for Trunk."
     32   :group 'lsp-mode
     33   :link `(url-link "https://docs.trunk.io"))
     34 
     35 (defcustom lsp-trunk-executable "trunk"
     36   "Path to the trunk executable"
     37   :group 'lsp-trunk
     38   :type 'string)
     39 
     40 (defcustom lsp-trunk-args '("lsp-proxy")
     41   "Additional arguments to pass to the trunk startup"
     42   :group 'lsp-trunk
     43   :type '(repeat string))
     44 
     45 (defun lsp-trunk-check-for-init (filename &optional _)
     46   "Check if the file exists in a workspace that has a .trunk/trunk.yaml"
     47   (let ((dir (file-name-directory filename))
     48         (trunk-file ".trunk/trunk.yaml"))
     49     (locate-dominating-file dir trunk-file)))
     50 
     51 (defun lsp-trunk-check-disable (command)
     52   "Disable a linter in your repo."
     53   (shell-command
     54    (concat lsp-trunk-executable " check disable "
     55            (mapconcat 'identity (gethash "arguments" command) " "))))
     56 
     57 (defun lsp-trunk-check-enable (command)
     58   "Enable a linter in your repo."
     59   (shell-command
     60    (concat lsp-trunk-executable " check enable "
     61            (mapconcat 'identity (gethash "arguments" command) " "))))
     62 
     63 (defun lsp-trunk-open-config (&optional _command)
     64   "Open the trunk config file."
     65   (find-file ".trunk/trunk.yaml"))
     66 
     67 (lsp-register-client
     68  (make-lsp-client
     69   :activation-fn #'lsp-trunk-check-for-init
     70   :new-connection (lsp-stdio-connection 
     71                    (lambda () (append (list lsp-trunk-executable) lsp-trunk-args)))
     72   :server-id 'trunk-lsp
     73   :initialization-options (lambda ()
     74                             (list
     75                              :version "0.1.0"
     76                              :clientType "emacs"
     77                              :clientVersion (symbol-value 'emacs-version)))
     78   :notification-handlers (ht ("$/progress" #'ignore))
     79   :action-handlers (ht ("trunk.checkDisable" #'lsp-trunk-check-disable)
     80                        ("trunk.checkEnable" #'lsp-trunk-check-enable)
     81                        ("trunk.openConfigFile" #'lsp-trunk-open-config))
     82   :priority -2
     83   :add-on? t))
     84 
     85 (lsp-consistency-check lsp-trunk)
     86 
     87 (provide 'lsp-trunk)
     88 ;;; lsp-trunk.el ends here