Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite trigger UI to use a ViewPlugin instead of a Widget #32

Merged
merged 11 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1 class="text-4xl font-bold mb-4">
AI features like inline Cmd+L to edit code.
</p>
<div class="border rounded-lg bg-white shadow-sm">
<div id="editor" class="min-h-[400px] h-full"></div>
<div id="editor" class="h-[400px] overflow-y-auto"></div>
</div>
</main>
<script type="module" src="./index.ts"></script>
Expand Down
4 changes: 2 additions & 2 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const logger = console;
(async () => {
const extensions = [
basicSetup,
EditorView.lineWrapping,
// EditorView.lineWrapping,
python(),
aiExtension({
onAcceptEdit: (opts) => {
Expand All @@ -27,7 +27,7 @@ const logger = console;
];

const editor = new EditorView({
doc: `
doc: `# A very long comment that can be a selection start point that will stretch off screen of this demo and should still work
class DataProcessor:
def __init__(self, data: list[int]):
self.data = data
Expand Down
7 changes: 4 additions & 3 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("index.ts exports", () => {
"closeAiEditInput",
"completionState",
"defaultKeymaps",
"defaultTriggerRenderer",
"inputPromptDecoration",
"inputState",
"inputValueState",
Expand All @@ -27,9 +28,9 @@ describe("index.ts exports", () => {
"showAiEditInput",
"showCompletion",
"showInput",
"showTooltip",
"tooltipState",
"tooltipVisibilityListener",
"triggerOptions",
"triggerPlugin",
"triggerViewPlugin",
]
`);
});
Expand Down
108 changes: 108 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { EditorSelection } from "@codemirror/state";
import type { Command, EditorView } from "@codemirror/view";
import {
showInput,
setInputFocus,
setInputValue,
setLoading,
completionState,
showCompletion,
} from "./state.js";

// Validation constants
const MIN_SELECTION_LENGTH = 1;

/**
* Command to show the input prompt
*/
export const showAiEditInput: Command = (view: EditorView) => {
const { state } = view;
const selection = state.selection.main;

// If the selection is empty, hide the tooltip.
if (selection.empty) {
return false;
}

const doc = state.doc;
const fromLine = doc.lineAt(selection.from);
const toLine = doc.lineAt(selection.to);

// Get the full line content by using line boundaries
const selectionText = state.sliceDoc(fromLine.from, toLine.to);
if (selectionText.trim().length < MIN_SELECTION_LENGTH) {
return false;
}

// Ensure line numbers are within document bounds
const safeLineFrom = Math.max(1, Math.min(fromLine.number, doc.lines));
const safeLineTo = Math.max(1, Math.min(toLine.number, doc.lines));

view.dispatch({
effects: [
showInput.of({
show: true,
lineFrom: safeLineFrom,
lineTo: safeLineTo,
}),
setInputFocus.of(true),
setInputValue.of(""),
],
selection: EditorSelection.cursor(fromLine.from),
});
return true;
};

/** Command to close the input prompt */
export const closeAiEditInput: Command = (view: EditorView) => {
view.dispatch({
effects: [
showInput.of({ show: false, lineFrom: 0, lineTo: 0 }),
setInputFocus.of(false),
setInputValue.of(""),
setLoading.of(false),
],
});
return true;
};

/** Command to accept the completion */
export const acceptAiEdit: Command = (view: EditorView) => {
const completionStateValue = view.state.field(completionState);
if (completionStateValue) {
view.dispatch({
effects: [
showCompletion.of(null),
showInput.of({ show: false, lineFrom: 0, lineTo: 0 }),
setInputFocus.of(false),
setInputValue.of(""),
setLoading.of(false),
],
});
return true;
}
return false;
};

/** Command to reject the completion */
export const rejectAiEdit: Command = (view: EditorView) => {
const completionStateValue = view.state.field(completionState);
if (completionStateValue) {
view.dispatch({
changes: {
from: completionStateValue.from,
to: completionStateValue.to,
insert: completionStateValue.oldCode,
},
effects: [
showCompletion.of(null),
showInput.of({ show: false, lineFrom: 0, lineTo: 0 }),
setInputFocus.of(false),
setInputValue.of(""),
setLoading.of(false),
],
});
return true;
}
return false;
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./inline-edit.js";
export * from "./state.js";
export * from "./theme.js";
export * from "./trigger.js";
export * from "./commands.js";
Loading