config

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

lsp-toml.el (5770B)


      1 ;;; lsp-toml.el --- lsp-mode TOML integration  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2021  Taiki Sugawara
      4 
      5 ;; Author: Taiki Sugawara <buzz.taiki@gmail.com>
      6 ;; Keywords: lsp, toml
      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 ;; Client for taplo.
     24 
     25 ;;; Code:
     26 
     27 (require 'lsp-mode)
     28 (require 'ht)
     29 (require 'f)
     30 
     31 (defgroup lsp-toml nil
     32   "LSP support for TOML, using Taplo."
     33   :group 'lsp-mode
     34   :link '(url-link "https://github.com/tamasfe/taplo"))
     35 
     36 (defcustom lsp-toml-command "taplo"
     37   "Path to taplo command."
     38   :type 'string
     39   :group 'lsp-toml
     40   :package-version '(lsp-mode . "9.0.0"))
     41 
     42 (defcustom lsp-toml-cache-path (expand-file-name
     43                                 (locate-user-emacs-file (f-join ".cache" "lsp-toml")))
     44   "Path to cache."
     45   :type 'string
     46   :group 'lsp-toml
     47   :package-version '(lsp-mode . "9.0.0"))
     48 
     49 (lsp-defcustom lsp-toml-taplo-config-file-path nil
     50   "An absolute, or workspace relative path to the Taplo configuration file."
     51   :type 'string
     52   :group 'lsp-toml
     53   :package-version '(lsp-mode . "9.0.0")
     54   :lsp-path "evenBetterToml.taplo.configFile.path")
     55 
     56 (lsp-defcustom lsp-toml-taplo-config-file-enabled t
     57   "Whether to enable the usage of a Taplo configuration file."
     58   :type 'boolean
     59   :group 'lsp-toml
     60   :package-version '(lsp-mode . "9.0.0")
     61   :lsp-path "evenBetterToml.taplo.configFile.enabled")
     62 
     63 (lsp-defcustom lsp-toml-semantic-tokens nil
     64   "Enable semantic tokens for inline table and array keys."
     65   :type 'boolean
     66   :group 'lsp-toml
     67   :package-version '(lsp-mode . "9.0.0")
     68   :lsp-path "evenBetterToml.semanticTokens")
     69 
     70 (lsp-defcustom lsp-toml-schema-enabled t
     71   "Enable completion and validation based on JSON schemas."
     72   :type 'boolean
     73   :group 'lsp-toml
     74   :package-version '(lsp-mode . "9.0.0")
     75   :lsp-path "evenBetterToml.schema.enabled")
     76 
     77 (lsp-defcustom lsp-toml-schema-links nil
     78   "Whether to show clickable links for keys in the editor."
     79   :type 'boolean
     80   :group 'lsp-toml
     81   :package-version '(lsp-mode . "9.0.0")
     82   :lsp-path "evenBetterToml.schema.links")
     83 
     84 (lsp-defcustom lsp-toml-schema-catalogs
     85   ["https://www.schemastore.org/api/json/catalog.json"]
     86   "A list of URLs to schema catalogs where schemas and associations
     87 can be fetched from"
     88   :type 'lsp-string-vector
     89   :group 'lsp-toml
     90   :package-version '(lsp-mode . "9.0.0")
     91   :lsp-path "evenBetterToml.schema.catalogs")
     92 
     93 (lsp-defcustom lsp-toml-schema-associations nil
     94   "Additional document and schema associations.
     95 
     96 The key must be a regular expression, this pattern is used to
     97 associate schemas with absolute document URIs.
     98 
     99 The value must be an absolute URI to the JSON schema"
    100   :type '(alist :key-type symbol :value-type string)
    101   :group 'lsp-toml
    102   :package-version '(lsp-mode . "9.0.0")
    103   :lsp-path "evenBetterToml.schema.associations")
    104 
    105 (lsp-defcustom lsp-toml-schema-cache-memory-expiration 60
    106   "The amount of seconds after which schemas will be invalidated from memory."
    107   :type 'number
    108   :group 'lsp-toml
    109   :package-version '(lsp-mode . "9.0.0")
    110   :lsp-path "evenBetterToml.schema.cache.memoryExpiration")
    111 
    112 (lsp-defcustom lsp-toml-schema-cache-disk-expiration 600
    113   "The amount of seconds after which cached catalogs and schemas
    114 expire and will be attempted to be fetched again."
    115   :type 'number
    116   :group 'lsp-toml
    117   :package-version '(lsp-mode . "9.0.0")
    118   :lsp-path "evenBetterToml.schema.cache.diskExpiration")
    119 
    120 (lsp-defcustom lsp-toml-completion-max-keys 5
    121   "The maximum amount of keys in a dotted key to display during
    122 completion, 0 effectively disables key completions."
    123   :type 'number
    124   :group 'lsp-toml
    125   :package-version '(lsp-mode . "9.0.0")
    126   :lsp-path "evenBetterToml.completion.maxKeys")
    127 
    128 (lsp-defcustom lsp-toml-syntax-semantic-tokens t
    129   "Whether to enable semantic tokens for tables and arrays."
    130   :type 'boolean
    131   :group 'lsp-toml
    132   :package-version '(lsp-mode . "9.0.0")
    133   :lsp-path "evenBetterToml.syntax.semanticTokens")
    134 
    135 
    136 (defun lsp-toml--initialization-options ()
    137   "Initialization options for taplo."
    138   (list :configurationSection "evenBetterToml"
    139         :cachePath lsp-toml-cache-path))
    140 
    141 (defun lsp-toml--handle-message-with-output (_workspace params)
    142   "Handle taplo/messageWithOutput notification with PARAMS."
    143   (funcall (pcase (ht-get params "kind")
    144              ("error" 'lsp--error)
    145              ("warn" 'lsp--warn)
    146              ("info" 'lsp--info)
    147              (_ 'lsp--info))
    148            "lsp-toml: %s"
    149            (ht-get params "message")))
    150 
    151 (defun lsp-toml--check-enabled (_file-name _mode)
    152   "Check if the taplo language server should be enabled in this buffer."
    153   (when (string= (lsp-buffer-language) "toml")
    154     (make-directory lsp-toml-cache-path t)
    155     t))
    156 
    157 (lsp-register-client
    158  (make-lsp-client
    159   :new-connection (lsp-stdio-connection (lambda () (list lsp-toml-command "lsp" "stdio")))
    160   :activation-fn #'lsp-toml--check-enabled
    161   :initialization-options #'lsp-toml--initialization-options
    162   :notification-handlers (ht ("taplo/messageWithOutput" #'lsp-toml--handle-message-with-output)
    163                              ("taplo/didChangeSchemaAssociation" #'ignore))
    164   :multi-root t
    165   :server-id 'taplo
    166   :priority -1))
    167 
    168 (lsp-consistency-check lsp-toml)
    169 
    170 (provide 'lsp-toml)
    171 ;;; lsp-toml.el ends here