Monaco-Editor HoverProvider 获取单词鼠标悬停
Monaco-Editor HoverProvider get the word mouse hovers over
如何在 Monaco 编辑器中找到我悬停的词?
我想显示数组中保存的单词的特定值。因此,当用户将鼠标悬停在单词上时,我想将该单词与保存在我的数组中的单词进行比较,然后显示该单词的保存值。
我知道这两种方法:
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
you can simply use model.getWordAtPosition
。
HoverProvider
可以方便地用 model
和 position
调用,所以没问题。
提供一个可以由 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'
});
请注意,此 returns 一个 IWordAtPosition
对象具有三个属性:
endColumn: number
The column where the word ends.
startColumn: number
The column where the word starts.
word: string
The word.
因此,要在悬停位置获取字符串形式的单词,您需要访问 model.getWordAtPosition(position).word
。
如何在 Monaco 编辑器中找到我悬停的词?
我想显示数组中保存的单词的特定值。因此,当用户将鼠标悬停在单词上时,我想将该单词与保存在我的数组中的单词进行比较,然后显示该单词的保存值。
我知道这两种方法:
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
you can simply use model.getWordAtPosition
。
HoverProvider
可以方便地用 model
和 position
调用,所以没问题。
提供一个可以由 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'
});
请注意,此 returns 一个 IWordAtPosition
对象具有三个属性:
endColumn: number
The column where the word ends.
startColumn: number
The column where the word starts.
word: string
The word.
因此,要在悬停位置获取字符串形式的单词,您需要访问 model.getWordAtPosition(position).word
。