harn-hostlib 0.10.171

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! `rename_symbol` through the language-server backend.
//!
//! The server owns binding resolution, so the syntax engine's binding proof
//! and competing-declaration refusals do not apply here. What stays is what
//! the server does not check: the new name shadowing an existing identifier,
//! the requested scope, and that every rewritten file still parses.

use std::path::Path;

use harn_vm::VmValue;

use crate::ast::{api as ast_api, Language};
use crate::error::HostlibError;

use super::super::builtins::SharedIndex;
use super::super::lsp::{self, RenameRequest, Resolution};
use super::super::refactor_backend::{BackendOutcome, FileEdits, ServerRun};
use super::super::refactor_core::{
    collect_identifier_spans, files_in_scope, first_syntax_error, plan_file, read_source,
    write_plans, EditSpan, FilePlan, Scope, ShadowSite,
};
use super::super::rename_rust_residue::{doc_link_mentions, span_at, Residue};
use super::super::state::IndexState;
use super::super::symbol_graph::Node as SymbolNode;
use super::*;

pub(super) enum Answer {
    Response(VmValue),
    /// The server could not answer; carries why, for the response's
    /// `backend.fallback_reason`.
    Fallback(String),
}

pub(super) type WarmResult = Result<lsp::Warmed, (String, Option<ServerRun>)>;

/// Warm the server for `symbol_path` without holding the code-index lock.
/// `None` when no server could serve the file's language.
pub(super) fn warm(index: &SharedIndex, symbol_path: &str) -> Option<WarmResult> {
    let (root, path) = {
        let guard = index.lock().expect("code_index mutex poisoned");
        let state = guard.as_ref()?;
        (
            state.root.clone(),
            super::super::builtins::normalize_relative_path_for(state, symbol_path),
        )
    };
    let language = Language::detect(Path::new(&path), None)?;
    lsp::serves(language).then(|| lsp::warm(&root, &path, language))
}

pub(super) struct Request<'a> {
    pub state: &'a IndexState,
    pub seed: &'a SymbolNode,
    pub in_scope_files: &'a [String],
    pub session_id: Option<&'a str>,
    pub dry_run: bool,
    pub validate: bool,
    pub warmed: Option<WarmResult>,
}

