aboutsummaryrefslogtreecommitdiffstats
path: root/elpa/lsp-mode-20220505.630/lsp-toml.el
blob: 00ba4a2f4dc9d7928e5642f851caeb2c4066a557 (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
;;; lsp-toml.el --- lsp-mode TOML integration  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Taiki Sugawara

;; Author: Taiki Sugawara <buzz.taiki@gmail.com>
;; Keywords: lsp, toml

;; 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:

;; Client for taplo.

;;; Code:

(require 'lsp-mode)
(require 'ht)
(require 'f)

(defgroup lsp-toml nil
  "LSP support for TOML, using Taplo."
  :group 'lsp-mode
  :link '(url-link "https://github.com/tamasfe/taplo"))

(defcustom lsp-toml-command "taplo"
  "Path to taplo command."
  :type 'string
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1"))

(defcustom lsp-toml-cache-path (expand-file-name
                                (locate-user-emacs-file (f-join ".cache" "lsp-toml")))
  "Path to cache."
  :type 'string
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1"))

(defcustom-lsp lsp-toml-taplo-config-file-path nil
  "An absolute, or workspace relative path to the Taplo configuration file."
  :type 'string
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.taplo.configFile.path")

(defcustom-lsp lsp-toml-taplo-config-file-enabled t
  "Whether to enable the usage of a Taplo configuration file."
  :type 'boolean
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.taplo.configFile.enabled")

(defcustom-lsp lsp-toml-semantic-tokens nil
  "Enable semantic tokens for inline table and array keys."
  :type 'boolean
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.semanticTokens")

(defcustom-lsp lsp-toml-schema-enabled t
  "Enable completion and validation based on JSON schemas."
  :type 'boolean
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.schema.enabled")

(defcustom-lsp lsp-toml-schema-links nil
  "Whether to show clickable links for keys in the editor."
  :type 'boolean
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.schema.links")

(defcustom-lsp lsp-toml-schema-catalogs
  ["https://taplo.tamasfe.dev/schema_index.json"
   "https://www.schemastore.org/api/json/catalog.json"]
  "A list of URLs to schema catalogs where schemas and associations
can be fetched from"
  :type 'lsp-string-vector
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.schema.catalogs")

(defcustom-lsp lsp-toml-schema-associations nil
  "Additional document and schema associations.

The key must be a regular expression, this pattern is used to
associate schemas with absolute document URIs.

The value must be an absolute URI to the JSON schema"
  :type '(alist :key-type symbol :value-type string)
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.schema.associations")

(defcustom-lsp lsp-toml-schema-cache-memory-expiration 60
  "The amount of seconds after which schemas will be invalidated from memory."
  :type 'number
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.schema.cache.memoryExpiration")

(defcustom-lsp lsp-toml-schema-cache-disk-expiration 600
  "The amount of seconds after which cached catalogs and schemas
expire and will be attempted to be fetched again."
  :type 'number
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.schema.cache.diskExpiration")

(defcustom-lsp lsp-toml-completion-max-keys 5
  "The maximum amount of keys in a dotted key to display during
completion, 0 effectively disables key completions."
  :type 'number
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.completion.maxKeys")

(defcustom-lsp lsp-toml-syntax-semantic-tokens t
  "Whether to enable semantic tokens for tables and arrays."
  :type 'boolean
  :group 'lsp-toml
  :package-version '(lsp-mode . "8.0.1")
  :lsp-path "evenBetterToml.syntax.semanticTokens")


(defun lsp-toml--initialization-options ()
  "Initialization options for taplo."
  (list :configurationSection "evenBetterToml"
        :cachePath lsp-toml-cache-path))

(defun lsp-toml--handle-message-with-output (_workspace params)
  "Handle taplo/messageWithOutput notification with PARAMS."
  (funcall (pcase (ht-get params "kind")
             ("error" 'lsp--error)
             ("warn" 'lsp--warn)
             ("info" 'lsp--info)
             (_ 'lsp--info))
           "lsp-toml: %s"
           (ht-get params "message")))

(defun lsp-toml--check-enabled (_file-name _mode)
  "Check if the taplo language server should be enabled in this buffer."
  (when (string= (lsp-buffer-language) "toml")
    (make-directory lsp-toml-cache-path t)
    t))

(lsp-register-client
 (make-lsp-client
  :new-connection (lsp-stdio-connection (lambda () (list lsp-toml-command "lsp" "stdio")))
  :activation-fn #'lsp-toml--check-enabled
  :initialization-options #'lsp-toml--initialization-options
  :notification-handlers (ht ("taplo/messageWithOutput" #'lsp-toml--handle-message-with-output)
                             ("taplo/didChangeSchemaAssociation" #'ignore))
  :multi-root t
  :server-id 'taplo
  :priority -1))

(lsp-consistency-check lsp-toml)

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