Skip to content

Commit

Permalink
refactor: format all with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
HuakunShen committed Nov 7, 2024
1 parent 42b204e commit 9d38846
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 83 deletions.
85 changes: 42 additions & 43 deletions apps/desktop/src/lib/utils/command-score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@
//
// The best case, this character is a match, and either this is the start
// of the string, or the previous character was also a match.
const SCORE_CONTINUE_MATCH = 1;
const SCORE_CONTINUE_MATCH = 1
// A new match at the start of a word scores better than a new match
// elsewhere as it's more likely that the user will type the starts
// of fragments.
// NOTE: We score word jumps between spaces slightly higher than slashes, brackets
// hyphens, etc.
const SCORE_SPACE_WORD_JUMP = 0.9;
const SCORE_NON_SPACE_WORD_JUMP = 0.8;
const SCORE_SPACE_WORD_JUMP = 0.9
const SCORE_NON_SPACE_WORD_JUMP = 0.8
// Any other match isn't ideal, but we include it for completeness.
const SCORE_CHARACTER_JUMP = 0.17;
const SCORE_CHARACTER_JUMP = 0.17
// If the user transposed two letters, it should be significantly penalized.
//
// i.e. "ouch" is more likely than "curtain" when "uc" is typed.
const SCORE_TRANSPOSITION = 0.1;
const SCORE_TRANSPOSITION = 0.1
// The goodness of a match should decay slightly with each missing
// character.
//
// i.e. "bad" is more likely than "bard" when "bd" is typed.
//
// This will not change the order of suggestions based on SCORE_* until
// 100 characters are inserted between matches.
const PENALTY_SKIPPED = 0.999;
const PENALTY_SKIPPED = 0.999
// The goodness of an exact-case match should be higher than a
// case-insensitive match by a small amount.
//
// i.e. "HTML" is more likely than "haml" when "HM" is typed.
//
// This will not change the order of suggestions based on SCORE_* until
// 1000 characters are inserted between matches.
const PENALTY_CASE_MISMATCH = 0.9999;
const PENALTY_CASE_MISMATCH = 0.9999
// Match higher for letters closer to the beginning of the word
const PENALTY_DISTANCE_FROM_START = 0.9;
const PENALTY_DISTANCE_FROM_START = 0.9
// If the word has more characters than the user typed, it should
// be penalised slightly.
//
Expand All @@ -48,12 +48,12 @@ const PENALTY_DISTANCE_FROM_START = 0.9;
// ordering (like alphabetical) that it makes sense to rely on when
// there are many prefix matches, so we don't make the penalty increase
// with the number of tokens.
const PENALTY_NOT_COMPLETE = 0.99;
const PENALTY_NOT_COMPLETE = 0.99

