Skip to content

Commit 8f98983

Browse files
Merge pull request #3674 from GauBen/feat/sherlock-nanoid
feat(sherlock): add a nanoid generator for the key extractor
2 parents 6e59814 + 13cc390 commit 8f98983

File tree

8 files changed

+722
-3329
lines changed

8 files changed

+722
-3329
lines changed

.changeset/gentle-sites-act.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"vs-code-extension": minor
3+
---
4+
5+
https://github.com/opral/monorepo/pull/3674
6+
7+
adds a new sherlock.extract.generator config key, and deprecates sherlock.extract.autoHumanId.enabled

inlang/packages/sherlock/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,27 @@
210210
"sherlock.extract": {
211211
"type": "object",
212212
"properties": {
213+
"generator": {
214+
"type": "string",
215+
"description": "The generator to use for the extracted message IDs. Default is `humanId`.",
216+
"anyOf": [
217+
{
218+
"const": "humanId",
219+
"description": "A human-readable id, like `direct_legal_giraffe_empower`."
220+
},
221+
{
222+
"const": "nanoid",
223+
"description": "A short, high-entropy id, like `vOpwTYdaKl`."
224+
},
225+
{
226+
"const": "none",
227+
"description": "The field is left empty, for you to fill in."
228+
}
229+
]
230+
},
213231
"autoHumanId": {
214232
"type": "object",
233+
"deprecated": true,
215234
"properties": {
216235
"enabled": {
217236
"type": "boolean",
@@ -266,6 +285,7 @@
266285
"kysely": "^0.27.4",
267286
"lit-html": "^3.1.2",
268287
"lodash-es": "^4.17.21",
288+
"nanoid": "^5.1.5",
269289
"prettier": "^3.3.3",
270290
"require-from-string": "^2.0.2",
271291
"throttle-debounce": "^5.0.0",

inlang/packages/sherlock/src/commands/extractMessage.test.ts

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { CONFIGURATION } from "../configuration.js"
66
import { getSetting } from "../utilities/settings/index.js"
77
import { state } from "../utilities/state.js"
88
import { upsertBundleNested } from "@inlang/sdk"
9-
import { capture } from "../services/telemetry/index.js"
109

1110
// Mocking the necessary modules
1211
vi.mock("../utilities/state", () => ({
@@ -60,12 +59,18 @@ vi.mock("../utilities/settings/index.js", () => ({
6059
}))
6160

6261
vi.mock("@inlang/sdk", () => ({
63-
humanId: vi.fn().mockReturnValue("generatedId123"),
64-
createBundle: vi.fn().mockReturnValue({ id: "generatedId123", alias: "alias123" }),
65-
createMessage: vi.fn().mockReturnValue({ id: "messageId123", bundleId: "generatedId123" }),
62+
humanId: vi.fn().mockReturnValue("generated_human_id"),
63+
createBundle: vi.fn().mockReturnValue({ id: "bundleId123", alias: "alias123" }),
64+
createMessage: vi
65+
.fn()
66+
.mockReturnValue({ id: "messageId123", bundleId: "bundleId123bundleId123" }),
6667
upsertBundleNested: vi.fn(),
6768
}))
6869

70+
vi.mock("nanoid", () => ({
71+
customAlphabet: vi.fn().mockReturnValue(() => "nanoidnano"),
72+
}))
73+
6974
vi.mock("../utilities/messages/isQuoted", () => ({
7075
isQuoted: vi.fn(),
7176
stripQuotes: vi.fn(),
@@ -503,4 +508,87 @@ describe("extractMessageCommand", () => {
503508
expect(CONFIGURATION.EVENTS.ON_DID_EXTRACT_MESSAGE.fire).toHaveBeenCalled()
504509
expect(msg).toHaveBeenCalledWith("Message extracted.")
505510
})
511+
512+
it("should use the right generator if configured", async () => {
513+
vi.mocked(state).mockReturnValue({
514+
project: {
515+
plugins: {
516+
get: async () => [
517+
{
518+
key: "plugin1",
519+
meta: {
520+
"app.inlang.ideExtension": {
521+
extractMessageOptions: [
522+
{
523+
callback: vi.fn(() => ({
524+
bundleId: "generatedId123",
525+
messageReplacement: "Replacement Text",
526+
})),
527+
},
528+
],
529+
},
530+
},
531+
},
532+
],
533+
},
534+
// @ts-expect-error
535+
settings: {
536+
get: async () => ({ baseLocale: "en", locales: ["en"] }),
537+
},
538+
db: {
539+
// @ts-expect-error
540+
transaction: () => ({
541+
execute: vi.fn().mockResolvedValueOnce(true),
542+
}),
543+
},
544+
},
545+
})
546+
547+
const mockTextEditor = {
548+
selection: {
549+
isEmpty: false,
550+
start: {
551+
line: 1,
552+
character: 1,
553+
},
554+
end: {
555+
line: 1,
556+
character: 20,
557+
},
558+
},
559+
document: {
560+
getText: () => "Some text",
561+
},
562+
edit: vi.fn(),
563+
}
564+
565+
for (const [generator, expected] of [
566+
["humanId", "generated_human_id"],
567+
["nanoid", "nanoidnano"],
568+
["none", ""],
569+
]) {
570+
vi.mocked(getSetting).mockResolvedValue(generator)
571+
vi.mocked(window.showInputBox).mockResolvedValueOnce("generatedId123")
572+
// @ts-expect-error
573+
vi.mocked(window.showQuickPick).mockResolvedValueOnce("Replacement Text")
574+
575+
// @ts-expect-error
576+
await extractMessageCommand.callback(mockTextEditor)
577+
578+
expect(window.showInputBox).toHaveBeenCalledWith(
579+
expect.objectContaining({
580+
value: expected,
581+
})
582+
)
583+
584+
expect(mockTextEditor.edit).toHaveBeenCalled()
585+
expect(CONFIGURATION.EVENTS.ON_DID_EXTRACT_MESSAGE.fire).toHaveBeenCalled()
586+
expect(msg).toHaveBeenCalledWith("Message extracted.")
587+
588+
vi.mocked(window.showInputBox).mockReset()
589+
vi.mocked(mockTextEditor.edit).mockReset()
590+
vi.mocked(CONFIGURATION.EVENTS.ON_DID_EXTRACT_MESSAGE.fire).mockReset()
591+
vi.mocked(msg).mockReset()
592+
}
593+
})
506594
})

inlang/packages/sherlock/src/commands/extractMessage.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import {
1313
} from "@inlang/sdk"
1414
import { v4 as uuidv4 } from "uuid"
1515
import { saveProject } from "../main.js"
16+
import { customAlphabet } from "nanoid"
17+
18+
/**
19+
* Generators used to populate the message key field.
20+
*/
21+
const generators = {
22+
humanId,
23+
nanoid: customAlphabet("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 8),
24+
none: () => "",
25+
}
1626

1727
/**
1828
* Helps the user to extract messages from the active text editor.
@@ -63,13 +73,29 @@ export const extractMessageCommand = {
6373
)
6474
}
6575

66-
const autoHumanId = await getSetting("extract.autoHumanId.enabled").catch(() => true)
76+
// Favor the modern option
77+
let generator = await getSetting("extract.generator").catch(() => undefined)
78+
79+
// If undefined or empty, use the deprecated option
80+
if (!generator) {
81+
generator = (await getSetting("extract.autoHumanId.enabled").catch(() => true))
82+
? "humanId"
83+
: "none"
84+
}
85+
86+
const isKnownGenerator = Object.hasOwn(generators, generator)
87+
const generatedValue = isKnownGenerator
88+
? generators[generator as keyof typeof generators]()
89+
: ""
90+
const showRandomNamesTip = generator !== "none"
91+
6792
const bundleId = await window.showInputBox({
6893
title: "Enter the ID:",
69-
value: autoHumanId ? humanId() : "",
70-
prompt:
71-
autoHumanId &&
72-
"Tip: It's best practice to use random names for your messages. Read this [guide](https://inlang.com/documentation/concept/message#idhuman-readable) for more information.",
94+
value: generatedValue,
95+
// Show the random-names tip only when a random generator is active
96+
prompt: showRandomNamesTip
97+
? "Tip: It's best practice to use random names for your messages. Read this [guide](https://inlang.com/documentation/concept/message#idhuman-readable) for more information."
98+
: undefined,
7399
})
74100

75101
if (bundleId === undefined) {

inlang/packages/sherlock/src/utilities/settings/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const settingsProperty = [
1313
"editorColors.error.foreground",
1414
"editorColors.error.background",
1515
"editorColors.error.border",
16-
"extract.autoHumanId.enabled",
16+
"extract.generator",
17+
"extract.autoHumanId.enabled", // DEPRECATED, TODO: remove in the next major
1718
"inlineAnnotations.enabled",
1819
"appRecommendations.ninja.enabled",
1920
] as const

packages/lix-plugin-md/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"remark-stringify": "^11.0.0",
3131
"remark-gfm": "^3.0.1",
3232
"remark-frontmatter": "^4.0.1",
33-
"unified": "11.0.3",
33+
"unified": "^11.0.5",
3434
"unist-util-visit": "5.0.0"
3535
},
3636
"devDependencies": {

packages/markdown-wc/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
"rehype-rewrite": "3.0.6",
3434
"rehype-sanitize": "6.0.0",
3535
"rehype-slug": "6.0.0",
36-
"rehype-stringify": "10.0.0",
36+
"rehype-stringify": "^10.0.1",
3737
"remark-frontmatter": "^5.0.0",
3838
"remark-gfm": "3.0.1",
3939
"remark-parse": "10.0.2",
4040
"remark-rehype": "10.1.0",
41-
"unified": "11.0.3",
41+
"unified": "^11.0.5",
4242
"unist-util-visit": "5.0.0",
4343
"yaml": "^2.1.3"
4444
},

0 commit comments

Comments
 (0)