revert: "feat: rename labels (#1858)" (#2084)

This reverts commit 14c7a92944. Didn't
pass the test `label_indir.typ.snap`.
This commit is contained in:
Myriad-Dreamin 2025-08-26 14:07:42 +08:00 committed by GitHub
parent 2cbf081408
commit 856a1e4485
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 46 additions and 255 deletions

View file

@ -1,7 +0,0 @@
/// compile: true
#set heading(numbering: "1.")
= Labeled <title_label>
/* position after */ @title_label

View file

@ -1,5 +0,0 @@
/// compile: true
#let test1(body) = figure(body)
#test1([Test1]) <fig:test1>
/* position after */ @fig:test1

View file

@ -1,9 +0,0 @@
/// compile: true
#let test1(body) = figure(body)
#test1([Test1]) <fig:test1>
@fig:test1
#let test2(body) = test1(body)
#test2([Test2]) <fig:test2>
/* position after */ @fig:test2

View file

@ -1,9 +0,0 @@
---
source: crates/tinymist-query/src/prepare_rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/label.typ
---
{
"placeholder": "title_label",
"range": "6:21:6:33"
}

View file

@ -1,9 +0,0 @@
---
source: crates/tinymist-query/src/prepare_rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/label_indir.typ
---
{
"placeholder": "fig:test1",
"range": "4:21:4:31"
}

View file

@ -1,9 +0,0 @@
---
source: crates/tinymist-query/src/prepare_rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/label_indir2.typ
---
{
"placeholder": "fig:test2",
"range": "8:21:8:31"
}

View file

@ -1,29 +0,0 @@
---
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/label.typ
---
{
"changeAnnotations": {
"Typst Rename Labels": {
"description": "The language server searched the labels ambiguously",
"label": "Typst Rename Labels",
"needsConfirmation": true
}
},
"documentChanges": [
{
"edits": [
{
"annotationId": "Typst Rename Labels",
"newText": "new_name",
"range": "6:21:6:33"
}
],
"textDocument": {
"uri": "s0.typ",
"version": null
}
}
]
}

View file

@ -1,29 +0,0 @@
---
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/label_indir.typ
---
{
"changeAnnotations": {
"Typst Rename Labels": {
"description": "The language server searched the labels ambiguously",
"label": "Typst Rename Labels",
"needsConfirmation": true
}
},
"documentChanges": [
{
"edits": [
{
"annotationId": "Typst Rename Labels",
"newText": "new_name",
"range": "4:21:4:31"
}
],
"textDocument": {
"uri": "s0.typ",
"version": null
}
}
]
}

View file

@ -1,29 +0,0 @@
---
source: crates/tinymist-query/src/rename.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/rename/label_indir2.typ
---
{
"changeAnnotations": {
"Typst Rename Labels": {
"description": "The language server searched the labels ambiguously",
"label": "Typst Rename Labels",
"needsConfirmation": true
}
},
"documentChanges": [
{
"edits": [
{
"annotationId": "Typst Rename Labels",
"newText": "new_name",
"range": "8:21:8:31"
}
],
"textDocument": {
"uri": "s0.typ",
"version": null
}
}
]
}

View file

@ -38,53 +38,42 @@ impl StatefulRequest for PrepareRenameRequest {
let doc = graph.snap.success_doc.as_ref();
let source = ctx.source_by_path(&self.path).ok()?;
let syntax = ctx.classify_for_decl(&source, self.position)?;
if bad_syntax(&syntax) {
if matches!(syntax.node().kind(), SyntaxKind::FieldAccess) {
// todo: rename field access
log::info!("prepare_rename: field access is not a definition site");
return None;
}
let origin_selection_range = ctx.to_lsp_range(syntax.node().range(), &source);
let def = ctx.def_of_syntax(&source, doc, syntax.clone())?;
let name = prepare_renaming(&syntax, &def)?;
let (name, range) = prepare_renaming(&syntax, &def)?;
Some(PrepareRenameResponse::RangeWithPlaceholder {
range: origin_selection_range,
range: range.unwrap_or(origin_selection_range),
placeholder: name,
})
}
}
fn bad_syntax(syntax: &SyntaxClass) -> bool {
if matches!(syntax.node().kind(), SyntaxKind::FieldAccess) {
// todo: rename field access
log::info!("prepare_rename: field access is not a definition site");
return true;
}
if syntax.contains_error() {
return true;
}
false
}
pub(crate) fn prepare_renaming(syntax: &SyntaxClass, def: &Definition) -> Option<String> {
if bad_syntax(syntax) {
return None;
}
pub(crate) fn prepare_renaming(
deref_target: &SyntaxClass,
def: &Definition,
) -> Option<(String, Option<LspRange>)> {
let name = def.name().clone();
let def_fid = def.file_id()?;
if WorkspaceResolver::is_package_file(def_fid) {
crate::log_debug_ct!(
"prepare_rename: is in a package {pkg:?}, def: {def:?}",
"prepare_rename: {name} is in a package {pkg:?}",
pkg = def_fid.package(),
);
return None;
}
let decl_name = || def.name().clone().to_string();
let var_rename = || Some((name.to_string(), None));
crate::log_debug_ct!("prepare_rename: {name}");
use Decl::*;
match def.decl.as_ref() {
// Cannot rename headings or blocks
@ -93,17 +82,17 @@ pub(crate) fn prepare_renaming(syntax: &SyntaxClass, def: &Definition) -> Option
// LexicalKind::Mod(Star) => None,
// Cannot rename expression import
// LexicalKind::Mod(Module(ModSrc::Expr(..))) => None,
Var(..) | Label(..) | ContentRef(..) => Some(decl_name()),
Func(..) | Closure(..) => validate_fn_renaming(def).map(|_| decl_name()),
Var(..) => var_rename(),
Func(..) | Closure(..) => validate_fn_renaming(def).map(|_| (name.to_string(), None)),
Module(..) | ModuleAlias(..) | PathStem(..) | ImportPath(..) | IncludePath(..)
| ModuleImport(..) => {
let node = syntax.node().get().clone();
let node = deref_target.node().get().clone();
let path = node.cast::<ast::Str>()?;
let name = path.get().to_string();
Some(name)
Some((name, None))
}
// todo: bibkey renaming
BibEntry(..) => None,
// todo: label renaming, bibkey renaming
BibEntry(..) | Label(..) | ContentRef(..) => None,
ImportAlias(..) | Constant(..) | IdentRef(..) | Import(..) | StrName(..) | Spread(..) => {
None
}
@ -146,16 +135,13 @@ mod tests {
snapshot_testing("rename", &|ctx, path| {
let source = ctx.source_by_path(&path).unwrap();
let docs = find_module_level_docs(&source).unwrap_or_default();
let properties = get_test_properties(&docs);
let graph = compile_doc_for_test(ctx, &properties);
let request = PrepareRenameRequest {
path: path.clone(),
position: find_test_position(&source),
};
let snap = WorldComputeGraph::from_world(ctx.world.clone());
let result = request.request(ctx, graph);
let result = request.request(ctx, snap);
assert_snapshot!(JsonRepr::new_redacted(result, &REDACT_LOC));
});
}

View file

@ -1,6 +1,6 @@
use lsp_types::{
AnnotatedTextEdit, ChangeAnnotation, DocumentChangeOperation, DocumentChanges, OneOf,
OptionalVersionedTextDocumentIdentifier, RenameFile, TextDocumentEdit,
DocumentChangeOperation, DocumentChanges, OneOf, OptionalVersionedTextDocumentIdentifier,
RenameFile, TextDocumentEdit,
};
use rustc_hash::FxHashSet;
use tinymist_std::path::{PathClean, unix_slash};
@ -78,7 +78,7 @@ impl StatefulRequest for RenameRequest {
let mut edits: HashMap<Url, Vec<TextEdit>> = HashMap::new();
do_rename_file(ctx, def_fid, diff, &mut edits);
let mut document_changes = edits_to_document_changes(edits, None);
let mut document_changes = edits_to_document_changes(edits);
document_changes.push(lsp_types::DocumentChangeOperation::Op(
lsp_types::ResourceOp::Rename(RenameFile {
@ -96,8 +96,6 @@ impl StatefulRequest for RenameRequest {
})
}
_ => {
let is_label = matches!(def.decl.kind(), DefKind::Reference);
let references = find_references(ctx, &source, doc, syntax)?;
let mut edits = HashMap::new();
@ -112,30 +110,12 @@ impl StatefulRequest for RenameRequest {
});
}
crate::log_debug_ct!("rename edits: {edits:?}");
log::info!("rename edits: {edits:?}");
if !is_label {
Some(WorkspaceEdit {
changes: Some(edits),
..Default::default()
})
} else {
let change_id = "Typst Rename Labels";
let document_changes = edits_to_document_changes(edits, Some(change_id));
let change_annotations = Some(create_change_annotation(
change_id,
true,
Some("The language server searched the labels ambiguously".to_string()),
));
Some(WorkspaceEdit {
document_changes: Some(DocumentChanges::Operations(document_changes)),
change_annotations,
..Default::default()
})
}
Some(WorkspaceEdit {
changes: Some(edits),
..Default::default()
})
}
}
}
@ -322,47 +302,19 @@ impl RenameFileWorker<'_> {
pub(crate) fn edits_to_document_changes(
edits: HashMap<Url, Vec<TextEdit>>,
change_id: Option<&str>,
) -> Vec<DocumentChangeOperation> {
let mut document_changes = vec![];
for (uri, edits) in edits {
document_changes.push(lsp_types::DocumentChangeOperation::Edit(TextDocumentEdit {
text_document: OptionalVersionedTextDocumentIdentifier { uri, version: None },
edits: edits
.into_iter()
.map(|edit| match change_id {
Some(change_id) => OneOf::Right(AnnotatedTextEdit {
text_edit: edit,
annotation_id: change_id.to_owned(),
}),
None => OneOf::Left(edit),
})
.collect(),
edits: edits.into_iter().map(OneOf::Left).collect(),
}));
}
document_changes
}
pub(crate) fn create_change_annotation(
label: &str,
needs_confirmation: bool,
description: Option<String>,
) -> HashMap<String, ChangeAnnotation> {
let mut change_annotations = HashMap::new();
change_annotations.insert(
label.to_owned(),
ChangeAnnotation {
label: label.to_owned(),
needs_confirmation: Some(needs_confirmation),
description,
},
);
change_annotations
}
#[cfg(test)]
mod tests {
use super::*;
@ -373,17 +325,14 @@ mod tests {
snapshot_testing("rename", &|ctx, path| {
let source = ctx.source_by_path(&path).unwrap();
let docs = find_module_level_docs(&source).unwrap_or_default();
let properties = get_test_properties(&docs);
let graph = compile_doc_for_test(ctx, &properties);
let request = RenameRequest {
path: path.clone(),
position: find_test_position(&source),
new_name: "new_name".to_string(),
};
let snap = WorldComputeGraph::from_world(ctx.world.clone());
let mut result = request.request(ctx, graph);
let mut result = request.request(ctx, snap);
// sort the edits to make the snapshot stable
if let Some(r) = result.as_mut().and_then(|r| r.changes.as_mut()) {
for edits in r.values_mut() {

View file

@ -1,4 +1,6 @@
use crate::{create_change_annotation, do_rename_file, edits_to_document_changes, prelude::*};
use lsp_types::ChangeAnnotation;
use crate::{do_rename_file, edits_to_document_changes, prelude::*};
/// Handle [`workspace/willRenameFiles`] request is sent from the client to the
/// server.
@ -35,21 +37,25 @@ impl StatefulRequest for WillRenameFilesRequest {
})
.collect::<Option<Vec<()>>>()?;
log::info!("did rename edits: {edits:?}");
let document_changes = edits_to_document_changes(edits, None);
let document_changes = edits_to_document_changes(edits);
if document_changes.is_empty() {
return None;
}
let change_annotations = Some(create_change_annotation(
"Typst Rename Files",
true,
Some("Renaming files should update imports".to_string()),
));
let mut change_annotations = HashMap::new();
change_annotations.insert(
"Typst Rename Files".to_string(),
ChangeAnnotation {
label: "Typst Rename Files".to_string(),
needs_confirmation: Some(true),
description: Some("Rename files should update imports".to_string()),
},
);
Some(WorkspaceEdit {
changes: None,
document_changes: Some(lsp_types::DocumentChanges::Operations(document_changes)),
change_annotations,
change_annotations: Some(change_annotations),
})
}
}