Skip to content

Commit 163c34e

Browse files
committed
Repair container prompt
1 parent 5972886 commit 163c34e

File tree

2 files changed

+63
-70
lines changed

2 files changed

+63
-70
lines changed

src/cpp/index.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,61 @@ export function initCppCompletion(monaco: typeof import('monaco-editor')) {
1616
provideCompletionItems: (model, position) => {
1717
const word = model.getWordUntilPosition(position);
1818
const range = getRange(position, word);
19-
19+
2020
const code = model.getValue();
21-
const variables = extractVariables(code, position); // 传入当前光标位置,获取局部变量和全局变量
22-
23-
const variableSuggestions: { label: string; kind: languages.CompletionItemKind; insertText: string; documentation: string; range: { startLineNumber: any; endLineNumber: any; startColumn: any; endColumn: any; }; }[] = [];
24-
variables.forEach((type, name) => {
21+
const variables = extractVariables(code, position);
22+
23+
const variableSuggestions: { label: string; kind: languages.CompletionItemKind; insertText: string; documentation: string; range: any; }[] = [];
24+
variables.forEach(({ type }, name) => {
2525
variableSuggestions.push({
26-
label: name, // 只提示变量名,而不是完整声明
26+
label: name,
2727
kind: monaco.languages.CompletionItemKind.Variable,
2828
insertText: name,
2929
documentation: `Variable of type ${type}`,
3030
range: range
3131
});
3232
});
33-
33+
3434
const lineContent = model.getLineContent(position.lineNumber);
3535
const textBeforeCursor = lineContent.substring(0, position.column - 1).trim();
3636
const lastDotIndex = textBeforeCursor.lastIndexOf('.');
37-
let functionSuggestions: ({ label: string; kind: languages.CompletionItemKind; insertText: string; insertTextRules: languages.CompletionItemInsertTextRule; documentation: string; range: any; } | { label: string; kind: languages.CompletionItemKind; insertText: string; documentation: string; range: any; insertTextRules?: undefined; })[] = [];
37+
let functionSuggestions: { label: string; kind: languages.CompletionItemKind; insertText: string; documentation: string; range: any; }[] = [];
3838
let isDotAfterVariable = false;
39-
39+
4040
if (lastDotIndex !== -1) {
4141
const varName = textBeforeCursor.substring(0, lastDotIndex).trim();
4242
if (variables.has(varName)) {
43-
const varType = variables.get(varName);
43+
const varInfo = variables.get(varName);
4444
isDotAfterVariable = true;
45-
46-
if (varType?.startsWith('vector')) {
47-
functionSuggestions = getVectorSuggestions(monaco, range);
48-
} else if (varType?.startsWith('stack')) {
49-
functionSuggestions = getStackSuggestions(monaco, range);
50-
} else if (varType?.startsWith('queue')) {
51-
functionSuggestions = getQueueSuggestions(monaco, range);
52-
} else if (varType?.startsWith('deque')) {
53-
functionSuggestions = getDequeSuggestions(monaco, range);
54-
} else if (varType?.startsWith('map')) {
55-
functionSuggestions = getMapSuggestions(monaco, range);
56-
} else if (varType?.startsWith('unordered_map')) {
57-
functionSuggestions = getUnorderedMapSuggestions(monaco, range);
45+
46+
// 根据变量类型返回相应的成员方法提示
47+
switch (varInfo.type) {
48+
case 'template':
49+
functionSuggestions = getVectorSuggestions(monaco, range);
50+
break;
51+
case 'stack':
52+
functionSuggestions = getStackSuggestions(monaco, range);
53+
break;
54+
case 'queue':
55+
functionSuggestions = getQueueSuggestions(monaco, range);
56+
break;
57+
case 'deque':
58+
functionSuggestions = getDequeSuggestions(monaco, range);
59+
break;
60+
case 'map':
61+
functionSuggestions = getMapSuggestions(monaco, range);
62+
break;
63+
case 'unordered_map':
64+
functionSuggestions = getUnorderedMapSuggestions(monaco, range);
65+
break;
66+
default:
67+
break;
5868
}
5969
}
6070
}
61-
71+
6272
if (!isDotAfterVariable) {
6373
const generalSuggestions = getGeneralSuggestions(monaco, range);
64-
6574
return {
6675
suggestions: [...generalSuggestions, ...variableSuggestions]
6776
};
@@ -72,6 +81,4 @@ export function initCppCompletion(monaco: typeof import('monaco-editor')) {
7281
}
7382
}
7483
});
75-
76-
7784
}

src/cpp/utils.ts

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,45 @@
11
export function extractVariables(code: string, position: any) {
2-
const variableRegex = /\b(?:vector|stack|queue|deque|map|unordered_map|set|unordered_set|list|forward_list|multiset|unordered_multiset|multimap|unordered_multimap|priority_queue|pair|tuple|array|int|float|double|char|bool|string)\s+(\w+)\s*(\[.*?\])?;/g;
2+
// 支持匹配模板类型(例如 vector<int>, map<int, int>)
3+
const variableRegex = /\b(?:vector|stack|queue|deque|map|unordered_map|set|unordered_set|list|forward_list|multiset|unordered_multiset|multimap|unordered_multimap|priority_queue|pair|tuple|array)\s*<[^>]+>\s+(\w+)\s*;/g;
4+
const basicTypeRegex = /\b(?:int|float|double|char|bool|string|auto)\s+(\w+)\s*;/g; // 基本类型匹配
35
const funcParamRegex = /\(([^)]*)\)/g; // 提取函数参数列表
4-
const variables = new Map<string, string>();
56

6-
// 提取全局变量
7-
let match;
8-
while ((match = variableRegex.exec(code)) !== null) {
9-
const [, varName] = match;
10-
variables.set(varName, "global");
11-
}
7+
const variables = new Map<string, { type: string, kind: string }>();
128

13-
// 提取函数形参
14-
const lines = code.split('\n');
15-
let insideFunction = false;
16-
let currentScopeStartLine = -1;
17-
18-
for (let i = 0; i < lines.length; i++) {
19-
const line = lines[i].trim();
9+
// 仅解析光标之前的代码
10+
const lines = code.split('\n').slice(0, position.lineNumber - 1);
11+
let currentLine = code.split('\n')[position.lineNumber - 1].substring(0, position.column - 1);
12+
lines.push(currentLine);
13+
const codeBeforeCursor = lines.join('\n');
2014

21-
// 进入函数体时提取函数形参
22-
if (line.match(/^\w+\s+\w+\s*\(/)) {
23-
insideFunction = true;
24-
currentScopeStartLine = i;
25-
26-
// 提取函数参数
27-
const funcMatch = funcParamRegex.exec(line);
28-
if (funcMatch) {
29-
const params = funcMatch[1].split(',');
30-
params.forEach(param => {
31-
const parts = param.trim().split(/\s+/);
32-
const paramName = parts[parts.length - 1];
33-
variables.set(paramName, "parameter");
34-
});
35-
}
36-
}
15+
// 提取模板类型变量(如 vector<int>)
16+
let match;
17+
while ((match = variableRegex.exec(codeBeforeCursor)) !== null) {
18+
const varName = match[1]; // 获取变量名
19+
variables.set(varName, { type: "template", kind: "variable" }); // 设置为模板类型
20+
}
3721

38-
// 解析函数体中的局部变量
39-
if (insideFunction) {
40-
while ((match = variableRegex.exec(line)) !== null) {
41-
const [varName] = match;
42-
variables.set(varName, "local");
43-
}
22+
// 提取基本类型变量
23+
while ((match = basicTypeRegex.exec(codeBeforeCursor)) !== null) {
24+
const varName = match[1]; // 获取变量名
25+
variables.set(varName, { type: "basic", kind: "variable" }); // 设置为基本类型
26+
}
4427

45-
// 函数体结束,退出函数作用域
46-
if (line.includes('}')) {
47-
insideFunction = false;
48-
}
49-
}
28+
// 提取函数形参
29+
const funcMatch = funcParamRegex.exec(codeBeforeCursor);
30+
if (funcMatch) {
31+
const params = funcMatch[1].split(',');
32+
params.forEach(param => {
33+
const parts = param.trim().split(/\s+/);
34+
const paramName = parts[parts.length - 1]; // 获取形参名
35+
variables.set(paramName, { type: "parameter", kind: "parameter" }); // 设置为形参
36+
});
5037
}
5138

5239
return variables;
5340
}
5441

5542

56-
5743
export function getRange(position: any, word: any) {
5844
return {
5945
startLineNumber: position.lineNumber,

0 commit comments

Comments
 (0)