const IS_GAP_REGEXP = /[\\/_+.#"@[({&]/;
const COUNT_GAPS_REGEXP = /[\\/_+.#"@[({&]/g;
const IS_SPACE_REGEXP = /[\s-]/;
const COUNT_SPACE_REGEXP = /[\s-]/g;
const IS_GAP_REGEXP = /[\\/_+.#"@[({&]/
const COUNT_GAPS_REGEXP = /[\\/_+.#"@[({&]/g
const IS_SPACE_REGEXP = /[\s-]/
const COUNT_SPACE_REGEXP = /[\s-]/g

function commandScoreInner(
string,
Expand All @@ -66,21 +66,21 @@ function commandScoreInner(
) {
if (abbreviationIndex === abbreviation.length) {
if (stringIndex === string.length) {
return SCORE_CONTINUE_MATCH;
return SCORE_CONTINUE_MATCH
}
return PENALTY_NOT_COMPLETE;
return PENALTY_NOT_COMPLETE
}

const memoizeKey = `${stringIndex},${abbreviationIndex}`;
const memoizeKey = `${stringIndex},${abbreviationIndex}`
if (memoizedResults[memoizeKey] !== undefined) {
return memoizedResults[memoizeKey];
return memoizedResults[memoizeKey]
}

const abbreviationChar = lowerAbbreviation.charAt(abbreviationIndex);
let index = lowerString.indexOf(abbreviationChar, stringIndex);
let highScore = 0;
const abbreviationChar = lowerAbbreviation.charAt(abbreviationIndex)
let index = lowerString.indexOf(abbreviationChar, stringIndex)
let highScore = 0

let score, transposedScore, wordBreaks, spaceBreaks;
let score, transposedScore, wordBreaks, spaceBreaks

while (index >= 0) {
score = commandScoreInner(
Expand All @@ -91,38 +91,37 @@ function commandScoreInner(
index + 1,
abbreviationIndex + 1,
memoizedResults
);
)
if (score > highScore) {
if (index === stringIndex) {
score *= SCORE_CONTINUE_MATCH;
score *= SCORE_CONTINUE_MATCH
} else if (IS_GAP_REGEXP.test(string.charAt(index - 1))) {
score *= SCORE_NON_SPACE_WORD_JUMP;
wordBreaks = string.slice(stringIndex, index - 1).match(COUNT_GAPS_REGEXP);
score *= SCORE_NON_SPACE_WORD_JUMP
wordBreaks = string.slice(stringIndex, index - 1).match(COUNT_GAPS_REGEXP)
if (wordBreaks && stringIndex > 0) {
score *= PENALTY_SKIPPED ** wordBreaks.length;
score *= PENALTY_SKIPPED ** wordBreaks.length
}
} else if (IS_SPACE_REGEXP.test(string.charAt(index - 1))) {
score *= SCORE_SPACE_WORD_JUMP;
spaceBreaks = string.slice(stringIndex, index - 1).match(COUNT_SPACE_REGEXP);
score *= SCORE_SPACE_WORD_JUMP
spaceBreaks = string.slice(stringIndex, index - 1).match(COUNT_SPACE_REGEXP)
if (spaceBreaks && stringIndex > 0) {
score *= PENALTY_SKIPPED ** spaceBreaks.length;
score *= PENALTY_SKIPPED ** spaceBreaks.length
}
} else {
score *= SCORE_CHARACTER_JUMP;
score *= SCORE_CHARACTER_JUMP
if (stringIndex > 0) {
score *= PENALTY_SKIPPED ** (index - stringIndex);
score *= PENALTY_SKIPPED ** (index - stringIndex)
}
}

if (string.charAt(index) !== abbreviation.charAt(abbreviationIndex)) {
score *= PENALTY_CASE_MISMATCH;
score *= PENALTY_CASE_MISMATCH
}
}

if (
(score < SCORE_TRANSPOSITION &&
lowerString.charAt(index - 1) ===
lowerAbbreviation.charAt(abbreviationIndex + 1)) ||
lowerString.charAt(index - 1) === lowerAbbreviation.charAt(abbreviationIndex + 1)) ||
(lowerAbbreviation.charAt(abbreviationIndex + 1) ===
lowerAbbreviation.charAt(abbreviationIndex) && // allow duplicate letters. Ref #7428
lowerString.charAt(index - 1) !== lowerAbbreviation.charAt(abbreviationIndex))
Expand All @@ -135,35 +134,35 @@ function commandScoreInner(
index + 1,
abbreviationIndex + 2,
memoizedResults
);
)

if (transposedScore * SCORE_TRANSPOSITION > score) {
score = transposedScore * SCORE_TRANSPOSITION;
score = transposedScore * SCORE_TRANSPOSITION
}
}

if (score > highScore) {
highScore = score;
highScore = score
}

index = lowerString.indexOf(abbreviationChar, index + 1);
index = lowerString.indexOf(abbreviationChar, index + 1)
}

memoizedResults[memoizeKey] = highScore;
return highScore;
memoizedResults[memoizeKey] = highScore
return highScore
}

function formatInput(string) {
// convert all valid space characters to space so they match each other
return string.toLowerCase().replace(COUNT_SPACE_REGEXP, " ");
return string.toLowerCase().replace(COUNT_SPACE_REGEXP, " ")
}

export function commandScore(string: string, abbreviation: string, aliases?: string[]): number {
/* NOTE:
* in the original, we used to do the lower-casing on each recursive call, but this meant that toLowerCase()
* was the dominating cost in the algorithm, passing both is a little ugly, but considerably faster.
*/
string = aliases && aliases.length > 0 ? `${`${string} ${aliases?.join(" ")}`}` : string;
string = aliases && aliases.length > 0 ? `${`${string} ${aliases?.join(" ")}`}` : string
return commandScoreInner(
string,
abbreviation,
Expand All @@ -172,5 +171,5 @@ export function commandScore(string: string, abbreviation: string, aliases?: str
0,
0,
{}
);
)
}
12 changes: 6 additions & 6 deletions apps/desktop/src/routes/extension/create-quick-link/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { z } from "zod";
import { z } from "zod"

export const formSchema = z.object({
username: z.string().min(2).max(50),
});
export type FormSchema = typeof formSchema;
username: z.string().min(2).max(50)
})

export type FormSchema = typeof formSchema
2 changes: 1 addition & 1 deletion packages/ui/src/components/main/QuickLinks.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Command } from "@kksh/svelte5"
import { IconMultiplexer } from "@kksh/ui"
import { DraggableCommandGroup } from "../custom"
import { type CmdQuery, CmdValue } from "./types"
import { CmdValue, type CmdQuery } from "./types"
const { quickLinks }: { quickLinks: CmdQuery[] } = $props()
</script>
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/src/components/theme/mode-toggle.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script lang="ts">
import Sun from "lucide-svelte/icons/sun"
import { Button } from "@kksh/svelte5"
import Moon from "lucide-svelte/icons/moon"
import Sun from "lucide-svelte/icons/sun"
import { toggleMode } from "mode-watcher"
import { Button } from "@kksh/svelte5"
</script>