pub(super) fn rename(
    env: &mut ResponseEnv<'_>,
    mut request: Request<'_>,
) -> Result<Answer, HostlibError> {
    let seed_path = request.seed.path.as_ref();
    let Some(language) = Language::detect(Path::new(seed_path), None) else {
        return Ok(Answer::Fallback(
            "the declaration's language is unknown".into(),
        ));
    };
    if !lsp::serves(language) {
        return Ok(Answer::Fallback(format!(
            "no language server is registered for {}",
            language.name()
        )));
    }
    let source = read_source(BUILTIN, &request.state.root, seed_path, request.session_id)?;
    let Some(offset) = declaration_offset(&source, language, env.symbol_name, request.seed.line)
    else {
        return Ok(Answer::Fallback(format!(
            "no `{}` identifier on line {}",
            env.symbol_name, request.seed.line
        )));
    };
    // Every workspace file that mentions the name, whatever the scope: the
    // server sees them as Harn does, and the scope check below covers them.
    let mentions = if env.scope == Scope::Workspace {
        request.in_scope_files.to_vec()
    } else {
        files_in_scope(
            request.state,
            Scope::Workspace,
            env.symbol_name,
            seed_path,
            request.session_id,
        )
    };
    let warmed = match request.warmed.take() {
        Some(Ok(warmed)) => warmed,
        Some(Err((reason, run))) => {
            env.backend.language_server = run;
            return Ok(Answer::Fallback(reason));
        }
        None => {
            return Ok(Answer::Fallback(format!(
                "no language server could start for {}",
                language.name()
            )))
        }
    };
    let lsp_request = RenameRequest {
        index_root: &request.state.root,
        path: seed_path,
        language,
        source: &source,
        offset,
        old_name: env.symbol_name,
        new_name: env.new_name,
        session_id: request.session_id,
        mentions: &mentions,
    };
    let (outcome, run) = lsp::rename(&lsp_request, &warmed);
    env.backend.language_server = Some(run);
    let mut changeset = match outcome {
        BackendOutcome::Changeset(changeset) => changeset,
        BackendOutcome::Declined(details) => {
            return Ok(Answer::Response(emit_response(
                env,
                "declined",
                ResponseExtras {
                    details: format!("rename aborted without writes: {details}"),
                    ..Default::default()
                },
            )))
        }
        BackendOutcome::Unavailable(reason) => return Ok(Answer::Fallback(reason)),
    };
    // The server's edit proves where references are; check the scope before
    // any fallback, which would rename only inside the scope.
    let changed: Vec<&str> = changeset.iter().map(|file| file.path.as_str()).collect();
    if let Some(response) = scope_exceeded(env, request.in_scope_files, &changed) {
        return Ok(Answer::Response(response));
    }
    if language == Language::Rust {
        super::super::rename_rust_residue::complete(
            Residue {
                builtin: BUILTIN,
                state: request.state,
                seed_path,
                name: env.symbol_name,
                new_name: env.new_name,
                files: &mentions,
                session_id: request.session_id,
                module_level: request.seed.container.is_none(),
                is_type: request.seed.kind == NodeKind::Type,
            },
            &mut changeset,
        )?;
    }
    // Files the server edited but the index does not list are checked too.
    let mut checked: Vec<String> = mentions.clone();
    checked.extend(changeset.iter().map(|file| file.path.clone()));
    checked.sort();
    checked.dedup();
    if language == Language::Rust {
        // rust-analyzer resolves intra-doc links but does not rename them;
        // rename the ones it resolves to the declaration.
        let links = doc_link_sites(
            &request,
            env.symbol_name,
            &checked,
            &changeset,
            &mut env.stale,
        )?;
        if !links.is_empty() {
            let queries: Vec<(String, usize)> = links
                .iter()
                .map(|site| (site.path.clone(), site.offset))
                .collect();
            let resolutions = match lsp::resolve(&lsp_request, &warmed, &queries, false) {
                Ok(resolutions) => resolutions,
                Err(reason) => return Ok(Answer::Fallback(reason)),
            };
            let seed_file = request
                .state
                .root
                .join(seed_path)
                .canonicalize()
                .unwrap_or_else(|_| request.state.root.join(seed_path));
            let seed_modules = module_path(&source, language, offset);
            // A second type of the name in the file (one local to a
            // function, say) makes a bare `Name::` ambiguous there.
            let sole_type =
                type_declarations(&source, language, env.symbol_name, &seed_modules) == 1;
            for (site, resolution) in links.iter().zip(resolutions) {
                // The server resolves a link as a whole, so `[`Span::new`]`
                // answers with `new`. The `Span` segment names the
                // declaration when that answer lies in an impl, enum or trait
                // body for the name in the declaration's own file and module:
                // one module cannot hold two types with one name.
                let own_member = site.qualifies
                    && sole_type
                    && site.path == seed_path
                    && matches!(&resolution, Resolution::Elsewhere(Some(target))
                        if target.path == seed_file
                            && body_owner_is(&source, language, target.offset, env.symbol_name)
                            && module_path(&source, language, target.offset) == seed_modules);
                if site.renameable && (resolution == Resolution::Seed || own_member) {
                    add_edit(
                        &request,
                        &mut changeset,
                        site,
                        env.symbol_name,
                        env.new_name,
                    )?;
                } else {
                    env.stale.push(format!(
                        "{}:{} (doc link left unchanged)",
                        site.path, site.line
                    ));
                }
            }
        }
    }
    if changeset.is_empty() {
        return Ok(Answer::Response(no_match_response(env)));
    }
    let changed: Vec<&str> = changeset.iter().map(|file| file.path.as_str()).collect();
    if let Some(response) = scope_exceeded(env, request.in_scope_files, &changed) {
        return Ok(Answer::Response(response));
    }

    // Every mention the edit leaves must be something else, by the server's
    // own resolution. One it cannot resolve sits in code it did not analyze
    // (an unloaded module, an inactive `cfg`, an unexpanded macro).
    let sites = remaining_mentions(&request, env.symbol_name, language, &checked, &changeset)?;
    if sites.len() > MAX_RESOLVED_MENTIONS {
        return Ok(Answer::Fallback(format!(
            "{} mentions of `{}` are left after the server's edit; more than the {MAX_RESOLVED_MENTIONS} Harn checks",
            sites.len(),
            env.symbol_name
        )));
    }
    let queries: Vec<(String, usize)> = sites
        .iter()
        .map(|site| (site.path.clone(), site.offset))
        .collect();
    let stop_at_miss = env.scope == Scope::Workspace;
    let resolutions = match lsp::resolve(&lsp_request, &warmed, &queries, stop_at_miss) {
        Ok(resolutions) => resolutions,
        Err(reason) => return Ok(Answer::Fallback(reason)),
    };
    let missed: Vec<(&Site, Resolution)> = sites
        .iter()
        .zip(resolutions)
        .filter(|(_, resolution)| !resolution.is_elsewhere())
        .collect();
    if let Some((site, resolution)) = missed.first() {
        let missed_paths: Vec<&str> = missed.iter().map(|(site, _)| site.path.as_str()).collect();
        if let Some(response) = scope_exceeded(env, request.in_scope_files, &missed_paths) {
            return Ok(Answer::Response(response));
        }
        let what = match resolution {
            Resolution::Seed => "left a reference to",
            Resolution::Encloses => "could not tell what this refers to:",
            _ => "could not resolve",
        };
        return Ok(Answer::Fallback(format!(
            "the language server {what} `{}` at {}:{}",
            env.symbol_name, site.path, site.line
        )));
    }

    let mut plans: Vec<FilePlan> = Vec::new();
    let mut shadows: Vec<ShadowSite> = Vec::new();
    for file in changeset {
        let Some(file_language) = Language::detect(Path::new(&file.path), None) else {
            return Ok(Answer::Response(unsupported_language_response(
                env, &file.path, None,
            )));
        };
        let tree = match ast_api::parse_tree(&file.source, file_language) {
            Ok(tree) => tree,
            Err(err) => {
                return Ok(Answer::Response(syntax_error_response(
                    env,
                    format!("`{}` failed to parse: {err}", file.path),
                )))
            }
        };
        if let Some(kinds) = file_language.rename_identifier_kinds() {
            let mut ignored = Vec::new();
            collect_identifier_spans(
                tree.root_node(),
                file.source.as_bytes(),
                "",
                env.new_name,
                kinds,
                &file.path,
                &mut ignored,
                &mut shadows,
            );
        }
        if request.validate {
            if let Some(detail) = first_syntax_error(&file.source, file_language) {
                return Ok(Answer::Response(syntax_error_response(
                    env,
                    format!(
                        "`{}` does not parse before the edit ({detail}); fix it first",
                        file.path
                    ),
                )));
            }
        }
        let path = file.path.clone();
        match plan_file(
            file.path,
            file_language,
            file.source,
            file.edits,
            request.validate,
        ) {
            Ok(plan) => plans.push(plan),
            Err(detail) => {
                return Ok(Answer::Response(syntax_error_response(
                    env,
                    format!("rewriting `{path}` produced syntax errors: {detail}"),
                )))
            }
        }
    }
    if !shadows.is_empty() {
        return Ok(Answer::Response(conflict_response(env, &shadows)));
    }

    let failed = if request.dry_run {
        Vec::new()
    } else {
        let failed = write_plans(BUILTIN, &request.state.root, &plans, request.session_id)?;
        if request.session_id.is_none() {
            let written: Vec<String> = plans.iter().map(|plan| plan.path.clone()).collect();
            lsp::files_written(&request.state.root, &written);
        }
        failed
    };
    Ok(Answer::Response(applied_response(
        env,
        &plans,
        request.dry_run,
        failed,
    )))
}

