From b04aa672673b14ef86d560b5a62e7a6e5fbc7535 Mon Sep 17 00:00:00 2001 From: tadashi-aikawa Date: Sat, 13 May 2023 01:41:09 +0900 Subject: [PATCH] Fix a bug that the suggestions don't disappear even after moving a cursor (#221) --- src/ui/AutoCompleteSuggest.ts | 16 +++++++++++++++- src/util/strings.test.ts | 15 +++++++++++++++ src/util/strings.ts | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ui/AutoCompleteSuggest.ts b/src/ui/AutoCompleteSuggest.ts index 3efc89b..b60c53b 100644 --- a/src/ui/AutoCompleteSuggest.ts +++ b/src/ui/AutoCompleteSuggest.ts @@ -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) { @@ -98,6 +103,7 @@ export class AutoCompleteSuggest contextStartCh: number; pastCurrentTokenSeparatedWhiteSpace = ""; + previousCurrentLine = ""; previousLinksCacheInActiveFile: Set = new Set(); // unsafe!! @@ -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("---")) { diff --git a/src/util/strings.test.ts b/src/util/strings.test.ts index e1afbf2..2817858 100644 --- a/src/util/strings.test.ts +++ b/src/util/strings.test.ts @@ -4,6 +4,7 @@ import { allNumbersOrFewSymbols, capitalizeFirstLetter, encodeSpace, + equalsAsLiterals, excludeEmoji, excludeSpace, findCommonPrefix, @@ -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} diff --git a/src/util/strings.ts b/src/util/strings.ts index 8a73767..64e21d4 100644 --- a/src/util/strings.ts +++ b/src/util/strings.ts @@ -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_\-.]+$/)); }