KScript 2 language server

The language server gives editors diagnostics, completion, hover, signature help, go to definition, find references, rename, an outline, semantic highlighting, quick fixes, document links and formatting for KScript 2 scripts (.kscript, and .ks.xml for the XML form), pages (.html) and Markdown pages (.md). It targets KScript 2 only: v1 elements, attributes and commands are reported as retired, with the KScript 2 replacement and a quick fix where one is mechanical.

Using it

Command What it does
kscript lsp Runs the server over stdio (LSP 3.17). Any LSP client can use it.
kscript check [file-or-folder]... [--warnings-as-errors] Prints problems as file(line,col): error KS4001: ... and exits 1 on errors, for CI. With no path it checks the project the current folder is in. A folder with _layout.html, _components/, _layouts/ or kscript.json is a site, so its .html and .md pages are checked too.
kscript check --print-config [file-or-folder] Prints the lint and format settings that apply, after the project’s standards and kscript.json are combined, with the layer that set each one.
kscript fmt [file-or-folder]... [--check] Tidies scripts and pages in place, or with --check lists the files that aren’t formatted and exits 1. The server’s textDocument/formatting uses the same formatter (KScript.Language/Formatting). See Formatting.

Both follow the project’s kscript.json (see Project settings) and the standard it names: lint.rules changes or turns off diagnostics by code, lint.exclude silences files and folders, and lint.include chooses what a folder check covers. The server re-reads kscript.json and its standard files when they change, using the open (unsaved) text of either. The rules are applied in LintRules.Apply (KScript.Language), after analysis, so the analyzer itself never needs to know about them.

kscript check and the server share one checker, ProjectChecker (KScript.LanguageServer): it picks the files a project covers, analyses each with the project’s rules, applies the lint settings and keeps the check cache (.kscript/cache/check.json). So the editor’s problems for a file are always the ones kscript check prints. The server publishes problems for every file of the projects in the editor’s folders, not only the open ones (the folder’s own project, or the projects found a few folders below it). Closed files are checked one at a time whenever no message is waiting, again after every change, and the cache means only the files a change reaches are analysed again. A setting in kscript.json that stops every script (an unknown package) is shown on kscript.json. Clients that want open files only pass "workspaceDiagnostics": false in initializationOptions (VS Code: kscript.problems.workspace).

The VS Code extension is in editors/vscode. It starts the installed kscript lsp (found through kscript.server.path, then the installers’ folders, then PATH) and adds a TextMate grammar, plus an injection grammar for { expressions } in pages. In that folder: npm test runs the unit tests, npm run package builds kscript-vscode.vsix, npm run smoke -- <KScript.dll> checks the protocol against a built server, and npm run test:vscode -- <KScript.dll> runs the extension inside a downloaded VS Code. kscript ide install --vsix kscript-vscode.vsix installs a local build.

How it is built

Project Contents
KScript.Syntax Tolerant markup parser (XML for scripts, HTML5 rules for pages), the KScript 2 expression and template parsers, and the metadata types (ElementInfo, CallableInfo) packages describe themselves with. The interpreter uses the same parsers.
KScript.Language The catalog, the analyzer and the editor features.
KScript.LanguageServer A small JSON-RPC/LSP layer over KScript.Language, plus kscript check and kscript fmt.
KScript.Language.Tests Parser, analyzer, feature, protocol and catalog contract tests.

Types

TypeInference gives every expression a TypeShape: a type, the fields of a map and the item shape of a list. Shapes come from literals, the catalog’s return types, @read_json files, loop and lambda items, parameter types and defaults, and what functions return (Analyzer.Types.cs). A function’s return type comes from its value or its <return>s; because a call can come before the function, Analyzer.Analyze repeats the analysis (at most five passes) until the return types stop changing. Imported files are analysed for their functions’ return types and top-level variables. <set> fills in a variable whose type wasn’t known (declared as null).

$config is typed from the project’s settings (Analyzer.Config.cs): kscript.json’s "config" section, every kscript.<env>.json and the map configure(environment) returns in config.kscript, so $config. offers their keys. When every source can be read (no <config> in the file, no app.user_config, and configure returns one map written out), a key none of them sets is a KS5010 error. In config.kscript, database.default offers and checks the names under database.connections. A migration’s columns are typed slots too (Analyzer.Migrations.cs): default: must fit the column’s type (one of an enum’s values:), references: must name a table a migration makes, and a CreateTable lists each column once.

Shapes drive member completion, hovers (TypeShape.Pretty), inlay hints (textDocument/inlayHint) and method details. The server also answers textDocument/foldingRange and its own kscript/blocks request, which the VS Code extension uses to draw notebook-style lines between a script’s top-level blocks.

For running code from the editor, kscript/tests lists the tests and describe groups of a test file (open or on disk), each with its full name (group › test, as kscript test --filter matches it) and range. textDocument/codeLens puts “Run” above a script’s first statement and “Run test” above each test. The lenses call the editor commands kscript.run, kscript.runTest and kscript.runTestsInFile with the document’s URI first, so the server only offers them when the editor sends initializationOptions: { "codeLens": true } to say it has those commands. A new, unsaved .kscript document (language id kscript-code) is parsed as code even though it has no file name yet.

The same lenses put “3 uses” above each function something calls, or “used in other files” when only another file of the project does. That lens calls kscript.showReferences with the URI and the position of the function’s name. workspace/symbol finds the functions and constants of every script in the folders the editor opened (workspaceFolders or rootUri), by any part of the name.

Suggestions are ordered by sortText: the statements and methods used most come first (Ranking in KScript.Language/Features holds the lists, and a test checks every name in them exists), then the rest by name, and a script’s own variables come before built-in ones, nearest first. Inlay hints name the parameter beside a number, text, list or map written straight into a call with several arguments. Hovers on commands and methods show the catalog’s example in the file’s own format, and end with an “Open docs” link when the editor sends initializationOptions: { "docsCommand": "<command>" }: the link runs that command with the name as its argument.

Imports and typed values

An import with an alias (import "../controllers/accounts.kscript" as accounts) gives the alias a symbol for each function and constant the file declares (Symbol.Members, each with Alias set). Uses such as @accounts.show_login and accounts.limit are references to those symbols, so completion after @accounts. lists them with their signatures and docs, hover shows them, go to definition opens the imported file, and calls through the alias have their arguments checked. A name the file doesn’t have is KS4005. textDocument/references on a function or constant other scripts import also searches the rest of the project (LanguageService.GetProjectReferences), and textDocument/rename on one renames it in every file that uses or imports it, so the project keeps working. A type’s name is a reference wherever it is written, in a parameter, field or variable’s type as well as in code, so renaming the type follows all of them. Text in single quotes is never a reference: 'Hello $name' only mentions the name. A package alias (import KScript.Math as math) completes and hovers its package’s commands.

Renaming a type whose file is named after it (types/order-status.kscript for OrderStatus) renames the file too, and the imports that point at it follow. Renaming a component also changes its tag in the pages (<note-card> becomes <memo-card>). Moving or renaming files in the editor (workspace/willRenameFiles) updates the import "…" paths that point at them, and the moved files’ own relative imports.

Some fixes reach other files. KS6008 (a second type in a file) offers “Move OrderStatus to types/order-status.kscript”: it makes the file with the type and the imports it uses, takes the type out, and imports the new file where it was and in every file that used it through the old one. A type inside a component has the same move as a refactoring, and a file that names such a type gets it as a fix. A type the file doesn’t import, such as Post when models/post.kscript declares it, has the fix “Import models/post.kscript”. Completion offers the project’s types from types/, models/ and errors/ with the import they need. The language server sends these as documentChanges with create and rename operations, so an editor must support those to be offered them.

Where the type of a value is known ($tz: timezone = ‸, a typed parameter, a named argument), completion lists the values the type accepts before anything else: an enum’s values, and what a native type suggests. A native type supplies those through NativeType.Suggest (KScript.Core/Runtime/Natives), which gets a NativeValueContext (the file, the project folder, what is typed, the type’s options and the project’s routes, read from the route files without running them) and returns NativeValueSuggestions. By default it gives the type’s Completions; timezone, country, currency and locale override it with every value, and slash_path with the project’s routes. Hover on a type name and signature help say what the type accepts. Straight after @ only commands and functions are offered (those that give the expected type first), and straight after $ only variables.

An editor that runs a script can set KSCRIPT_ERROR_PATHS=full, and the error a script stops with then names its file with the whole path (ERROR /work/app/hello.kscript:3:5: ...) so the editor can link to the line.

The parsers never throw and keep exact source positions (including through &lt; entities), so every feature works on half-typed code. Entry points: ExpressionParser.ParseExpression, TemplateParser.Parse, MarkupParser.Parse and Analyzer.Analyze.

Describing the language: the catalog

LanguageCatalog is the single source of truth for what the tools know. It is built from the packages the interpreter has (PackageRegistry): each package registers its elements, commands, methods and variables with an ElementInfo or CallableInfo that holds the description, attributes, parameters, example and placement rules, so a new element, command or method shows up in completion, hover and diagnostics without touching the language server. Every entry records its package, which is how the analyzer knows a script needs <import package="...">.

KScript 1 elements, attributes and commands are in KScript.Runtime.Retired, with what to write instead; both the interpreter and the analyzer report them from there. Parts of the KScript 2 proposal that no package implements yet (the browser’s <render>, <on> and @dom) are described in LanguageCatalog itself and marked planned; once a package implements one, the package wins, as KScript.AI’s <llm> did.

A script imports KScript.Core, the packages in the nearest kscript.json, the ones named in <import package>, and the owner of any <KScript.X> element. Pages need no imports, but a page’s <import package> gives it the package’s variables ($session from KScript.Auth).

