mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
A diagnostics event for one file made the Problems view merge, filter and sort every file's diagnostics again, and the tab header and tool window badge recounted every error in the workspace. Each file's merged list and Problems node are now cached by its sources map (replaced only when that file changes), and the store keeps project severity counts up to date per event, recomputing them on a profile change. Output is unchanged. Part of #3937 Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com>
237 lines
8.8 KiB
TypeScript
237 lines
8.8 KiB
TypeScript
/**
|
|
* Project-wide diagnostics for the Problems tool window, status bar and file
|
|
* decorations. The backend sends full replacements per (file, producer), so
|
|
* the store keeps one list per source and merges them per file.
|
|
*/
|
|
import { create } from "zustand";
|
|
import type { Diagnostic, Severity } from "@/bindings";
|
|
import { onIdeEvent } from "@/platform/ipc";
|
|
import { onLocalDiagnostics } from "@/platform/localDiagnostics";
|
|
import {
|
|
applyProfile,
|
|
inspectionProfile,
|
|
subscribeInspectionProfile,
|
|
type ProfileOverrides,
|
|
} from "@/platform/inspections";
|
|
|
|
export type FileDiagnostics = ReadonlyMap<string, readonly Diagnostic[]>;
|
|
/** path → source → diagnostics. */
|
|
export type ProblemsMap = ReadonlyMap<string, FileDiagnostics>;
|
|
|
|
export interface SeverityCounts {
|
|
error: number;
|
|
warning: number;
|
|
info: number;
|
|
hint: number;
|
|
}
|
|
|
|
/** Replaces `source`'s diagnostics for `path`; returns the same map when nothing changed. */
|
|
export function mergeDiagnostics(
|
|
map: ProblemsMap,
|
|
path: string,
|
|
source: string,
|
|
items: readonly Diagnostic[],
|
|
): ProblemsMap {
|
|
const prev = map.get(path);
|
|
if (!items.length && !prev?.has(source)) return map;
|
|
const next = new Map(map);
|
|
const sources = new Map(prev ?? []);
|
|
if (items.length)
|
|
sources.set(
|
|
source,
|
|
items.map((d) => (d.source ? d : { ...d, source })),
|
|
);
|
|
else sources.delete(source);
|
|
if (sources.size) next.set(path, sources);
|
|
else next.delete(path);
|
|
return next;
|
|
}
|
|
|
|
const SEVERITY_ORDER: Record<Severity, number> = { error: 0, warning: 1, info: 2, hint: 3 };
|
|
|
|
// A file's sources map is replaced whenever its diagnostics change, so it keys the merged list.
|
|
const merged = new WeakMap<FileDiagnostics, readonly Diagnostic[]>();
|
|
|
|
/** All diagnostics of a file across sources, ordered by position then severity. */
|
|
export function fileDiagnostics(map: ProblemsMap, path: string): readonly Diagnostic[] {
|
|
const sources = map.get(path);
|
|
if (!sources) return [];
|
|
let items = merged.get(sources);
|
|
if (!items) {
|
|
items = [...sources.values()]
|
|
.flat()
|
|
.sort(
|
|
(a, b) =>
|
|
a.range.start.line - b.range.start.line ||
|
|
a.range.start.col - b.range.start.col ||
|
|
SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity],
|
|
);
|
|
merged.set(sources, items);
|
|
}
|
|
return items;
|
|
}
|
|
|
|
/** The producer (inspection) that reported `d`, one of `path`'s problems in `map`. */
|
|
/**
|
|
* The file the Problems "File" tab shows: the active text editor's, or the active tab's
|
|
* own path when it is another editor kind (a notebook keeps its cells' diagnostics there).
|
|
*/
|
|
export function currentProblemsPath(
|
|
textPath: string | null,
|
|
activeInput: { kind: string; path?: string | null } | null,
|
|
): string | null {
|
|
if (activeInput && activeInput.kind !== "file" && activeInput.kind !== "virtual" && activeInput.path)
|
|
return activeInput.path;
|
|
return textPath;
|
|
}
|
|
|
|
const CELL_SCHEME = "vscode-notebook-cell:";
|
|
|
|
/** The notebook a cell's diagnostics key (`vscode-notebook-cell:<notebook path>#<cell>`) belongs to. */
|
|
function cellNotebook(key: string): string | null {
|
|
if (!key.startsWith(CELL_SCHEME)) return null;
|
|
const rest = key.slice(CELL_SCHEME.length);
|
|
const hash = rest.indexOf("#");
|
|
const path = decodeURIComponent(hash < 0 ? rest : rest.slice(0, hash));
|
|
return /^\/[A-Za-z]:\//.test(path) ? path.slice(1) : path;
|
|
}
|
|
|
|
/** The diagnostics keys of one file: the file itself and, for a notebook, its cells. */
|
|
export function filePaths(map: ProblemsMap, path: string): string[] {
|
|
const out = map.has(path) ? [path] : [];
|
|
for (const key of map.keys()) if (cellNotebook(key) === path) out.push(key);
|
|
return out;
|
|
}
|
|
|
|
export function producerFor(map: ProblemsMap, path: string, d: Diagnostic): string | null {
|
|
for (const [source, items] of map.get(path) ?? []) if (items.includes(d)) return source;
|
|
return null;
|
|
}
|
|
|
|
export interface ProblemOrder {
|
|
/** Errors before warnings before infos (IDEA's default). */
|
|
bySeverity: boolean;
|
|
/** By message text, then position; otherwise by position, then text. */
|
|
byName: boolean;
|
|
}
|
|
|
|
export const DEFAULT_PROBLEM_ORDER: ProblemOrder = { bySeverity: true, byName: false };
|
|
|
|
const natural = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
|
|
|
/** A file's problems in the Problems view order (Sort by Severity / Name). */
|
|
export function sortProblems(items: readonly Diagnostic[], order: ProblemOrder): Diagnostic[] {
|
|
const position = (a: Diagnostic, b: Diagnostic) =>
|
|
a.range.start.line - b.range.start.line || a.range.start.col - b.range.start.col;
|
|
return [...items].sort(
|
|
(a, b) =>
|
|
(order.bySeverity ? SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity] : 0) ||
|
|
(order.byName
|
|
? natural.compare(a.message, b.message) || position(a, b)
|
|
: position(a, b) || natural.compare(a.message, b.message)),
|
|
);
|
|
}
|
|
|
|
export function countSeverities(diags: Iterable<Diagnostic>): SeverityCounts {
|
|
const c: SeverityCounts = { error: 0, warning: 0, info: 0, hint: 0 };
|
|
for (const d of diags) c[d.severity]++;
|
|
return c;
|
|
}
|
|
|
|
export function projectCounts(map: ProblemsMap): SeverityCounts {
|
|
const c: SeverityCounts = { error: 0, warning: 0, info: 0, hint: 0 };
|
|
for (const sources of map.values()) for (const items of sources.values()) for (const d of items) c[d.severity]++;
|
|
return c;
|
|
}
|
|
|
|
export function worstSeverity(diags: Iterable<Diagnostic>): Severity | null {
|
|
let best: Severity | null = null;
|
|
for (const d of diags) if (best === null || SEVERITY_ORDER[d.severity] < SEVERITY_ORDER[best]) best = d.severity;
|
|
return best;
|
|
}
|
|
|
|
/** `raw` under the inspection profile: disabled inspections dropped, severities remapped. */
|
|
export function profiledProblems(raw: ProblemsMap, profile: ProfileOverrides): ProblemsMap {
|
|
let out: ProblemsMap = new Map();
|
|
for (const [path, sources] of raw) {
|
|
for (const [source, items] of sources) {
|
|
out = mergeDiagnostics(out, path, source, applyProfile(profile, source, items));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
interface ProblemsState {
|
|
/** As reported, before the inspection profile. */
|
|
raw: ProblemsMap;
|
|
/** What the views show. */
|
|
map: ProblemsMap;
|
|
/** `projectCounts(map)`, kept up to date per event. */
|
|
counts: SeverityCounts;
|
|
}
|
|
|
|
const NO_COUNTS: SeverityCounts = { error: 0, warning: 0, info: 0, hint: 0 };
|
|
|
|
export const useProblems = create<ProblemsState>(() => ({ raw: new Map(), map: new Map(), counts: NO_COUNTS }));
|
|
|
|
function recount(counts: SeverityCounts, removed: readonly Diagnostic[], added: readonly Diagnostic[]) {
|
|
const c = { ...counts };
|
|
for (const d of removed) c[d.severity]--;
|
|
for (const d of added) c[d.severity]++;
|
|
return c;
|
|
}
|
|
|
|
export function applyDiagnosticsEvent(path: string, source: string, items: readonly Diagnostic[]) {
|
|
useProblems.setState((s) => {
|
|
const raw = mergeDiagnostics(s.raw, path, source, items);
|
|
if (raw === s.raw) return s;
|
|
const map = mergeDiagnostics(s.map, path, source, applyProfile(inspectionProfile(), source, items));
|
|
if (map === s.map) return { raw };
|
|
const before = s.map.get(path)?.get(source) ?? [];
|
|
return { raw, map, counts: recount(s.counts, before, map.get(path)?.get(source) ?? []) };
|
|
});
|
|
}
|
|
|
|
export function clearProblems() {
|
|
useProblems.setState({ raw: new Map(), map: new Map(), counts: NO_COUNTS });
|
|
}
|
|
|
|
export function installProblemsEvents(): () => void {
|
|
const off = onIdeEvent("diagnostics", (e) => applyDiagnosticsEvent(e.path, e.source, e.items));
|
|
// Typo, file references, SQL: inspections the editor runs itself.
|
|
const offLocal = onLocalDiagnostics(applyDiagnosticsEvent);
|
|
const offProfile = subscribeInspectionProfile(() =>
|
|
useProblems.setState((s) => {
|
|
const map = profiledProblems(s.raw, inspectionProfile());
|
|
return { map, counts: projectCounts(map) };
|
|
}),
|
|
);
|
|
return () => {
|
|
off();
|
|
offLocal();
|
|
offProfile();
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Errors and warnings as `rel:line:col: severity: message (source)` lines, errors first, at most
|
|
* `max`; null when there are none. For an agent's context.
|
|
*/
|
|
export function problemsReport(map: ProblemsMap, root: string, max = 60): string | null {
|
|
const rows: { severity: Severity; text: string }[] = [];
|
|
for (const path of map.keys()) {
|
|
const rel = path.startsWith(`${root}/`) ? path.slice(root.length + 1) : path;
|
|
for (const d of fileDiagnostics(map, path)) {
|
|
if (d.severity !== "error" && d.severity !== "warning") continue;
|
|
const at = `${rel}:${d.range.start.line + 1}:${d.range.start.col + 1}`;
|
|
const source = d.source ? ` (${d.source})` : "";
|
|
rows.push({ severity: d.severity, text: `${at}: ${d.severity}: ${d.message.split("\n")[0]}${source}` });
|
|
}
|
|
}
|
|
if (!rows.length) return null;
|
|
rows.sort((a, b) => Number(a.severity !== "error") - Number(b.severity !== "error"));
|
|
const shown = rows.slice(0, max).map((r) => r.text);
|
|
if (rows.length > max) shown.push(`… ${rows.length - max} more`);
|
|
return shown.join("\n");
|
|
}
|