config

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

lsp-golangci-lint.el (6511B)


      1 ;;; lsp-golangci-lint.el --- golangci-lint-langserver Client settings -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2023  Jim Myhrberg
      4 
      5 ;; Author: Jim Myhrberg
      6 ;; Keywords: lsp, go, golang, golangci-lint
      7 
      8 ;; This file is not part of GNU Emacs
      9 
     10 ;;; License:
     11 ;;
     12 ;; This program is free software: you can redistribute it and/or modify
     13 ;; it under the terms of the GNU General Public License as published by
     14 ;; the Free Software Foundation, either version 3 of the License, or
     15 ;; (at your option) any later version.
     16 ;;
     17 ;; This program is distributed in the hope that it will be useful,
     18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 ;; GNU General Public License for more details.
     21 ;;
     22 ;; You should have received a copy of the GNU General Public License
     23 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     24 
     25 ;;; Commentary:
     26 ;;
     27 ;; lsp-golangci-lint client
     28 
     29 ;;; Code:
     30 
     31 (require 'lsp-mode)
     32 (require 'lsp-go)
     33 (require 'cl-lib)
     34 
     35 (defgroup lsp-golangci-lint nil
     36   "Configuration options for lsp-golangci-lint."
     37   :group 'lsp-mode
     38   :link '(url-lint "https://github.com/nametake/golangci-lint-langserver")
     39   :package-version '(lsp-mode . "9.0.0"))
     40 
     41 (defcustom lsp-golangci-lint-server-path "golangci-lint-langserver"
     42   "Command to run golangci-lint-langserver."
     43   :type 'string
     44   :package-version '(lsp-mode . "9.0.0"))
     45 
     46 (defcustom lsp-golangci-lint-server-debug nil
     47   "Whether to run golangci-lint-langserver in debug mode or not."
     48   :type 'boolean
     49   :package-version '(lsp-mode . "9.0.0"))
     50 
     51 (defcustom lsp-golangci-lint-server-args nil
     52   "Arguments to pass to golangci-lint-langserver."
     53   :type '(repeat string)
     54   :package-version '(lsp-mode . "9.0.0"))
     55 
     56 (defcustom lsp-golangci-lint-path "golangci-lint"
     57   "Command to run golangci-lint."
     58   :type 'string
     59   :package-version '(lsp-mode . "9.0.0"))
     60 
     61 (defcustom lsp-golangci-lint-allow-parallel-runners t
     62   "If not nil, pass --allow-parallel-runners flag to golangci-lint run."
     63   :type 'boolean
     64   :package-version '(lsp-mode . "9.0.0"))
     65 
     66 (defcustom lsp-golangci-lint-build-tags nil
     67   "If non-empty list, pass as --build-tags flag value to golangci-lint run."
     68   :type '(repeat string)
     69   :package-version '(lsp-mode . "9.0.0"))
     70 
     71 (defcustom lsp-golangci-lint-fast nil
     72   "If not nil, pass --fast flag to golangci-lint run."
     73   :type 'boolean
     74   :package-version '(lsp-mode . "9.0.0"))
     75 
     76 (defcustom lsp-golangci-lint-enable-all nil
     77   "If not nil, pass --enable-all flag to golangci-lint run."
     78   :type 'boolean
     79   :package-version '(lsp-mode . "9.0.0"))
     80 
     81 (defcustom lsp-golangci-lint-enable nil
     82   "If non-empty list, pass as --enable flag value to golangci-lint run."
     83   :type '(repeat string)
     84   :package-version '(lsp-mode . "9.0.0"))
     85 
     86 (defcustom lsp-golangci-lint-disable-all nil
     87   "If not nil, pass --disable-all to golangci-lint run."
     88   :type 'boolean
     89   :package-version '(lsp-mode . "9.0.0"))
     90 
     91 (defcustom lsp-golangci-lint-disable nil
     92   "If non-empty list, pass as --disable flag value to golangci-lint run."
     93   :type '(repeat string)
     94   :package-version '(lsp-mode . "9.0.0"))
     95 
     96 (defcustom lsp-golangci-lint-config nil
     97   "If set, pass value as --config flag to golangci-lint run."
     98   :type 'string
     99   :package-version '(lsp-mode . "9.0.0"))
    100 
    101 (defcustom lsp-golangci-lint-no-config nil
    102   "If not nil, pass --no-config flag to golangci-lint run."
    103   :type 'boolean
    104   :package-version '(lsp-mode . "9.0.0"))
    105 
    106 (defcustom lsp-golangci-lint-run-args nil
    107   "Arguments to pass to golangci-lint run command."
    108   :type '(repeat string)
    109   :package-version '(lsp-mode . "9.0.0"))
    110 
    111 (defun lsp-golangci-lint-server--stdio-command ()
    112   "Return the command and args to start golangci-lint-langserver."
    113   (let ((args (list lsp-golangci-lint-server-path)))
    114     (when (and (listp lsp-golangci-lint-server-args)
    115                (> (length lsp-golangci-lint-server-args) 0))
    116       (setq args (append args lsp-golangci-lint-server-args)))
    117     (when lsp-golangci-lint-server-debug
    118       (setq args (append args '("-debug"))))
    119     args))
    120 
    121 (defun lsp-golangci-lint--run-args ()
    122   "Return the arguments to pass to golangci-lint run command."
    123   (let* ((tags (string-join lsp-golangci-lint-build-tags " "))
    124          (enable (string-join lsp-golangci-lint-enable ","))
    125          (disable (string-join lsp-golangci-lint-disable ","))
    126          (args (cl-loop for (condition flag value) in
    127                         `((,lsp-golangci-lint-fast "--fast" nil)
    128                           (,(not (string-empty-p tags)) "--build-tags" ,tags)
    129                           (,lsp-golangci-lint-enable-all "--enable-all" nil)
    130                           (,lsp-golangci-lint-disable-all "--disable-all" nil)
    131                           (,(not (string-empty-p enable)) "--enable" ,enable)
    132                           (,(not (string-empty-p disable)) "--disable" ,disable)
    133                           (,lsp-golangci-lint-allow-parallel-runners
    134                            "--allow-parallel-runners" nil)
    135                           (,(and (stringp lsp-golangci-lint-config)
    136                                  (not (string-empty-p lsp-golangci-lint-config)))
    137                            "--config" lsp-golangci-lint-config))
    138                         when condition
    139                         append (if value (list flag value) (list flag)))))
    140     (when (and (listp lsp-golangci-lint-run-args)
    141                (> (length lsp-golangci-lint-run-args) 0))
    142       (setq args (append args lsp-golangci-lint-run-args)))
    143     args))
    144 
    145 (defun lsp-golangci-lint--get-initialization-options ()
    146   "Return initialization options for golangci-lint-langserver."
    147   (let ((opts (make-hash-table :test 'equal))
    148         (command (vconcat `(,lsp-golangci-lint-path)
    149                           ["run" "--out-format=json" "--issues-exit-code=1"]
    150                           (lsp-golangci-lint--run-args))))
    151     (puthash "command" command opts)
    152     opts))
    153 
    154 (lsp-register-client
    155  (make-lsp-client :new-connection (lsp-stdio-connection
    156                                    #'lsp-golangci-lint-server--stdio-command)
    157                   :activation-fn (lsp-activate-on "go")
    158                   :language-id "go"
    159                   :priority 0
    160                   :server-id 'golangci-lint
    161                   :add-on? t
    162                   :library-folders-fn #'lsp-go--library-default-directories
    163                   :initialization-options #'lsp-golangci-lint--get-initialization-options))
    164 
    165 (lsp-consistency-check lsp-golangci-lint)
    166 
    167 (provide 'lsp-golangci-lint)
    168 ;;; lsp-golangci-lint.el ends here