Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/node/parserService.ts
13401 views
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 type * as vscode from 'vscode';
7
import { createServiceIdentifier } from '../../../util/common/services';
8
import { Range } from '../../../vscodeTypes';
9
import { TextDocumentSnapshot } from '../../editing/common/textDocumentSnapshot';
10
import { BlockNameDetail, DetailBlock, QueryMatchTree } from './chunkGroupTypes';
11
import { OverlayNode, TreeSitterExpressionInfo, TreeSitterOffsetRange, TreeSitterPointRange } from './nodes';
12
import type * as parser from './parserImpl';
13
import { TestableNode } from './testGenParsing';
14
import { WASMLanguage } from './treeSitterLanguages';
15
16
export const IParserService = createServiceIdentifier<IParserService>('IParserService');
17
18
export interface TreeSitterAST {
19
/**
20
* Get the positions of all function bodies nodes in the given piece of source code.
21
*/
22
getFunctionBodies(): Promise<TreeSitterOffsetRange[]>;
23
/**
24
* Get the position of the parent scope in the given piece of source code.
25
*/
26
getCoarseParentScope(range: TreeSitterPointRange): Promise<TreeSitterPointRange>;
27
/**
28
* Find the selection of interest for the /fix command
29
*/
30
getFixSelectionOfInterest(range: TreeSitterPointRange, maxNumberOfLines: number): Promise<TreeSitterPointRange>;
31
/**
32
* Get call expression info for all function calls in the given piece of source code.
33
*/
34
getCallExpressions(selection: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;
35
/**
36
* Get function definition info for all function definitions in the given piece of source code.
37
*/
38
getFunctionDefinitions(): Promise<TreeSitterExpressionInfo[]>;
39
/**
40
* Get the positions of all class references in the given piece of source code.
41
*/
42
getClassReferences(selection: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;
43
/**
44
* Get class declaration info for all class declarations in the given piece of source code.
45
*/
46
getClassDeclarations(): Promise<TreeSitterExpressionInfo[]>;
47
/**
48
* Get type declaration info for all type declarations in the given piece of source code.
49
*/
50
getTypeDeclarations(): Promise<TreeSitterExpressionInfo[]>;
51
/**
52
* Get the positions of all type references in the given piece of source code.
53
*/
54
getTypeReferences(selection: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;
55
/**
56
* Get all symbol names the appear in a given range. This includes variables, properties, and types.
57
*/
58
getSymbols(range: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;
59
/**
60
* @param range The range to document.
61
*/
62
getDocumentableNodeIfOnIdentifier(range: TreeSitterOffsetRange): Promise<{ identifier: string; nodeRange?: TreeSitterOffsetRange } | undefined>;
63
/**
64
* @param range The range to test.
65
*/
66
getTestableNode(range: TreeSitterOffsetRange): Promise<TestableNode | null>;
67
getTestableNodes(): Promise<TestableNode[] | null>;
68
/**
69
* Starting from the smallest AST node that wraps `selection` and climbs up the AST until it sees a "documentable" node.
70
* See {@link isDocumentableNode} for definition of a "documentable" node.
71
*
72
* @param range The range to document.
73
* @returns An object containing the smallest node containing the selection range, its parent node, the node to document, and the number of nodes climbed up to reach the documentable node.
74
*/
75
getNodeToDocument(range: TreeSitterOffsetRange): Promise<parser.NodeToDocumentContext>;
76
/**
77
* @param range The range to explain.
78
*/
79
getNodeToExplain(range: TreeSitterOffsetRange): Promise<parser.NodeToExplainContext | undefined>;
80
/**
81
* @param range The range of interest.
82
* @returns All enclosing fine scopes for the {@link range range of interest}.
83
*/
84
getFineScopes(range: TreeSitterOffsetRange): Promise<TreeSitterOffsetRange[] | undefined>;
85
86
getStructure(): Promise<OverlayNode | undefined>;
87
88
findLastTest(): Promise<TreeSitterOffsetRange | null>;
89
90
/**
91
* Get the number of parse errors in the given piece of source code.
92
*/
93
getParseErrorCount(): Promise<number>;
94
}
95
96
export interface IParserService {
97
98
readonly _serviceBrand: undefined;
99
100
/**
101
* @returns an AST for the given document OR `undefined` if the document language is not supported
102
*/
103
getTreeSitterAST(document: { readonly languageId: string; getText(): string }): TreeSitterAST | undefined;
104
105
/**
106
* @returns an AST parsing the source with the given language
107
*/
108
getTreeSitterASTForWASMLanguage(language: WASMLanguage, source: string): TreeSitterAST;
109
110
/**
111
* Get a `QueryMatchTree` with all of the MatchGroups that semantic chunking needs for its header.
112
*/
113
getSemanticChunkTree(language: WASMLanguage, source: string): Promise<QueryMatchTree<DetailBlock>>;
114
/**
115
* Get a `BlockNameNode` that is the root of a tree of semantic chunk names for a source
116
*/
117
getSemanticChunkNames(language: WASMLanguage, source: string): Promise<QueryMatchTree<BlockNameDetail>>;
118
119
}
120
121
export class ParserWorkerTimeoutError extends Error {
122
constructor() {
123
super('Parser worker call timed out');
124
this.name = 'ParserWorkerTimeoutError';
125
}
126
}
127
128
export function vscodeToTreeSitterRange(range: vscode.Range): TreeSitterPointRange {
129
return {
130
startPosition: { row: range.start.line, column: range.start.character },
131
endPosition: { row: range.end.line, column: range.end.character }
132
};
133
}
134
135
export function treeSitterToVSCodeRange(range: TreeSitterPointRange): Range {
136
return new Range(
137
range.startPosition.row, range.startPosition.column,
138
range.endPosition.row, range.endPosition.column
139
);
140
}
141
142
export function vscodeToTreeSitterOffsetRange(range: Range, document: TextDocumentSnapshot): TreeSitterOffsetRange {
143
return {
144
startIndex: document.offsetAt(range.start),
145
endIndex: document.offsetAt(range.end)
146
};
147
}
148
149
export function treeSitterOffsetRangeToVSCodeRange(document: TextDocumentSnapshot, range: TreeSitterOffsetRange): vscode.Range {
150
return new Range(document.positionAt(range.startIndex), document.positionAt(range.endIndex));
151
}
152
153
export type NodeToDocumentContext = parser.NodeToDocumentContext;
154
155