@@ -331,11 +331,59 @@ IDE、编辑器作为开发者的主要工具,其设计和学习成本也
331331
332332#### 补全模式:Inlay
333333
334- 在自动代码补全上,国内的厂商主要参考的是 GitHub Copilot 的实现,逻辑也不复杂,主要是:
334+ 在自动代码补全上,国内的厂商主要参考的是 GitHub Copilot 的实现,逻辑也不复杂。
335335
336- ```xml
336+ **采用快捷键方式触发**
337+
338+ 其主要是在 Action 里监听用户的输入,然后:
339+
340+ | 功能 | 快捷键 | 说明 |
341+ |--------------------|-------------|-----------------------|
342+ | requestCompletions | `Alt` + `/` | 获取当前的上下文,然后通过模型获取补全结果 |
343+ | applyInlays | `TAB` | 将补全结果展示在 IDE 上 |
344+ | disposeInlays | `ESC` | 取消补全 |
345+ | cycleNextInlays | `Alt` + `[` | 切换到下一个补全结果 |
346+ | cyclePrevInlays | `Alt` + `[` | 切换到上一个补全结果 |
347+
348+ **采用自动触发方式**
349+
350+ 其主要通过 `EditorFactoryListener` 监听用户的输入,然后:根据不同的输入,触发不同的补全结果。核心代码如下:
351+
352+ ```kotlin
353+ class AutoDevEditorListener : EditorFactoryListener {
354+ override fun editorCreated(event: EditorFactoryEvent) {
355+ //...
356+ editor.document.addDocumentListener(AutoDevDocumentListener(editor), editorDisposable)
357+ editor.caretModel.addCaretListener(AutoDevCaretListener(editor), editorDisposable)
358+ //...
359+ }
360+
361+ class AutoDevCaretListener(val editor: Editor) : CaretListener {
362+ override fun caretPositionChanged(event: CaretEvent) {
363+ //...
364+ val wasTypeOver = TypeOverHandler.getPendingTypeOverAndReset(editor)
365+ //...
366+ llmInlayManager.disposeInlays(editor, InlayDisposeContext.CaretChange)
367+ }
368+ }
369+
370+ class AutoDevDocumentListener(val editor: Editor) : BulkAwareDocumentListener {
371+ override fun documentChangedNonBulk(event: DocumentEvent) {
372+ //...
373+ val llmInlayManager = LLMInlayManager.getInstance()
374+ llmInlayManager
375+ .editorModified(editor, changeOffset)
376+ }
377+ }
378+ }
337379```
338380
381+ 再根据不同的输入,触发不同的补全结果,并对结构进行处理。
382+
383+ **渲染补全代码**
384+
385+ 随后,我们需要实现一个 Inlay Render,它继承自 `EditorCustomElementRenderer`。
386+
339387#### 日常辅助功能开发
340388
341389结合
@@ -355,9 +403,10 @@ IDE、编辑器作为开发者的主要工具,其设计和学习成本也
355403在编写 ShowIntentionsGroup 时,我们可以参考 AutoDev 的实现来构建对应的 Group:
356404
357405```xml
406+
358407<group id="AutoDevIntentionsActionGroup" class="cc.unitmesh.devti.intentions.IntentionsActionGroup"
359408 icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT" searchable="false">
360- <add-to-group group-id="ShowIntentionsGroup" relative-to-action="ShowIntentionActions" anchor="after"/>
409+ <add-to-group group-id="ShowIntentionsGroup" relative-to-action="ShowIntentionActions" anchor="after"/>
361410</group>
362411```
363412
0 commit comments