[英]Monaco-Editor HoverProvider get the word mouse hovers over
How can I get the word I am hovering over in Monaco editor ? 我如何在摩纳哥编辑器中得到我要悬停的单词?
I want to show a specific value over a word that is saved in my array. 我想显示保存在数组中的单词的特定值。 So when the user hovers over the word I want to compare that word with the words saved in my array and then show the saved value of that word.
因此,当用户将鼠标悬停在该单词上时,我想将该单词与数组中保存的单词进行比较,然后显示该单词的保存值。
I know these two methods: 我知道这两种方法:
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.
Here is my code, I just need the help with the method getValueInRange: 这是我的代码,我只需要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 }
],
};
}
}
};
Has anyone maybe a good Idea how I can get the text I am hovering over ? 有没有人可能是个好主意,我该如何获得悬停的文字? Or how to calculate the startColumn and endColumn in getvalueInRange method ?
或者如何在getvalueInRange方法中计算startColumn和endColumn?
Here is the Monaco-Editor HoverProvider Playground 这是摩纳哥编辑器HoverProvider游乐场
You don't need to go through model.getValueInRange
you can simply use model.getWordAtPosition
. 您无需遍历
model.getValueInRange
,只需使用model.getWordAtPosition
。
Conveniently the HoverProvider
is called with model
and position
so that's no problem. 方便地,使用
model
和position
调用HoverProvider
,所以没有问题。
To provide a minimal example that can be executed by the 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'
});
Note that this returns a IWordAtPosition
object which has three properties: 请注意,这将返回一个
IWordAtPosition
对象,该对象具有三个属性:
endColumn: number
endColumn:数字
The column where the word ends.
单词结尾的列。
startColumn: number
startColumn:数字
The column where the word starts.
单词开始的列。
word: string
字:字符串
The word.
这个单词。
So to get the word as string at the hovered position, you need to access model.getWordAtPosition(position).word
. 因此,要在悬停位置获得单词作为字符串,您需要访问
model.getWordAtPosition(position).word
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.