A KScript.Http view (views/auth/account.html) is typed from the scripts that render it: Analysis/ViewInputs.cs finds each @view('auth/account', ...) in the project’s scripts and declares the values it passes, with their shapes, as the view’s variables. A package says what fields a command’s result has with ResultShapes (("@api_tokens", "{ id: string, name: string }[]")), read by Analysis/ShapeText.cs, so a loop over $data.tokens knows each token’s fields.

Completion only offers what the file can use: built-ins and items from the packages it imports, at the top level, inside functions and after ->. A command from a package that isn’t imported is offered too, and picking it adds the import. Using an item from a package that isn’t imported is KS3009, with a quick fix that adds the import.

Constants that other scripts in the project declare at their top level are offered the same way. With const $puzzle_input = ... in lib/puzzle-input.kscript, typing puz or $puz in main.kscript offers $puzzle_input, and picking it adds import "lib/puzzle-input.kscript" when the file isn’t imported yet (Analysis/ProjectConstants.cs finds them). A $puzzle_input written without the import is KS4001, saying which file declares it, with a quick fix that imports it. A line that is only the name (puzzle_input) says to write it with its $, and its fix writes the $ (and the import, when one is needed). At the start of a line, a constant is offered as $limit, since a constant is only ever used there, never given a new value.

In a model header, completion reads field names and types from the model being declared. In hidden: [key_hash, ‸], it suggests the remaining fields, including fields declared later in the body. The same lookup powers key, guarded, index and unique, including multiline headers and composite indexes. An unknown field gets a diagnostic with a spelling fix when a close match exists.

The expected type also guides value completion. An enum parameter or typed array offers its members. A record constructor or typed map offers the values accepted by each field. A refinement with literal one_of choices offers those choices. When you type $ for a known parameter type, compatible variables appear first. Inside a type declaration, completion offers rules such as min, pattern and validate, with documentation on hover.

A parameter with no type takes one from the calls in its file when they all agree (Analyzer.Parameters.cs): the analysis runs again until those types settle, as it does for return types. The type shows as an inlay hint, and KS6007 warns about the missing type with a quick fix that writes it in. Functions inside a job are skipped, because the queue calls them.

The kscript serve page engine (KScript.Web/Pages) runs pages, including <live>, <state>, on-* handlers and bind. PageEngineContractTests read the engine’s source and fail when one of its methods, commands or elements is missing from the catalog. The tests also check that Example Sites/team analyzes without errors.

CatalogContractTests keep packages and tools in step: every package element, command and method must be in the catalog with a description and a handler, no two packages may define the same name, renames must point at real elements and attributes, and examples must analyze cleanly. When one fails after a package change, the fix is almost always in the description the package registers.

Diagnostic codes

Range Area
KS1xxx Markup (malformed XML, unclosed tags, front matter)
KS2xxx Expressions (syntax, assignment outside let/set/do, bare identifiers), KS2005 'single quotes' around text that names a variable (only "double quotes" fill it in), KS2006 a fn printed instead of called
KS3xxx Elements and attributes (unknown, retired, missing, invalid values, placement), imports (KS3009 missing import, KS3010 unknown package)
KS4xxx Names (undefined variables and functions, constants, duplicates)
KS5xxx Calls (unknown or retired commands and methods, argument counts, type mismatches), KS5010 a field a <type>’s records don’t have, a misspelt field of a map or a $config key no settings file sets, KS5011 an error type in catch, throw, extends or expect_error that isn’t declared or built in, KS5012 a literal that doesn’t fit its declared type ($a: Age = 200, $e: email = bob@), KS5013 a column a KScript.Data model doesn’t have (User->where(nmae: 'x')) or a table a migration’s references: names that no migration makes, KS5014 a model the migrations don’t match
KS6xxx Old spellings and style. An old spelling in a .kscript file (KS6006, KS6009 to KS6012, KS6014, KS6019) is an error with a quick fix, whatever the standard says, because the script doesn’t run. The style rules keep code easy to read: they are only on when a standard turns them on; kscript:recommended, the default, makes them warnings. KS6001 flags else and elif in scripts, where an early return, continue or break reads better (see Early returns instead of else). KS6002 to KS6005 flag unused functions, variables, parameters and imports, counting uses from the rest of the project, and are sent with the Unnecessary tag so editors fade the code (see Unused code). KS6006 marks a list written as one string in a .kscript file, with a fix that writes [a, b]

Every code is explained for users in Error codes, and the server sends that page as each diagnostic’s codeDescription, so the code in the editor is a link. DiagnosticCodeDocsTests fails when a code has no section there.

In a .kscript file, messages use that format’s words (Unknown statement 'x', never <x>), and words from other languages (print, function, x++, @len()) are answered with what KScript writes and a quick fix: see Analyzer.Habits.cs.

The constants are in DiagnosticCodes; each has positive and negative tests in AnalyzerTests.

Edit this page on GitHub