Skip to content

Commit

Permalink
fix autocomplete on empty work and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jul 21, 2021
1 parent 00d95d9 commit 7a0be5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/completer/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class CompletionHandler implements IDisposable {
const cursorBeforeChange = editor.getOffsetAt(editor.getCursorPosition());
// we need to update the shared model in a single transaction so that the undo manager works as expected
editor.model.sharedModel.updateSource(start, end, value);
if (cursorBeforeChange < end && cursorBeforeChange >= start) {
if (cursorBeforeChange <= end && cursorBeforeChange >= start) {
editor.setCursorPosition(editor.getPositionAt(start + value.length)!);
}
}
Expand Down
36 changes: 36 additions & 0 deletions packages/completer/test/handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,41 @@ describe('@jupyterlab/completer', () => {
console.warn(editor.getCursorPosition());
});
});

it('should update cursor position after autocomplete on empty word', () => {
const model = new CompleterModel();
const patch = 'foobar';
const completer = new Completer({ editor: null, model });
const handler = new TestCompletionHandler({ completer, connector });
const editor = createEditorWidget().editor;
const text = 'eggs\n # comment\nbaz';
const want = 'eggs\n foobar # comment\nbaz';
const line = 1;
const column = 1;
const request: Completer.ITextState = {
column: column,
line,
lineHeight: 0,
charWidth: 0,
coords: null,
text
};

handler.editor = editor;
handler.editor.model.value.text = text;
handler.editor.model.sharedModel.clearUndoHistory();
handler.editor.setCursorPosition({ line, column });
model.original = request;
const offset = handler.editor.getOffsetAt({ line, column });
model.cursor = { start: offset, end: offset };
// Make the completion, check its value and cursor position.
(completer.selected as any).emit(patch);
expect(editor.model.value.text).toBe(want);
expect(editor.getCursorPosition()).toEqual({
line,
column: column + 6
});
console.warn(editor.getCursorPosition());
});
});
});

0 comments on commit 7a0be5f

Please sign in to comment.