Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/node/util.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 { SyntaxNode } from 'web-tree-sitter';
7
import { WASMLanguage } from './treeSitterLanguages';
8
9
/**
10
* Extracts the identifier for the given node (is based on heuristics, so not 100% accurate for all languages)
11
*/
12
export function extractIdentifier(node: SyntaxNode, languageId: string): string | undefined {
13
switch (languageId) {
14
case 'python':
15
case 'csharp':
16
return node.children.find(c => c.type.match(/identifier/))?.text;
17
case 'go': {
18
const identifierChild = node.children.find(c => c.type.match(/identifier/));
19
if (identifierChild) { return identifierChild.text; }
20
const specChild = node.children.find(c => c.type.match(/spec/));
21
return specChild?.children.find(c => c.type.match(/identifier/))?.text;
22
}
23
case 'javascript':
24
case 'javascriptreact':
25
case 'typescript':
26
case 'typescriptreact':
27
case 'cpp': {
28
const declarator = node.children.find(c => c.type.match(/declarator/));
29
if (declarator) { return declarator.children.find(c => c.type.match(/identifier/))?.text; }
30
const identifierChild = node.children.find(c => c.type.match(/identifier/));
31
return identifierChild?.text;
32
}
33
case 'java': {
34
/*
35
handles
36
```java
37
// class identifier
38
class Fo<<>>o { }
39
40
class Foo {
41
// method identifier
42
public void ba<<>>r() { }
43
}
44
45
// enum identifier
46
enum F<<>>oo {
47
// enum constant identifier
48
BA<<>>Z
49
}
50
51
// interface identifier
52
interface Fo<<>>o { }
53
```
54
*/
55
const identifierChild = node.children.find(c => c.type === 'identifier');
56
return identifierChild?.text;
57
}
58
case 'ruby':
59
return node.children.find(c => c.type.match(/constant|identifier/))?.text;
60
default:
61
return node.children.find(c => c.type.match(/identifier/))?.text;
62
}
63
}
64
65
66
export function isDocumentableNode(node: SyntaxNode, language: WASMLanguage) {
67
switch (language) {
68
case WASMLanguage.TypeScript:
69
case WASMLanguage.TypeScriptTsx:
70
case WASMLanguage.JavaScript:
71
return node.type.match(/definition|declaration|declarator|export_statement/);
72
case WASMLanguage.Go:
73
return node.type.match(/definition|declaration|declarator|var_spec/);
74
case WASMLanguage.Cpp:
75
return node.type.match(/definition|declaration|class_specifier/);
76
case WASMLanguage.Ruby:
77
return node.type.match(/module|class|method|assignment/);
78
default:
79
return node.type.match(/definition|declaration|declarator/);
80
}
81
}
82
83