config

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

lsp-html.el (8153B)


      1 ;;; lsp-html.el --- vscode-html-languageserver configuration  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2019  Vibhav Pant
      4 
      5 ;; Author: Vibhav Pant <vibhavp@gmail.com>
      6 ;; Keywords:
      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 ;;; Code:
     24 
     25 (require 'lsp-mode)
     26 
     27 (defgroup lsp-html nil
     28   "LSP support for HTML, using vscode's built-in language server."
     29   :group 'lsp-mode
     30   :link '(url-link "https://github.com/microsoft/vscode/tree/main/extensions/html-language-features/server")
     31   :package-version '(lsp-mode . "6.1"))
     32 
     33 (defcustom lsp-html-custom-data []
     34   "A list of JSON file paths that define custom tags, properties and other HTML
     35 syntax constructs. Only workspace folder setting will be read.
     36 All json file paths should be relative to your workspace folder."
     37   :type 'lsp-repeatable-vector
     38   :group 'lsp-html
     39   :package-version '(lsp-mode . "9.0.0"))
     40 
     41 (defcustom lsp-html-format-enable t
     42   "Enable/disable default HTML formatter."
     43   :type 'boolean
     44   :group 'lsp-html
     45   :package-version '(lsp-mode . "6.1"))
     46 
     47 (defcustom lsp-html-format-wrap-line-length 120
     48   "Maximum amount of characters per line (0 = disable)."
     49   :type 'number
     50   :group 'lsp-html
     51   :package-version '(lsp-mode . "6.1"))
     52 
     53 (defcustom lsp-html-format-unformatted "wbr"
     54   nil
     55   :type '(choice (const nil) string)
     56   :group 'lsp-html
     57   :package-version '(lsp-mode . "6.1"))
     58 
     59 (defcustom lsp-html-format-content-unformatted "pre,code,textarea"
     60   nil
     61   :group 'lsp-html
     62   :type '(choice (const nil) string)
     63   :package-version '(lsp-mode . "6.1"))
     64 
     65 (defcustom lsp-html-format-indent-inner-html nil
     66   nil
     67   :type 'boolean
     68   :group 'lsp-html
     69   :package-version '(lsp-mode . "6.1"))
     70 
     71 (defcustom lsp-html-format-preserve-new-lines t
     72   "Controls whether existing line breaks before elements should be preserved.
     73 Only works before elements, not inside tags or for text."
     74   :type 'boolean
     75   :group 'lsp-html
     76   :package-version '(lsp-mode . "6.1"))
     77 
     78 (defcustom lsp-html-format-max-preserve-new-lines nil
     79   nil
     80   :type '(choice (const nil) integer)
     81   :group 'lsp-html
     82   :package-version '(lsp-mode . "6.1"))
     83 
     84 (defcustom lsp-html-format-indent-handlebars nil nil
     85   :type 'boolean
     86   :group 'lsp-html
     87   :package-version '(lsp-mode . "6.1"))
     88 
     89 (defcustom lsp-html-format-end-with-newline nil
     90   "End with a newline."
     91   :type 'boolean
     92   :group 'lsp-html
     93   :package-version '(lsp-mode . "6.1"))
     94 
     95 (defcustom lsp-html-format-extra-liners "head, body, /html"
     96   nil
     97   :type '(choice (const nil) string)
     98   :group 'lsp-html
     99   :package-version '(lsp-mode . "6.1"))
    100 
    101 (defcustom lsp-html-format-wrap-attributes "auto"
    102   "Wrap attributes."
    103   :type '(choice
    104           (const "auto")
    105           (const "force")
    106           (const "force-aligned")
    107           (const "force-expand-multiline")
    108           (const "aligned-multiple")
    109           (const "preserve")
    110           (const "preserve-aligned"))
    111   :group 'lsp-html
    112   :package-version '(lsp-mode . "6.1"))
    113 
    114 (defcustom lsp-html-suggest-html5 t
    115   "Controls whether the built-in HTML language support suggests HTML5 tags,
    116 properties and values."
    117   :type 'boolean
    118   :group 'lsp-html
    119   :package-version '(lsp-mode . "6.1"))
    120 
    121 (defcustom lsp-html-validate-scripts t
    122   "Controls whether the built-in HTML language support validates embedded
    123 scripts."
    124   :type 'boolean
    125   :group 'lsp-html
    126   :package-version '(lsp-mode . "6.1"))
    127 
    128 (defcustom lsp-html-validate-styles t
    129   "Controls whether the built-in HTML language support validates embedded
    130 styles."
    131   :type 'boolean
    132   :group 'lsp-html
    133   :package-version '(lsp-mode . "6.1"))
    134 
    135 (defcustom lsp-html-auto-closing-tags t
    136   "Enable/disable autoclosing of HTML tags."
    137   :type 'boolean
    138   :group 'lsp-html
    139   :package-version '(lsp-mode . "6.1"))
    140 
    141 (defcustom lsp-html-hover-documentation t
    142   "Whether to show documentation strings on hover or not."
    143   :type 'boolean
    144   :group 'lsp-html
    145   :package-version '(lsp-mode . "9.0.0"))
    146 
    147 (defcustom lsp-html-hover-references t
    148   "Whether to show MDN references in documentation popups."
    149   :type 'boolean
    150   :group 'lsp-html
    151   :package-version '(lsp-mode . "9.0.0"))
    152 
    153 (defcustom lsp-html-trace-server "off"
    154   "Traces the communication between VS Code and the HTML language server."
    155   :type '(choice
    156           (const "off")
    157           (const "messages")
    158           (const "verbose"))
    159   :group 'lsp-html
    160   :package-version '(lsp-mode . "6.1"))
    161 
    162 (lsp-register-custom-settings
    163  '(("html.trace.server" lsp-html-trace-server)
    164    ("html.autoClosingTags" lsp-html-auto-closing-tags t)
    165    ("html.validate.styles" lsp-html-validate-styles t)
    166    ("html.validate.scripts" lsp-html-validate-scripts t)
    167    ("html.suggest.html5" lsp-html-suggest-html5 t)
    168    ("html.format.wrapAttributes" lsp-html-format-wrap-attributes)
    169    ("html.format.extraLiners" lsp-html-format-extra-liners)
    170    ("html.format.endWithNewline" lsp-html-format-end-with-newline t)
    171    ("html.format.indentHandlebars" lsp-html-format-indent-handlebars t)
    172    ("html.format.maxPreserveNewLines" lsp-html-format-max-preserve-new-lines)
    173    ("html.format.preserveNewLines" lsp-html-format-preserve-new-lines t)
    174    ("html.format.indentInnerHtml" lsp-html-format-indent-inner-html t)
    175    ("html.format.contentUnformatted" lsp-html-format-content-unformatted)
    176    ("html.format.unformatted" lsp-html-format-unformatted)
    177    ("html.format.wrapLineLength" lsp-html-format-wrap-line-length)
    178    ("html.format.enable" lsp-html-format-enable t)
    179    ("html.hover.documentation" lsp-html-hover-documentation t)
    180    ("html.hover.references" lsp-html-hover-references t)
    181    ("html.customData" lsp-html-custom-data)))
    182 
    183 (defcustom lsp-html-server-command-args '("--stdio")
    184   "Command to start html-languageserver."
    185   :type '(repeat string)
    186   :group 'lsp-html
    187   :package-version '(lsp-mode . "6.3"))
    188 
    189 ;; Caveat: uri seems to be sent as a single length vector.
    190 (defun lsp-html--get-content (_workspace files callback)
    191   "Helper function for getting the content of a URI/filename."
    192   (let* ((filename (aref files 0))
    193          (uri (f-join (lsp-workspace-root) filename))
    194          (file-content (f-read-text uri)))
    195     (funcall callback file-content)))
    196 
    197 (lsp-dependency 'html-language-server
    198                 '(:system "vscode-html-language-server")
    199                 '(:npm :package "vscode-langservers-extracted"
    200                        :path "vscode-html-language-server"))
    201 
    202 (lsp-register-client
    203  (make-lsp-client :new-connection (lsp-stdio-connection
    204                                    (lambda ()
    205                                      (cons (lsp-package-path 'html-language-server)
    206                                            lsp-html-server-command-args)))
    207                   :activation-fn (lsp-activate-on "html")
    208                   :priority -4
    209                   :completion-in-comments? t
    210                   :server-id 'html-ls
    211                   :initialization-options (lambda ()
    212                                             (list :dataPaths lsp-html-custom-data))
    213                   :async-request-handlers (ht ("html/customDataContent" #'lsp-html--get-content))
    214                   :initialized-fn (lambda (w)
    215                                     (with-lsp-workspace w
    216                                       (lsp--set-configuration
    217                                        (lsp-configuration-section "html"))))
    218                   :download-server-fn (lambda (_client callback error-callback _update?)
    219                                         (lsp-package-ensure
    220                                          'html-language-server callback
    221                                          error-callback))))
    222 
    223 (lsp-consistency-check lsp-html)
    224 
    225 (provide 'lsp-html)
    226 ;;; lsp-html.el ends here