Skip to content

Commit 628c78f

Browse files
authored
Provide API for language service (#180)
1 parent 263c277 commit 628c78f

File tree

7 files changed

+72
-49
lines changed

7 files changed

+72
-49
lines changed

language-service/package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

language-service/package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
"bugs": {
1313
"url": "https://github.com/Microsoft/lsif-node/issues"
1414
},
15-
"main": "lib/main.js",
16-
"typings": "lib/main.d.ts",
15+
"engines": {
16+
"node": ">=20.9.0"
17+
},
18+
"exports": {
19+
".": {
20+
"types": "./lib/api.d.ts",
21+
"default": "./lib/api.js"
22+
}
23+
},
1724
"dependencies": {
1825
"vscode-uri": "^3.0.8",
1926
"semver": "^7.6.2",
20-
"lsif-protocol": "0.6.0-next.7",
21-
"vscode-languageserver-types": "^3.17.5"
27+
"lsif-protocol": "0.6.0-next.7"
2228
},
2329
"devDependencies": {
2430
"@types/semver": "^7.5.7",

language-service/src/api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
import { UriTransformer, Database, noopTransformer } from './database';
7+
import { JsonStore } from './jsonStore';
8+
import { FileType, DocumentInfo, FileStat } from './files';
9+
10+
export * from 'lsif-protocol';
11+
export { UriTransformer, noopTransformer, Database, JsonStore, FileType, DocumentInfo, FileStat };

language-service/src/database.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
* ------------------------------------------------------------------------------------------ */
55
import { URI } from 'vscode-uri';
6-
import * as lsp from 'vscode-languageserver-types';
7-
import { Range, Id } from 'lsif-protocol';
6+
import { Range, Id, types } from 'lsif-protocol';
87

98
import { FileType, FileSystem, DocumentInfo, FileStat } from './files';
109

@@ -73,34 +72,34 @@ export abstract class Database {
7372
return result;
7473
}
7574

76-
protected abstract findFile(uri: string):{ id: Id; hash: string | undefined } | undefined;
75+
protected abstract findFile(uri: string): { id: Id; hash: string | undefined } | undefined;
7776

7877
protected abstract fileContent( info: { id: Id; hash: string | undefined } ) : string | undefined;
7978

80-
public abstract foldingRanges(uri: string): lsp.FoldingRange[] | undefined;
79+
public abstract foldingRanges(uri: string): types.FoldingRange[] | undefined;
8180

82-
public abstract documentSymbols(uri: string): lsp.DocumentSymbol[] | undefined;
81+
public abstract documentSymbols(uri: string): types.DocumentSymbol[] | undefined;
8382

84-
public abstract hover(uri: string, position: lsp.Position): lsp.Hover | undefined;
83+
public abstract hover(uri: string, position: types.Position): types.Hover | undefined;
8584

86-
public abstract declarations(uri: string, position: lsp.Position): lsp.Location | lsp.Location[] | undefined;
85+
public abstract declarations(uri: string, position: types.Position): types.Location | types.Location[] | undefined;
8786

88-
public abstract definitions(uri: string, position: lsp.Position): lsp.Location | lsp.Location[] | undefined;
87+
public abstract definitions(uri: string, position: types.Position): types.Location | types.Location[] | undefined;
8988

90-
public abstract references(uri: string, position: lsp.Position, context: lsp.ReferenceContext): lsp.Location[] | undefined;
89+
public abstract references(uri: string, position: types.Position, context: types.ReferenceContext): types.Location[] | undefined;
9190

92-
protected asDocumentSymbol(range: Range): lsp.DocumentSymbol | undefined {
91+
protected asDocumentSymbol(range: Range): types.DocumentSymbol | undefined {
9392
const tag = range.tag;
9493
if (tag === undefined || !(tag.type === 'declaration' || tag.type === 'definition')) {
9594
return undefined;
9695
}
97-
return lsp.DocumentSymbol.create(
96+
return types.DocumentSymbol.create(
9897
tag.text, tag.detail || '', tag.kind,
9998
tag.fullRange, this.asRange(range)
10099
);
101100
}
102101

103-
protected asRange(value: Range): lsp.Range {
102+
protected asRange(value: Range): types.Range {
104103
return {
105104
start: {
106105
line: value.start.line,

language-service/src/jsonStore.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import * as readline from 'readline';
99
import { URI } from 'vscode-uri';
1010
import * as SemVer from 'semver';
1111

12-
import * as lsp from 'vscode-languageserver-types';
1312
import {
1413
Id, Vertex, Project, Document, Range, DiagnosticResult, DocumentSymbolResult, FoldingRangeResult, DocumentLinkResult, DefinitionResult,
1514
TypeDefinitionResult, HoverResult, ReferenceResult, ImplementationResult, Edge, RangeBasedDocumentSymbol, DeclarationResult,
16-
ElementTypes, VertexLabels, EdgeLabels, ItemEdgeProperties, EventScope, EventKind, ProjectEvent, Moniker as PMoniker, MonikerKind
15+
ElementTypes, VertexLabels, EdgeLabels, ItemEdgeProperties, EventScope, EventKind, ProjectEvent, Moniker as PMoniker, MonikerKind,
16+
types
1717
} from 'lsif-protocol';
1818

1919
import { DocumentInfo } from './files';
@@ -72,7 +72,7 @@ interface ResultPath<T> {
7272
}
7373

7474
namespace Locations {
75-
export function makeKey(location: lsp.Location): string {
75+
export function makeKey(location: types.Location): string {
7676
const range = location.range;
7777
return crypto.createHash('md5').update(JSON.stringify({ d: location.uri, sl: range.start.line, sc: range.start.character, el: range.end.line, ec: range.end.character }, undefined, 0)).digest('base64');
7878
}
@@ -372,7 +372,7 @@ export class JsonStore extends Database {
372372
return this.indices.contents.get(info.hash);
373373
}
374374

375-
public foldingRanges(uri: string): lsp.FoldingRange[] | undefined {
375+
public foldingRanges(uri: string): types.FoldingRange[] | undefined {
376376
const value = this.indices.documents.get(this.toDatabase(uri));
377377
if (value === undefined) {
378378
return undefined;
@@ -384,14 +384,14 @@ export class JsonStore extends Database {
384384
if (foldingRangeResult === undefined) {
385385
return undefined;
386386
}
387-
const result: lsp.FoldingRange[] = [];
387+
const result: types.FoldingRange[] = [];
388388
for (const item of foldingRangeResult.result) {
389389
result.push(Object.assign(Object.create(null), item));
390390
}
391391
return result;
392392
}
393393

394-
public documentSymbols(uri: string): lsp.DocumentSymbol[] | undefined {
394+
public documentSymbols(uri: string): types.DocumentSymbol[] | undefined {
395395
const value = this.indices.documents.get(this.toDatabase(uri));
396396
if (value === undefined) {
397397
return undefined;
@@ -404,8 +404,8 @@ export class JsonStore extends Database {
404404
return undefined;
405405
}
406406
const first = documentSymbolResult.result[0];
407-
const result: lsp.DocumentSymbol[] = [];
408-
if (lsp.DocumentSymbol.is(first)) {
407+
const result: types.DocumentSymbol[] = [];
408+
if (types.DocumentSymbol.is(first)) {
409409
for (const item of documentSymbolResult.result) {
410410
result.push(Object.assign(Object.create(null), item));
411411
}
@@ -420,13 +420,13 @@ export class JsonStore extends Database {
420420
return result;
421421
}
422422

423-
private toDocumentSymbol(value: RangeBasedDocumentSymbol): lsp.DocumentSymbol | undefined {
423+
private toDocumentSymbol(value: RangeBasedDocumentSymbol): types.DocumentSymbol | undefined {
424424
const range = this.vertices.ranges.get(value.id)!;
425425
const tag = range.tag;
426426
if (tag === undefined || !(tag.type === 'declaration' || tag.type === 'definition')) {
427427
return undefined;
428428
}
429-
const result: lsp.DocumentSymbol = lsp.DocumentSymbol.create(
429+
const result: types.DocumentSymbol = types.DocumentSymbol.create(
430430
tag.text, tag.detail || '', tag.kind,
431431
tag.fullRange, this.asRange(range)
432432
);
@@ -442,7 +442,7 @@ export class JsonStore extends Database {
442442
return result;
443443
}
444444

445-
public hover(uri: string, position: lsp.Position): lsp.Hover | undefined {
445+
public hover(uri: string, position: types.Position): types.Hover | undefined {
446446
const ranges = this.findRangesFromPosition(this.toDatabase(uri), position);
447447
if (ranges === undefined) {
448448
return undefined;
@@ -463,21 +463,21 @@ export class JsonStore extends Database {
463463
};
464464
}
465465

466-
public declarations(uri: string, position: lsp.Position): lsp.Location | lsp.Location[] | undefined {
466+
public declarations(uri: string, position: types.Position): types.Location | types.Location[] | undefined {
467467
return this.findTargets(uri, position, this.out.declaration);
468468
}
469469

470-
public definitions(uri: string, position: lsp.Position): lsp.Location | lsp.Location[] | undefined {
470+
public definitions(uri: string, position: types.Position): types.Location | types.Location[] | undefined {
471471
return this.findTargets(uri, position, this.out.definition);
472472
}
473473

474-
private findTargets<T extends (DefinitionResult | DeclarationResult)>(uri: string, position: lsp.Position, edges: Map<Id, T>): lsp.Location | lsp.Location[] | undefined {
474+
private findTargets<T extends (DefinitionResult | DeclarationResult)>(uri: string, position: types.Position, edges: Map<Id, T>): types.Location | types.Location[] | undefined {
475475
const ranges = this.findRangesFromPosition(this.toDatabase(uri), position);
476476
if (ranges === undefined) {
477477
return undefined;
478478
}
479479

480-
const resolveTargets = (result: lsp.Location[], dedupLocations: Set<string>, targetResult: T): void => {
480+
const resolveTargets = (result: types.Location[], dedupLocations: Set<string>, targetResult: T): void => {
481481
const ranges = this.item(targetResult);
482482
if (ranges === undefined) {
483483
return undefined;
@@ -487,7 +487,7 @@ export class JsonStore extends Database {
487487
}
488488
};
489489

490-
const _findTargets = (result: lsp.Location[], dedupLocations: Set<string>, dedupMonikers: Set<string>, range: Range): void => {
490+
const _findTargets = (result: types.Location[], dedupLocations: Set<string>, dedupMonikers: Set<string>, range: Range): void => {
491491
const resultPath = this.getResultPath(range.id, edges);
492492
if (resultPath.result === undefined) {
493493
return undefined;
@@ -520,7 +520,7 @@ export class JsonStore extends Database {
520520
}
521521
};
522522

523-
const result: lsp.Location[] = [];
523+
const result: types.Location[] = [];
524524
const dedupLocations: Set<string> = new Set();
525525
const dedupMonikers: Set<string> = new Set();
526526
for (const range of ranges) {
@@ -529,13 +529,13 @@ export class JsonStore extends Database {
529529
return result;
530530
}
531531

532-
public references(uri: string, position: lsp.Position, context: lsp.ReferenceContext): lsp.Location[] | undefined {
532+
public references(uri: string, position: types.Position, context: types.ReferenceContext): types.Location[] | undefined {
533533
const ranges = this.findRangesFromPosition(this.toDatabase(uri), position);
534534
if (ranges === undefined) {
535535
return undefined;
536536
}
537537

538-
const findReferences = (result: lsp.Location[], dedupLocations: Set<string>, dedupMonikers: Set<string>, range: Range): void => {
538+
const findReferences = (result: types.Location[], dedupLocations: Set<string>, dedupMonikers: Set<string>, range: Range): void => {
539539
const resultPath = this.getResultPath(range.id, this.out.references);
540540
if (resultPath.result === undefined) {
541541
return;
@@ -569,7 +569,7 @@ export class JsonStore extends Database {
569569
}
570570
};
571571

572-
const result: lsp.Location[] = [];
572+
const result: types.Location[] = [];
573573
const dedupLocations: Set<string> = new Set();
574574
const dedupMonikers: Set<string> = new Set();
575575
for (const range of ranges) {
@@ -614,7 +614,7 @@ export class JsonStore extends Database {
614614
return this.in.moniker.get(moniker.id);
615615
}
616616

617-
private resolveReferenceResult(locations: lsp.Location[], dedupLocations: Set<string>, monikers: Moniker[], referenceResult: ReferenceResult, context: lsp.ReferenceContext): void {
617+
private resolveReferenceResult(locations: types.Location[], dedupLocations: Set<string>, monikers: Moniker[], referenceResult: ReferenceResult, context: types.ReferenceContext): void {
618618
const targets = this.item(referenceResult);
619619
if (targets === undefined) {
620620
return undefined;
@@ -648,13 +648,13 @@ export class JsonStore extends Database {
648648
}
649649
}
650650

651-
private addLocation(result: lsp.Location[], value: Range | lsp.Location, dedup: Set<string>): void {
652-
let location: lsp.Location;
653-
if (lsp.Location.is(value)) {
651+
private addLocation(result: types.Location[], value: Range | types.Location, dedup: Set<string>): void {
652+
let location: types.Location;
653+
if (types.Location.is(value)) {
654654
location = value;
655655
} else {
656656
const document = this.in.contains.get(value.id)!;
657-
location = lsp.Location.create(this.fromDatabase((document as Document).uri), this.asRange(value));
657+
location = types.Location.create(this.fromDatabase((document as Document).uri), this.asRange(value));
658658
}
659659
const key = Locations.makeKey(location);
660660
if (!dedup.has(key)) {
@@ -663,7 +663,7 @@ export class JsonStore extends Database {
663663
}
664664
}
665665

666-
private findRangesFromPosition(file: string, position: lsp.Position): Range[] | undefined {
666+
private findRangesFromPosition(file: string, position: types.Position): Range[] | undefined {
667667
const value = this.indices.documents.get(file);
668668
if (value === undefined) {
669669
return undefined;
@@ -698,7 +698,7 @@ export class JsonStore extends Database {
698698
return result.length > 0 ? result : undefined;
699699
}
700700

701-
private static containsPosition(range: lsp.Range, position: lsp.Position): boolean {
701+
private static containsPosition(range: types.Range, position: types.Position): boolean {
702702
if (position.line < range.start.line || position.line > range.end.line) {
703703
return false;
704704
}
@@ -714,7 +714,7 @@ export class JsonStore extends Database {
714714
/**
715715
* Test if `otherRange` is in `range`. If the ranges are equal, will return true.
716716
*/
717-
public static containsRange(range: lsp.Range, otherRange: lsp.Range): boolean {
717+
public static containsRange(range: types.Range, otherRange: types.Range): boolean {
718718
if (otherRange.start.line < range.start.line || otherRange.end.line < range.start.line) {
719719
return false;
720720
}

protocol/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111
"bugs": {
1212
"url": "https://github.com/Microsoft/lsif-node/issues"
1313
},
14-
"main": "lib/main.js",
15-
"typings": "lib/main.d.ts",
14+
"engines": {
15+
"node": ">=20.9.0"
16+
},
17+
"exports": {
18+
".": {
19+
"types": "./lib/main.d.ts",
20+
"default": "./lib/main.js"
21+
}
22+
},
1623
"dependencies": {
1724
"vscode-languageserver-types": "^3.17.5"
1825
},

tsconfig.base.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"compilerOptions": {
33
"module": "node16",
4-
"target": "es2020",
4+
"moduleResolution": "Node16",
5+
"target": "es2022",
56
"noImplicitAny": true,
67
"noImplicitReturns": true,
78
"noUnusedLocals": true,
@@ -10,7 +11,7 @@
1011
"declarationMap": true,
1112
"strict": true,
1213
"lib": [
13-
"es2020"
14+
"es2022"
1415
]
1516
}
1617
}

0 commit comments

Comments
 (0)