Skip to content

Commit

Permalink
Account for range offsets when tab completing to not replace unrelate…
Browse files Browse the repository at this point in the history
…d characters

Signed-off-by: Michael Telatynski <[email protected]>
  • Loading branch information
t3chguy committed Nov 14, 2024
1 parent 62b173a commit 572afd7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/editor/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { KeyboardEvent } from "react";

import { Part, CommandPartCreator, PartCreator } from "./parts";
import DocumentPosition from "./position";
import { ICompletion } from "../autocomplete/Autocompleter";
import { ICompletion, ISelectionRange } from "../autocomplete/Autocompleter";
import Autocomplete from "../components/views/rooms/Autocomplete";

export interface ICallback {
replaceParts?: Part[];
range?: ISelectionRange;
close?: boolean;
}

Expand Down Expand Up @@ -82,6 +83,7 @@ export default class AutocompleteWrapperModel {
this.updateCallback({
replaceParts: this.partForCompletion(completion),
close: true,
range: completion.range,
});
}

Expand Down
16 changes: 13 additions & 3 deletions src/editor/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,24 @@ export default class EditorModel {
return Promise.resolve();
}

private onAutoComplete = ({ replaceParts, close }: ICallback): void => {
private onAutoComplete = ({ replaceParts, close, range }: ICallback): void => {
let pos: DocumentPosition | undefined;
if (replaceParts) {
const autoCompletePartIdx = this.autoCompletePartIdx || 0;
this._parts.splice(autoCompletePartIdx, this.autoCompletePartCount, ...replaceParts);

this.replaceRange(
new DocumentPosition(autoCompletePartIdx, range?.start ?? 0),
new DocumentPosition(
autoCompletePartIdx + this.autoCompletePartCount - 1,
range?.end ?? this.parts[autoCompletePartIdx + this.autoCompletePartCount - 1].text.length,
),
replaceParts,
);

this.autoCompletePartCount = replaceParts.length;
const lastPart = replaceParts[replaceParts.length - 1];
const lastPartIndex = autoCompletePartIdx + replaceParts.length - 1;
// `replaceRange` merges adjacent parts so we need to find it in the new parts list
const lastPartIndex = this.parts.indexOf(lastPart);
pos = new DocumentPosition(lastPartIndex, lastPart.text.length);
}
if (close) {
Expand Down

0 comments on commit 572afd7

Please sign in to comment.