Skip to content

Commit 5329c08

Browse files
committed
Add schema validation for vhdl_ls.toml
1 parent bbffa1d commit 5329c08

File tree

10 files changed

+218
-32
lines changed

10 files changed

+218
-32
lines changed

README.md

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,15 @@ opening a certain file type, see the [Neovim LSP documentation](https://neovim.i
130130

131131
## Configuration
132132

133-
The language server needs to know your library mapping to perform full analysis of the code. For this it uses a
134-
configuration file in the [TOML](https://github.com/toml-lang/toml) format named `vhdl_ls.toml`.
135-
136-
`vhdl_ls` will load configuration files in the following order of priority (first to last):
137-
138-
1. A file named `.vhdl_ls.toml` in the user home folder.
139-
2. A file name from the `VHDL_LS_CONFIG` environment variable.
140-
3. A file named `vhdl_ls.toml` in the workspace root.
141-
142-
Settings in a later files overwrites those from previously loaded files.
143-
144-
Define the VHDL revision to use for parsing and analysis with the `standard` key.
145-
The expected value is the year associated the VHDL standard.
146-
Supported standards are 1993, 2008 and 2019 where both the long version ("2008") and the short version ("08") can be
147-
used.
148-
If nothing is specified, 2008 is used.
133+
The language server needs to know your library mapping to perform full analysis of the code. For this it uses a configuration file in the [TOML](https://github.com/toml-lang/toml) format named `vhdl_ls.toml`.
149134

150135
> [!NOTE]
151-
> Defining the standard feature is a relatively new feature (since april 2024).
152-
> Anything but the 2008 standard will not change much at the moment.
136+
> Read the full documentation in [the wiki](https://github.com/VHDL-LS/rust_hdl/wiki/VHDL%E2%80%90LS-Configuration)
153137
154-
**Example vhdl_ls.toml**
138+
### Example vhdl_ls.toml / Quickstart
155139

156140
```toml
157-
# What standard to use. This is optional and defaults to VHDL2008.
141+
# What standard to use. This is optional and defaults to VHDL 2008.
158142
standard = "2008"
159143
# The preferred case for completions.
160144
preferred_case = "lower"
@@ -189,18 +173,6 @@ unused = 'error' # Upgrade the 'unused' diagnostic to the 'error' severity
189173
unnecessary_work_library = false # Disable linting for the 'library work;' statement
190174
```
191175

192-
Using the `lint` table, you can configure the severity of diagnostics or turn of diagnostics altogether.
193-
194-
> [!WARNING]
195-
> You can overwrite every diagnostic error code including syntax or analysis errors using the lint table.
196-
> However, the intended use-case is for lints only.
197-
> Overwriting syntax or analysis errors (e.g., error codes `mismatched_kinds` or `syntax`) can cause unwanted side
198-
> effects
199-
200-
Paths in the `vhdl_ls.toml` can contain glob patterns (i.e., `.../*/`).
201-
On Unix machines, they can contain environment variables using the `$NAME` or `${NAME}` syntax.
202-
On Windows machines, use the `%NAME%` syntax to substitute environment variables.
203-
204176
## Ignoring errors
205177

206178
You can use the comment-pair `-- vhdl_ls off` and `-- vhdl_ls on` to conditionally disable and re-enable parsing of

schemas/tests/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Schemas tests
2+
3+
This folder contains Schema tests for `vhdl_ls.schema.json`.
4+
5+
## Running the tests locally:
6+
7+
> [!NOTE]
8+
> Usage of [uv](https://docs.astral.sh/uv/) is recommended. The minimum Python version is 3.11
9+
10+
### Using `uv`
11+
12+
`uv run schemas/validate_tests.py`
13+
14+
### Using regular Python
15+
16+
1. Install the Python dependency for schema validation:
17+
18+
```
19+
python3 -m pip install jsonschema
20+
```
21+
22+
2. Run the validator script from the repository root:
23+
24+
```
25+
python3 schemas/validate_tests.py
26+
```
27+
28+
29+
## Return codes:
30+
31+
- `0`: all tests behaved as expected
32+
- `1`: There was at least one failure
33+
- `2`: The `vhdl_ls.schema.json` file was not found
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[libraries.mylib]
2+
files = ["a.vhd"]
3+
unexpected = 123
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[libraries.work]
2+
files = ["top.vhd"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[libraries.ieee2008]
2+
files = ["std_logic.vhd"]
3+
4+
[lint]
5+
rule-number = 123
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
preferred_case = "upper"
2+
standard = "2019"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
preferred_case = "lower"
2+
standard = "2008"
3+
4+
[libraries.ieee2008]
5+
files = ["std_logic.vhd", "types.vhd"]
6+
exclude = ["legacy/*"]
7+
is_third_party = false
8+
9+
[libraries.my_lib]
10+
files = ["core.vhd", "utils.vhd"]
11+
12+
[lint]
13+
some-rule = false
14+
another-rule = "warning"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[libraries.ieee2008]
2+
files = ["std_logic.vhd"]
3+

schemas/validate_tests.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "jsonschema",
5+
# ]
6+
# ///
7+
"""
8+
Validate JSON tests under `schemas/tests` against `schemas/vhdl_ls.schema.json`.
9+
10+
Usage:
11+
uv run schemas/validate_tests.py or
12+
python3 schemas/validate_tests.py
13+
"""
14+
import json
15+
from pathlib import Path
16+
import sys
17+
18+
import jsonschema
19+
from jsonschema import Draft4Validator
20+
import tomllib
21+
22+
23+
ROOT = Path(__file__).parent
24+
SCHEMA_PATH = ROOT / "vhdl_ls.schema.json"
25+
TESTS_DIR = ROOT / "tests"
26+
27+
28+
def load_toml(path):
29+
with open(path, "rb") as file:
30+
return tomllib.load(file)
31+
32+
33+
def load_json(path):
34+
with open(path, "rb") as file:
35+
return json.load(file)
36+
37+
38+
def main():
39+
if not SCHEMA_PATH.exists():
40+
print(f"Schema not found at {SCHEMA_PATH}")
41+
return 2
42+
43+
schema = load_json(SCHEMA_PATH)
44+
validator = Draft4Validator(schema)
45+
46+
failures = 0
47+
total = 0
48+
49+
for sub in ("valid", "invalid"):
50+
folder = TESTS_DIR / sub
51+
for path in sorted(folder.iterdir()):
52+
total += 1
53+
data = load_toml(path)
54+
errors = list(validator.iter_errors(data))
55+
should_be_valid = (sub == "valid")
56+
57+
if should_be_valid and errors:
58+
print(f"FAIL (expected valid): {path}")
59+
for e in errors[:5]:
60+
print(" -", e.message)
61+
failures += 1
62+
elif (not should_be_valid) and (not errors):
63+
print(f"FAIL (expected invalid): {path} -- no validation errors found")
64+
failures += 1
65+
else:
66+
print(f"OK: {path}")
67+
68+
print(f"\nChecked {total} files: {total - failures} OK, {failures} FAILED")
69+
return 1 if failures else 0
70+
71+
72+
if __name__ == "__main__":
73+
sys.exit(main())

schemas/vhdl_ls.schema.json

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema",
3+
"title": "Schema for the vhdl_ls language server toml configuration",
4+
"description": "Configuration schema for vhdl_ls, the VHDL language server. Defines libraries, linting rules, and code formatting preferences.",
5+
"type": "object",
6+
"properties": {
7+
"preferred_case": {
8+
"description": "The preferred letter case for code completions and suggestions (e.g., lower, upper_snake, pascal).",
9+
"type": "string",
10+
"enum": [
11+
"lower",
12+
"snake",
13+
"upper",
14+
"upper_snake",
15+
"pascal",
16+
"upper_camel"
17+
]
18+
},
19+
"standard": {
20+
"description": "The VHDL standard version for analysis and linting. Supports short (e.g., '08') and full (e.g., '2008') notation.",
21+
"type": "string",
22+
"enum": ["1993", "93", "2008", "08", "2019", "19"]
23+
},
24+
"libraries": {
25+
"type": "object",
26+
"description": "Defines VHDL libraries in the project. Each library name (except 'work') maps to an object specifying source files.",
27+
"patternProperties": {
28+
"^(?!work$).+$": {
29+
"type": "object",
30+
"description": "A VHDL library definition. Library names must not be 'work' (reserved by VHDL spec).",
31+
"properties": {
32+
"files": {
33+
"type": "array",
34+
"description": "Array of source file paths or glob patterns belonging to this library. Either absolute or relative to vhdl_ls.toml.",
35+
"items": {
36+
"type": "string",
37+
"description": "A file path or glob pattern (e.g., 'src/**/*.vhd', 'lib/ieee.vhd')."
38+
}
39+
},
40+
"exclude": {
41+
"type": "array",
42+
"description": "Array of source file paths or glob patterns to exclude from this library.",
43+
"items": {
44+
"type": "string",
45+
"description": "A file path or glob pattern to exclude (e.g., 'legacy/*', 'src/*_old.vhd')."
46+
}
47+
},
48+
"is_third_party": {
49+
"type": "boolean",
50+
"description": "Whether this library is third-party. May affect diagnostic reporting. Defaults to false.",
51+
"default": false
52+
}
53+
},
54+
"additionalProperties": false,
55+
"required": ["files"]
56+
}
57+
},
58+
"additionalProperties": false
59+
},
60+
"lint": {
61+
"type": "object",
62+
"description": "Configures linting rules. Each key is a rule name, and the value controls its behavior (true/false to enable/disable, or a string for specific severity).",
63+
"patternProperties": {
64+
"^.+$": {
65+
"anyOf": [
66+
{ "type": "boolean" },
67+
{
68+
"type": "string",
69+
"enum": ["hint", "info", "warning", "error"]
70+
}
71+
],
72+
"description": "Lint rule configuration. Can be a boolean (enable/disable) or a string (e.g., 'warning', 'error')."
73+
}
74+
},
75+
"additionalProperties": false
76+
}
77+
},
78+
"required": ["libraries"]
79+
}

0 commit comments

Comments
 (0)