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

Leap exit #6

Merged
merged 5 commits into from
Feb 3, 2023
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
12 changes: 0 additions & 12 deletions .env

This file was deleted.

96 changes: 95 additions & 1 deletion src/vs/editor/contrib/leap/browser/Leap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Leap implements IEditorContribution {
private _mdRenderer: MarkdownRenderer;
private _lastCompletions: string[] | undefined; // TODO (kas) This is a bad idea... we need to carefully think about how to handle state.
private _lastCursorPos: Position | undefined;
private _leapOn: boolean = false;

public constructor(
editor: ICodeEditor,
Expand Down Expand Up @@ -89,14 +90,21 @@ class Leap implements IEditorContribution {

public dispose(): void {
this._panel?.remove();
this._panel = undefined;
}

public async toggle(): Promise<void> {
// TODO (kas) We should probably think more carefully about the interface for interacting
// with leap. For now, this will do as a simple on-off toggle.

this._leapOn = !this._leapOn;

// TODO (kas) implement the "simple on-off toggle" :/
this.showCompletions();
if (this._leapOn) {
this.showCompletions();
} else {
this.hideCompletions();
}
}

/**
Expand Down Expand Up @@ -183,6 +191,13 @@ class Leap implements IEditorContribution {

}

public hideCompletions(): void {
// first, hide the exploration panel
this.dispose();
// second, clean up the comment markups
this.removeCompletionComment();
}

/**
* Makes the Codex API to get completions for the given text.
* @param textInBuffer The text up to the current cursor position.
Expand Down Expand Up @@ -324,6 +339,7 @@ class Leap implements IEditorContribution {
return block;
}

// (lisa) why is it async?
private async previewCompletion(index: number): Promise<void> {
// TODO (kas) error handling.
if (!this._lastCompletions || this._lastCompletions.length <= index) {
Expand Down Expand Up @@ -376,6 +392,84 @@ class Leap implements IEditorContribution {
Leap.ID,
[{ range: range, text: completion }]);
}

private removeCompletionComment(): void {
// TODO (lisa) error handling.
if (!this._lastCompletions) {
return;
}

// First, get the start and end range to perform the edits.
// - Get the model for the buffer content
const model = this._editor.getModel();
const text = model?.getValue();
if (!model || !text) {
return;
}

// - See if there are completion comments to remove
const startIdx = text.indexOf(Leap.completionComment);
const endIdx = text.lastIndexOf(Leap.completionComment);

// -- if there are no completion comments, then no edit is necessary
if (startIdx < 0) {
// then endIdx must be less than 0 as well
console.log(`No completion comment removal is necessary`);
return;
} else {
const model = this._editor.getModel()!;
// -- else, we do the edits

const start = model.getPositionAt(startIdx);
const end = model.getPositionAt(endIdx + Leap.completionComment.length);

let textEndLine: number;
let textEndCol: number;
let editEndLine: number;
let editEndCol: number;

if (startIdx === endIdx) {
// there is only one completion comment for whatever weird reason...
// user accidentally removed the last completion comment? maybe
// simply removing that line would be enough.

// get the end point of the last character in the file
const numLines = model.getLineCount();
textEndLine = numLines;
textEndCol = model.getLineLength(numLines) + 1;

// record the ending point of the edit range, which would be the end of the entire file
editEndLine = textEndLine;
editEndCol = textEndCol;

} else {
// there are more than one completion comments.
// remove the one at the beginning and the one at the end

// get the end point of the text in between the comments
const endHead = model.getPositionAt(endIdx);
const lastLine = model.getLineContent(end.lineNumber - 1);
textEndLine = end.lineNumber - 1;
textEndCol = endHead.column + lastLine.length;

// record the ending point of the edit range
editEndLine = end.lineNumber;
editEndCol = end.column;

}

// get the existing content within the range
const textRange = new Range(start.lineNumber + 1, start.column, textEndLine, textEndCol);
const text = model.getValueInRange(textRange);

// Finally, keep only the innerText within the edit range
const editRange = new Range(start.lineNumber, start.column, editEndLine, editEndCol);
this._editor.pushUndoStop();
this._editor.executeEdits(
Leap.ID,
[{ range: editRange, text: text }]);
}
}
}

class LeapAction extends EditorAction {
Expand Down