1. 装饰器使用样例

  1. import * as vscode from 'vscode';
  2. function get_selection_postition(editor: vscode.TextEditor) {
  3. let selection = editor?.selection;
  4. let selection_start = selection?.start;
  5. let selection_end = selection?.end;
  6. return [selection_start, selection_end]
  7. }
  8. function hight_light(context: vscode.ExtensionContext) {
  9. let disposable = vscode.commands.registerCommand("CustomPlugins:HightLight", () => {
  10. let decorationType = vscode.window.createTextEditorDecorationType({
  11. backgroundColor: '#fff'
  12. });
  13. let editor = vscode.window.activeTextEditor;
  14. let [selection_start, selection_end] = get_selection_postition(editor)
  15. editor?.setDecorations(decorationType, [new vscode.Range(selection_start, selection_end)])
  16. })
  17. context.subscriptions.push(disposable)
  18. }
  19. exports.activate = hight_light;
  20. function deactivate() {
  21. }
  22. exports.deactivate = deactivate;
  • 作用:将文档中被使用光标选中的部分使用 #fff 进行高亮标记

2. 内容详解

  1. 获取光标选中位置, let [selection_start, selection_end] = get_selection_postition(editor)
  2. 创建 vscode.window.createTextEditorDecoration 对象,添加属性 backgroundColor
  3. 通过装饰器在指定的代码片段Range上将背景色进行高亮

3. 可用参数说明

  1. export interface DecorationRenderOptions extends ThemableDecorationRenderOptions {
  2. isWholeLine?: boolean;
  3. rangeBehavior?: DecorationRangeBehavior;
  4. overviewRulerLane?: OverviewRulerLane;
  5. light?: ThemableDecorationRenderOptions;
  6. dark?: ThemableDecorationRenderOptions;
  7. };
  8. export interface ThemableDecorationRenderOptions {
  9. backgroundColor?: string | ThemeColor;
  10. outline?: string;
  11. outlineColor?: string | ThemeColor;
  12. outlineStyle?: string;
  13. outlineWidth?: string;
  14. border?: string;
  15. borderColor?: string | ThemeColor;
  16. borderRadius?: string;
  17. borderSpacing?: string;
  18. borderStyle?: string;
  19. borderWidth?: string;
  20. fontStyle?: string;
  21. fontWeight?: string;
  22. textDecoration?: string;
  23. cursor?: string;
  24. color?: string | ThemeColor;
  25. opacity?: string;
  26. letterSpacing?: string;
  27. gutterIconPath?: string | Uri;
  28. gutterIconSize?: string;
  29. overviewRulerColor?: string | ThemeColor;
  30. before?: ThemableDecorationAttachmentRenderOptions;
  31. after?: ThemableDecorationAttachmentRenderOptions;
  32. }

1. 颜色相关(color)

  1. backgroundColor?: string | ThemeColor;
  2. borderColor?: string | ThemeColor;
  3. color?: string | ThemeColor;
  4. overviewRulerColor?: string | ThemeColor;
  5. new vscode.ThemeColor('editorWarning.foreground');
  • 注意这些参数除了string类型外,还可以直接使用ThemeColor(如上面例子),这样对应的颜色就会随着主题的变化而变化

    2. 边框相关(border)

    1. border?: string;
    2. borderColor?: string | ThemeColor;
    3. borderRadius?: string;
    4. borderSpacing?: string;
    5. borderStyle?: string;
    6. borderWidth?: string;

    3. 轮廓相关(Outline)

    1. outline?: string;
    2. outlineColor?: string | ThemeColor;
    3. outlineStyle?: string;
    4. outlineWidth?: string;

4. 字体相关(Font)

  1. fontStyle?: string;
  2. fontWeight?: string;
  3. opacity?: string;
  4. letterSpacing?: string;
  • opacity:控制字体的透明度
  • letterSpacing:控制文字之间的间距

    5. 其他

    1. gutterIconPath?: string | Uri;
    2. gutterIconSize?: string;
  • 可以通过这两个属性在行号旁边添加图标

  • 类似 VSCode中的断点,就可以通过这个属性在插件中实现