forked from r.muellner/velum-raycast
feat: group translation mappings and restore canonical terms
This commit is contained in:
43
src/lib/mapping-groups.ts
Normal file
43
src/lib/mapping-groups.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import type { PlaceholderMapping } from "../types";
|
||||
|
||||
export function mappingRoot(key: string, mapping: PlaceholderMapping): string {
|
||||
const initial = mapping[key];
|
||||
if (!initial) throw new Error(`Unbekannte Zuordnung: ${key}`);
|
||||
const seen = new Set<string>();
|
||||
let current = key;
|
||||
while (mapping[current].merged_into) {
|
||||
if (seen.has(current)) throw new Error(`Zyklische Zuordnung: ${key}`);
|
||||
seen.add(current);
|
||||
const target = mapping[current].merged_into!;
|
||||
if (!mapping[target] || mapping[target].type !== initial.type) {
|
||||
throw new Error(`Ungültiger Hauptbegriff für ${key}: ${target}`);
|
||||
}
|
||||
current = target;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
/** Reverse lookups use the group's main term, while the saved variants remain intact. */
|
||||
export function mappingForRestoration(mapping: PlaceholderMapping): PlaceholderMapping {
|
||||
return Object.fromEntries(Object.entries(mapping).map(([key, entry]) => [key, {
|
||||
type: entry.type,
|
||||
original: mapping[mappingRoot(key, mapping)].original,
|
||||
}]));
|
||||
}
|
||||
|
||||
export function mergeMappingGroups(mapping: PlaceholderMapping, source: string, target: string): PlaceholderMapping {
|
||||
const from = mappingRoot(source, mapping);
|
||||
const into = mappingRoot(target, mapping);
|
||||
if (from === into) throw new Error("Diese Zuordnungen sind bereits zusammengefasst.");
|
||||
if (mapping[from].type !== mapping[into].type) {
|
||||
throw new Error("Nur Zuordnungen derselben Kategorie können zusammengefasst werden.");
|
||||
}
|
||||
return Object.fromEntries(Object.entries(mapping).map(([key, entry]) => [key,
|
||||
mappingRoot(key, mapping) === from ? { ...entry, merged_into: into } : { ...entry },
|
||||
]));
|
||||
}
|
||||
|
||||
export function detachMappingGroup(mapping: PlaceholderMapping, key: string): PlaceholderMapping {
|
||||
if (!mapping[key]?.merged_into) throw new Error("Diese Zuordnung ist kein Verweis.");
|
||||
return { ...mapping, [key]: { ...mapping[key], merged_into: undefined } };
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import type { EntityType, MappingEntry, PlaceholderMapping } from "../types";
|
||||
import { whitelistKey } from "./project-whitelist";
|
||||
import type { ProjectWhitelistEntry } from "./project-whitelist";
|
||||
import { restoreOriginalCase } from "./restore-case";
|
||||
import { mappingForRestoration } from "./mapping-groups";
|
||||
|
||||
export { whitelistKey } from "./project-whitelist";
|
||||
export type { ProjectWhitelistEntry } from "./project-whitelist";
|
||||
@@ -415,19 +416,19 @@ export function prepareManualMappingEntry(current: PlaceholderMapping, earlier:
|
||||
}
|
||||
|
||||
/** Save a mapping correction as a new immutable project stand without rewriting project files. */
|
||||
export async function addProjectMappingEntry(projectPath: string,
|
||||
input: { original: string; type: string; placeholder?: string }, support = environment.supportPath): Promise<ProjectRun> {
|
||||
export async function updateProjectMapping(projectPath: string,
|
||||
change: (current: PlaceholderMapping, earlier: PlaceholderMapping[]) => PlaceholderMapping,
|
||||
support = environment.supportPath): Promise<ProjectRun> {
|
||||
const entries = await listRuns(projectPath, support);
|
||||
const latest = entries[0];
|
||||
if (!latest) throw new Error("Für dieses Projekt gibt es noch keinen gespeicherten Stand.");
|
||||
const { placeholder, entry } = prepareManualMappingEntry(latest.run.mapping,
|
||||
entries.slice(1).map((saved) => saved.run.mapping), input);
|
||||
const mapping = change(latest.run.mapping, entries.slice(1).map((saved) => saved.run.mapping));
|
||||
const created = await createRun(projectPath, support);
|
||||
const { run, directory } = created;
|
||||
run.createdAt = new Date(Math.max(Date.now(), Date.parse(latest.run.createdAt) + 1)).toISOString();
|
||||
run.origin = latest.run.origin;
|
||||
run.revisedFrom = latest.run.id;
|
||||
run.mapping = { ...latest.run.mapping, [placeholder]: entry };
|
||||
run.mapping = mapping;
|
||||
run.matchModes = { ...latest.run.matchModes };
|
||||
run.files = latest.run.files.map((file) => ({ ...file }));
|
||||
run.inPlace = latest.run.inPlace;
|
||||
@@ -454,6 +455,14 @@ export async function addProjectMappingEntry(projectPath: string,
|
||||
return run;
|
||||
}
|
||||
|
||||
export async function addProjectMappingEntry(projectPath: string,
|
||||
input: { original: string; type: string; placeholder?: string }, support = environment.supportPath): Promise<ProjectRun> {
|
||||
return updateProjectMapping(projectPath, (current, earlier) => {
|
||||
const { placeholder, entry } = prepareManualMappingEntry(current, earlier, input);
|
||||
return { ...current, [placeholder]: entry };
|
||||
}, support);
|
||||
}
|
||||
|
||||
export function runCopiesPath(run: ProjectRun, directory: string): string {
|
||||
return run.copyPath ?? join(directory, "files");
|
||||
}
|
||||
@@ -547,7 +556,7 @@ export async function mappingForMatchingSnapshots(relPath: string, currentHash:
|
||||
const matching = await Promise.all(earlier.filter(({ run }) =>
|
||||
run.files.some((file) => file.output === relPath))
|
||||
.map(async ({ run, directory }) => await matchesPreviousOutput(directory, relPath, currentHash)
|
||||
? run.mapping : undefined));
|
||||
? mappingForRestoration(run.mapping) : undefined));
|
||||
return withUnambiguousHistoricalMappings({},
|
||||
matching.filter((mapping): mapping is PlaceholderMapping => Boolean(mapping)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user