Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"workspaceContains:mcpp.toml",
"onLanguage:cpp",
"onLanguage:mcpp-build",
"onLanguage:mcpp-toml",
"onCommand:mcpp.configureClangd",
"onCommand:mcpp.refreshCompilationDatabase",
"onCommand:mcpp.checkModuleSupport",
Expand Down Expand Up @@ -146,6 +147,12 @@
"default": true,
"scope": "resource",
"description": "配置 clangd 时,是否询问关闭 Microsoft C/C++ IntelliSense。"
},
"mcpp.tomlCompletion": {
"type": "boolean",
"default": false,
"scope": "resource",
"description": "为 mcpp.toml 提供代码补全:段头、各段字段、枚举/布尔取值,以及依赖与 feature 的写法模板。默认关闭。注意:该功能尚不完善,字段表可能与最新 mcpp 不同步或存在缺陷,仅供参考。"
}
}
},
Expand Down
51 changes: 51 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
} from "./workflow";
import { classifyTaskExit, type TaskCompletion } from "./tasks";
import { MCPP_MANIFEST_GLOB, registerInProjectContext } from "./inProject";
import { computeMcppTomlCompletions } from "./mcppTomlCompletion";

const COMMAND_CONFIGURE = "mcpp.configureClangd";
const COMMAND_REFRESH = "mcpp.refreshCompilationDatabase";
Expand Down Expand Up @@ -852,6 +853,49 @@ async function autoConfigureModulesWizard(
appendOutputLine(output, `[一键配置] 一键配置完成。clangd:${resolvedClangd.path}`);
}

// mcpp.toml 的补全建议由纯函数 computeMcppTomlCompletions 计算,这里只做 vscode 类型映射。
const mcppTomlCompletionKinds = {
section: vscode.CompletionItemKind.Folder,
key: vscode.CompletionItemKind.Field,
value: vscode.CompletionItemKind.EnumMember,
template: vscode.CompletionItemKind.Snippet,
} as const;

const mcppTomlCompletionProvider: vscode.CompletionItemProvider = {
provideCompletionItems(document, position) {
// mcpp.toml 补全默认关闭(mcpp.tomlCompletion),按文档作用域读取。
if (!vscode.workspace.getConfiguration("mcpp", document.uri).get<boolean>("tomlCompletion", false)) {
return undefined;
}
const lines: string[] = [];
for (let line = 0; line <= position.line; line += 1) {
lines.push(document.lineAt(line).text);
}
const textBefore = lines[position.line].slice(0, position.character);
return computeMcppTomlCompletions(lines, position.line, position.character).map((suggestion) => {
const item = new vscode.CompletionItem(
suggestion.label,
mcppTomlCompletionKinds[suggestion.kind],
);
item.detail = suggestion.detail;
if (suggestion.documentation !== undefined) {
item.documentation = new vscode.MarkdownString(suggestion.documentation);
}
if (suggestion.insertSnippet !== undefined) {
item.insertText = new vscode.SnippetString(suggestion.insertSnippet);
}
if (suggestion.kind === "section") {
// 段头建议要覆盖已输入的 "[xxx",否则默认词范围会留下多余的 "["。
const bracket = textBefore.lastIndexOf("[");
if (bracket >= 0) {
item.range = new vscode.Range(position.line, bracket, position.line, position.character);
}
}
return item;
});
},
};

export async function activate(extensionContext: vscode.ExtensionContext): Promise<void> {
moduleStatusByProject.clear();
moduleCheckOperations.clear();
Expand Down Expand Up @@ -1028,6 +1072,13 @@ export async function activate(extensionContext: vscode.ExtensionContext): Promi
output,
status,
...cliController.register(),
vscode.languages.registerCompletionItemProvider(
{ language: "mcpp-toml" },
mcppTomlCompletionProvider,
"[",
"=",
'"',
),
vscode.commands.registerCommand(COMMAND_CONFIGURE, runGuarded(async () => {
const project = findCurrentProject();
if (project === undefined) {
Expand Down
Loading