badness 0.13.0

A language server, formatter, and linter for LaTeX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Tests for the cross-file inclusion graph (`project/graph.rs`) over the salsa
//! firewall (`incremental.rs`): that the per-file `include_edges` query backdates
//! so a body edit doesn't rebuild `project_graph`, that an edge change *does*
//! rebuild it, and that re-interning an unchanged membership reuses the memo.
//!
//! The unit tests of the pure
//! extraction and graph algorithm live in `src/project/`.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use badness::file_discovery::FileKind;
use badness::incremental::{IncrementalDatabase, QueryKind, QueryLogEntry, SourceFile};
use badness::project::{
    IncludeKind, Project, ProjectMember, project_graph, resolved_citations, resolved_labels,
};

fn count_by_kind(entries: &[QueryLogEntry]) -> HashMap<QueryKind, usize> {
    let mut counts = HashMap::new();
    for entry in entries {
        *counts.entry(entry.kind).or_insert(0) += 1;
    }
    counts
}

/// The path a `SourceFile` is actually tracked under. `upsert_file` lexically
/// normalizes (absolutizes) the path, which is a no-op for the `/proj/...`
/// literals on Unix but prepends a drive prefix on Windows. Member paths and
/// assertions must use this stored form so the include-graph and resolution
/// lookups (keyed on that same normalized space) match on every platform.
fn fpath(db: &IncrementalDatabase, file: SourceFile) -> PathBuf {
    db.file_path(file).to_path_buf()
}

/// The raw (unsorted) `{main.tex, part.tex}` membership under `/proj`, in
/// insertion order. Callers that need a normalized key sort it themselves.
fn members(db: &IncrementalDatabase, main: SourceFile, part: SourceFile) -> Vec<ProjectMember> {
    vec![
        ProjectMember {
            file: main,
            path: fpath(db, main),
            kind: FileKind::Tex,
        },
        ProjectMember {
            file: part,
            path: fpath(db, part),
            kind: FileKind::Tex,
        },
    ]
}

/// Intern the membership `{main.tex, part.tex}` under `/proj`. Re-interns from a
/// fresh (sorted) snapshot on each call, as a real consumer would — so the
/// interned `Project` borrow never spans a `&mut db` write.
fn project_main_part<'db>(
    db: &'db IncrementalDatabase,
    main: SourceFile,
    part: SourceFile,
) -> Project<'db> {
    let mut members = vec![
        ProjectMember {
            file: main,
            path: fpath(db, main),
            kind: FileKind::Tex,
        },
        ProjectMember {
            file: part,
            path: fpath(db, part),
            kind: FileKind::Tex,
        },
    ];
    members.sort_by(|a, b| a.path.cmp(&b.path));
    Project::new(db, members)
}

/// Intern the membership `{main.tex, refs.bib}` under `/proj`, with `main` the
/// document root. Re-interns from a fresh sorted snapshot on each call, as a real
/// consumer would.
fn project_main_bib<'db>(
    db: &'db IncrementalDatabase,
    main: SourceFile,
    bib: SourceFile,
) -> Project<'db> {
    let mut members = vec![
        ProjectMember {
            file: main,
            path: fpath(db, main),
            kind: FileKind::Tex,
        },
        ProjectMember {
            file: bib,
            path: fpath(db, bib),
            kind: FileKind::Bib,
        },
    ];
    members.sort_by(|a, b| a.path.cmp(&b.path));
    Project::new(db, members)
}

fn main_bib(main_text: &str, bib_text: &str) -> (IncrementalDatabase, SourceFile, SourceFile) {
    let mut db = IncrementalDatabase::default();
    let main = db.upsert_file(Path::new("/proj/main.tex"), main_text.to_string());
    let bib = db.upsert_file(Path::new("/proj/refs.bib"), bib_text.to_string());
    (db, main, bib)
}

fn main_part(main_text: &str, part_text: &str) -> (IncrementalDatabase, SourceFile, SourceFile) {
    let mut db = IncrementalDatabase::default();
    let main = db.upsert_file(Path::new("/proj/main.tex"), main_text.to_string());
    let part = db.upsert_file(Path::new("/proj/part.tex"), part_text.to_string());
    (db, main, part)
}

