-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcompletion-core.ts
More file actions
163 lines (133 loc) · 4.61 KB
/
completion-core.ts
File metadata and controls
163 lines (133 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import type { DataTypeInfo, DecoratorInfo } from './intellisense-catalog';
import { getDecoratorCommentText } from './diagnostics-core';
type LineDocument = {
lineCount: number;
lineAt(line: number): { text: string };
};
const CONFIG_ITEM_PATTERN = /^\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_.-]*\s*=/;
const DIVIDER_PATTERN = /^\s*#\s*(?:---+|===+)(?:\s|$)/;
const DECORATOR_PATTERN = /@([A-Za-z][\w-]*)/g;
const INCOMPATIBLE_DECORATORS = new Map<string, Set<string>>([
['required', new Set(['optional'])],
['optional', new Set(['required'])],
['sensitive', new Set(['public'])],
['public', new Set(['sensitive'])],
]);
/** Thin wrapper kept for API compatibility with existing callers and tests. */
export function getDecoratorCommentPrefix(lineText: string) {
return getDecoratorCommentText(lineText);
}
function splitArgs(input: string) {
const parts: Array<string> = [];
let current = '';
let quote: '"' | '\'' | '' = '';
let depth = 0;
for (const char of input) {
if (quote) {
current += char;
if (char === quote) quote = '';
continue;
}
if (char === '"' || char === '\'') {
quote = char;
current += char;
continue;
}
if (char === '(') {
depth += 1;
current += char;
continue;
}
if (char === ')') {
depth = Math.max(depth - 1, 0);
current += char;
continue;
}
if (char === ',' && depth === 0) {
const value = current.trim();
if (value) parts.push(value);
current = '';
continue;
}
current += char;
}
const value = current.trim();
if (value) parts.push(value);
return parts;
}
export function isInHeader(document: LineDocument, lineNumber: number) {
for (let line = lineNumber + 1; line < document.lineCount; line += 1) {
const text = document.lineAt(line).text.trim();
if (!text) break;
if (DIVIDER_PATTERN.test(text)) break;
if (CONFIG_ITEM_PATTERN.test(text)) return false;
if (!text.startsWith('#')) break;
}
for (let line = 0; line < lineNumber; line += 1) {
if (CONFIG_ITEM_PATTERN.test(document.lineAt(line).text)) return false;
}
return true;
}
export function getExistingDecoratorNames(
document: LineDocument,
lineNumber: number,
commentPrefix: string,
) {
const names = new Set<string>();
if (isInHeader(document, lineNumber)) {
for (let line = 0; line < lineNumber; line += 1) {
const text = document.lineAt(line).text.trim();
if (CONFIG_ITEM_PATTERN.test(text)) break;
if (!text.startsWith('#')) continue;
const decoratorCommentPrefix = getDecoratorCommentPrefix(text);
if (!decoratorCommentPrefix) continue;
for (const match of decoratorCommentPrefix.matchAll(DECORATOR_PATTERN)) {
names.add(match[1]);
}
}
} else {
for (let line = lineNumber - 1; line >= 0; line -= 1) {
const text = document.lineAt(line).text.trim();
if (!text.startsWith('#')) break;
const decoratorCommentPrefix = getDecoratorCommentPrefix(text);
if (!decoratorCommentPrefix) continue;
for (const match of decoratorCommentPrefix.matchAll(DECORATOR_PATTERN)) {
names.add(match[1]);
}
}
}
for (const match of commentPrefix.matchAll(DECORATOR_PATTERN)) {
names.add(match[1]);
}
return names;
}
export function filterAvailableDecorators(
decorators: Array<DecoratorInfo>,
existingDecoratorNames: Set<string>,
) {
return decorators.filter((decorator) => {
if (!decorator.isFunction && existingDecoratorNames.has(decorator.name)) return false;
const incompatible = INCOMPATIBLE_DECORATORS.get(decorator.name);
if (!incompatible) return true;
return ![...incompatible].some((name) => existingDecoratorNames.has(name));
});
}
export function splitEnumArgs(input: string) {
return splitArgs(input).map((value) => value.replace(/^['"]|['"]$/g, '').trim()).filter(Boolean);
}
export function getEnumValuesFromPrecedingComments(document: LineDocument, lineNumber: number) {
for (let line = lineNumber - 1; line >= 0; line -= 1) {
const text = document.lineAt(line).text.trim();
if (!text.startsWith('#')) break;
const decoratorCommentPrefix = getDecoratorCommentPrefix(text);
if (!decoratorCommentPrefix) continue;
const match = decoratorCommentPrefix.match(/@type=enum\((.*)\)/);
if (match) return splitEnumArgs(match[1]);
}
return undefined;
}
export function getTypeOptionDataType(dataTypes: Array<DataTypeInfo>, commentPrefix: string) {
const match = commentPrefix.match(/(^|\s)@type=([A-Za-z][\w-]*)\([^#)]*$/);
if (!match) return undefined;
return dataTypes.find((dataType) => dataType.name === match[2]);
}