Skip to content

Commit 1705043

Browse files
Update language service documentation with getDiagnostics API
- Added Diagnostics feature to the Features section - Added getDiagnostics() to Basic Usage example - Updated Monaco Editor sample description to mention diagnostics demo - Added complete ls.getDiagnostics() API reference section - Added Monaco Editor integration example for diagnostics - Updated Exported Types to include GetDiagnosticsParams and ArityInfo - Updated LSP Types to include Diagnostic and DiagnosticSeverity Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
1 parent cb03462 commit 1705043

1 file changed

Lines changed: 66 additions & 3 deletions

File tree

docs/language-service.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ The library includes a built-in language service that provides IDE-like features
1212
- **Variable value previews** - Hovers on variables show a truncated JSON preview of the value
1313
- **Nested path support** - Hovering over `user.name` resolves and shows the value at that path
1414
- **Syntax Highlighting** - Token-based highlighting for numbers, strings, keywords, operators, etc.
15+
- **Diagnostics** - Error detection for function argument count validation
16+
- **Too few arguments** - Reports when a function is called with fewer arguments than required (e.g., `pow(2)` needs 2 arguments)
17+
- **Too many arguments** - Reports when a function is called with more arguments than allowed (e.g., `random(1, 2, 3)` accepts at most 1)
18+
- **Variadic functions** - Correctly handles functions that accept unlimited arguments (e.g., `min`, `max`, `coalesce`)
1519

1620
## Basic Usage
1721

@@ -39,6 +43,9 @@ const hover = ls.getHover({
3943

4044
// Get syntax highlighting tokens
4145
const tokens = ls.getHighlighting(doc);
46+
47+
// Get diagnostics (function argument count errors)
48+
const diagnostics = ls.getDiagnostics({ textDocument: doc });
4249
```
4350

4451
## Monaco Editor Integration Sample
@@ -56,13 +63,15 @@ Then open http://localhost:8080 in your browser. The sample demonstrates:
5663
- Hover documentation for functions and variables
5764
- Live syntax highlighting
5865
- Real-time expression evaluation
66+
- **Diagnostics** - Red squiggly underlines for function argument count errors (select the "Diagnostics Demo" example to see this in action)
5967

6068
The sample code is located in `samples/language-service-sample/` and shows how to:
6169

6270
1. Register a custom language with Monaco
6371
2. Connect the language service to Monaco's completion and hover providers
6472
3. Apply syntax highlighting using decorations
6573
4. Create an LSP-compatible text document wrapper for Monaco models
74+
5. Display diagnostics using Monaco's `setModelMarkers` API
6675

6776
## Advanced Features
6877

@@ -318,6 +327,54 @@ tokens.forEach(token => {
318327
});
319328
```
320329
330+
### ls.getDiagnostics(params)
331+
332+
Returns a list of diagnostics for the given text document. Currently validates function argument counts.
333+
334+
**Parameters:**
335+
- `params`: `GetDiagnosticsParams`
336+
- `textDocument`: `TextDocument` - The text document to analyze
337+
338+
**Returns:** `Diagnostic[]` - Array of LSP-compatible diagnostic objects
339+
340+
**Diagnostic Properties:**
341+
- `range`: `Range` - The range of the problematic function call
342+
- `severity`: `DiagnosticSeverity` - The severity level (Error)
343+
- `message`: `string` - Human-readable description of the issue
344+
- `source`: `string` - Always `'expr-eval'`
345+
346+
**Example:**
347+
```js
348+
const diagnostics = ls.getDiagnostics({ textDocument: doc });
349+
diagnostics.forEach(d => {
350+
console.log(`${d.message} at line ${d.range.start.line}`);
351+
});
352+
353+
// For expression "pow(2) + random(1, 2, 3)":
354+
// "Function 'pow' expects at least 2 arguments, but got 1." at line 0
355+
// "Function 'random' expects at most 1 argument, but got 3." at line 0
356+
```
357+
358+
**Monaco Editor Integration:**
359+
```js
360+
function applyDiagnostics() {
361+
const doc = makeTextDocument(model);
362+
const diagnostics = ls.getDiagnostics({ textDocument: doc });
363+
364+
const markers = diagnostics.map(d => ({
365+
severity: monaco.MarkerSeverity.Error,
366+
message: d.message,
367+
startLineNumber: d.range.start.line + 1,
368+
startColumn: d.range.start.character + 1,
369+
endLineNumber: d.range.end.line + 1,
370+
endColumn: d.range.end.character + 1,
371+
source: d.source
372+
}));
373+
374+
monaco.editor.setModelMarkers(model, 'expr-eval', markers);
375+
}
376+
```
377+
321378
## TypeScript Types
322379
323380
The library exports the following TypeScript types for use in your applications:
@@ -330,17 +387,21 @@ import type {
330387
HoverV2,
331388
GetCompletionsParams,
332389
GetHoverParams,
390+
GetDiagnosticsParams,
333391
HighlightToken,
334-
LanguageServiceOptions
392+
LanguageServiceOptions,
393+
ArityInfo
335394
} from '@pro-fa/expr-eval';
336395
```
337396
338-
- **`LanguageServiceApi`** - The main language service interface with `getCompletions`, `getHover`, and `getHighlighting` methods
397+
- **`LanguageServiceApi`** - The main language service interface with `getCompletions`, `getHover`, `getHighlighting`, and `getDiagnostics` methods
339398
- **`HoverV2`** - Extended Hover type with guaranteed `MarkupContent` for contents (not deprecated string/array formats)
340399
- **`GetCompletionsParams`** - Parameters for `getCompletions`: `textDocument`, `position`, and optional `variables`
341400
- **`GetHoverParams`** - Parameters for `getHover`: `textDocument`, `position`, and optional `variables`
401+
- **`GetDiagnosticsParams`** - Parameters for `getDiagnostics`: `textDocument`
342402
- **`HighlightToken`** - Syntax highlighting token with `type`, `start`, `end`, and optional `value`
343403
- **`LanguageServiceOptions`** - Configuration options for creating a language service, including optional `operators` map
404+
- **`ArityInfo`** - Describes a function's expected argument count with `min` and optional `max` (undefined for variadic functions)
344405
345406
### LSP Types
346407
@@ -354,7 +415,9 @@ import type {
354415
CompletionItemKind,
355416
MarkupContent,
356417
MarkupKind,
357-
InsertTextFormat
418+
InsertTextFormat,
419+
Diagnostic,
420+
DiagnosticSeverity
358421
} from 'vscode-languageserver-types';
359422

360423
import type { TextDocument } from 'vscode-languageserver-textdocument';

0 commit comments

Comments
 (0)