Skip to content

Commit

Permalink
Introduce YANG LSP support
Browse files Browse the repository at this point in the history
This addresses #4355

Signed-off-by: Siddharth Sharma <[email protected]>
  • Loading branch information
esmasth committed Mar 23, 2024
1 parent 193c714 commit 834943f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
113 changes: 113 additions & 0 deletions clients/lsp-yang.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
;;; lsp-yang.el --- YANG Client settings -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Siddharth Sharma

;; Author: Siddharth Sharma <[email protected]>
;; Keywords: languages, yang, lsp

;; 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 support for YANG using using an external language server. Currently
;; the supported server is:
;;
;; yang-lsp (yls).
;; See https://github.com/TypeFox/yang-lsp/blob/master/docs/Settings.md
;; for setting up the project file.

;;; Code:

(require 'lsp-mode)

(defgroup lsp-yang nil
"LSP support for the YANG data modeling language using yang-lsp server."
:group 'lsp-yang
:link '(url-link "https://github.com/TypeFox/yang-lsp"))

(defcustom lsp-yang-yls-executable "yang-language-server"
"The yang-lsp server executable to use.
Leave as just the executable name to use the default behavior of finding the
executable with variable `exec-path'."
:group 'lsp-yang
:type 'string)

(defcustom lsp-yang-yls-version "0.7.5"
"yang-lsp server version to download.
It has to be set before `lsp-yang.el' is loaded and it has to
be available here: https://github.com/TypeFox/yang-lsp/releases/"
:type 'string
:group 'lsp-yang
:package-version '(lsp-mode . "8.0.0"))

(defcustom lsp-yang-yls-download-url
(format "https://github.com/TypeFox/yang-lsp/releases/download/v%s/yang-language-server_%s.zip"
lsp-yang-yls-version
lsp-yang-yls-version)
"Automatic download url for yang-lsp server"
:type 'string
:group 'lsp-yang
:package-version '(lsp-mode . "8.0.0"))

(defcustom lsp-yang-yls-store-path
(f-join lsp-server-install-dir "yang-lsp" "yang-lsp")
"The path to the file in which `yang-language-server' will be stored."
:type 'file
:group 'lsp-yang
:package-version '(lsp-mode . "8.0.0"))

(defcustom lsp-yang-yls-binary-path
(f-join lsp-server-install-dir (format "yang-lsp/yang-language-server-%s/bin"
lsp-yang-yls-version)
(pcase system-type
('windows-nt "yang-language-server.bat")
(_ "yang-language-server")))
"The path to `yang-language-server' binary."
:type 'file
:group 'lsp-yang
:package-version '(lsp-mode . "8.0.0"))

(defun lsp-yang--stored-yls-executable ()
"Return the stored yang-lsp server executable."
(executable-find lsp-yang-yls-binary-path))

(lsp-dependency
'yls
'(:system "yls")
`(:download :url lsp-yang-yls-download-url
:decompress :zip
:store-path lsp-yang-yls-store-path
:binary-path lsp-yang-yls-binary-path
:set-exectutable? t))

(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection
(lambda () (or (executable-find lsp-yang-yls-executable)
(lsp-yang--stored-yls-executable)))
(lambda () (or (executable-find lsp-yang-yls-executable)
(file-executable-p (lsp-yang--stored-yls-executable)))))
:activation-fn (lsp-activate-on "yang")
:major-modes '(yang-mode)
:language-id "YANG"
:priority -1
:server-id 'lsp-yang
:download-server-fn (lambda (_client callback error-callback _update?)
(lsp-package-ensure 'yls callback error-callback))))

(lsp-consistency-check lsp-yang)

(provide 'lsp-yang)
;;; lsp-yang.el ends here
8 changes: 8 additions & 0 deletions docs/lsp-clients.json
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,14 @@
"lsp-install-server": "yamlls",
"debugger": "Not available"
},
{
"name": "yang",
"full-name": "YANG",
"server-name": "yang-lsp",
"server-url": "https://github.com/TypeFox/yang-lsp",
"installation-url": "https://github.com/TypeFox/yang-lsp/releases",
"debugger": "Not available"
},
{
"name": "zig",
"full-name": "Zig",
Expand Down
10 changes: 10 additions & 0 deletions docs/manual-language-docs/lsp-yang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
author: esmasth
root_file: docs/manual-language-docs/lsp-yang.md
---
<!-- markdownlint-disable-file MD041 -->
## YANG Language Server (yang-lsp) Client

### Configuration

### Known Issues
8 changes: 6 additions & 2 deletions lsp-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ As defined by the Language Server Protocol 3.16."
lsp-sonarlint lsp-tailwindcss lsp-tex lsp-terraform lsp-toml lsp-ttcn3
lsp-typeprof lsp-v lsp-vala lsp-verilog lsp-vetur lsp-volar lsp-vhdl
lsp-vimscript lsp-wgsl lsp-xml lsp-yaml lsp-ruby-lsp lsp-ruby-syntax-tree
lsp-solidity lsp-sqls lsp-svelte lsp-steep lsp-tilt lsp-trunk lsp-zig lsp-jq)
lsp-solidity lsp-sqls lsp-svelte lsp-steep lsp-tilt lsp-trunk lsp-zig lsp-jq
lsp-yang)
"List of the clients to be automatically required."
:group 'lsp-mode
:type '(repeat symbol))
Expand Down Expand Up @@ -795,6 +796,7 @@ Changes take effect only when a new session is started."
("^PKGBUILD$" . "shellscript")
("^go\\.mod\\'" . "go.mod")
("^settings.json$" . "jsonc")
("^yang\\.settings$" . "jsonc")
(ada-mode . "ada")
(ada-ts-mode . "ada")
(awk-mode . "awk")
Expand Down Expand Up @@ -956,7 +958,8 @@ Changes take effect only when a new session is started."
(jq-ts-mode . "jq")
(protobuf-mode . "protobuf")
(nushell-mode . "nushell")
(nushell-ts-mode . "nushell"))
(nushell-ts-mode . "nushell")
(yang-mode . "yang"))
"Language id configuration.")

(defvar lsp--last-active-workspaces nil
Expand Down Expand Up @@ -6033,6 +6036,7 @@ Request codeAction/resolve for more info if server supports."
(typescript-mode . typescript-indent-level) ; Typescript
(typescript-ts-mode . typescript-ts-mode-indent-offset) ; Typescript (tree-sitter, Emacs29)
(yaml-mode . yaml-indent-offset) ; YAML
(yang-mode . c-basic-offset) ; YANG (yang-mode)

(default . standard-indent)) ; default fallback
"A mapping from `major-mode' to its indent variable.")
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ nav:
- wgsl: page/lsp-wgsl.md
- XML: page/lsp-xml.md
- YAML: page/lsp-yaml.md
- YANG: page/lsp-yang.md
- Zig: page/lsp-zig.md
- Debugging:
- https://emacs-lsp.github.io/dap-mode
Expand Down

0 comments on commit 834943f

Please sign in to comment.