常用的vscode開發(fā)與配置
1. vscode字體設(shè)置
let firstLetterDecoration = vscode.window.createTextEditorDecorationType({color: "#ff0000"});
let range = new vscode.Range(insertPosition, endInsertPosition);
editor.setDecorations(firstLetterDecoration, [range]);
2. CompletionItemProvider 輸入文件的提示內(nèi)容
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. 獲取當(dāng)前編輯文件信息
//文件名
vscode.window.activeTextEditor.document.fileName
//擴(kuò)展名
vscode.window.activeTextEditor.document.languageId
//uri
vscode.window.activeTextEditor.document.uri
4. 獲取當(dāng)前的workspace根目錄
vscode.workspace.rootPath
5. 顯示output輸出框
let outputChannel: vscode.OutputChannel;
//創(chuàng)建output
outputChannel = vscode.window.createOutputChannel('outputname');
//打開并顯示output
outputChannel.show();
//清空output內(nèi)容
outputChannel.clear();
//寫入內(nèi)容到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
詳細(xì)可以參考: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監(jiān)聽與處理
主要包括onDidOpenTextDocument,onDidCloseTextDocument侨舆,onDidChangeTextDocument秒紧,onWillSaveTextDocument,onDidSaveTextDocument
//文件打開
vscode.workspace.onDidOpenTextDocument((() => doLint()).bind(this));
//文件保存
vscode.workspace.onDidSaveTextDocument((() => doLint()).bind(this));
9. 文檔打開以后側(cè)邊提示信息
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);
});