Monaco 使用 ReferenceProvider

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;
    },
  });
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值