VS Code 开发笔记
相对路径 require.toUrl
let webview = document.createElement('webview');
webview.preload = require.toUrl('./media/browser/index.html');
webview.src = require.toUrl('./media/browser/index.html');
vscode 字体设置
let firstLetterDecoration = vscode.window.createTextEditorDecorationType({
color: "#ff0000"
});
let range = new vscode.Range(insertPosition, endInsertPosition);
editor.setDecorations(firstLetterDecoration, [range]);
CompletionItemProvider 输入文件的提示内容
class CppHoverProvider implements vscode.CompletionItemProvider {
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.CompletionItem[]{
var completionItems:vscode.CompletionItem[] = [];
var completionItem:vscode.CompletionItem = new vscode.CompletionItem("bb");
completionItem.kind = vscode.CompletionItemKind.Snippet;
completionItem.detail = "bde";
completionItem.filterText = "bb";
completionItem.insertText = "bb";
completionItems.push(completionItem);
return completionItems;
}
resolveCompletionItem(item: vscode.CompletionItem, token: vscode.CancellationToken): any {
var completionItem1:vscode.CompletionItem = new vscode.CompletionItem("id");
completionItem1.detail = "aaa";
completionItem1.kind = vscode.CompletionItemKind.Snippet;
completionItem1.filterText = "aa";
completionItem1.insertText = "aa";
return completionItem1;
}
dispose() {
}
}
获取当前编辑文件信息
// 文件名
vscode.window.activeTextEditor.document.fileName;
// 扩展名
vscode.window.activeTextEditor.document.languageId;
// uri
vscode.window.activeTextEditor.document.uri;
获取当前的 workspace 根目录
vscode.workspace.rootPath;
显示 output 输出框
let outputChannel: vscode.OutputChannel;
// 创建output
outputChannel = vscode.window.createOutputChannel("outputname");
// 打开并显示output
outputChannel.show();
// 清空output内容
outputChannel.clear();
// 写入内容到ouput中
let result = "ouput msg";
outputChannel.appendLine(result);
显示提示信息
显示提示信息包括 showErrorMessage、showInformationMessage、showWarningMessage
vscode.window.showInformationMessage("Cpplint deactivated");
interface CommandQuickPickItem extends vscode.QuickPickItem {
command: () => Promise<void>;
}
let items: CommandQuickPickItem[] = [];
items.push({
description: "Runs the analyzer on the current file.",
label: "Analyze current file",
command: runAnalysis
});
items.push({
description: "Runs the analyzer on the entire workspace.",
label: "Analyze workspace",
command: runAnalysisAllFiles
});
items.push({
description: "Opens your web browser to the Cppcheck manual.",
label: "Read the manual",
command: readTheManual
});
vscode.window
.showQuickPick(items, { matchOnDetail: true, matchOnDescription: true })
.then(selectedItem => {
if (selectedItem && typeof selectedItem.command === "function") {
selectedItem.command();
}
});
其他显示还有 showOpenDialog,showInputBox,showSaveDialog,showTextDocument,showWorkspaceFolderPick 详细可以参考:https://code.visualstudio.com/docs/extensionAPI/vscode-api
获取配置信息
let settings = vscode.WorkspaceConfiguration;
let cpplintPath = settings.get("cpplintPath", null);
text 文档 event 监听与处理
主要包括 onDidOpenTextDocument、onDidCloseTextDocument、onDidChangeTextDocument、onWillSaveTextDocument、onDidSaveTextDocument
// 文件打开
vscode.workspace.onDidOpenTextDocument((() => doLint()).bind(this));
// 文件保存
vscode.workspace.onDidSaveTextDocument((() => doLint()).bind(this));
文档打开以后侧边提示信息
let diagnosticCollection: vscode.DiagnosticCollection = vscode.languages.createDiagnosticCollection('cppcheck');
diagnosticCollection.clear();
let severity = "";
let message = "";
let level = vscode.DiagnosticSeverity.Information;
vscode.workspace.openTextDocument(fileName).then((doc: vscode.TextDocument) => {
let diagnostics: vscode.Diagnostic[] = [];
let d = new vscode.Diagnostic(r, `(${severity}) ${message}`, <vscode.DiagnosticSeverity>level);
d.source = 'cppcheck';
diagnostics.push(d);
diagnosticCollection.set(doc.uri, diagnostics);
});