繁体   English   中英

摩纳哥编辑器HoverProvider将鼠标悬停一词

[英]Monaco-Editor HoverProvider get the word mouse hovers over

我如何在摩纳哥编辑器中得到我要悬停的单词?

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

我知道这两种方法:

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?

这是摩纳哥编辑器HoverProvider游乐场

您无需遍历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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM