0
# Language Features
1
2
Complete Language Server Protocol feature implementations including completion, hover, diagnostics, and all standard LSP capabilities. These features provide the core intelligent code editing experience.
3
4
## Capabilities
5
6
### Completion
7
8
Provides code completion suggestions including functions, variables, keywords, and snippets.
9
10
```typescript { .api }
11
/**
12
* Register handler for completion requests
13
* @param handler Function to handle completion requests
14
*/
15
onCompletion(handler: RequestHandler<CompletionParams, CompletionItem[] | CompletionList | null, CompletionItem>): Disposable;
16
17
/**
18
* Register handler for completion item resolve requests
19
* @param handler Function to resolve additional completion item details
20
*/
21
onCompletionResolve(handler: RequestHandler<CompletionItem, CompletionItem, void>): Disposable;
22
23
interface CompletionParams extends TextDocumentPositionParams, WorkDoneProgressParams, PartialResultParams {
24
/** The completion context */
25
context?: CompletionContext;
26
}
27
28
interface CompletionItem {
29
/** The label of this completion item */
30
label: string;
31
/** The kind of this completion item */
32
kind?: CompletionItemKind;
33
/** A human-readable string with additional information about this item */
34
detail?: string;
35
/** A human-readable string that represents a doc-comment */
36
documentation?: string | MarkupContent;
37
/** Indicates if this item is deprecated */
38
deprecated?: boolean;
39
/** The string that should be inserted into a document when selecting this completion */
40
insertText?: string;
41
/** The format of the insert text */
42
insertTextFormat?: InsertTextFormat;
43
/** An edit which is applied to a document when selecting this completion */
44
textEdit?: TextEdit | InsertReplaceEdit;
45
/** Additional text edits that are applied when selecting this completion */
46
additionalTextEdits?: TextEdit[];
47
/** An optional command that is executed after inserting this completion */
48
command?: Command;
49
/** Data that is preserved during completion resolve */
50
data?: any;
51
}
52
53
interface CompletionList {
54
/** This list is not complete */
55
isIncomplete: boolean;
56
/** The completion items */
57
items: CompletionItem[];
58
}
59
```
60
61
**Usage Example:**
62
63
```typescript
64
connection.onCompletion((params): CompletionItem[] => {
65
return [
66
{
67
label: 'console.log',
68
kind: CompletionItemKind.Function,
69
detail: 'Log to console',
70
documentation: 'Outputs a message to the console',
71
insertText: 'console.log($1)',
72
insertTextFormat: InsertTextFormat.Snippet
73
}
74
];
75
});
76
77
connection.onCompletionResolve((item): CompletionItem => {
78
if (item.data === 'console.log') {
79
item.documentation = {
80
kind: 'markdown',
81
value: '**console.log(message: any, ...optionalParams: any[]): void**\n\nOutputs a message to the console.'
82
};
83
}
84
return item;
85
});
86
```
87
88
### Hover
89
90
Provides hover information when users hover over symbols.
91
92
```typescript { .api }
93
/**
94
* Register handler for hover requests
95
* @param handler Function to handle hover requests
96
*/
97
onHover(handler: RequestHandler<HoverParams, Hover | null, void>): Disposable;
98
99
interface HoverParams extends TextDocumentPositionParams, WorkDoneProgressParams {
100
}
101
102
interface Hover {
103
/** The hover's content */
104
contents: MarkedString | MarkedString[] | MarkupContent;
105
/** An optional range is a range inside a text document that is used to visualize a hover */
106
range?: Range;
107
}
108
109
type MarkedString = string | { language: string; value: string };
110
111
interface MarkupContent {
112
/** The type of the Markup */
113
kind: MarkupKind;
114
/** The content itself */
115
value: string;
116
}
117
118
enum MarkupKind {
119
PlainText = 'plaintext',
120
Markdown = 'markdown'
121
}
122
```
123
124
**Usage Example:**
125
126
```typescript
127
connection.onHover((params): Hover | null => {
128
const document = documents.get(params.textDocument.uri);
129
if (!document) return null;
130
131
const position = params.position;
132
const word = getWordAtPosition(document, position);
133
134
if (word === 'console') {
135
return {
136
contents: {
137
kind: MarkupKind.Markdown,
138
value: '**console**\n\nProvides access to the browser or Node.js debugging console.'
139
},
140
range: getWordRangeAtPosition(document, position)
141
};
142
}
143
144
return null;
145
});
146
```
147
148
### Go to Definition
149
150
Provides navigation to symbol definitions.
151
152
```typescript { .api }
153
/**
154
* Register handler for go to definition requests
155
* @param handler Function to handle definition requests
156
*/
157
onDefinition(handler: RequestHandler<DefinitionParams, Definition | DefinitionLink[] | null, void>): Disposable;
158
159
interface DefinitionParams extends TextDocumentPositionParams, WorkDoneProgressParams, PartialResultParams {
160
}
161
162
type Definition = Location | Location[];
163
164
interface DefinitionLink {
165
/** Span of the origin of this link */
166
originSelectionRange?: Range;
167
/** The target resource identifier of this link */
168
targetUri: DocumentUri;
169
/** The full target range of this link */
170
targetRange: Range;
171
/** The range that should be selected and revealed when this link is followed */
172
targetSelectionRange: Range;
173
}
174
175
interface Location {
176
uri: DocumentUri;
177
range: Range;
178
}
179
```
180
181
### Find References
182
183
Finds all references to a symbol.
184
185
```typescript { .api }
186
/**
187
* Register handler for find references requests
188
* @param handler Function to handle references requests
189
*/
190
onReferences(handler: RequestHandler<ReferenceParams, Location[] | null, void>): Disposable;
191
192
interface ReferenceParams extends TextDocumentPositionParams, WorkDoneProgressParams, PartialResultParams {
193
context: ReferenceContext;
194
}
195
196
interface ReferenceContext {
197
/** Include the declaration of the current symbol */
198
includeDeclaration: boolean;
199
}
200
```
201
202
### Document Highlights
203
204
Highlights all occurrences of a symbol in a document.
205
206
```typescript { .api }
207
/**
208
* Register handler for document highlight requests
209
* @param handler Function to handle document highlight requests
210
*/
211
onDocumentHighlight(handler: RequestHandler<DocumentHighlightParams, DocumentHighlight[] | null, void>): Disposable;
212
213
interface DocumentHighlightParams extends TextDocumentPositionParams, WorkDoneProgressParams, PartialResultParams {
214
}
215
216
interface DocumentHighlight {
217
/** The range this highlight applies to */
218
range: Range;
219
/** The highlight kind */
220
kind?: DocumentHighlightKind;
221
}
222
223
enum DocumentHighlightKind {
224
/** A textual occurrence */
225
Text = 1,
226
/** Read-access of a symbol */
227
Read = 2,
228
/** Write-access of a symbol */
229
Write = 3
230
}
231
```
232
233
### Document Symbols
234
235
Provides hierarchical symbols for a document.
236
237
```typescript { .api }
238
/**
239
* Register handler for document symbol requests
240
* @param handler Function to handle document symbol requests
241
*/
242
onDocumentSymbol(handler: RequestHandler<DocumentSymbolParams, SymbolInformation[] | DocumentSymbol[] | null, void>): Disposable;
243
244
interface DocumentSymbolParams extends WorkDoneProgressParams, PartialResultParams {
245
/** The text document */
246
textDocument: TextDocumentIdentifier;
247
}
248
249
interface DocumentSymbol {
250
/** The name of this symbol */
251
name: string;
252
/** More detail for this symbol */
253
detail?: string;
254
/** The kind of this symbol */
255
kind: SymbolKind;
256
/** Tags for this symbol */
257
tags?: SymbolTag[];
258
/** Indicates if this symbol is deprecated */
259
deprecated?: boolean;
260
/** The range enclosing this symbol */
261
range: Range;
262
/** The range that should be selected and revealed when this symbol is being picked */
263
selectionRange: Range;
264
/** Children of this symbol */
265
children?: DocumentSymbol[];
266
}
267
268
interface SymbolInformation {
269
/** The name of this symbol */
270
name: string;
271
/** The kind of this symbol */
272
kind: SymbolKind;
273
/** Tags for this symbol */
274
tags?: SymbolTag[];
275
/** Indicates if this symbol is deprecated */
276
deprecated?: boolean;
277
/** The location of this symbol */
278
location: Location;
279
/** The name of the symbol containing this symbol */
280
containerName?: string;
281
}
282
```
283
284
### Workspace Symbols
285
286
Provides workspace-wide symbol search.
287
288
```typescript { .api }
289
/**
290
* Register handler for workspace symbol requests
291
* @param handler Function to handle workspace symbol requests
292
*/
293
onWorkspaceSymbol(handler: RequestHandler<WorkspaceSymbolParams, SymbolInformation[] | WorkspaceSymbol[] | null, WorkspaceSymbol>): Disposable;
294
295
/**
296
* Register handler for workspace symbol resolve requests
297
* @param handler Function to resolve workspace symbol details
298
*/
299
onWorkspaceSymbolResolve(handler: RequestHandler<WorkspaceSymbol, WorkspaceSymbol, void>): Disposable;
300
301
interface WorkspaceSymbolParams extends WorkDoneProgressParams, PartialResultParams {
302
/** A query string to filter symbols by */
303
query: string;
304
}
305
306
interface WorkspaceSymbol {
307
/** The name of this symbol */
308
name: string;
309
/** The kind of this symbol */
310
kind: SymbolKind;
311
/** Tags for this symbol */
312
tags?: SymbolTag[];
313
/** The name of the symbol containing this symbol */
314
containerName?: string;
315
/** The location of the symbol */
316
location: Location | { uri: DocumentUri };
317
/** Data that is preserved during resolve */
318
data?: any;
319
}
320
```
321
322
### Code Actions
323
324
Provides code actions like quick fixes and refactorings.
325
326
```typescript { .api }
327
/**
328
* Register handler for code action requests
329
* @param handler Function to handle code action requests
330
*/
331
onCodeAction(handler: RequestHandler<CodeActionParams, (Command | CodeAction)[] | null, void>): Disposable;
332
333
/**
334
* Register handler for code action resolve requests
335
* @param handler Function to resolve code action details
336
*/
337
onCodeActionResolve(handler: RequestHandler<CodeAction, CodeAction, void>): Disposable;
338
339
interface CodeActionParams extends WorkDoneProgressParams, PartialResultParams {
340
/** The document in which the command was invoked */
341
textDocument: TextDocumentIdentifier;
342
/** The range for which the command was invoked */
343
range: Range;
344
/** Context carrying additional information */
345
context: CodeActionContext;
346
}
347
348
interface CodeActionContext {
349
/** An array of diagnostics known on the client side overlapping the range provided to the code action request */
350
diagnostics: Diagnostic[];
351
/** Requested kinds of actions to return */
352
only?: CodeActionKind[];
353
/** The reason why code actions were requested */
354
triggerKind?: CodeActionTriggerKind;
355
}
356
357
interface CodeAction {
358
/** A short, human-readable, title for this code action */
359
title: string;
360
/** The kind of the code action */
361
kind?: CodeActionKind;
362
/** The diagnostics that this code action resolves */
363
diagnostics?: Diagnostic[];
364
/** Marks this as a preferred action */
365
isPreferred?: boolean;
366
/** Marks that the code action cannot currently be applied */
367
disabled?: { reason: string };
368
/** The workspace edit this code action performs */
369
edit?: WorkspaceEdit;
370
/** A command this code action executes */
371
command?: Command;
372
/** Data that is preserved during resolve */
373
data?: any;
374
}
375
```
376
377
### Formatting
378
379
Provides document and range formatting capabilities.
380
381
```typescript { .api }
382
/**
383
* Register handler for document formatting requests
384
* @param handler Function to handle document formatting requests
385
*/
386
onDocumentFormatting(handler: RequestHandler<DocumentFormattingParams, TextEdit[] | null, void>): Disposable;
387
388
/**
389
* Register handler for document range formatting requests
390
* @param handler Function to handle range formatting requests
391
*/
392
onDocumentRangeFormatting(handler: RequestHandler<DocumentRangeFormattingParams, TextEdit[] | null, void>): Disposable;
393
394
/**
395
* Register handler for document on-type formatting requests
396
* @param handler Function to handle on-type formatting requests
397
*/
398
onDocumentOnTypeFormatting(handler: RequestHandler<DocumentOnTypeFormattingParams, TextEdit[] | null, void>): Disposable;
399
400
interface DocumentFormattingParams extends WorkDoneProgressParams {
401
/** The document to format */
402
textDocument: TextDocumentIdentifier;
403
/** The format options */
404
options: FormattingOptions;
405
}
406
407
interface DocumentRangeFormattingParams extends WorkDoneProgressParams {
408
/** The document to format */
409
textDocument: TextDocumentIdentifier;
410
/** The range to format */
411
range: Range;
412
/** The format options */
413
options: FormattingOptions;
414
}
415
416
interface DocumentOnTypeFormattingParams extends TextDocumentPositionParams {
417
/** The character that has been typed */
418
ch: string;
419
/** The format options */
420
options: FormattingOptions;
421
}
422
423
interface FormattingOptions {
424
/** Size of a tab in spaces */
425
tabSize: number;
426
/** Prefer spaces over tabs */
427
insertSpaces: boolean;
428
/** Trim trailing whitespace on a line */
429
trimTrailingWhitespace?: boolean;
430
/** Insert a newline character at the end of the file if one does not exist */
431
insertFinalNewline?: boolean;
432
/** Trim all newlines after the final newline at the end of the file */
433
trimFinalNewlines?: boolean;
434
}
435
```
436
437
### Rename
438
439
Provides symbol renaming capabilities.
440
441
```typescript { .api }
442
/**
443
* Register handler for rename requests
444
* @param handler Function to handle rename requests
445
*/
446
onRename(handler: RequestHandler<RenameParams, WorkspaceEdit | null, void>): Disposable;
447
448
/**
449
* Register handler for prepare rename requests
450
* @param handler Function to handle prepare rename requests
451
*/
452
onPrepareRename(handler: RequestHandler<PrepareRenameParams, Range | { range: Range; placeholder: string } | { defaultBehavior: boolean } | null, void>): Disposable;
453
454
interface RenameParams extends TextDocumentPositionParams, WorkDoneProgressParams {
455
/** The new name of the symbol */
456
newName: string;
457
}
458
459
interface PrepareRenameParams extends TextDocumentPositionParams, WorkDoneProgressParams {
460
}
461
462
interface WorkspaceEdit {
463
/** Holds changes to existing resources */
464
changes?: { [uri: DocumentUri]: TextEdit[] };
465
/** Depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes are either an array of `TextDocumentEdit`s to express changes to n different text documents where each text document edit addresses a specific version of a text document */
466
documentChanges?: (TextDocumentEdit | CreateFile | RenameFile | DeleteFile)[];
467
/** A map of change annotations that can be referenced in document edits */
468
changeAnnotations?: { [id: string]: ChangeAnnotation };
469
}
470
```
471
472
## Core Types
473
474
```typescript { .api }
475
interface TextDocumentPositionParams {
476
/** The text document */
477
textDocument: TextDocumentIdentifier;
478
/** The position inside the text document */
479
position: Position;
480
}
481
482
interface WorkDoneProgressParams {
483
/** An optional token that a server can use to report work done progress */
484
workDoneToken?: ProgressToken;
485
}
486
487
interface PartialResultParams {
488
/** An optional token that a server can use to report partial results to the client */
489
partialResultToken?: ProgressToken;
490
}
491
492
type ProgressToken = number | string;
493
494
interface Command {
495
/** Title of the command */
496
title: string;
497
/** The identifier of the actual command handler */
498
command: string;
499
/** Arguments that the command handler should be invoked with */
500
arguments?: any[];
501
}
502
503
enum SymbolKind {
504
File = 1,
505
Module = 2,
506
Namespace = 3,
507
Package = 4,
508
Class = 5,
509
Method = 6,
510
Property = 7,
511
Field = 8,
512
Constructor = 9,
513
Enum = 10,
514
Interface = 11,
515
Function = 12,
516
Variable = 13,
517
Constant = 14,
518
String = 15,
519
Number = 16,
520
Boolean = 17,
521
Array = 18,
522
Object = 19,
523
Key = 20,
524
Null = 21,
525
EnumMember = 22,
526
Struct = 23,
527
Event = 24,
528
Operator = 25,
529
TypeParameter = 26
530
}
531
532
enum CompletionItemKind {
533
Text = 1,
534
Method = 2,
535
Function = 3,
536
Constructor = 4,
537
Field = 5,
538
Variable = 6,
539
Class = 7,
540
Interface = 8,
541
Module = 9,
542
Property = 10,
543
Unit = 11,
544
Value = 12,
545
Enum = 13,
546
Keyword = 14,
547
Snippet = 15,
548
Color = 16,
549
File = 17,
550
Reference = 18,
551
Folder = 19,
552
EnumMember = 20,
553
Constant = 21,
554
Struct = 22,
555
Event = 23,
556
Operator = 24,
557
TypeParameter = 25
558
}
559
560
enum InsertTextFormat {
561
PlainText = 1,
562
Snippet = 2
563
}
564
```