Monaco-Editor HoverProvider 让鼠标悬停在这个词上

Dev*_*vid 3 javascript typescript monaco-editor

我怎样才能得到我在摩纳哥编辑器中悬停的词?

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

我知道这两种方法:

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.
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我只需要 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 }
                ],
            };
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

有没有人可能有一个好主意,我怎样才能得到我悬停在上面的文本?或者如何在 getvalueInRange 方法中计算 startColumn 和 endColumn ?

这是Monaco-Editor HoverProvider Playground

MSe*_*ert 5

你不需要通过model.getValueInRange你可以简单地使用model.getWordAtPosition.

方便地HoverProvider调用 with modelposition所以没问题。

提供一个可以由 monaco playground 执行的最小示例:

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'
});
Run Code Online (Sandbox Code Playgroud)

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

结束列:数字

单词结束的列。

开始列:数字

单词开始的列。

词:字符串

这个单词。

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