arity 0.25.0

A language server, formatter, and linter for R
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use super::*;

/// Reuse the snapshot's syntax and semantics only while they describe the live
/// buffer. A missing file, stale text, or cancelled read falls back to fresh
/// analysis. The callback borrows the cached model without cloning it.
pub(crate) fn with_document_semantics<T>(
    snapshot: &Analysis,
    path: &Path,
    buffer: &TextBuffer,
    compute: impl Fn(&SyntaxNode, &SemanticModel) -> T,
) -> T {
    let cached = salsa::Cancelled::catch(AssertUnwindSafe(|| {
        let file = snapshot.lookup_file(path)?;
        if !snapshot.file_text_is(file, &buffer.text_arc()) {
            return None;
        }
        let root = snapshot.parsed_tree(file);
        Some(compute(&root, snapshot.semantic_model(file)))
    }));
    if let Ok(Some(result)) = cached {
        return result;
    }
    let root = parse(buffer.text()).cst;
    let model = SemanticModel::build(&root);
    compute(&root, &model)
}

/// A read-only request the lint thread services by cloning its salsa db and
/// running the work off-thread on the read pool. Each variant carries a shared
/// handle to the live buffer and the main loop's `out` channel, so the worker replies with an
/// [`Outbound::ReadReply`] that the loop gates (cancellation, stale version)
/// before it reaches the client; the lint thread only adds the db snapshot. See
/// [`run_read`].
pub(crate) enum ReadJob {
    Format {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        style: FormatStyle,
        out: Sender<Outbound>,
    },
    FormatRange {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        range: Range,
        style: FormatStyle,
        out: Sender<Outbound>,
    },
    Hover {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        position: Position,
        out: Sender<Outbound>,
    },
    InlayHints {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        /// The client's visible range, honored verbatim.
        range: Range,
        out: Sender<Outbound>,
    },
    Completion {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        position: Position,
        out: Sender<Outbound>,
    },
    SignatureHelp {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        position: Position,
        out: Sender<Outbound>,
    },
    ResolveCompletion {
        id: RequestId,
        // Boxed: `CompletionItem` is large and would bloat every `ReadJob`.
        item: Box<CompletionItem>,
        out: Sender<Outbound>,
    },
    Definition {
        id: RequestId,
        path: PathBuf,
        /// The current document's URI — an intra-file hit reports a `Location`
        /// back into it, so unlike the other jobs this one needs the URI too.
        uri: Uri,
        buffer: Arc<TextBuffer>,
        position: Position,
        out: Sender<Outbound>,
    },
    References {
        id: RequestId,
        path: PathBuf,
        /// In-file reads report `Location`s back into this URI; cross-file reads
        /// carry their own.
        uri: Uri,
        buffer: Arc<TextBuffer>,
        position: Position,
        include_declaration: bool,
        out: Sender<Outbound>,
    },
    Rename {
        id: RequestId,
        path: PathBuf,
        /// In-file edits land in this URI; cross-file edits carry their own.
        uri: Uri,
        buffer: Arc<TextBuffer>,
        /// The cursor's byte offset, already resolved on the main thread (via the
        /// `prepareRename` anchor when present, else the request position) so the
        /// anchor state never crosses the thread boundary.
        offset: usize,
        new_name: String,
        out: Sender<Outbound>,
    },
    WillRenameFiles {
        id: RequestId,
        /// `(old, new)` filesystem path pairs for the files being renamed.
        renames: Vec<(PathBuf, PathBuf)>,
        out: Sender<Outbound>,
    },
    DocumentSymbol {
        id: RequestId,
        path: PathBuf,
        buffer: Arc<TextBuffer>,
        cached: Arc<OnceLock<Vec<DocumentSymbol>>>,
        out: Sender<Outbound>,
    },
    WorkspaceSymbol {
        id: RequestId,
        /// The fuzzy name filter; an empty string requests every symbol.
        query: String,
        out: Sender<Outbound>,
    },
    PrepareCallHierarchy {
        id: RequestId,
        path: PathBuf,
        /// An intra-file item reports back into this URI; cross-file items carry
        /// their own.
        uri: Uri,
        buffer: Arc<TextBuffer>,
        position: Position,
        out: Sender<Outbound>,
    },
    IncomingCalls {
        id: RequestId,
        /// The prepared item, round-tripped from the client; the target function
        /// is recovered from its `uri` + `name`.
        item: Box<CallHierarchyItem>,
        out: Sender<Outbound>,
    },
    OutgoingCalls {
        id: RequestId,
        item: Box<CallHierarchyItem>,
        out: Sender<Outbound>,
    },
    PrepareTypeHierarchy {
        id: RequestId,
        path: PathBuf,
        /// An intra-file item reports back into this URI; cross-file items carry
        /// their own.
        uri: Uri,
        buffer: Arc<TextBuffer>,
        position: Position,
        out: Sender<Outbound>,
    },
    Supertypes {
        id: RequestId,
        /// The prepared item, round-tripped from the client; the target class is
        /// recovered from its `name`.
        item: Box<TypeHierarchyItem>,
        out: Sender<Outbound>,
    },
    Subtypes {
        id: RequestId,
        item: Box<TypeHierarchyItem>,
        out: Sender<Outbound>,
    },
}

