Skip to content

Commit

Permalink
Refactoring (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadashi-aikawa committed Nov 27, 2021
1 parent 1651d67 commit 083a45b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 53 deletions.
70 changes: 70 additions & 0 deletions src/suggester/suggester.ts
@@ -0,0 +1,70 @@
import { Word } from "../CustomDictionaryService";
import {
capitalizeFirstLetter,
lowerStartsWith,
lowerStartsWithoutSpace,
startsWithoutSpace,
} from "../util/strings";

interface Judgement {
word: Word;
value?: string;
alias: boolean;
}

function judge(
word: Word,
query: string,
queryStartWithUpper: boolean
): Judgement {
if (word.value === query) {
return { word: word, alias: false };
}

if (
word.value.startsWith("[[")
? lowerStartsWithoutSpace(word.value.replace("[[", ""), query)
: startsWithoutSpace(word.value, query)
) {
return { word: word, value: word.value, alias: false };
}

if (
queryStartWithUpper &&
startsWithoutSpace(capitalizeFirstLetter(word.value), query)
) {
word.value = capitalizeFirstLetter(word.value);
return { word: word, value: word.value, alias: false };
}

const matchedAlias = word.aliases?.find((a) =>
lowerStartsWithoutSpace(a, query)
);
if (matchedAlias) {
return { word: word, value: matchedAlias, alias: true };
}

return { word: word, alias: false };
}

export function suggestWords(
words: Word[],
query: string,
max: number
): Word[] {
const queryStartWithUpper = capitalizeFirstLetter(query) === query;
return Array.from(words)
.map((x) => judge(x, query, queryStartWithUpper))
.filter((x) => x.value !== undefined)
.sort((a, b) => {
const aliasP = (Number(a.alias) - Number(b.alias)) * 10000;
const startP =
(Number(lowerStartsWith(b.value!, query)) -
Number(lowerStartsWith(a.value!, query))) *
1000;
const lengthP = a.value!.length - b.value!.length;
return aliasP + startP + lengthP;
})
.map((x) => x.word)
.slice(0, max);
}
54 changes: 1 addition & 53 deletions src/ui/AutoCompleteSuggest.ts
Expand Up @@ -13,65 +13,13 @@ import {
Scope,
TFile,
} from "obsidian";
import {
capitalizeFirstLetter,
lowerStartsWith,
lowerStartsWithoutSpace,
startsWithoutSpace,
} from "../util/strings";
import { createTokenizer, Tokenizer } from "../tokenizer/tokenizer";
import { TokenizeStrategy } from "../tokenizer/TokenizeStrategy";
import { Settings } from "../settings";
import { CustomDictionaryService, Word } from "../CustomDictionaryService";
import { uniq } from "../util/collection-helper";
import { AppHelper } from "../app-helper";

function suggestWords(words: Word[], query: string, max: number): Word[] {
const queryStartWithUpper = capitalizeFirstLetter(query) === query;
return Array.from(words)
.map((x) => {
if (x.value === query) {
return { word: x, alias: false };
}

if (
x.value.startsWith("[[")
? lowerStartsWithoutSpace(x.value.replace("[[", ""), query)
: startsWithoutSpace(x.value, query)
) {
return { word: x, value: x.value, alias: false };
}

if (
queryStartWithUpper &&
startsWithoutSpace(capitalizeFirstLetter(x.value), query)
) {
x.value = capitalizeFirstLetter(x.value);
return { word: x, value: x.value, alias: false };
}

const matchedAlias = x.aliases?.find((a) =>
lowerStartsWithoutSpace(a, query)
);
if (matchedAlias) {
return { word: x, value: matchedAlias, alias: true };
}

return { word: x, alias: false };
})
.filter((x) => x.value !== undefined)
.sort((a, b) => {
const aliasP = (Number(a.alias) - Number(b.alias)) * 10000;
const startP =
(Number(lowerStartsWith(b.value!, query)) -
Number(lowerStartsWith(a.value!, query))) *
1000;
const lengthP = a.value!.length - b.value!.length;
return aliasP + startP + lengthP;
})
.map((x) => x.word)
.slice(0, max);
}
import { suggestWords } from "../suggester/suggester";

// This is an unsafe code..!!
interface UnsafeEditorSuggestInterface {
Expand Down

0 comments on commit 083a45b

Please sign in to comment.