|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +import * as Common from '../../core/common/common.js'; |
| 5 | +import * as Root from '../../core/root/root.js'; |
| 6 | +import * as AiCodeCompletion from '../../models/ai_code_completion/ai_code_completion.js'; |
| 7 | +import type * as Workspace from '../../models/workspace/workspace.js'; |
| 8 | +import * as CodeMirror from '../../third_party/codemirror.next/codemirror.next.js'; |
| 9 | +import * as TextEditor from '../../ui/components/text_editor/text_editor.js'; |
| 10 | +import * as UI from '../../ui/legacy/legacy.js'; |
| 11 | +import * as VisualLogging from '../../ui/visual_logging/visual_logging.js'; |
| 12 | +import * as PanelCommon from '../common/common.js'; |
| 13 | + |
| 14 | +import {Plugin} from './Plugin.js'; |
| 15 | + |
| 16 | +export class AiCodeCompletionPlugin extends Plugin { |
| 17 | + #aiCodeCompletionSetting = Common.Settings.Settings.instance().createSetting('ai-code-completion-enabled', false); |
| 18 | + #aiCodeCompletionTeaserDismissedSetting = |
| 19 | + Common.Settings.Settings.instance().createSetting('ai-code-completion-teaser-dismissed', false); |
| 20 | + #teaserCompartment = new CodeMirror.Compartment(); |
| 21 | + #teaser?: PanelCommon.AiCodeCompletionTeaser; |
| 22 | + #teaserDisplayTimeout?: number; |
| 23 | + #editor?: TextEditor.TextEditor.TextEditor; |
| 24 | + |
| 25 | + #boundEditorKeyDown: (event: Event) => Promise<void>; |
| 26 | + #boundOnAiCodeCompletionSettingChanged: () => void; |
| 27 | + |
| 28 | + constructor(uiSourceCode: Workspace.UISourceCode.UISourceCode) { |
| 29 | + super(uiSourceCode); |
| 30 | + if (!this.#isAiCodeCompletionEnabled()) { |
| 31 | + throw new Error('AI code completion feature is not enabled.'); |
| 32 | + } |
| 33 | + this.#boundEditorKeyDown = this.#editorKeyDown.bind(this); |
| 34 | + this.#boundOnAiCodeCompletionSettingChanged = this.#onAiCodeCompletionSettingChanged.bind(this); |
| 35 | + const showTeaser = !this.#aiCodeCompletionSetting.get() && !this.#aiCodeCompletionTeaserDismissedSetting.get(); |
| 36 | + if (showTeaser) { |
| 37 | + this.#teaser = new PanelCommon.AiCodeCompletionTeaser({onDetach: this.#detachAiCodeCompletionTeaser.bind(this)}); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static override accepts(uiSourceCode: Workspace.UISourceCode.UISourceCode): boolean { |
| 42 | + return uiSourceCode.contentType().hasScripts() || uiSourceCode.contentType().hasStyleSheets(); |
| 43 | + } |
| 44 | + |
| 45 | + override dispose(): void { |
| 46 | + this.#teaser = undefined; |
| 47 | + this.#aiCodeCompletionSetting.removeChangeListener(this.#boundOnAiCodeCompletionSettingChanged); |
| 48 | + this.#editor?.removeEventListener('keydown', this.#boundEditorKeyDown); |
| 49 | + super.dispose(); |
| 50 | + } |
| 51 | + |
| 52 | + override editorInitialized(editor: TextEditor.TextEditor.TextEditor): void { |
| 53 | + this.#editor = editor; |
| 54 | + this.#editor.addEventListener('keydown', this.#boundEditorKeyDown); |
| 55 | + this.#aiCodeCompletionSetting.addChangeListener(this.#boundOnAiCodeCompletionSettingChanged); |
| 56 | + this.#onAiCodeCompletionSettingChanged(); |
| 57 | + } |
| 58 | + |
| 59 | + override editorExtension(): CodeMirror.Extension { |
| 60 | + return [ |
| 61 | + CodeMirror.EditorView.updateListener.of(update => this.#editorUpdate(update)), |
| 62 | + this.#teaserCompartment.of([]), |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + #editorUpdate(update: CodeMirror.ViewUpdate): void { |
| 67 | + if (this.#teaser) { |
| 68 | + if (update.docChanged) { |
| 69 | + update.view.dispatch({effects: this.#teaserCompartment.reconfigure([])}); |
| 70 | + this.#addTeaserPluginToCompartment(update); |
| 71 | + } else if (update.selectionSet) { |
| 72 | + update.view.dispatch({effects: this.#teaserCompartment.reconfigure([])}); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + async #editorKeyDown(event: Event): Promise<void> { |
| 78 | + if (!this.#teaser?.isShowing()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + const keyboardEvent = (event as KeyboardEvent); |
| 82 | + if (UI.KeyboardShortcut.KeyboardShortcut.eventHasCtrlEquivalentKey(keyboardEvent)) { |
| 83 | + if (keyboardEvent.key === 'i') { |
| 84 | + keyboardEvent.consume(true); |
| 85 | + void VisualLogging.logKeyDown(event.currentTarget, event, 'ai-code-completion-teaser.fre'); |
| 86 | + await this.#teaser?.onAction(event); |
| 87 | + } else if (keyboardEvent.key === 'x') { |
| 88 | + keyboardEvent.consume(true); |
| 89 | + void VisualLogging.logKeyDown(event.currentTarget, event, 'ai-code-completion-teaser.dismiss'); |
| 90 | + this.#teaser?.onDismiss(event); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + #addTeaserPluginToCompartment = Common.Debouncer.debounce((update: CodeMirror.ViewUpdate) => { |
| 96 | + if (this.#teaserDisplayTimeout) { |
| 97 | + window.clearTimeout(this.#teaserDisplayTimeout); |
| 98 | + this.#teaserDisplayTimeout = undefined; |
| 99 | + } |
| 100 | + this.#teaserDisplayTimeout = window.setTimeout(() => { |
| 101 | + if (this.#teaser) { |
| 102 | + update.view.dispatch( |
| 103 | + {effects: this.#teaserCompartment.reconfigure([aiCodeCompletionTeaserExtension(this.#teaser)])}); |
| 104 | + } |
| 105 | + }, AiCodeCompletion.AiCodeCompletion.DELAY_BEFORE_SHOWING_RESPONSE_MS); |
| 106 | + }, AiCodeCompletion.AiCodeCompletion.AIDA_REQUEST_DEBOUNCE_TIMEOUT_MS); |
| 107 | + |
| 108 | + #setAiCodeCompletion(): void { |
| 109 | + if (this.#teaser) { |
| 110 | + this.#detachAiCodeCompletionTeaser(); |
| 111 | + this.#teaser = undefined; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + #onAiCodeCompletionSettingChanged(): void { |
| 116 | + if (this.#aiCodeCompletionSetting.get()) { |
| 117 | + this.#setAiCodeCompletion(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + #detachAiCodeCompletionTeaser(): void { |
| 122 | + this.#editor?.dispatch({ |
| 123 | + effects: this.#teaserCompartment.reconfigure([]), |
| 124 | + }); |
| 125 | + this.#teaser = undefined; |
| 126 | + } |
| 127 | + |
| 128 | + #isAiCodeCompletionEnabled(): boolean { |
| 129 | + return Boolean(Root.Runtime.hostConfig.devToolsAiCodeCompletion?.enabled); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export function aiCodeCompletionTeaserExtension(teaser: PanelCommon.AiCodeCompletionTeaser): CodeMirror.Extension { |
| 134 | + const teaserPlugin = CodeMirror.ViewPlugin.fromClass(class { |
| 135 | + #teaserDecoration: CodeMirror.DecorationSet; |
| 136 | + |
| 137 | + constructor(readonly view: CodeMirror.EditorView) { |
| 138 | + const cursorPosition = this.view.state.selection.main.head; |
| 139 | + const line = this.view.state.doc.lineAt(cursorPosition); |
| 140 | + const column = cursorPosition - line.from; |
| 141 | + const isCursorAtEndOfLine = column >= line.length; |
| 142 | + if (isCursorAtEndOfLine) { |
| 143 | + this.#teaserDecoration = CodeMirror.Decoration.set([ |
| 144 | + CodeMirror.Decoration |
| 145 | + .widget({ |
| 146 | + widget: new TextEditor.AiCodeCompletionTeaserPlaceholder.AiCodeCompletionTeaserPlaceholder(teaser), |
| 147 | + side: 1 |
| 148 | + }) |
| 149 | + .range(cursorPosition), |
| 150 | + ]); |
| 151 | + } else { |
| 152 | + this.#teaserDecoration = CodeMirror.Decoration.none; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + declare update: () => void; |
| 157 | + |
| 158 | + get decorations(): CodeMirror.DecorationSet { |
| 159 | + return this.#teaserDecoration; |
| 160 | + } |
| 161 | + }, { |
| 162 | + decorations: v => v.decorations, |
| 163 | + }); |
| 164 | + return teaserPlugin; |
| 165 | +} |
0 commit comments