#[test]
fn graph_resolves_an_input_edge() {
    let (db, main, part) = main_part("\\input{part}\n", "hello\n");
    let graph = project_graph(&db, project_main_part(&db, main, part));

    let out = graph.outgoing(&fpath(&db, main));
    assert_eq!(out.len(), 1);
    assert_eq!(out[0].to, fpath(&db, part));
    assert_eq!(out[0].kind, IncludeKind::Input);
    assert_eq!(graph.included_by(&fpath(&db, part)), &[fpath(&db, main)]);
    assert!(graph.unresolved().is_empty());
}

#[test]
fn body_edit_does_not_rebuild_graph() {
    // The firewall: editing part.tex's text changes its parse but not its
    // inclusion edges (it has none), so `include_edges` backdates and the
    // cross-file graph memo is reused.
    let (mut db, main, part) = main_part("\\input{part}\n", "hello\n");
    let _ = project_graph(&db, project_main_part(&db, main, part));

    db.clear_query_log();

    // Edit part's body only — still no include edges.
    db.set_file_text(part, "hello world\n");
    let _ = project_graph(&db, project_main_part(&db, main, part));

    let counts = count_by_kind(&db.query_log());
    // part re-parses and its edges are recomputed (the text changed)...
    assert_eq!(counts.get(&QueryKind::IncludeEdges), Some(&1));
    // ...but the edges are unchanged, so the graph memo is reused.
    assert_eq!(
        counts.get(&QueryKind::ProjectGraph),
        None,
        "project graph must not rebuild on a body edit"
    );
}

#[test]
fn edge_change_rebuilds_graph() {
    // The complement: adding an `\input` changes main's edges, so the graph
    // *must* rebuild (the firewall doesn't over-cache).
    let (mut db, main, part) = main_part("\\input{part}\n", "hello\n");
    let _ = project_graph(&db, project_main_part(&db, main, part));

    db.clear_query_log();

    db.set_file_text(main, "\\input{part}\n\\input{extra}\n");
    let graph = project_graph(&db, project_main_part(&db, main, part));

    let counts = count_by_kind(&db.query_log());
    assert_eq!(
        counts.get(&QueryKind::ProjectGraph),
        Some(&1),
        "project graph must rebuild when an edge changes"
    );
    // The new edge targets a non-member, so it lands in `unresolved`.
    assert_eq!(graph.unresolved().len(), 1);
    assert_eq!(graph.unresolved()[0].from, fpath(&db, main));
}

#[test]
fn resolved_labels_unions_across_the_include_graph() {
    // main.tex is the document root and `\input`s part.tex, which defines the
    // label `\ref`-ed from main — so the ref resolves cross-file.
    let (db, main, part) = main_part(
        "\\documentclass{article}\n\\input{part}\n\\ref{a}\n",
        "\\label{a}\n",
    );
    let resolved = resolved_labels(&db, project_main_part(&db, main, part));

    assert!(resolved.is_defined(&fpath(&db, main), "a"));
    assert!(!resolved.is_defined(&fpath(&db, main), "missing"));
    // `\input{part}` resolves to an analyzed member, and main is a document root.
    assert!(resolved.is_closed(&fpath(&db, main)));
    assert!(resolved.is_root_component(&fpath(&db, part)));
}

#[test]
fn prose_edit_does_not_rebuild_resolved_labels() {
    // The label/reference firewalls together: a prose edit to part.tex changes its
    // semantic model (so `file_labels` and `file_refs` both re-execute) but neither
    // its `\label`-name set nor its `\ref`-key set — so both backdate and the
    // cross-file `resolved_labels` memo is reused.
    let (mut db, main, part) = main_part(
        "\\documentclass{article}\n\\input{part}\n\\ref{a}\n",
        "\\label{a}\\ref{a}\n",
    );
    let _ = resolved_labels(&db, project_main_part(&db, main, part));

    db.clear_query_log();

    // The model changes (added prose) but the label set is still `{a}` and the ref
    // set is still `{a}`.
    db.set_file_text(part, "Prose.\n\\label{a}\\ref{a}\n");
    let _ = resolved_labels(&db, project_main_part(&db, main, part));

    let counts = count_by_kind(&db.query_log());
    // part's label and ref sets are recomputed (its model changed)...
    assert_eq!(counts.get(&QueryKind::FileLabels), Some(&1));
    assert_eq!(counts.get(&QueryKind::FileRefs), Some(&1));
    // ...but both sets are unchanged, so the resolution memo is reused.
    assert_eq!(
        counts.get(&QueryKind::ResolvedLabels),
        None,
        "resolved labels must not rebuild when neither the label nor ref set changed"
    );
}

