@gesslar/vscode-theme-schema
v1.2.2
Published
You _can_ handle the truth, and VS Code will tell it to us.
Downloads
80
Maintainers
Readme
@gesslar/vscode-theme-schema
Utilities for VS Code extensions that need to load the built-in workbench color schema and validate theme color values against it.
Features
- Loads
vscode://schemas/workbench-colorsin the extension host. - Resolves local
$refentries in property definitions andoneOfoptions. - Normalizes schema entries into a
Map. - Derives helpful metadata:
alphaRequired: whether a property requires alpha-enabled hex colors.sample: example value (#ffffffor#ffffffaa).
- Validates user color values, including schema pattern constraints and deprecation warnings.
- Parses JSON/JSONC into an AST with full source-location mapping for jump-to-property/value navigation.
Installation
npm install @gesslar/vscode-theme-schemaQuick Start (VS Code Extension Host)
import {VSCodeSchema, Validator} from "@gesslar/vscode-theme-schema"
const schema = await VSCodeSchema.new()
const results = await Validator.validate(schema.map, {
"editor.background": "#1e1e1e",
"editor.selectionBackground": "#264f78aa",
"edgyMemelord": "#000000",
"statusBar.debuggingBackground": "default"
})
for(const result of results) {
if(result.status !== "valid")
console.log(`${result.property}: ${result.message}`)
}API
VSCodeSchema
static async new(): Promise<VSCodeSchema>- Creates (and caches) a singleton schema instance from
vscode://schemas/workbench-colors.
- Creates (and caches) a singleton schema instance from
new VSCodeSchema(schemaData: string)- Parses and resolves raw schema text (JSON5).
map: Map<string, object>- Resolved property schema map.
size: number- Number of color properties in the schema.
valueOf(): Map<string, object>- Returns the same internal schema map.
Validator
static async validate(schema, userColors): Promise<ValidationResult[]>- Validates each provided color property and returns per-property results.
statusis one ofvalid,warn, orinvalid.- Accepts values
default,#RGB,#RGBA,#RRGGBB,#RRGGBBAA. - Unknown properties are marked
invalid.
Result shape:
type ValidationResult = {
property: string
status: "valid" | "warn" | "invalid"
description: string
value: string
message?: string
}JsonSource
Parses JSON/JSONC source text into an AST and builds a dotted-path → source-location map, enabling jump-to-property and jump-to-value navigation in VS Code extensions.
new JsonSource(text: string, filePath?: string)- Parses source text and builds the internal location map.
filePathis an optional label used informatLocation()output.
static async fromFile(file: FileObject, cwd?: DirectoryObject): Promise<JsonSource | null>- Factory for
FileObjectinputs (.json,.jsonc,.json5). Returnsnullfor unsupported extensions or parse failures.
- Factory for
getLocation(dottedPath: string): SourceLocation | null- Returns the
{line, column}of the key for a dotted path.
- Returns the
getValueLocation(dottedPath: string): SourceLocation | null- Returns the
{line, column}of the value for a dotted path.
- Returns the
formatLocation(dottedPath: string, target?: "key" | "value"): string | null- Returns a formatted
"file:line:column"or"line:column"string.
- Returns a formatted
ast: object— The rawjsonc-eslint-parserAST.filePath: string | null— The label passed at construction.locationMap: Map<string, LocationEntry>— The full path → location map.
import {JsonSource} from "@gesslar/vscode-theme-schema"
const src = new JsonSource(`{
"colors": {
"editor.background": "#1e1e1e"
}
}`, "theme.json")
src.formatLocation("colors.editor.background") // "theme.json:3:5"
src.formatLocation("colors.editor.background", "value") // "theme.json:3:26"
src.getLocation("colors.editor.background") // { line: 3, column: 4 }Location types:
type SourceLocation = { line: number; column: number }
type LocationEntry = { key: SourceLocation; value: SourceLocation }Runtime Notes
VSCodeSchema.new()depends on thevscodeAPI and is intended to run inside the VS Code extension host.- If you already have schema text, you can construct directly:
const schema = new VSCodeSchema(schemaText)Development
npm run lint # lint source files
npm run lint:fix # lint and auto-fix
npm run types # regenerate declarations in types/License
@gesslar/vscode-theme-schema is released into the public domain under the 0BSD.
This package includes or depends on third-party components under their own licenses:
| Dependency | License | | --- | --- | | @gesslar/toolkit | 0BSD | | json5 | MIT | | jsonc-eslint-parser | MIT |
