1. 装饰器使用样例
import * as vscode from 'vscode';
function get_selection_postition(editor: vscode.TextEditor) {
let selection = editor?.selection;
let selection_start = selection?.start;
let selection_end = selection?.end;
return [selection_start, selection_end]
}
function hight_light(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand("CustomPlugins:HightLight", () => {
let decorationType = vscode.window.createTextEditorDecorationType({
backgroundColor: '#fff'
});
let editor = vscode.window.activeTextEditor;
let [selection_start, selection_end] = get_selection_postition(editor)
editor?.setDecorations(decorationType, [new vscode.Range(selection_start, selection_end)])
})
context.subscriptions.push(disposable)
}
exports.activate = hight_light;
function deactivate() {
}
exports.deactivate = deactivate;
- 作用:将文档中被使用光标选中的部分使用
#fff
进行高亮标记
2. 内容详解
- 获取光标选中位置,
let [selection_start, selection_end] = get_selection_postition(editor)
- 创建
vscode.window.createTextEditorDecoration
对象,添加属性 backgroundColor
- 通过装饰器在指定的代码片段Range上将背景色进行高亮
3. 可用参数说明
export interface DecorationRenderOptions extends ThemableDecorationRenderOptions {
isWholeLine?: boolean;
rangeBehavior?: DecorationRangeBehavior;
overviewRulerLane?: OverviewRulerLane;
light?: ThemableDecorationRenderOptions;
dark?: ThemableDecorationRenderOptions;
};
export interface ThemableDecorationRenderOptions {
backgroundColor?: string | ThemeColor;
outline?: string;
outlineColor?: string | ThemeColor;
outlineStyle?: string;
outlineWidth?: string;
border?: string;
borderColor?: string | ThemeColor;
borderRadius?: string;
borderSpacing?: string;
borderStyle?: string;
borderWidth?: string;
fontStyle?: string;
fontWeight?: string;
textDecoration?: string;
cursor?: string;
color?: string | ThemeColor;
opacity?: string;
letterSpacing?: string;
gutterIconPath?: string | Uri;
gutterIconSize?: string;
overviewRulerColor?: string | ThemeColor;
before?: ThemableDecorationAttachmentRenderOptions;
after?: ThemableDecorationAttachmentRenderOptions;
}
1. 颜色相关(color)
backgroundColor?: string | ThemeColor;
borderColor?: string | ThemeColor;
color?: string | ThemeColor;
overviewRulerColor?: string | ThemeColor;
new vscode.ThemeColor('editorWarning.foreground');
- 注意这些参数除了string类型外,还可以直接使用ThemeColor(如上面例子),这样对应的颜色就会随着主题的变化而变化
2. 边框相关(border)
border?: string;
borderColor?: string | ThemeColor;
borderRadius?: string;
borderSpacing?: string;
borderStyle?: string;
borderWidth?: string;
3. 轮廓相关(Outline)
outline?: string;
outlineColor?: string | ThemeColor;
outlineStyle?: string;
outlineWidth?: string;
4. 字体相关(Font)
fontStyle?: string;
fontWeight?: string;
opacity?: string;
letterSpacing?: string;