#[test]
fn ref_change_rebuilds_resolved_labels() {
    // The reference firewall's complement: adding a `\ref` changes part's ref set,
    // so the cross-file resolution *must* rebuild — `unreferenced-label` depends on
    // the cross-file reference union, so the memo cannot over-cache across it.
    let (mut db, main, part) = main_part(
        "\\documentclass{article}\n\\input{part}\n\\ref{a}\n",
        "\\label{a}\\label{b}\n",
    );
    let _ = resolved_labels(&db, project_main_part(&db, main, part));

    db.clear_query_log();

    // Adding `\ref{b}` grows part's ref set from `{a}` to `{a, b}`.
    db.set_file_text(part, "\\label{a}\\label{b}\\ref{b}\n");
    let resolved = resolved_labels(&db, project_main_part(&db, main, part));

    let counts = count_by_kind(&db.query_log());
    assert_eq!(counts.get(&QueryKind::FileRefs), Some(&1));
    assert_eq!(
        counts.get(&QueryKind::ResolvedLabels),
        Some(&1),
        "resolved labels must rebuild when a ref set changes"
    );
    assert!(resolved.is_referenced(&fpath(&db, main), "b"));
}

#[test]
fn glossary_key_set_survives_key_preserving_edit() {
    // The glossary firewall (`file_glossary_keys`): a `\gls` use edit re-runs the
    // projection (its semantic model changed) but the key set stays *equal*, so
    // salsa can backdate — the property glossary completion's per-member reads
    // rely on. Keys arrive sorted and deduped.
    let (mut db, main, _part) = main_part(
        "\\newacronym{fps}{FPS}{frames per second}\n\\newglossaryentry{ex}{name={x}}\n",
        "\\gls{fps}\n",
    );
    assert_eq!(db.file_glossary_keys(main), ["ex", "fps"]);

    db.set_file_text(
        main,
        "\\newacronym{fps}{FPS}{frames per second}\n\\newglossaryentry{ex}{name={x}}\n\\gls{ex}\n",
    );
    assert_eq!(db.file_glossary_keys(main), ["ex", "fps"]);
}

#[test]
fn label_change_rebuilds_resolved_labels() {
    // The complement: adding a `\label` changes part's label set, so the
    // cross-file resolution *must* rebuild (the firewall doesn't over-cache).
    let (mut db, main, part) = main_part(
        "\\documentclass{article}\n\\input{part}\n\\ref{a}\n",
        "\\label{a}\n",
    );
    let _ = resolved_labels(&db, project_main_part(&db, main, part));

    db.clear_query_log();

    db.set_file_text(part, "\\label{a}\\label{b}\n");
    let resolved = resolved_labels(&db, project_main_part(&db, main, part));

    let counts = count_by_kind(&db.query_log());
    assert_eq!(
        counts.get(&QueryKind::ResolvedLabels),
        Some(&1),
        "resolved labels must rebuild when a label set changes"
    );
    assert!(resolved.is_defined(&fpath(&db, main), "b"));
}

#[test]
fn resolved_citations_unions_referenced_bib_keys() {
    // main.tex is the document root and `\addbibresource`s refs.bib, which defines
    // the cite key `\cite`d from main — so the citation resolves cross-file.
    let (db, main, bib) = main_bib(
        "\\documentclass{article}\n\\addbibresource{refs.bib}\n\\cite{knuth}\n",
        "@article{knuth, title={x}}\n",
    );
    let resolved = resolved_citations(&db, project_main_bib(&db, main, bib));

    assert!(resolved.is_defined(&fpath(&db, main), "knuth"));
    assert!(!resolved.is_defined(&fpath(&db, main), "missing"));
    // The bib resource resolves to an analyzed member, and main is a document root.
    assert!(resolved.is_closed(&fpath(&db, main)));
    assert!(resolved.is_root_component(&fpath(&db, main)));
}

