Skip to content

Commit

Permalink
Fix a bug that the suggestions don't disappear even after moving a cu…
Browse files Browse the repository at this point in the history
…rsor (#221)
  • Loading branch information
tadashi-aikawa committed May 12, 2023
1 parent b4adcb8 commit b04aa67
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/ui/AutoCompleteSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ import {
SelectionHistoryStorage,
type SelectionHistoryTree,
} from "../storage/SelectionHistoryStorage";
import { encodeSpace, excludeEmoji, findCommonPrefix } from "../util/strings";
import {
encodeSpace,
equalsAsLiterals,
excludeEmoji,
findCommonPrefix,
} from "../util/strings";
import { DEFAULT_HISTORIES_PATH } from "../util/path";

function buildLogMessage(message: string, msec: number) {
Expand Down Expand Up @@ -98,6 +103,7 @@ export class AutoCompleteSuggest
contextStartCh: number;

pastCurrentTokenSeparatedWhiteSpace = "";
previousCurrentLine = "";
previousLinksCacheInActiveFile: Set<string> = new Set();

// unsafe!!
Expand Down Expand Up @@ -883,6 +889,14 @@ export class AutoCompleteSuggest
return null;
}

const cl = this.appHelper.getCurrentLine(editor);
if (equalsAsLiterals(this.previousCurrentLine, cl) && !this.runManually) {
this.previousCurrentLine = cl;
onReturnNull("Don't show suggestions because there are no changes");
return null;
}
this.previousCurrentLine = cl;

const currentLineUntilCursor =
this.appHelper.getCurrentLineUntilCursor(editor);
if (currentLineUntilCursor.startsWith("---")) {
Expand Down
15 changes: 15 additions & 0 deletions src/util/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
allNumbersOrFewSymbols,
capitalizeFirstLetter,
encodeSpace,
equalsAsLiterals,
excludeEmoji,
excludeSpace,
findCommonPrefix,
Expand All @@ -18,6 +19,20 @@ import {
synonymAliases,
} from "./strings";

describe.each<{ one: string; another: string; expected: boolean }>`
one | another | expected
${"aaa"} | ${"aaa"} | ${true}
${" \taaa\t "} | ${"aaa"} | ${true}
${"aaa"} | ${" \taaa\t "} | ${true}
${" a a a "} | ${"\ta\ta\ta\t"} | ${true}
${"aaa"} | ${"aaA"} | ${false}
${" aaa "} | ${"aaA"} | ${false}
`("equalsAsLiterals", ({ one, another, expected }) => {
test(`equalsAsLiterals(${one}, ${another}) = ${expected}`, () => {
expect(equalsAsLiterals(one, another)).toBe(expected);
});
});

describe.each<{ text: string; expected: boolean }>`
text | expected
${"2020-01-01"} | ${true}
Expand Down
4 changes: 4 additions & 0 deletions src/util/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import emojiRegex from "emoji-regex";

const regEmoji = new RegExp(` *(${emojiRegex().source}) *`, "g");

export function equalsAsLiterals(one: string, another: string): boolean {
return one.replace(/[ \t]/g, "") === another.replace(/[ \t]/g, "");
}

export function allNumbersOrFewSymbols(text: string): boolean {
return Boolean(text.match(/^[0-9_\-.]+$/));
}
Expand Down

0 comments on commit b04aa67

Please sign in to comment.