Skip to content

Commit 0bfdc29

Browse files
committed
Make tag autocomplete work better
1 parent e937795 commit 0bfdc29

6 files changed

Lines changed: 54 additions & 18 deletions

File tree

backend/src/plugin/core/public/command.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ interface FlagOption extends BaseOption {
140140
position?: undefined;
141141
}
142142

143+
export const MAX_AUTOCOMPLETE_CHOICES = 25;
144+
143145
interface StringOption extends BaseOption {
144146
type: OptionType.String;
145147
minLength?: number;

backend/src/plugin/tags/command/tag.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
44
import { permissionsGuard } from "#plugin/core/public/helper/commandGuards.ts";
55
import { icons } from "#plugin/core/public/icons.ts";
66
import { MAX_TAG_NAME_LENGTH } from "#plugin/tags/constants.ts";
7+
import { autocompleteTags } from "#plugin/tags/helper/command.ts";
78
import { tagsConfigStore } from "#plugin/tags/index.ts";
8-
import { getTag, searchTags } from "#plugin/tags/storage/tags.ts";
9+
import { getTag } from "#plugin/tags/storage/tags.ts";
910

1011
export default defineCommand({
1112
name: ["tag", "tagsend", "sendtag"],
@@ -18,8 +19,7 @@ export default defineCommand({
1819
position: 0,
1920
maxLength: MAX_TAG_NAME_LENGTH,
2021

21-
autocomplete: (ctx, value) =>
22-
searchTags(ctx.squirrelCtx.db, ctx.guild.id, value),
22+
autocomplete: (ctx, value) => autocompleteTags(ctx, value),
2323
},
2424
},
2525

backend/src/plugin/tags/command/tagDelete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
44
import { permissionsGuard } from "#plugin/core/public/helper/commandGuards.ts";
55
import { icons } from "#plugin/core/public/icons.ts";
66
import { MAX_TAG_NAME_LENGTH } from "#plugin/tags/constants.ts";
7+
import { autocompleteTags } from "#plugin/tags/helper/command.ts";
78
import { tagsConfigStore } from "#plugin/tags/index.ts";
8-
import { deleteTag, searchTags } from "#plugin/tags/storage/tags.ts";
9+
import { deleteTag } from "#plugin/tags/storage/tags.ts";
910

1011
export default defineCommand({
1112
name: ["tagdelete", "deletetag", "tagdel", "deltag", "tagrm", "rmtag"],
@@ -19,8 +20,7 @@ export default defineCommand({
1920
position: 0,
2021
maxLength: MAX_TAG_NAME_LENGTH,
2122

22-
autocomplete: (ctx, value) =>
23-
searchTags(ctx.squirrelCtx.db, ctx.guild.id, value),
23+
autocomplete: (ctx, value) => autocompleteTags(ctx, value),
2424
},
2525
},
2626

backend/src/plugin/tags/command/tagSet.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ import {
77
MAX_TAG_CONTENT_LENGTH,
88
MAX_TAG_NAME_LENGTH,
99
} from "#plugin/tags/constants.ts";
10+
import { autocompleteTags } from "#plugin/tags/helper/command.ts";
1011
import { tagsConfigStore } from "#plugin/tags/index.ts";
11-
import {
12-
createTag,
13-
searchTags,
14-
updateTag,
15-
upsertTag,
16-
} from "#plugin/tags/storage/tags.ts";
12+
import { createTag, updateTag, upsertTag } from "#plugin/tags/storage/tags.ts";
1713

1814
export default defineCommand({
1915
name: ["tagset", "settag"],
@@ -30,8 +26,22 @@ export default defineCommand({
3026
maxLength: MAX_TAG_NAME_LENGTH,
3127
greedy: false,
3228

33-
autocomplete: (ctx, value) =>
34-
searchTags(ctx.squirrelCtx.db, ctx.guild.id, value),
29+
async autocomplete(ctx, value) {
30+
const names = await autocompleteTags(ctx, value);
31+
32+
// add the current value so pressing tab won't rudely correct it to something else
33+
if (value.length !== 0) {
34+
const valueIndex = names.indexOf(value);
35+
36+
if (valueIndex !== -1) {
37+
names.splice(names.indexOf(value), 1);
38+
}
39+
40+
names.unshift(value);
41+
}
42+
43+
return names;
44+
},
3545
},
3646
content: {
3747
type: OptionType.String,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {
2+
MAX_AUTOCOMPLETE_CHOICES,
3+
type AutocompleteContext,
4+
} from "#plugin/core/public/command.ts";
5+
import { searchTagNames } from "#plugin/tags/storage/tags.ts";
6+
7+
export async function autocompleteTags(
8+
ctx: AutocompleteContext,
9+
value: string,
10+
): Promise<string[]> {
11+
return await searchTagNames(ctx.squirrelCtx.db, ctx.guild.id, {
12+
name: value,
13+
limit: MAX_AUTOCOMPLETE_CHOICES,
14+
});
15+
}

backend/src/plugin/tags/storage/tags.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export type Tag = z.output<typeof Tag>;
1414
const JustInserted = z.strictObject({ inserted: z.boolean() });
1515
const JustNameArray = z.strictObject({ name: z.string() }).array();
1616

17+
export interface TagQuery {
18+
name: string;
19+
20+
limit: number;
21+
}
22+
1723
export async function getTag(
1824
db: Pool,
1925
guildID: string,
@@ -34,17 +40,20 @@ export async function getTag(
3440
return dbParse(Tag, result.rows[0]);
3541
}
3642

37-
export async function searchTags(
43+
export async function searchTagNames(
3844
db: Pool,
3945
guildID: string,
40-
name: string,
46+
query: TagQuery,
4147
): Promise<string[]> {
48+
// FIXME: use casefold
4249
const result = await db.query(
4350
`
4451
SELECT "name" FROM "tags_tags"
45-
WHERE "guildID" = $1 AND position($2 in "name") > 0
52+
WHERE "guildID" = $1 AND position(lower($2) in lower("name")) > 0
53+
ORDER BY position(lower($2) in lower("name")), "name" ASC
54+
LIMIT $3
4655
`,
47-
[guildID, name],
56+
[guildID, query.name, query.limit],
4857
);
4958

5059
return dbParse(JustNameArray, result.rows).map(({ name }) => name);

0 commit comments

Comments
 (0)