/// Service a read-only job against a db `snapshot`, replying to the client.
/// Runs on a read-pool worker; the `snapshot` is dropped on return so it never
/// blocks the lint thread's next write longer than the job itself.
pub(crate) fn run_read(snapshot: Analysis, encoding: PositionEncoding, job: ReadJob) {
    match job {
        ReadJob::Format {
            id,
            path,
            buffer,
            style,
            out,
        } => {
            let result = format_edits_via_db(&snapshot, &path, &buffer, style, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::FormatRange {
            id,
            path,
            buffer,
            range,
            style,
            out,
        } => {
            let result =
                format_range_edits_via_db(&snapshot, &path, &buffer, range, style, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::Hover {
            id,
            path,
            buffer,
            position,
            out,
        } => {
            let result = hover_via_db(&snapshot, &path, &buffer, position, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::InlayHints {
            id,
            path,
            buffer,
            range,
            out,
        } => {
            let result = inlay_hints_via_db(&snapshot, &path, &buffer, range, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::Completion {
            id,
            path,
            buffer,
            position,
            out,
        } => {
            let result = completion_via_db(&snapshot, &path, &buffer, position, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::SignatureHelp {
            id,
            path,
            buffer,
            position,
            out,
        } => {
            let result = signature_help_via_db(&snapshot, &path, &buffer, position, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::ResolveCompletion { id, item, out } => {
            let result = resolve_completion(*item, &snapshot.library_data().unwrap_or_default());
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::Definition {
            id,
            path,
            uri,
            buffer,
            position,
            out,
        } => {
            let result = definition_via_db(&snapshot, &path, &uri, &buffer, position, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::References {
            id,
            path,
            uri,
            buffer,
            position,
            include_declaration,
            out,
        } => {
            let result = references_via_db(
                &snapshot,
                &path,
                &uri,
                &buffer,
                position,
                include_declaration,
                encoding,
            );
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::Rename {
            id,
            path,
            uri,
            buffer,
            offset,
            new_name,
            out,
        } => {
            let result =
                rename_via_db(&snapshot, &path, &uri, &buffer, offset, &new_name, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::WillRenameFiles { id, renames, out } => {
            let result = will_rename_via_db(&snapshot, &renames, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::DocumentSymbol {
            id,
            path,
            buffer,
            cached,
            out,
        } => {
            let result =
                cached.get_or_init(|| document_symbols_via_db(&snapshot, &path, &buffer, encoding));
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::WorkspaceSymbol { id, query, out } => {
            let symbols = workspace_symbols_via_db(&snapshot, &query, encoding);
            let response = WorkspaceSymbolResponse::Nested(symbols);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, response)));
        }
        ReadJob::PrepareCallHierarchy {
            id,
            path,
            uri,
            buffer,
            position,
            out,
        } => {
            let result =
                prepare_call_hierarchy_via_db(&snapshot, &path, &uri, &buffer, position, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::IncomingCalls { id, item, out } => {
            let result = incoming_calls_via_db(&snapshot, &item, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::OutgoingCalls { id, item, out } => {
            let result = outgoing_calls_via_db(&snapshot, &item, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::PrepareTypeHierarchy {
            id,
            path,
            uri,
            buffer,
            position,
            out,
        } => {
            let result =
                prepare_type_hierarchy_via_db(&snapshot, &path, &uri, &buffer, position, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::Supertypes { id, item, out } => {
            let result = supertypes_via_db(&snapshot, &item, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
        ReadJob::Subtypes { id, item, out } => {
            let result = subtypes_via_db(&snapshot, &item, encoding);
            let _ = out.send(Outbound::ReadReply(Response::new_ok(id, result)));
        }
    }
}

/// Sort and dedup `locations` (by URI then range) so a union of overlapping
/// components doesn't report the same site twice.
pub(crate) fn dedup_locations(locations: &mut Vec<Location>) {
    locations.sort_by(|a, b| {
        (a.uri.as_str(), pos_key(a.range.start), pos_key(a.range.end)).cmp(&(
            b.uri.as_str(),
            pos_key(b.range.start),
            pos_key(b.range.end),
        ))
    });
    locations.dedup();
}

/// A totally-ordered key for an LSP [`Position`].
pub(crate) fn pos_key(position: Position) -> (u32, u32) {
    (position.line, position.character)
}

/// A `Location` for `range` in the workspace file at `path`, mapping the byte
/// span through that file's *current* text. `None` if the file isn't tracked or
/// its path has no URI.
pub(crate) fn location_in(
    snapshot: &Analysis,
    path: &Path,
    range: TextRange,
    encoding: PositionEncoding,
) -> Option<Location> {
    let file = snapshot.lookup_file(path)?;
    let target_uri = uri::from_path(path)?;
    let target_index = snapshot.line_index(file);
    Some(Location {
        uri: target_uri,
        range: text_range_to_lsp_range(target_index, range, encoding),
    })
}

/// The [`TextEdit`] rewriting `range` to `new_name` in the workspace file at
/// `path`, paired with that file's URI. The write mirror of [`location_in`]: the
/// byte span is mapped through the file's *current* text via its own line index.
/// `None` if the file isn't tracked or its path has no URI.
pub(crate) fn text_edit_in(
    snapshot: &Analysis,
    path: &Path,
    range: TextRange,
    new_name: &str,
    encoding: PositionEncoding,
) -> Option<(Uri, TextEdit)> {
    let file = snapshot.lookup_file(path)?;
    let target_uri = uri::from_path(path)?;
    let target_index = snapshot.line_index(file);
    Some((
        target_uri,
        TextEdit {
            range: text_range_to_lsp_range(target_index, range, encoding),
            new_text: new_name.to_string(),
        },
    ))
}

/// Sort and dedup each file's edits, dropping empties, and wrap them in a
/// [`WorkspaceEdit`]. `None` when nothing is left to rewrite.
pub(crate) fn finalize_rename(mut changes: HashMap<Uri, Vec<TextEdit>>) -> Option<WorkspaceEdit> {
    changes.retain(|_, edits| {
        edits.sort_by_key(|a| (a.range.start, a.range.end));
        edits.dedup();
        !edits.is_empty()
    });
    (!changes.is_empty()).then(|| WorkspaceEdit {
        changes: Some(changes),
        ..Default::default()
    })
}

#[cfg(test)]
mod document_semantics_tests {
    use super::*;

    #[test]
    fn matching_buffers_reuse_the_semantic_model() {
        let buffer = buf("f <- function(x) { y <- x; y }\nf(1)\n");
        let mut db = IncrementalDatabase::default();
        let file = db.upsert_file(test_path(), buffer.text_arc());
        let snapshot = db.snapshot();
        let cached = snapshot.semantic_model(file);
        db.clear_query_log();

        // Equal text in a separate allocation must take the same path as the
        // shared live buffer; pointer identity alone cannot establish staleness.
        for live in [&buffer, &buf(buffer.text())] {
            with_document_semantics(&snapshot, test_path(), live, |root, model| {
                assert_eq!(root.text().to_string(), live.text());
                assert!(std::ptr::eq(model, cached), "reuse the cached model");
            });
        }
        assert!(db.query_log().is_empty());
    }

    #[test]
    fn stale_and_missing_models_use_the_live_buffer() {
        let buffer = buf("# shifted 😀\nf <- function(x) { y <- x; y }\nf(1)\n");
        let expected = SemanticModel::build(&parse(buffer.text()).cst);
        let mut db = IncrementalDatabase::default();
        let file = db.upsert_file(test_path(), "old <- 1\n");
        let _ = db.semantic_model(file);
        let empty = IncrementalDatabase::default();
        for snapshot in [db.snapshot(), empty.snapshot()] {
            with_document_semantics(&snapshot, test_path(), &buffer, |root, model| {
                assert_eq!(root.text().to_string(), buffer.text());
                assert_eq!(model, &expected);
            });
        }
    }

    #[test]
    fn cancelled_document_read_retries_with_fresh_semantics() {
        let buffer = buf("x <- 1\nx\n");
        let mut db = IncrementalDatabase::default();
        let file = db.upsert_file(test_path(), buffer.text_arc());
        let snapshot = db.snapshot();
        let cached = snapshot.semantic_model(file);
        let calls = std::cell::Cell::new(0);

        let result = with_document_semantics(&snapshot, test_path(), &buffer, |root, model| {
            calls.set(calls.get() + 1);
            if calls.get() == 1 {
                assert!(std::ptr::eq(model, cached));
                // Inject the unwind at the read boundary without racing a
                // writer or relying on thread scheduling in the test.
                std::panic::resume_unwind(Box::new(salsa::Cancelled::PendingWrite));
            }
            assert!(!std::ptr::eq(model, cached));
            assert_eq!(model, cached);
            root.text().to_string()
        });
        assert_eq!(result, buffer.text());
        assert_eq!(calls.get(), 2);
    }
}