<Button onclick={toggleMode} variant="outline" size="icon">
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/components/ui/form/form-description.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts">
import * as FormPrimitive from "formsnap";
import type { WithoutChild } from "bits-ui";
import { cn } from "@kksh/ui/utils";
import { cn } from "@kksh/ui/utils"
import type { WithoutChild } from "bits-ui"
import * as FormPrimitive from "formsnap"
let {
ref = $bindable(null),
class: className,
...restProps
}: WithoutChild<FormPrimitive.DescriptionProps> = $props();
}: WithoutChild<FormPrimitive.DescriptionProps> = $props()
</script>

<FormPrimitive.Description
Expand Down
10 changes: 5 additions & 5 deletions packages/ui/src/components/ui/form/form-field-errors.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import * as FormPrimitive from "formsnap";
import type { WithoutChild } from "bits-ui";
import { cn } from "@kksh/ui/utils";
import { cn } from "@kksh/ui/utils"
import type { WithoutChild } from "bits-ui"
import * as FormPrimitive from "formsnap"
let {
ref = $bindable(null),
Expand All @@ -10,8 +10,8 @@
children: childrenProp,
...restProps
}: WithoutChild<FormPrimitive.FieldErrorsProps> & {
errorClasses?: string | undefined | null;
} = $props();
errorClasses?: string | undefined | null
} = $props()
</script>

<FormPrimitive.FieldErrors
Expand Down
15 changes: 8 additions & 7 deletions packages/ui/src/components/ui/form/form-fieldset.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<script lang="ts" module>
import type { FormPath as _FormPath } from "sveltekit-superforms";
type T = Record<string, unknown>;
type U = _FormPath<T>;
import type { FormPath as _FormPath } from "sveltekit-superforms"
type T = Record<string, unknown>
type U = _FormPath<T>
</script>
<script lang="ts" generics="T extends Record<string, unknown>, U extends _FormPath<T>">
import * as FormPrimitive from "formsnap";
import type { WithoutChild } from "bits-ui";
import { cn } from "@kksh/ui/utils";
import { cn } from "@kksh/ui/utils"
import type { WithoutChild } from "bits-ui"
import * as FormPrimitive from "formsnap"
let {
ref = $bindable(null),
class: className,
form,
name,
...restProps
}: WithoutChild<FormPrimitive.FieldsetProps<T, U>> = $props();
}: WithoutChild<FormPrimitive.FieldsetProps<T, U>> = $props()
</script>

<FormPrimitive.Fieldset bind:ref {form} {name} class={cn("space-y-2", className)} {...restProps} />
8 changes: 4 additions & 4 deletions packages/ui/src/components/ui/form/form-legend.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<script lang="ts">
import * as FormPrimitive from "formsnap";
import type { WithoutChild } from "bits-ui";
import { cn } from "@kksh/ui/utils";
import { cn } from "@kksh/ui/utils"
import type { WithoutChild } from "bits-ui"
import * as FormPrimitive from "formsnap"
let {
ref = $bindable(null),
class: className,
...restProps
}: WithoutChild<FormPrimitive.LegendProps> = $props();
}: WithoutChild<FormPrimitive.LegendProps> = $props()
</script>

<FormPrimitive.Legend
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/components/ui/label/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Root from "./label.svelte";
import Root from "./label.svelte"

export {
Root,
//
Root as Label,
};
Root as Label
}
10 changes: 3 additions & 7 deletions packages/ui/src/components/ui/label/label.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<script lang="ts">
import { Label as LabelPrimitive } from "bits-ui";
import { cn } from "@kksh/ui/utils";
import { cn } from "@kksh/ui/utils"
import { Label as LabelPrimitive } from "bits-ui"
let {
ref = $bindable(null),
class: className,
...restProps
}: LabelPrimitive.RootProps = $props();
let { ref = $bindable(null), class: className, ...restProps }: LabelPrimitive.RootProps = $props()
</script>

<LabelPrimitive.Root
Expand Down

0 comments on commit 9d38846

Please sign in to comment.