【问题标题】:VS Code extension how to edit in context?VS Code 扩展如何在上下文中编辑?
【发布时间】:2018-05-16 07:39:09
【问题描述】:

她是我使用的类自动大写true,false,...

export class StUpdater {
    private _lines: number;
    private _strings: Array<string>;

    constructor() {
        this._lines = 0;
        this._strings = ['true', 'false', 'exit', 'continue', 'return'];
    }

    Update(Cntx: boolean = false) {
        let editor = window.activeTextEditor;
        if (!editor || (editor.document.languageId != 'st')) {
            window.showErrorMessage('No editor!')
            return;
        }

        let doc = editor.document;

        if (Cntx == false) {
            if (this._lines >= doc.lineCount) {
                this._lines = doc.lineCount;
                return;
            }
            this._lines = doc.lineCount;

            let AutoFormat = workspace.getConfiguration('st').get('autoFormat');

            if (!AutoFormat) {
                return;
            }
        }


        let edit = new WorkspaceEdit();

        for (let line = 0; line < doc.lineCount; line++) {
            const element = doc.lineAt(line);
            for (let i = 0; i < this._strings.length; i++) {
                let str = this._strings[i];
                let last_char = 0;
                while (element.text.indexOf(str, last_char) >= 0) {
                    let char = element.text.indexOf(str, last_char);
                    last_char = char + str.length;
                    edit.replace(
                        doc.uri,
                        new Range(
                            new Position(line, char),
                            new Position(line, last_char)
                        ),
                        str.toUpperCase()
                    );
                }
            }
        }

        return workspace.applyEdit(edit);
    }

    public dispose() {

    }
}

这段代码运行良好,但我不想在字符串或注释中替换它。我怎么做?我找不到替换的 preg 版本,即使我找到了,在一行中我不知道它是否是注释,如果它是多行注释。

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    如果我理解正确,您只想大写某些元素(可能是标识符),而不是 cmets 或字符串中的单词,对吗?这需要识别文本中的词汇元素,这是一系列字母到词汇类型的映射。这通常由a lexer 完成。在当前处理的基础上遍历字符并找到可以操作的范围,手写一个并不难。

    【讨论】:

    • 这几乎不能回答我的问题。我有我现在拥有的确切例子。如果我在哪里将文档解析为文本并使用标准 JS 函数,我认为我不会有问题。我想要的是使用 VS Code API 或 Formatter API 来实现。
    • 您在问题中没有提到这一点。那我想你没有解决办法。 AFAIK 没有 API 可以被扩展程序用来获取文本的词汇信息。
    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 2022-11-30
    • 2017-04-18
    • 2020-04-13
    • 2018-04-10
    • 2022-01-19
    • 1970-01-01
    • 2020-04-04
    相关资源
    最近更新 更多