我怎样才能得到我在摩纳哥编辑中徘徊的字眼?
我想在我的数组中保存的单词上显示特定值。因此,当用户将鼠标悬停在单词上时,我想将该单词与我的数组中保存的单词进行比较,然后显示该单词的已保存值。
我知道这两种方法:
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?
答案 0 :(得分:2)
您不需要浏览model.getValueInRange
,只需使用model.getWordAtPosition
即可。
通过model
和position
方便地调用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:number
单词结束的列。
startColumn:数字
单词开头的列。
字:字符串
这个词。
因此,要将单词作为字符串放在悬停位置,您需要访问model.getWordAtPosition(position).word
。