aboutsummaryrefslogtreecommitdiffstats
path: root/elpa/lsp-mode-20220505.630/lsp-ansible.el
blob: 8917b6598817f61a93b692cd3cb8f15c506412cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; lsp-ansible.el --- lsp-mode ansible integration -*- lexical-binding: t; -*-

;; Copyright (C) 2022 emacs-lsp maintainers

;; Author: lsp-mode maintainers
;; Keywords: lsp, yaml, ansible

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; LSP Client for the Ansible Language

;;; Code:

(require 'lsp-mode)

;;; Ansible
(defgroup lsp-ansible nil
  "Settings for the Ansible Language Server."
  :group 'lsp-mode
  :link '(url-link "https://github.com/ansible/ansible-language-server")
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-language-server-command
  '("ansible-language-server" "--stdio")
  "The command that starts the ansible language server."
  :type '(repeat :tag "List of string values" string)
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-ansible-path "ansible"
  "Path to the ansible executable.
$PATH is searched for the executable."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-use-fully-qualified-collection-names t
  "Toggles use of fully qualified collection names when inserting a module name.
Disabling it will only use FQCNs when necessary, that is when the collection is
not configured for the task."
  :type 'boolean
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-ansible-lint-arguments ""
  "Optional command line arguments to be appended to ansible-lint invocation.
See ansible-lint documentation."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-ansible-lint-enabled t
  "Enables/disables use of ansible-lint."
  :type 'boolean
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-ansible-lint-path "ansible-lint"
  "Path to the ansible-lint executable.
$PATH is searched for the executable."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-ansible-navigator-path "ansible-navigator"
  "Path to the ansible-navigator executable."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-ansible-playbook-path "ansible-playbook"
  "Path to the ansible-playbook executable."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-execution-environment-container-engine "auto"
  "The container engine to be used while running with execution environment.
Valid values are auto, podman and docker.  For auto it will look for podman then
docker."
  :type '(choice (const "auto")
                 (const "podman")
                 (const "docker"))
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-execution-environment-enabled nil
  "Enable or disable the use of an execution environment."
  :type 'boolean
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-execution-environment-image "quay.io/ansible/creator-ee:latest"
  "Specify the name of the execution environment image."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-execution-environment-pull-policy "missing"
  "Specify the image pull policy.
Valid values are always, missing, never and tag.  Setting always will always
pull the image when extension is activated or reloaded.  Setting missing will
pull if not locally available.  Setting never will never pull the image and
setting tag will always pull if the image tag is ‘latest’, otherwise pull
if not locally available."
  :type '(choice (const "always")
                 (const "missing")
                 (const "never")
                 (const "tag"))
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-python-interpreter-path ""
  "Path to the python/python3 executable.
This setting may be used to make the extension work with ansible and
ansible-lint installations in a Python virtual environment."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-ansible-python-activation-script ""
  "Path to a custom activate script.
It will be used instead of `lsp-ansible-python-interpreter-path' to run in a
Python virtual environment."
  :type 'string
  :group 'lsp-ansible
  :package-version '(lsp-mode . "8.0.1"))

(lsp-dependency 'ansible-language-server
                '(:system "ansible-language-server")
                '(:npm :package "@ansible/ansible-language-server"
                       :path "ansible-language-server"))

(lsp-register-custom-settings
 '(("ansible.ansible.path" lsp-ansible-ansible-path)
   ("ansible.ansible.useFullyQualifiedCollectionNames" lsp-ansible-use-fully-qualified-collection-names t)
   ("ansible.ansibleLint.arguments" lsp-ansible-ansible-lint-arguments)
   ("ansible.ansibleLint.enabled" lsp-ansible-ansible-lint-enabled t)
   ("ansible.ansibleLint.path" lsp-ansible-ansible-lint-path)
   ("ansible.ansibleNavigator.path" lsp-ansible-ansible-navigator-path)
   ("ansible.ansiblePlaybook.path" lsp-ansible-ansible-playbook-path)
   ("ansible.executionEnvironment.containerEngine" lsp-ansible-execution-environment-container-engine)
   ("ansible.executionEnvironment.enabled" lsp-ansible-execution-environment-enabled t)
   ("ansible.executionEnvironment.image" lsp-ansible-execution-environment-image)
   ("ansible.executionEnvironment.pullPolicy" lsp-ansible-execution-environment-pull-policy)
   ("ansible.python.interpreterPath" lsp-ansible-python-interpreter-path)
   ("ansible.python.activationScript" lsp-ansible-python-activation-script)))

(defun lsp-ansible-check-ansible-minor-mode (&rest _)
  "Check whether ansible minor mode is active.
This prevents the Ansible server from being turned on in all yaml files."
  (and (eq major-mode 'yaml-mode)
       ;; emacs-ansible provides ansible, not ansible-mode
       (with-no-warnings (bound-and-true-p ansible))))

(lsp-register-client
 (make-lsp-client
  :new-connection (lsp-stdio-connection
                   (lambda ()
                     `(,(or (executable-find
                             (cl-first lsp-ansible-language-server-command))
                            (lsp-package-path 'ansible-language-server))
                       ,@(cl-rest lsp-ansible-language-server-command))))
  :priority 1
  :activation-fn #'lsp-ansible-check-ansible-minor-mode
  :server-id 'ansible-ls
  :download-server-fn (lambda (_client callback error-callback _update?)
                        (lsp-package-ensure 'ansible-language-server callback error-callback))))

(lsp-consistency-check lsp-ansible)

(provide 'lsp-ansible)
;;; lsp-ansible.el ends here