Monaco ReferenceProvider 是指引用跳转,用户可以右键点击变量名称,可以跳转列表进行选择,选择之后跳转到对应位置的功能。
通过 registerReferenceProvider 注册 ReferenceProvider 实现函数
实现 provideReferences 并返回可跳转位置
代码实现如下
export function referenceProvider(
editor: monacoEditor.editor.IStandaloneCodeEditor,
monaco: typeof monacoEditor
) {
return monaco.languages.registerReferenceProvider("javascript", {
provideReferences: function (model, position, context, token) {
// Get the word at the position
const wordInfo = model.getWordAtPosition(position);
const word = wordInfo ? wordInfo.word : "";
// Find all occurrences of the word in the model
const matches = [];
const text = model.getValue();
const lines = text.split("\n");
for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
const line = lines[lineNumber];
let index = line.indexOf(word);
while (index !== -1) {
matches.push({
uri: model.uri,
range: new monaco.Range(
lineNumber + 1,
index + 1,
lineNumber + 1,
index + word.length + 1
),
});
index = line.indexOf(word, index + word.length);
}
}
return matches;
},
});
}