Skip to content

Commit 4e37c36

Browse files
authored
Introduce YANG LSP support (#4390)
1 parent d3173ad commit 4e37c36

File tree

6 files changed

+205
-19
lines changed

6 files changed

+205
-19
lines changed

CHANGELOG.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
* Add Cucumber support.
117117
* Add COBOL support.
118118
* Add Common Lisp support.
119+
* Add YANG support using TypeFox/yang-lsp Server.
119120

120121
** Release 8.0.0
121122
* Add ~lsp-clients-angular-node-get-prefix-command~ to get the Angular server from another location which is still has ~/lib/node_modules~ in it.

clients/lsp-yang.el

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
;;; lsp-yang.el --- YANG Client settings -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2024 Siddharth Sharma
4+
5+
;; Author: Siddharth Sharma <siddharth.sharma@ericsson.com>
6+
;; Keywords: languages, yang, lsp
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+
;; LSP support for YANG using using an external language server. Currently
24+
;; the supported server is:
25+
;;
26+
;; yang-lsp (yls).
27+
;; See https://github.com/TypeFox/yang-lsp/blob/master/docs/Settings.md
28+
;; for setting up the user/project/workspace files.
29+
30+
;;; Code:
31+
32+
(require 'lsp-mode)
33+
34+
(defgroup lsp-yang nil
35+
"LSP support for the YANG data modeling language using yang-lsp server."
36+
:group 'lsp-yang
37+
:link '(url-link "https://github.com/TypeFox/yang-lsp"))
38+
39+
(defcustom lsp-yang-yls-version "0.7.6"
40+
"yang-lsp server version to download.
41+
42+
It has to be set before `lsp-yang.el' is loaded and it has to
43+
be available here: https://github.com/TypeFox/yang-lsp/releases/"
44+
:type 'string
45+
:group 'lsp-yang
46+
:package-version '(lsp-mode . "8.0.1"))
47+
48+
(add-to-list 'auto-mode-alist '("^yang\\.settings$" . jsonc-mode))
49+
50+
(defcustom lsp-yang-yls-settings-schema-url
51+
(format "https://raw.githubusercontent.com/TypeFox/yang-lsp/v%s/schema/yang-lsp-settings-schema.json"
52+
lsp-yang-yls-version)
53+
"URL for yang-lsp server settings schema"
54+
:type 'string
55+
:group 'lsp-yang
56+
:package-version '(lsp-mode . "8.0.1"))
57+
58+
(defcustom lsp-yang-yls-executable "yang-language-server"
59+
"The yang-lsp server executable to use.
60+
61+
Leave as just the executable name to use the default behavior of finding the
62+
executable with variable `exec-path'."
63+
:group 'lsp-yang
64+
:type 'string)
65+
66+
(defcustom lsp-yang-yls-download-url
67+
(format "https://github.com/TypeFox/yang-lsp/releases/download/v%s/yang-language-server_%s.zip"
68+
lsp-yang-yls-version
69+
lsp-yang-yls-version)
70+
"Automatic download url for yang-lsp server"
71+
:type 'string
72+
:group 'lsp-yang
73+
:package-version '(lsp-mode . "8.0.1"))
74+
75+
(defcustom lsp-yang-yls-store-path
76+
(f-join lsp-server-install-dir "yang-lsp" "yang-lsp")
77+
"The path to the file in which `yang-language-server' will be stored."
78+
:type 'file
79+
:group 'lsp-yang
80+
:package-version '(lsp-mode . "8.0.1"))
81+
82+
(defcustom lsp-yang-yls-binary-path
83+
(f-join lsp-server-install-dir (format "yang-lsp/yang-language-server-%s/bin"
84+
lsp-yang-yls-version)
85+
(pcase system-type
86+
('windows-nt "yang-language-server.bat")
87+
(_ "yang-language-server")))
88+
"The path to `yang-language-server' binary."
89+
:type 'file
90+
:group 'lsp-yang
91+
:package-version '(lsp-mode . "8.0.1"))
92+
93+
(defun lsp-yang--stored-yls-executable ()
94+
"Return the stored yang-lsp server executable."
95+
(executable-find lsp-yang-yls-binary-path))
96+
97+
(lsp-dependency
98+
'yang-lsp
99+
`(:download :url lsp-yang-yls-download-url
100+
:decompress :zip
101+
:store-path lsp-yang-yls-store-path
102+
:binary-path lsp-yang-yls-binary-path
103+
:set-exectutable? t))
104+
105+
(lsp-register-client
106+
(make-lsp-client
107+
:new-connection (lsp-stdio-connection
108+
(lambda () (or (executable-find lsp-yang-yls-executable)
109+
(lsp-yang--stored-yls-executable)))
110+
(lambda () (or (executable-find lsp-yang-yls-executable)
111+
(file-executable-p (lsp-yang--stored-yls-executable)))))
112+
:major-modes '(yang-mode)
113+
:language-id "YANG"
114+
:priority -1
115+
:server-id 'yls
116+
:download-server-fn (lambda (_client callback error-callback _update?)
117+
(lsp-package-ensure 'yang-lsp callback error-callback))))
118+
119+
(lsp-consistency-check lsp-yang)
120+
121+
(provide 'lsp-yang)
122+
;;; lsp-yang.el ends here

docs/lsp-clients.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,14 @@
12031203
"lsp-install-server": "yamlls",
12041204
"debugger": "Not available"
12051205
},
1206+
{
1207+
"name": "yang",
1208+
"full-name": "YANG",
1209+
"server-name": "yang-lsp",
1210+
"server-url": "https://github.com/TypeFox/yang-lsp",
1211+
"installation-url": "https://github.com/TypeFox/yang-lsp/releases",
1212+
"debugger": "Not available"
1213+
},
12061214
{
12071215
"name": "zig",
12081216
"full-name": "Zig",

docs/manual-language-docs/lsp-yang.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
author: esmasth
3+
root_file: docs/manual-language-docs/lsp-yang.md
4+
---
5+
# YANG (yang-lsp)
6+
7+
`lsp-mode` provides YANG language support via the [TypeFox/yang-lsp][1] Server.
8+
The server identifier is `yls` as an abbreviation of the released binary called
9+
`yang-language-server`.
10+
11+
## Configuration
12+
13+
Add following configuration in `.emacs` or `init.el` to hook lsp on YANG files,
14+
since [`yang-mode`][2] is the supported major mode.
15+
16+
```lisp
17+
(add-hook 'yang-mode-hook 'lsp)
18+
```
19+
20+
It recommended to add following configuration for the `yang.settings` file,
21+
which resides at the user/project/workspace root, to be validated via
22+
[`lsp-json`][5]. This may be automated by `lsp-yang` in later stages.
23+
24+
```lisp
25+
(setq lsp-json-schemas
26+
`[(:fileMatch ["yang.settings"] :url lsp-yang-yls-settings-schema-url)])
27+
```
28+
29+
To automatically trigger the [`lsp-json`][5] based validation, following
30+
configuration is recommended.
31+
32+
```lisp
33+
(add-hook 'jsonc-mode-hook 'lsp)
34+
```
35+
36+
## Known Issues
37+
38+
* Files in the project need to be opened in buffer to be known by LSP server
39+
* `yang.settings` is not associated with its [JSON schema][3]
40+
[`yang-lsp-settings-schema.json`][4] yet
41+
* yang-lsp settings file `yang.settings` is not respected
42+
* `lsp-format-buffer` does not follow `yang-mode` or `yang.settings`
43+
* Snippets have a bad format with extraneous characters
44+
45+
[1]: https://github.com/TypeFox/yang-lsp
46+
[2]: https://github.com/mbj4668/yang-mode
47+
[3]: https://json-schema.org/
48+
[4]: https://raw.githubusercontent.com/TypeFox/yang-lsp/v0.7.6/schema/yang-lsp-settings-schema.json
49+
[5]: https://emacs-lsp.github.io/lsp-mode/page/lsp-json/

lsp-mode.el

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,25 @@ As defined by the Language Server Protocol 3.16."
174174
:package-version '(lsp-mode . "6.1"))
175175

176176
(defcustom lsp-client-packages
177-
'( ccls lsp-actionscript lsp-ada lsp-angular lsp-ansible lsp-autotools lsp-awk
178-
lsp-asm lsp-astro lsp-bash lsp-beancount lsp-bufls lsp-clangd lsp-clojure
179-
lsp-cmake lsp-cobol lsp-credo lsp-crystal lsp-csharp lsp-css lsp-cucumber
180-
lsp-cypher lsp-d lsp-dart lsp-dhall lsp-docker lsp-dockerfile lsp-elm lsp-elixir
181-
lsp-emmet lsp-erlang lsp-eslint lsp-fortran lsp-fsharp lsp-gdscript lsp-go
182-
lsp-golangci-lint lsp-gleam lsp-glsl lsp-graphql lsp-hack lsp-grammarly
183-
lsp-groovy lsp-haskell lsp-haxe lsp-idris lsp-java lsp-javascript lsp-json
184-
lsp-kotlin lsp-latex lsp-lisp lsp-ltex lsp-lua lsp-markdown lsp-marksman lsp-mdx
185-
lsp-mint lsp-move lsp-nginx lsp-nim lsp-nix lsp-magik lsp-mojo lsp-metals
186-
lsp-mssql lsp-nushell lsp-ocaml lsp-openscad lsp-pascal lsp-perl lsp-perlnavigator
187-
lsp-pls lsp-php lsp-pwsh lsp-pyls lsp-pylsp lsp-pyright lsp-python-ms
188-
lsp-purescript lsp-qml lsp-r lsp-racket lsp-remark lsp-ruff-lsp lsp-rf lsp-rubocop
189-
lsp-rust lsp-semgrep lsp-shader lsp-solargraph lsp-sorbet lsp-sourcekit
190-
lsp-sonarlint lsp-tailwindcss lsp-tex lsp-terraform lsp-toml lsp-ttcn3
191-
lsp-typeprof lsp-v lsp-vala lsp-verilog lsp-vetur lsp-volar lsp-vhdl
192-
lsp-vimscript lsp-wgsl lsp-xml lsp-yaml lsp-ruby-lsp lsp-ruby-syntax-tree
193-
lsp-solidity lsp-sqls lsp-svelte lsp-steep lsp-tilt lsp-trunk lsp-zig lsp-jq)
177+
'( ccls lsp-actionscript lsp-ada lsp-angular lsp-ansible lsp-asm lsp-astro
178+
lsp-autotools lsp-awk lsp-bash lsp-beancount lsp-bufls lsp-clangd
179+
lsp-clojure lsp-cmake lsp-cobol lsp-credo lsp-crystal lsp-csharp lsp-css
180+
lsp-cucumber lsp-cypher lsp-d lsp-dart lsp-dhall lsp-docker lsp-dockerfile
181+
lsp-elixir lsp-elm lsp-emmet lsp-erlang lsp-eslint lsp-fortran lsp-fsharp
182+
lsp-gdscript lsp-gleam lsp-glsl lsp-go lsp-golangci-lint lsp-grammarly
183+
lsp-graphql lsp-groovy lsp-hack lsp-haskell lsp-haxe lsp-idris lsp-java
184+
lsp-javascript lsp-jq lsp-json lsp-kotlin lsp-latex lsp-lisp lsp-ltex
185+
lsp-lua lsp-magik lsp-markdown lsp-marksman lsp-mdx lsp-metals lsp-mint
186+
lsp-mojo lsp-move lsp-mssql lsp-nginx lsp-nim lsp-nix lsp-nushell lsp-ocaml
187+
lsp-openscad lsp-pascal lsp-perl lsp-perlnavigator lsp-php lsp-pls
188+
lsp-purescript lsp-pwsh lsp-pyls lsp-pylsp lsp-pyright lsp-python-ms
189+
lsp-qml lsp-r lsp-racket lsp-remark lsp-rf lsp-rubocop lsp-ruby-lsp
190+
lsp-ruby-syntax-tree lsp-ruff-lsp lsp-rust lsp-semgrep lsp-shader
191+
lsp-solargraph lsp-solidity lsp-sonarlint lsp-sorbet lsp-sourcekit lsp-sqls
192+
lsp-steep lsp-svelte lsp-tailwindcss lsp-terraform lsp-tex lsp-tilt
193+
lsp-toml lsp-trunk lsp-ttcn3 lsp-typeprof lsp-v lsp-vala lsp-verilog
194+
lsp-vetur lsp-vhdl lsp-vimscript lsp-volar lsp-wgsl lsp-xml lsp-yaml
195+
lsp-yang lsp-zig)
194196
"List of the clients to be automatically required."
195197
:group 'lsp-mode
196198
:type '(repeat symbol))
@@ -795,7 +797,8 @@ Changes take effect only when a new session is started."
795797
("\\ya?ml$" . "yaml")
796798
("^PKGBUILD$" . "shellscript")
797799
("^go\\.mod\\'" . "go.mod")
798-
("^settings.json$" . "jsonc")
800+
("^settings\\.json$" . "jsonc")
801+
("^yang\\.settings$" . "jsonc")
799802
(ada-mode . "ada")
800803
(ada-ts-mode . "ada")
801804
(awk-mode . "awk")
@@ -958,7 +961,8 @@ Changes take effect only when a new session is started."
958961
(jq-ts-mode . "jq")
959962
(protobuf-mode . "protobuf")
960963
(nushell-mode . "nushell")
961-
(nushell-ts-mode . "nushell"))
964+
(nushell-ts-mode . "nushell")
965+
(yang-mode . "yang"))
962966
"Language id configuration.")
963967

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

60406045
(default . standard-indent)) ; default fallback
60416046
"A mapping from `major-mode' to its indent variable.")

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ nav:
176176
- wgsl: page/lsp-wgsl.md
177177
- XML: page/lsp-xml.md
178178
- YAML: page/lsp-yaml.md
179+
- YANG: page/lsp-yang.md
179180
- Zig: page/lsp-zig.md
180181
- Debugging:
181182
- https://emacs-lsp.github.io/dap-mode

0 commit comments

Comments
 (0)