дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
About 270 UI strings were JS template literals with interpolation passed to exact catalog lookup, so they could never be translated; about 25 more spliced lowercase English fragments into sentences.
`tx` now also works as a template tag (`` tx`Remove data source '${name}'?` ``): it looks up the entry by the literal parts (`{name}` placeholders), translates interpolated values that have entries, and remembers generated text so it re-translates after a language switch or when the catalog loads later. Call sites use the tag; spliced fragments are written as whole sentences per variant. Adds 280 Chinese translations and removes stale ones.
The catalog guard now also fails on untagged template literals reaching exact lookup (unless allowlisted with a reason), tagged text missing from the catalog, case-changed or ternary-suffix interpolations, and duplicate template patterns.
Claude-Session: https://claude.ai/code/session_01VUYsdXA2nkF5v6zsC93dpn
173 рядки
5.3 KiB
TypeScript
173 рядки
5.3 KiB
TypeScript
/**
|
|
* Search / Replace Structurally. Search results go to the Find Usages tool
|
|
* window (`find.showUsageResults`); replacements are previewed and applied as
|
|
* one workspace edit.
|
|
*/
|
|
import { Channel } from "@tauri-apps/api/core";
|
|
import { activeEditor } from "@/editor/api";
|
|
import { executeAction } from "@/platform/actions";
|
|
import {
|
|
call,
|
|
commands,
|
|
isTauri,
|
|
nextRequestId,
|
|
type Location,
|
|
type RequestId,
|
|
type StructuralEvent,
|
|
type StructuralFile,
|
|
type StructuralQuery,
|
|
type StructuralSummary,
|
|
type WorkspaceEdit,
|
|
} from "@/platform/ipc";
|
|
import { notifications } from "@/workbench/contributions";
|
|
import { resolveScope, type ScopeChoice } from "./scope";
|
|
import { tx } from "@/platform/i18n";
|
|
|
|
const STATE_KEY = "openidea.analysis.structural.v1";
|
|
|
|
export interface StructuralForm {
|
|
pattern: string;
|
|
replacement: string;
|
|
language: string;
|
|
}
|
|
|
|
export function loadStructuralForm(): StructuralForm {
|
|
const language = activeEditor()?.ctx.language ?? "";
|
|
try {
|
|
const raw = globalThis.localStorage?.getItem(STATE_KEY);
|
|
if (raw) {
|
|
const saved = JSON.parse(raw) as Partial<StructuralForm>;
|
|
return {
|
|
pattern: saved.pattern ?? "",
|
|
replacement: saved.replacement ?? "",
|
|
language: language || saved.language || "",
|
|
};
|
|
}
|
|
} catch {
|
|
// storage unavailable
|
|
}
|
|
return { pattern: "", replacement: "", language };
|
|
}
|
|
|
|
export function saveStructuralForm(form: StructuralForm) {
|
|
try {
|
|
globalThis.localStorage?.setItem(STATE_KEY, JSON.stringify(form));
|
|
} catch {
|
|
// storage unavailable
|
|
}
|
|
}
|
|
|
|
/** Key of one match, for including / excluding it in the replace preview. */
|
|
export const matchKey = (path: string, i: number) => `${path}\u0000${i}`;
|
|
|
|
/** Locations of all matches (for the Find Usages view). */
|
|
export function matchLocations(files: readonly StructuralFile[]): Location[] {
|
|
return files.flatMap((f) => f.matches.map((m) => ({ path: f.path, range: m.range })));
|
|
}
|
|
|
|
/** One text edit per included match with a replacement. */
|
|
export function buildReplaceEdit(files: readonly StructuralFile[], excluded: ReadonlySet<string>): WorkspaceEdit {
|
|
const out: WorkspaceEdit = { files: [] };
|
|
for (const f of files) {
|
|
const edits = f.matches
|
|
.map((m, i) => ({ m, i }))
|
|
.filter(({ m, i }) => m.replacement != null && !excluded.has(matchKey(f.path, i)))
|
|
.map(({ m }) => ({ range: m.range, new_text: m.replacement! }));
|
|
if (edits.length) out.files.push({ kind: "edit", path: f.path, edits });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
let inflight: RequestId | null = null;
|
|
|
|
export function cancelStructural() {
|
|
if (inflight !== null) void commands.cancelRequest(inflight).catch(() => undefined);
|
|
}
|
|
|
|
/** Runs the search; resolves with the files with matches, or null when cancelled / failed. */
|
|
export async function searchStructurally(
|
|
query: StructuralQuery,
|
|
choice: ScopeChoice,
|
|
): Promise<{ files: StructuralFile[]; summary: StructuralSummary } | null> {
|
|
const scope = await resolveScope(choice);
|
|
if (typeof scope === "string") {
|
|
notifications().notify({ group: "analysis", level: "warning", title: "Search Structurally", body: scope });
|
|
return null;
|
|
}
|
|
if (!isTauri()) return null;
|
|
cancelStructural();
|
|
const id = nextRequestId();
|
|
inflight = id;
|
|
const files: StructuralFile[] = [];
|
|
let finish: (s: StructuralSummary | null) => void = () => {};
|
|
const finished = new Promise<StructuralSummary | null>((resolve) => (finish = resolve));
|
|
const channel = new Channel<StructuralEvent>();
|
|
channel.onmessage = (ev) => {
|
|
if (ev.type === "file") files.push(ev.file);
|
|
else finish(ev.summary);
|
|
};
|
|
let done: StructuralSummary | null;
|
|
try {
|
|
await call(commands.structuralSearch(id, scope, query, channel));
|
|
// Channel messages may trail the command's result slightly.
|
|
setTimeout(() => finish(null), 2000);
|
|
done = await finished;
|
|
} catch (e) {
|
|
notifications().notify({
|
|
group: "analysis",
|
|
level: "error",
|
|
title: "Search Structurally",
|
|
body: e instanceof Error ? e.message : String(e),
|
|
});
|
|
return null;
|
|
} finally {
|
|
if (inflight === id) inflight = null;
|
|
}
|
|
if (!done || done.cancelled) return null;
|
|
files.sort((a, b) => a.relPath.localeCompare(b.relPath));
|
|
return { files, summary: done };
|
|
}
|
|
|
|
/** Shows matches in the Find Usages tool window. */
|
|
export async function showStructuralResults(
|
|
pattern: string,
|
|
files: readonly StructuralFile[],
|
|
summary: StructuralSummary,
|
|
) {
|
|
if (!files.length) {
|
|
notifications().notify({
|
|
group: "analysis",
|
|
level: "info",
|
|
title: "Search Structurally",
|
|
body: "No occurrences found",
|
|
});
|
|
return;
|
|
}
|
|
const label = pattern.replace(/\s+/g, " ").trim();
|
|
await executeAction("find.showUsageResults", {
|
|
symbol: label.length > 60 ? `${label.slice(0, 59)}…` : label,
|
|
locations: matchLocations(files),
|
|
});
|
|
if (summary.limitHit) {
|
|
notifications().notify({
|
|
group: "analysis",
|
|
level: "warning",
|
|
title: "Search Structurally",
|
|
body: tx`Stopped after ${summary.matches} matches`,
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function applyStructuralReplace(edit: WorkspaceEdit): Promise<void> {
|
|
try {
|
|
await call(commands.applyWorkspaceEdit(edit));
|
|
} catch (e) {
|
|
notifications().notify({
|
|
group: "analysis",
|
|
level: "error",
|
|
title: "Replace Structurally",
|
|
body: e instanceof Error ? e.message : String(e),
|
|
});
|
|
}
|
|
}
|