/// Upper bound on leftover mentions resolved through the server per rename.
const MAX_RESOLVED_MENTIONS: usize = 200;

/// A mention of the old name the edit leaves in place.
struct Site {
    path: String,
    offset: usize,
    line: usize,
    /// An unqualified doc-link segment followed by `::`.
    qualifies: bool,
    /// False for a doc link whose comment block also defines a reference
    /// (`[Span]: path`): renaming the label alone would break the link.
    renameable: bool,
}

/// `scope_exceeded` when the scope is narrower than the workspace and
/// `paths` reach outside it.
fn scope_exceeded(env: &ResponseEnv<'_>, in_scope: &[String], paths: &[&str]) -> Option<VmValue> {
    if env.scope == Scope::Workspace {
        return None;
    }
    let mut outside: Vec<&str> = paths
        .iter()
        .copied()
        .filter(|path| !in_scope.iter().any(|kept| kept == path))
        .collect();
    outside.sort_unstable();
    outside.dedup();
    if outside.is_empty() {
        return None;
    }
    Some(emit_response(
        env,
        "scope_exceeded",
        ResponseExtras {
            warnings: outside.into_iter().map(str_value).collect(),
            details: format!(
                "references to `{}` exist outside scope `{}` (listed in `warnings`); renaming only inside the scope would break them. Re-run with scope `workspace`.",
                env.symbol_name,
                env.scope.as_str()
            ),
            ..Default::default()
        },
    ))
}

