[英]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?
您無需遍歷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:數字
單詞結尾的列。
startColumn:數字
單詞開始的列。
字:字符串
這個單詞。
因此,要在懸停位置獲得單詞作為字符串,您需要訪問model.getWordAtPosition(position).word
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.