Path: blob/main/extensions/copilot/src/platform/parser/node/parserService.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type * as vscode from 'vscode';6import { createServiceIdentifier } from '../../../util/common/services';7import { Range } from '../../../vscodeTypes';8import { TextDocumentSnapshot } from '../../editing/common/textDocumentSnapshot';9import { BlockNameDetail, DetailBlock, QueryMatchTree } from './chunkGroupTypes';10import { OverlayNode, TreeSitterExpressionInfo, TreeSitterOffsetRange, TreeSitterPointRange } from './nodes';11import type * as parser from './parserImpl';12import { TestableNode } from './testGenParsing';13import { WASMLanguage } from './treeSitterLanguages';1415export const IParserService = createServiceIdentifier<IParserService>('IParserService');1617export interface TreeSitterAST {18/**19* Get the positions of all function bodies nodes in the given piece of source code.20*/21getFunctionBodies(): Promise<TreeSitterOffsetRange[]>;22/**23* Get the position of the parent scope in the given piece of source code.24*/25getCoarseParentScope(range: TreeSitterPointRange): Promise<TreeSitterPointRange>;26/**27* Find the selection of interest for the /fix command28*/29getFixSelectionOfInterest(range: TreeSitterPointRange, maxNumberOfLines: number): Promise<TreeSitterPointRange>;30/**31* Get call expression info for all function calls in the given piece of source code.32*/33getCallExpressions(selection: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;34/**35* Get function definition info for all function definitions in the given piece of source code.36*/37getFunctionDefinitions(): Promise<TreeSitterExpressionInfo[]>;38/**39* Get the positions of all class references in the given piece of source code.40*/41getClassReferences(selection: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;42/**43* Get class declaration info for all class declarations in the given piece of source code.44*/45getClassDeclarations(): Promise<TreeSitterExpressionInfo[]>;46/**47* Get type declaration info for all type declarations in the given piece of source code.48*/49getTypeDeclarations(): Promise<TreeSitterExpressionInfo[]>;50/**51* Get the positions of all type references in the given piece of source code.52*/53getTypeReferences(selection: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;54/**55* Get all symbol names the appear in a given range. This includes variables, properties, and types.56*/57getSymbols(range: TreeSitterOffsetRange): Promise<TreeSitterExpressionInfo[]>;58/**59* @param range The range to document.60*/61getDocumentableNodeIfOnIdentifier(range: TreeSitterOffsetRange): Promise<{ identifier: string; nodeRange?: TreeSitterOffsetRange } | undefined>;62/**63* @param range The range to test.64*/65getTestableNode(range: TreeSitterOffsetRange): Promise<TestableNode | null>;66getTestableNodes(): Promise<TestableNode[] | null>;67/**68* Starting from the smallest AST node that wraps `selection` and climbs up the AST until it sees a "documentable" node.69* See {@link isDocumentableNode} for definition of a "documentable" node.70*71* @param range The range to document.72* @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.73*/74getNodeToDocument(range: TreeSitterOffsetRange): Promise<parser.NodeToDocumentContext>;75/**76* @param range The range to explain.77*/78getNodeToExplain(range: TreeSitterOffsetRange): Promise<parser.NodeToExplainContext | undefined>;79/**80* @param range The range of interest.81* @returns All enclosing fine scopes for the {@link range range of interest}.82*/83getFineScopes(range: TreeSitterOffsetRange): Promise<TreeSitterOffsetRange[] | undefined>;8485getStructure(): Promise<OverlayNode | undefined>;8687findLastTest(): Promise<TreeSitterOffsetRange | null>;8889/**90* Get the number of parse errors in the given piece of source code.91*/92getParseErrorCount(): Promise<number>;93}9495export interface IParserService {9697readonly _serviceBrand: undefined;9899/**100* @returns an AST for the given document OR `undefined` if the document language is not supported101*/102getTreeSitterAST(document: { readonly languageId: string; getText(): string }): TreeSitterAST | undefined;103104/**105* @returns an AST parsing the source with the given language106*/107getTreeSitterASTForWASMLanguage(language: WASMLanguage, source: string): TreeSitterAST;108109/**110* Get a `QueryMatchTree` with all of the MatchGroups that semantic chunking needs for its header.111*/112getSemanticChunkTree(language: WASMLanguage, source: string): Promise<QueryMatchTree<DetailBlock>>;113/**114* Get a `BlockNameNode` that is the root of a tree of semantic chunk names for a source115*/116getSemanticChunkNames(language: WASMLanguage, source: string): Promise<QueryMatchTree<BlockNameDetail>>;117118}119120export class ParserWorkerTimeoutError extends Error {121constructor() {122super('Parser worker call timed out');123this.name = 'ParserWorkerTimeoutError';124}125}126127export function vscodeToTreeSitterRange(range: vscode.Range): TreeSitterPointRange {128return {129startPosition: { row: range.start.line, column: range.start.character },130endPosition: { row: range.end.line, column: range.end.character }131};132}133134export function treeSitterToVSCodeRange(range: TreeSitterPointRange): Range {135return new Range(136range.startPosition.row, range.startPosition.column,137range.endPosition.row, range.endPosition.column138);139}140141export function vscodeToTreeSitterOffsetRange(range: Range, document: TextDocumentSnapshot): TreeSitterOffsetRange {142return {143startIndex: document.offsetAt(range.start),144endIndex: document.offsetAt(range.end)145};146}147148export function treeSitterOffsetRangeToVSCodeRange(document: TextDocumentSnapshot, range: TreeSitterOffsetRange): vscode.Range {149return new Range(document.positionAt(range.startIndex), document.positionAt(range.endIndex));150}151152export type NodeToDocumentContext = parser.NodeToDocumentContext;153154155