config

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

lsp-ansible.el (10324B)


      1 ;;; lsp-ansible.el --- lsp-mode ansible integration -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2022 emacs-lsp maintainers
      4 
      5 ;; Author: lsp-mode maintainers
      6 ;; Keywords: lsp, yaml, ansible
      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 Client for the Ansible Language
     24 
     25 ;;; Code:
     26 
     27 (require 'lsp-mode)
     28 (require 'json)
     29 
     30 ;;; Ansible
     31 (defgroup lsp-ansible nil
     32   "Settings for the Ansible Language Server."
     33   :group 'lsp-mode
     34   :link '(url-link "https://github.com/ansible/ansible-language-server")
     35   :package-version '(lsp-mode . "9.0.0"))
     36 
     37 (defcustom lsp-ansible-add-on? t
     38   "Make the client `add-on' so that it works with other language servers.
     39 `yamlls`is a common one.
     40 
     41 Enabled by default."
     42   :type 'boolean
     43   :group 'lsp-ansible
     44   :package-version '(lsp-mode . "9.0.0"))
     45 
     46 (defcustom lsp-ansible-language-server-command
     47   '("ansible-language-server" "--stdio")
     48   "The command that starts the ansible language server."
     49   :type '(repeat :tag "List of string values" string)
     50   :group 'lsp-ansible
     51   :package-version '(lsp-mode . "9.0.0"))
     52 
     53 (defcustom lsp-ansible-ansible-path "ansible"
     54   "Path to the ansible executable.
     55 $PATH is searched for the executable."
     56   :type 'string
     57   :group 'lsp-ansible
     58   :package-version '(lsp-mode . "9.0.0"))
     59 
     60 (defcustom lsp-ansible-use-fully-qualified-collection-names t
     61   "Toggles use of fully qualified collection names when inserting a module name.
     62 Disabling it will only use FQCNs when necessary, that is when the collection is
     63 not configured for the task."
     64   :type 'boolean
     65   :group 'lsp-ansible
     66   :package-version '(lsp-mode . "9.0.0"))
     67 
     68 (defcustom lsp-ansible-validation-enabled t
     69   "Toggle validation provider.
     70 If enabled and ansible-lint is disabled, validation falls back to
     71 ansible-playbook --syntax-check."
     72   :type 'boolean
     73   :group 'lsp-ansible
     74   :package-version '(lsp-mode . "9.0.0"))
     75 
     76 (defcustom lsp-ansible-validation-lint-arguments ""
     77   "Optional command line arguments to be appended to ansible-lint invocation.
     78 See ansible-lint documentation."
     79   :type 'string
     80   :group 'lsp-ansible
     81   :package-version '(lsp-mode . "9.0.0"))
     82 
     83 (defcustom lsp-ansible-validation-lint-enabled t
     84   "Enables/disables use of ansible-lint."
     85   :type 'boolean
     86   :group 'lsp-ansible
     87   :package-version '(lsp-mode . "9.0.0"))
     88 
     89 (defcustom lsp-ansible-validation-lint-path "ansible-lint"
     90   "Path to the ansible-lint executable.
     91 $PATH is searched for the executable."
     92   :type 'string
     93   :group 'lsp-ansible
     94   :package-version '(lsp-mode . "9.0.0"))
     95 
     96 (defcustom lsp-ansible-completion-provide-redirect-modules t
     97   "Toggle redirected module provider when completing modules."
     98   :type 'boolean
     99   :group 'lsp-ansible
    100   :package-version '(lsp-mode . "9.0.0"))
    101 
    102 (defcustom lsp-ansible-completion-provide-module-option-aliases t
    103   "Toggle alias provider when completing module options."
    104   :type 'boolean
    105   :group 'lsp-ansible
    106   :package-version '(lsp-mode . "9.0.0"))
    107 
    108 (defcustom lsp-ansible-execution-environment-container-engine "auto"
    109   "The container engine to be used while running with execution environment.
    110 Valid values are auto, podman and docker.  For auto it will look for podman then
    111 docker."
    112   :type '(choice (const "auto")
    113                  (const "podman")
    114                  (const "docker"))
    115   :group 'lsp-ansible
    116   :package-version '(lsp-mode . "9.0.0"))
    117 
    118 (defcustom lsp-ansible-execution-environment-enabled nil
    119   "Enable or disable the use of an execution environment."
    120   :type 'boolean
    121   :group 'lsp-ansible
    122   :package-version '(lsp-mode . "9.0.0"))
    123 
    124 (defcustom lsp-ansible-execution-environment-image "quay.io/ansible/creator-ee:latest"
    125   "Specify the name of the execution environment image."
    126   :type 'string
    127   :group 'lsp-ansible
    128   :package-version '(lsp-mode . "9.0.0"))
    129 
    130 (defcustom lsp-ansible-execution-environment-pull-policy "missing"
    131   "Specify the image pull policy.
    132 Valid values are always, missing, never and tag.  Setting always will always
    133 pull the image when extension is activated or reloaded.  Setting missing will
    134 pull if not locally available.  Setting never will never pull the image and
    135 setting tag will always pull if the image tag is ‘latest’, otherwise pull
    136 if not locally available."
    137   :type '(choice (const "always")
    138                  (const "missing")
    139                  (const "never")
    140                  (const "tag"))
    141   :group 'lsp-ansible
    142   :package-version '(lsp-mode . "9.0.0"))
    143 
    144 (defcustom lsp-ansible-execution-environment-pull-arguments ""
    145   "Specify any additional parameters for the pull command.
    146 Example: ‘--tls-verify=false’"
    147   :type 'string
    148   :group 'lsp-ansible
    149   :package-version '(lsp-mode . "9.0.0"))
    150 
    151 (defcustom lsp-ansible-execution-environment-container-options ""
    152   "Extra parameters passed to the container engine command.
    153 Example: ‘-–net=host’"
    154   :type 'string
    155   :group 'lsp-ansible
    156   :package-version '(lsp-mode . "9.0.0"))
    157 
    158 (defcustom lsp-ansible-execution-environment-volume-mounts []
    159   "Additonnal volumes to mount in container.
    160 The value is a vector of plists.  Expected properties are:
    161 - src: the name of the local volume or path to be mounted within execution
    162   environment
    163 - dest: the path where the file or directory are mounted in the container
    164 - options: the property is optional, and is a comma-separated list of options.
    165   Example: ro,Z"
    166   :type '(lsp-repeatable-vector plist)
    167   :group 'lsp-ansible
    168   :package-version '(lsp-mode . "9.0.0"))
    169 
    170 (defcustom lsp-ansible-python-interpreter-path ""
    171   "Path to the python/python3 executable.
    172 This setting may be used to make the extension work with ansible and
    173 ansible-lint installations in a Python virtual environment."
    174   :type 'string
    175   :group 'lsp-ansible
    176   :package-version '(lsp-mode . "9.0.0"))
    177 
    178 (defcustom lsp-ansible-python-activation-script ""
    179   "Path to a custom activate script.
    180 It will be used instead of `lsp-ansible-python-interpreter-path' to run in a
    181 Python virtual environment."
    182   :type 'string
    183   :group 'lsp-ansible
    184   :package-version '(lsp-mode . "9.0.0"))
    185 
    186 (lsp-dependency 'ansible-language-server
    187                 '(:system "ansible-language-server")
    188                 '(:npm :package "@ansible/ansible-language-server"
    189                        :path "ansible-language-server"))
    190 
    191 (lsp-register-custom-settings
    192  '(("ansible.ansible.path" lsp-ansible-ansible-path)
    193    ("ansible.ansible.useFullyQualifiedCollectionNames" lsp-ansible-use-fully-qualified-collection-names t)
    194    ("ansible.validation.enabled" lsp-ansible-validation-enabled t)
    195    ("ansible.validation.lint.arguments" lsp-ansible-validation-lint-arguments)
    196    ("ansible.validation.lint.enabled" lsp-ansible-validation-lint-enabled t)
    197    ("ansible.validation.lint.path" lsp-ansible-validation-lint-path)
    198    ("ansible.completion.provideRedirectModules" lsp-ansible-completion-provide-redirect-modules t)
    199    ("ansible.completion.provideModuleOptionAliases" lsp-ansible-completion-provide-module-option-aliases t)
    200    ("ansible.executionEnvironment.containerEngine" lsp-ansible-execution-environment-container-engine)
    201    ("ansible.executionEnvironment.enabled" lsp-ansible-execution-environment-enabled t)
    202    ("ansible.executionEnvironment.image" lsp-ansible-execution-environment-image)
    203    ("ansible.executionEnvironment.pull.policy" lsp-ansible-execution-environment-pull-policy)
    204    ("ansible.executionEnvironment.pull.arguments" lsp-ansible-execution-environment-pull-arguments)
    205    ("ansible.executionEnvironment.containerOptions" lsp-ansible-execution-environment-container-options)
    206    ("ansible.executionEnvironment.volumeMounts" lsp-ansible-execution-environment-volume-mounts)
    207    ("ansible.python.interpreterPath" lsp-ansible-python-interpreter-path)
    208    ("ansible.python.activationScript" lsp-ansible-python-activation-script)))
    209 
    210 (defun lsp-ansible-check-ansible-minor-mode (&rest _)
    211   "Check whether ansible minor mode is active.
    212 This prevents the Ansible server from being turned on in all yaml files."
    213   (and (or (derived-mode-p 'yaml-mode)
    214            (derived-mode-p 'yaml-ts-mode))
    215        ;; emacs-ansible provides ansible, not ansible-mode
    216        (with-no-warnings (bound-and-true-p ansible))))
    217 
    218 (declare-function lsp-completion--clear-cache "lsp-completion" (&optional keep-last-result))
    219 
    220 (defun lsp-ansible-resync-inventory ()
    221   "Resync the inventory cache used by Ansible Language Server for hosts completion."
    222   (interactive)
    223   (lsp-notify "resync/ansible-inventory" nil)
    224   (require 'lsp-completion)
    225   (lsp-completion--clear-cache))
    226 
    227 (defun lsp-ansible-update-metadata-handler (_workspace params)
    228   "Handler for `update/ansible-metadata' notification.
    229 Pretty print the content of PARAMS."
    230   (let ((json-encoding-pretty-print t))
    231     (message "Ansible Language Server metadata: %s" (json-encode params))))
    232 
    233 (defun lsp-ansible-show-server-metadata ()
    234   "Show information about Ansible environment used by the Ansible Language Server."
    235   (interactive)
    236   (lsp-notify "update/ansible-metadata" nil))
    237 
    238 (lsp-register-client
    239  (make-lsp-client
    240   :new-connection (lsp-stdio-connection
    241                    (lambda ()
    242                      `(,(or (executable-find
    243                              (cl-first lsp-ansible-language-server-command))
    244                             (lsp-package-path 'ansible-language-server))
    245                        ,@(cl-rest lsp-ansible-language-server-command))))
    246   :priority 1
    247   :add-on? lsp-ansible-add-on?
    248   :multi-root t
    249   :notification-handlers (ht ("update/ansible-metadata" #'lsp-ansible-update-metadata-handler))
    250   :activation-fn #'lsp-ansible-check-ansible-minor-mode
    251   :server-id 'ansible-ls
    252   :download-server-fn (lambda (_client callback error-callback _update?)
    253                         (lsp-package-ensure 'ansible-language-server callback error-callback))))
    254 
    255 (lsp-consistency-check lsp-ansible)
    256 
    257 (provide 'lsp-ansible)
    258 ;;; lsp-ansible.el ends here