【问题标题】:Monaco-Editor HoverProvider get the word mouse hovers overMonaco-Editor HoverProvider 获取鼠标悬停在上面
【发布时间】:2018-03-23 21:03:40
【问题描述】:

如何在 Monaco 编辑器中找到我悬停的单词?

我想在我的数组中保存的单词上显示一个特定的值。因此,当用户将鼠标悬停在单词上时,我想将该单词与保存在我的数组中的单词进行比较,然后显示该单词的保存值。

我知道这两种方法:

model.getValue() // gets all the text stored in the model
model.getValueInRange({startLineNumber, startColumn, endLineNumber, endColumn}) // gets the value in Range, but I don't now the start and end column.

这是我的代码,我只需要 getValueInRange 方法的帮助:

 public variableHoverProvider = <monaco.languages.HoverProvider>{
    // this is for getting the values on hover over context variables and shortcuts
    provideHover: (model, position, token) => {
        if (model.getLineContent(position.lineNumber).trim() !== '') { // if only whitespace don't do anything
            let current = this.store[this.store.length - 1]; // just the place where I store my words and there values

            console.log(model.getValueInRange({
                startLineNumber: position.lineNumber,
                startColumn: 1, // this is the information I am missing
                endLineNumber: position.lineNumber,
                endColumn: 5  // this is the information I am missing
            }));

             // TODO: I have to find somehow the word the mouse is hovering over
            // let getMatchingContextVariableValue = current.contextVariables.map(ctxVariable=>{
            //     if(ctxVariable)
            // });

            let test = current.contextVariables[22].value;

            return {
                contents: [
                    { value: test }
                ],
            };
        }
    }
};

有没有人知道如何获取我悬停的文本?或者getvalueInRange方法中的startColumn和endColumn如何计算?

这里是Monaco-Editor HoverProvider Playground

【问题讨论】:

    标签: javascript typescript monaco-editor


    【解决方案1】:

    您无需通过model.getValueInRange,只需使用model.getWordAtPosition即可。

    方便地使用modelposition 调用HoverProvider,这样没问题。

    提供一个可以由摩纳哥操场执行的最小示例:

    monaco.languages.register({ id: 'mySpecialLanguage' });
    
    monaco.languages.registerHoverProvider('mySpecialLanguage', {
        provideHover: function(model, position) { 
            // Log the current word in the console, you probably want to do something else here.
            console.log(model.getWordAtPosition(position));
        }
    });
    
    monaco.editor.create(document.getElementById("container"), {
        value: '\n\nHover over this text',
        language: 'mySpecialLanguage'
    });
    

    请注意,这会返回一个具有三个属性的 IWordAtPosition 对象:

    endColumn:数字

    单词结束的列。

    startColumn: 编号

    单词开始的列。

    单词:字符串

    这个词。

    所以要在悬停位置获取单词作为字符串,您需要访问model.getWordAtPosition(position).word

    【讨论】:

    • 我一直在寻找这个。我知道有一种简单的方法,因为我可以看到 monaco 编辑器正确放置了弹出窗口。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2018-09-03
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多