常用的vscode开发与配置
1. vscode字体设置
let firstLetterDecoration = vscode.window.createTextEditorDecorationType({color: "#ff0000"});
let range = new vscode.Range(insertPosition, endInsertPosition);
editor.setDecorations(firstLetterDecoration, [range]);
2. 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(){
}
}
3. 获取当前编辑文件信息
//文件名
vscode.window.activeTextEditor.document.fileName
//扩展名
vscode.window.activeTextEditor.document.languageId
//uri
vscode.window.activeTextEditor.document.uri
4. 获取当前的workspace根目录
vscode.workspace.rootPath
5. 显示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);
6. 显示提示信息
显示提示信息,包括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
7. 获取配置信息
let settings = vscode.WorkspaceConfiguration
//let settings = vscode.workspace.getConfiguration('cpplint');
let cpplintPath = settings.get('cpplintPath', null);
8. text文档event监听与处理
主要包括onDidOpenTextDocument,onDidCloseTextDocument,onDidChangeTextDocument,onWillSaveTextDocument,onDidSaveTextDocument
//文件打开
vscode.workspace.onDidOpenTextDocument((() => doLint()).bind(this));
//文件保存
vscode.workspace.onDidSaveTextDocument((() => doLint()).bind(this));
9. 文档打开以后侧边提示信息
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);
});