/// Identifier-position mentions of `name`, outside strings and comments, in
/// files the same server serves, that `changeset` does not rewrite.
fn remaining_mentions(
    request: &Request<'_>,
    name: &str,
    language: Language,
    mentions: &[String],
    changeset: &[FileEdits],
) -> Result<Vec<Site>, HostlibError> {
    let mut sites = Vec::new();
    for path in mentions {
        let Some(file_language) = Language::detect(Path::new(path), None) else {
            continue;
        };
        if !lsp::same_server(language, file_language) {
            continue;
        }
        let Some(kinds) = file_language.rename_identifier_kinds() else {
            continue;
        };
        let edited = changeset.iter().find(|file| &file.path == path);
        let source = match edited {
            Some(file) => file.source.clone(),
            None => read_source(BUILTIN, &request.state.root, path, request.session_id)?,
        };
        let Ok(tree) = ast_api::parse_tree(&source, file_language) else {
            continue;
        };
        let mut found = Vec::new();
        // `r#Span` is the same Rust identifier as `Span`.
        let raw = format!("r#{name}");
        let spellings: &[&str] = if file_language == Language::Rust {
            &[name, &raw]
        } else {
            &[name]
        };
        for spelling in spellings {
            collect_identifier_spans(
                tree.root_node(),
                source.as_bytes(),
                spelling,
                "",
                kinds,
                path,
                &mut found,
                &mut Vec::new(),
            );
        }
        for span in found {
            let covered = edited.is_some_and(|file| {
                file.edits
                    .iter()
                    .any(|edit| edit.span.start_byte == span.start_byte)
            });
            if !covered {
                sites.push(Site {
                    path: path.clone(),
                    offset: span.start_byte,
                    line: span.start_row + 1,
                    qualifies: false,
                    renameable: true,
                });
            }
        }
    }
    sites.sort_by(|left, right| (&left.path, left.offset).cmp(&(&right.path, right.offset)));
    Ok(sites)
}

/// Intra-doc-link mentions of `name` in Rust `paths` that `changeset` does
/// not rewrite. Reference-definition lines naming it go to `stale`.
fn doc_link_sites(
    request: &Request<'_>,
    name: &str,
    paths: &[String],
    changeset: &[FileEdits],
    stale: &mut Vec<String>,
) -> Result<Vec<Site>, HostlibError> {
    let mut sites = Vec::new();
    for path in paths {
        if Language::detect(Path::new(path), None) != Some(Language::Rust) {
            continue;
        }
        let edited = changeset.iter().find(|file| &file.path == path);
        let source = match edited {
            Some(file) => file.source.clone(),
            None => read_source(BUILTIN, &request.state.root, path, request.session_id)?,
        };
        let Ok(tree) = ast_api::parse_tree(&source, Language::Rust) else {
            continue;
        };
        let reference_definitions = has_reference_definition(&source, name);
        if reference_definitions {
            // A `[label]: path` target is not a link occurrence; report the
            // definition lines themselves.
            for (index, line) in source.lines().enumerate() {
                if is_reference_definition(line, name) {
                    stale.push(format!("{path}:{} (doc link left unchanged)", index + 1));
                }
            }
        }
        for offset in doc_link_mentions(tree.root_node(), &source, name) {
            let covered = edited
                .is_some_and(|file| file.edits.iter().any(|edit| edit.span.start_byte == offset));
            if !covered {
                let before = source.get(..offset).unwrap_or_default();
                let after = source.get(offset + name.len()..).unwrap_or_default();
                sites.push(Site {
                    path: path.clone(),
                    offset,
                    line: before.matches('\n').count() + 1,
                    qualifies: after.starts_with("::") && !before.ends_with("::"),
                    renameable: !reference_definitions,
                });
            }
        }
    }
    Ok(sites)
}