#[test]
fn cite_set_preserving_edit_does_not_rebuild_resolved_citations() {
    // The cite firewall: adding a `@string` to refs.bib changes its bib model (so
    // `file_cite_names` re-executes) but not its cite-key set — so `file_cite_names`
    // backdates and the cross-file `resolved_citations` memo is reused.
    let (mut db, main, bib) = main_bib(
        "\\documentclass{article}\n\\addbibresource{refs.bib}\n\\cite{knuth}\n",
        "@article{knuth, title={x}}\n",
    );
    let _ = resolved_citations(&db, project_main_bib(&db, main, bib));

    db.clear_query_log();

    // The model changes (a new `@string` def) but the cite-key set is still `{knuth}`.
    db.set_file_text(bib, "@article{knuth, title={x}}\n@string{foo = \"bar\"}\n");
    let _ = resolved_citations(&db, project_main_bib(&db, main, bib));

    let counts = count_by_kind(&db.query_log());
    // refs.bib's cite-key set is recomputed (its model changed)...
    assert_eq!(counts.get(&QueryKind::FileCiteNames), Some(&1));
    // ...but the set is unchanged, so the resolution memo is reused.
    assert_eq!(
        counts.get(&QueryKind::ResolvedCitations),
        None,
        "resolved citations must not rebuild when no cite-key set changed"
    );
}

#[test]
fn cite_key_change_rebuilds_resolved_citations() {
    // The complement: adding an entry changes refs.bib's cite-key set, so the
    // cross-file resolution *must* rebuild (the firewall doesn't over-cache).
    let (mut db, main, bib) = main_bib(
        "\\documentclass{article}\n\\addbibresource{refs.bib}\n\\cite{knuth}\n",
        "@article{knuth, title={x}}\n",
    );
    let _ = resolved_citations(&db, project_main_bib(&db, main, bib));

    db.clear_query_log();

    db.set_file_text(
        bib,
        "@article{knuth, title={x}}\n@article{lamport, title={y}}\n",
    );
    let resolved = resolved_citations(&db, project_main_bib(&db, main, bib));

    let counts = count_by_kind(&db.query_log());
    assert_eq!(
        counts.get(&QueryKind::ResolvedCitations),
        Some(&1),
        "resolved citations must rebuild when a cite-key set changes"
    );
    assert!(resolved.is_defined(&fpath(&db, main), "lamport"));
}

#[test]
fn reinterning_same_membership_reuses_graph_memo() {
    let (db, main, part) = main_part("\\input{part}\n", "hello\n");
    let project = project_main_part(&db, main, part);
    let _ = project_graph(&db, project);

    db.clear_query_log();

    // Re-intern the identical membership: same files, same sorted paths.
    let project2 = project_main_part(&db, main, part);
    assert!(
        project == project2,
        "same membership should re-intern to the same id"
    );

    let _ = project_graph(&db, project2);
    assert_eq!(
        count_by_kind(&db.query_log()).get(&QueryKind::ProjectGraph),
        None,
        "an unchanged membership must not rebuild the graph"
    );
}

#[test]
fn reinterning_reordered_membership_reuses_graph_memo() {
    // The `Analysis` interning path normalizes the member key, so the same set
    // built in a different order must still re-intern to the same id and reuse
    // the memo. This is the choke point (`intern_project` -> `normalize_members`)
    // making memo survival correct by construction — a caller can't churn the id
    // by reordering. Observed through the public `package_graph` API.
    let (db, main, part) = main_part("\\input{part}\n", "hello\n");
    let sorted = {
        let mut m = members(&db, main, part);
        m.sort_by(|a, b| a.path.cmp(&b.path));
        m
    };
    let reversed = {
        let mut m = sorted.clone();
        m.reverse();
        m
    };

    let snap = db.snapshot();
    let _ = snap.package_graph(sorted);
    drop(snap);
    db.clear_query_log();

    // Re-intern the identical set in the opposite order.
    let snap = db.snapshot();
    let _ = snap.package_graph(reversed);
    assert_eq!(
        count_by_kind(&db.query_log()).get(&QueryKind::PackageGraph),
        None,
        "a reordered but unchanged membership must not rebuild the graph"
    );
}