Monaco 多行提示的实现方式

AI 代码助手最近太火爆,国内有模型厂商都有代码助手,代码助手是个比较典型的 AI 应用,主要看前端,后端的模型都差不多,国内外都有专门的代码模型。现在都是集中在 VSCode 和 Idea的插件,本文通过 Monaco 实现一个多行代码提示,通过 monaco.languages.registerInlineCompletionsProvider 实现即可,Monaco 的 API 特别丰富,就是文档差点儿。代码中这个 range 没什么用,从 API 来看应该是代码插入代码的区域,但是 end-start 不要出现负值,否则会出问题。在调用的后端的时候,要用节流,请求就太多了,模型受不了。

复制代码
import logo from './logo.svg';
import './App.css';
import MonacoEditor from 'react-monaco-editor';

import { useRef, useState,useEffect }  from 'react';

function App() {
  const editorRef = useRef(null);
  const monacoRef = useRef(null);
  const decorationsRef = useRef([]);

  const handleEditorDidMount = (editor, monaco) => {
    editorRef.current = editor;
    monacoRef.current = monaco;
    

    monaco.languages.registerInlineCompletionsProvider('javascript', {
      provideInlineCompletions: (model, position, context, token) => {

        const multiLineCompletion = {
          text: "example() {\n\tconsole.log('Hello, world!');\n}\nexample();",
          range: {
            startLineNumber: position.lineNumber,
            startColumn: 10,
            endLineNumber: position.lineNumber,
            endColumn: 10,
          },
        };

        return {
          items: [
            {
              insertText: multiLineCompletion.text,
              range: new monaco.Range(
                multiLineCompletion.range.startLineNumber,
                multiLineCompletion.range.startColumn,
                multiLineCompletion.range.endLineNumber,
                multiLineCompletion.range.endColumn
              ),
            },
          ],
        };
      },
      freeInlineCompletions(arg) {
      }
    });

  };

  useEffect(() => {
    // Define custom styles for the decorations
    const style = document.createElement('style');
    style.innerHTML = `
      .myAfterContentDecoration::after {
        content: ' // 备注';
        color: green;
        font-weight: bold;
      }
    `;
    document.head.appendChild(style);
  }, []);
  return (
    <div style={{'margin':'100px auto', 'width': '800px'}}>
   <MonacoEditor
        width="800"
        height="600"
        language="javascript"
        theme="vs-dark"
        value={`// Write your JavaScript code here
function helloWorld() {
  console.log('Hello, world!');
}
helloWorld();`}
        options={{
          selectOnLineNumbers: true
        }}
        editorDidMount={handleEditorDidMount}
      />
    </div>
      
  );
}

export default App;
相关推荐
GISer_Jing8 小时前
2026前端技术潜在主流前沿方向
前端·人工智能·reactjs
可问春风_ren2 天前
前端文件上传详细解析
前端·ecmascript·reactjs·js
DEMO派6 天前
CSS优先级规则以及如何提升优先级方案详解
前端·javascript·css·vue.js·reactjs·html5·angular.js
我的golang之路果然有问题9 天前
实习中遇到的 CORS 同源策略自己的理解分析
前端·javascript·vue·reactjs·同源策略·cors
Sun_小杰杰哇12 天前
Dayjs常用操作使用
开发语言·前端·javascript·typescript·vue·reactjs·anti-design-vue
web小白成长日记14 天前
前端三个月速成,是否靠谱?
前端·react.js·前端框架·html·reactjs·webkit·scss
前端不太难1 个月前
RN 与原生通信时出现性能瓶颈(Bridge 卡顿)怎么办?
前端·前端框架·reactjs
聊天QQ:4877392781 个月前
MATLAB编程实现双轴两自由度车辆车桥耦合振动程序,可提取车体加速度响应及接触点响应并对比论文结果
reactjs
weixin79893765432...1 个月前
Monaco Editor 的使用与二次开发
monaco·monaco editor·二次开发 monaco
初遇你时动了情1 个月前
react native创建项目常用插件
react native·typescript·reactjs