config

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

lsp-ada.el (7769B)


      1 ;;; lsp-ada.el --- description -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020 emacs-lsp maintainers
      4 
      5 ;; Author: emacs-lsp maintainers
      6 ;; Keywords: lsp, ada
      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 ;; LSP Clients for the Ada Programming Language
     24 
     25 ;;; Code:
     26 
     27 (require 'lsp-mode)
     28 (require 'lsp-semantic-tokens)
     29 
     30 (defgroup lsp-ada nil
     31   "Settings for Ada Language Server."
     32   :group 'tools
     33   :tag "Language Server"
     34   :package-version '(lsp-mode . "6.2"))
     35 
     36 (lsp-defcustom lsp-ada-project-file "default.gpr"
     37   "Set the project file full path to configure the language server with.
     38   The ~ prefix (for the user home directory) is supported.
     39   See https://github.com/AdaCore/ada_language_server for a per-project
     40   configuration example."
     41   :type 'string
     42   :group 'lsp-ada
     43   :package-version '(lsp-mode . "6.2")
     44   :lsp-path "ada.projectFile")
     45 
     46 (lsp-defcustom lsp-ada-option-charset "UTF-8"
     47   "The charset to use by the Ada Language server. Defaults to 'UTF-8'."
     48   :type 'string
     49   :group 'lsp-ada
     50   :package-version '(lsp-mode . "6.2")
     51   :lsp-path "ada.defaultCharset")
     52 
     53 (lsp-defcustom lsp-ada-enable-diagnostics t
     54   "A boolean to disable diagnostics. Defaults to true."
     55   :type 'boolean
     56   :group 'lsp-ada
     57   :package-version '(lsp-mode . "6.2")
     58   :lsp-path "ada.enableDiagnostics")
     59 
     60 (defcustom lsp-ada-als-executable "ada_language_server"
     61   "Command to start the Ada language server."
     62   :group 'lsp-ada
     63   :risky t
     64   :type 'file)
     65 
     66 (defcustom lsp-ada-alire-executable "alr"
     67   "The alire executable to run when a project is detected."
     68   :type 'string
     69   :group 'lsp-ada
     70   :package-version '(lsp-mode "9.0.0"))
     71 
     72 (defcustom lsp-ada-semantic-token-face-overrides
     73   '(("namespace" . default)
     74     ("modifier"  . lsp-face-semhl-keyword))
     75   "Semantic token face overrides to be applied."
     76   :type '(alist :key-type string
     77                 :value-type (choice (face  :tag "Face")
     78                                     (const :tag "No Face" nil)))
     79   :group 'lsp-ada
     80   :package-version '(lsp-mode "9.0.0"))
     81 
     82 (defcustom lsp-ada-semantic-token-modifier-face-overrides
     83   '(("declaration")
     84     ("definition")
     85     ("implementation")
     86     ("static")
     87     ("modification")
     88     ("documentation")
     89     ("defaultLibrary"))
     90   "Semantic token modifier face overrides to be applied."
     91   :type '(alist :key-type string
     92                 :value-type (choice (face  :tag "Face")
     93                                     (const :tag "No Face" nil)))
     94   :group 'lsp-ada
     95   :package-version '(lsp-mode "9.0.0"))
     96 
     97 (defvar lsp-ada--als-download-url-cache nil)
     98 
     99 (defvar lsp-ada--als-downloaded-executable
    100   (f-join lsp-server-install-dir
    101           "ada-ls"
    102           (symbol-name (lsp-resolve-value lsp--system-arch))
    103           (pcase system-type
    104             ('gnu/linux  "linux")
    105             ('darwin     "darwin")
    106             ('windows-nt "win32")
    107             (_           "linux"))
    108           (concat "ada_language_server"
    109                   (pcase system-type
    110                     ('windows-nt ".exe")
    111                     (_ "")))))
    112 
    113 (defun lsp-ada--als-latest-release-url ()
    114   "URL for the latest release of the Ada Language Server."
    115   (setq lsp-ada--als-download-url-cache
    116         (lsp--find-latest-gh-release-url
    117          "https://api.github.com/repos/AdaCore/ada_language_server/releases/latest"
    118          (format "%s.zip"
    119                  (pcase (list system-type (lsp-resolve-value lsp--system-arch))
    120                    ('(gnu/linux  x64)   "Linux_amd64")
    121                    ('(gnu/linux  arm64) "Linux_aarch64")
    122                    ('(darwin     x64)   "macOS_amd64")
    123                    ('(darwin     arm64) "macOS_aarch64")
    124                    ('(windows-nt x64)   "Windows_amd64")
    125                    (`(,_         x64)   "Linux_amd64"))))))
    126 
    127 (defun lsp-ada--als-store-path ()
    128   "Store Path for the downloaded Ada Language Server."
    129   (f-join lsp-server-install-dir
    130           "ada-ls"
    131           (file-name-base (or lsp-ada--als-download-url-cache
    132                               (lsp-ada--als-latest-release-url)
    133                               "ada-ls"))))
    134 
    135 (defun lsp-ada--environment ()
    136   "Add environmental variables if needed."
    137   (let ((project-root (lsp-workspace-root)))
    138     ;; When there is an alire project, include its environment
    139     (when (file-exists-p
    140            (concat (file-name-as-directory project-root)
    141                    "alire.toml"))
    142       (let ((alr-executable (executable-find lsp-ada-alire-executable)))
    143         (if alr-executable
    144             ;; Transform output variables to environment
    145             (let ((env-output (shell-command-to-string (concat alr-executable " printenv --unix"))))
    146               (let ((var-strings (split-string env-output "\n")))
    147                 (mapcar (lambda (string)
    148                           (if (string-match (rx "export" space (group (one-or-more ascii)) "=" "\"" (group (one-or-more ascii)) "\"") string)
    149                               (let ((var-name (match-string 1 string))
    150                                     (var-value (match-string 2 string)))
    151                                 (cons var-name var-value))))
    152                         var-strings)))
    153           (lsp--error "Found alire.toml but the executable %s could not be found" alr-executable))))))
    154 
    155 (lsp-dependency
    156  'ada-ls
    157  '(:download :url lsp-ada--als-latest-release-url
    158              :store-path lsp-ada--als-store-path
    159              :decompress :zip
    160              :binary-path lsp-ada--als-downloaded-executable
    161              :set-executable? t)
    162  '(:system lsp-ada-als-executable))
    163 
    164 (lsp-register-client
    165  (make-lsp-client :new-connection (lsp-stdio-connection
    166                                    (lambda () (lsp-package-path 'ada-ls)))
    167                   :major-modes '(ada-mode ada-ts-mode)
    168                   :priority -1
    169                   :initialized-fn (lambda (workspace)
    170                                     (with-lsp-workspace workspace
    171                                       (lsp--set-configuration
    172                                        (lsp-configuration-section "ada"))))
    173                   :download-server-fn (lambda (_client callback error-callback _update?)
    174                                         (lsp-package-ensure 'ada-ls callback error-callback))
    175                   :semantic-tokens-faces-overrides `( :types ,lsp-ada-semantic-token-face-overrides
    176                                                       :modifiers ,lsp-ada-semantic-token-modifier-face-overrides)
    177                   :server-id 'ada-ls
    178                   :synchronize-sections '("ada")
    179                   :environment-fn 'lsp-ada--environment))
    180 
    181 (lsp-register-client
    182  (make-lsp-client :new-connection (lsp-stdio-connection
    183                                    (lambda () (list (lsp-package-path 'ada-ls)
    184                                                     "--language-gpr")))
    185                   :major-modes '(gpr-mode gpr-ts-mode)
    186                   :priority -1
    187                   :download-server-fn (lambda (_client callback error-callback _update?)
    188                                         (lsp-package-ensure 'ada-ls callback error-callback))
    189                   :server-id 'gpr-ls
    190                   :environment-fn #'lsp-ada--environment))
    191 
    192 (lsp-consistency-check lsp-ada)
    193 
    194 (provide 'lsp-ada)
    195 ;;; lsp-ada.el ends here