/// Rename the mention at `site`, reading the file into `changeset` if the
/// server's edit did not touch it.
fn add_edit(
    request: &Request<'_>,
    changeset: &mut Vec<FileEdits>,
    site: &Site,
    name: &str,
    new_name: &str,
) -> Result<(), HostlibError> {
    let at = match changeset.iter().position(|file| file.path == site.path) {
        Some(at) => at,
        None => {
            let source = read_source(BUILTIN, &request.state.root, &site.path, request.session_id)?;
            changeset.push(FileEdits {
                path: site.path.clone(),
                source,
                edits: Vec::new(),
            });
            changeset.len() - 1
        }
    };
    let file = &mut changeset[at];
    let edit = EditSpan {
        span: span_at(&file.source, site.offset, site.offset + name.len()),
        before: name.to_string(),
        after: new_name.to_string(),
    };
    file.edits.push(edit);
    file.edits.sort_by_key(|edit| edit.span.start_byte);
    Ok(())
}

/// Whether a doc comment defines a reference link (`[label]: target`) that
/// mentions `name` in its label or target.
fn has_reference_definition(source: &str, name: &str) -> bool {
    source
        .lines()
        .any(|line| is_reference_definition(line, name))
}

fn is_reference_definition(line: &str, name: &str) -> bool {
    let doc = line.trim_start();
    let body = doc
        .strip_prefix("///")
        .or_else(|| doc.strip_prefix("//!"))
        .unwrap_or_default()
        .trim_start();
    body.starts_with('[') && body.contains("]:") && body.contains(name)
}

/// Whether byte `offset` of `source` lies in an `impl`, `enum` or `trait`
/// body for the type `name`.
fn body_owner_is(source: &str, language: Language, offset: usize, name: &str) -> bool {
    let Ok(tree) = ast_api::parse_tree(source, language) else {
        return false;
    };
    let text = |node: tree_sitter::Node<'_>| node.utf8_text(source.as_bytes()).unwrap_or_default();
    let mut node = tree.root_node().descendant_for_byte_range(offset, offset);
    while let Some(current) = node {
        let owner = match current.kind() {
            "impl_item" => current
                .child_by_field_name("type")
                .map(|ty| ty.child_by_field_name("type").unwrap_or(ty)),
            "enum_item" | "trait_item" => current.child_by_field_name("name"),
            _ => None,
        };
        if let Some(owner) = owner {
            return text(owner) == name;
        }
        node = current.parent();
    }
    false
}

/// How many type-like items (struct, enum, union, trait, alias) named
/// `name` the file declares under the inline-module path `modules`,
/// including ones local to a function there.
fn type_declarations(source: &str, language: Language, name: &str, modules: &[String]) -> usize {
    let Ok(tree) = ast_api::parse_tree(source, language) else {
        return 0;
    };
    let mut count = 0;
    let mut stack = vec![tree.root_node()];
    while let Some(node) = stack.pop() {
        let declares = matches!(
            node.kind(),
            "struct_item" | "enum_item" | "union_item" | "trait_item" | "type_item"
        ) && node
            .child_by_field_name("name")
            .is_some_and(|ident| ident.utf8_text(source.as_bytes()) == Ok(name));
        count +=
            usize::from(declares && module_path(source, language, node.start_byte()) == modules);
        let mut cursor = node.walk();
        stack.extend(node.children(&mut cursor));
    }
    count
}

/// The names of the inline `mod` items enclosing byte `offset`, outermost
/// first.
fn module_path(source: &str, language: Language, offset: usize) -> Vec<String> {
    let Ok(tree) = ast_api::parse_tree(source, language) else {
        return Vec::new();
    };
    let mut names = Vec::new();
    let mut node = tree.root_node().descendant_for_byte_range(offset, offset);
    while let Some(current) = node {
        if current.kind() == "mod_item" {
            if let Some(name) = current.child_by_field_name("name") {
                names.push(
                    name.utf8_text(source.as_bytes())
                        .unwrap_or_default()
                        .to_string(),
                );
            }
        }
        node = current.parent();
    }
    names.reverse();
    names
}

/// Byte offset of the declaration's name: the identifier spelled `name` on
/// the seed's line.
fn declaration_offset(source: &str, language: Language, name: &str, line: u32) -> Option<usize> {
    let kinds = language.rename_identifier_kinds()?;
    let tree = ast_api::parse_tree(source, language).ok()?;
    let mut targets = Vec::new();
    collect_identifier_spans(
        tree.root_node(),
        source.as_bytes(),
        name,
        "",
        kinds,
        "",
        &mut targets,
        &mut Vec::new(),
    );
    targets
        .into_iter()
        .filter(|span| span.start_row + 1 == line as usize)
        .map(|span| span.start_byte)
        .min()
}