use crate::syntax::*; use crate::workspace::*; use futures_boxed::boxed; use lsp_types::*; use std::collections::HashMap; use std::sync::Arc; #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct LatexCommandPrepareRenameProvider; impl FeatureProvider for LatexCommandPrepareRenameProvider { type Params = TextDocumentPositionParams; type Output = Option; #[boxed] async fn execute<'a>( &'a self, request: &'a FeatureRequest, ) -> Option { let position = request.params.position; find_command(&request.document().tree, position).map(|cmd| cmd.range()) } } #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub struct LatexCommandRenameProvider; impl FeatureProvider for LatexCommandRenameProvider { type Params = RenameParams; type Output = Option; #[boxed] async fn execute<'a>( &'a self, request: &'a FeatureRequest, ) -> Option { let command = find_command( &request.document().tree, request.params.text_document_position.position, )?; let mut changes = HashMap::new(); for document in request.related_documents() { if let SyntaxTree::Latex(tree) = &document.tree { let edits: Vec = tree .commands .iter() .filter(|cmd| cmd.name.text() == command.name.text()) .map(|cmd| { TextEdit::new(cmd.name.range(), format!("\\{}", request.params.new_name)) }) .collect(); changes.insert(document.uri.clone().into(), edits); } } Some(WorkspaceEdit::new(changes)) } } fn find_command(tree: &SyntaxTree, position: Position) -> Option> { if let SyntaxTree::Latex(tree) = tree { tree.find_command_by_name(position) } else { None } } #[cfg(test)] mod tests { use super::*; use crate::range::RangeExt; use lsp_types::{Position, Range}; #[test] fn test() { let edit = test_feature( LatexCommandRenameProvider, FeatureSpec { files: vec![ FeatureSpec::file("foo.tex", "\\include{bar.tex}\n\\baz"), FeatureSpec::file("bar.tex", "\\baz"), ], main_file: "foo.tex", position: Position::new(1, 2), new_name: "qux", ..FeatureSpec::default() }, ); let mut changes = HashMap::new(); changes.insert( FeatureSpec::uri("foo.tex"), vec![TextEdit::new(Range::new_simple(1, 0, 1, 4), "\\qux".into())], ); changes.insert( FeatureSpec::uri("bar.tex"), vec![TextEdit::new(Range::new_simple(0, 0, 0, 4), "\\qux".into())], ); assert_eq!(edit, Some(WorkspaceEdit::new(changes))); } #[test] fn test_bibtex() { let edit = test_feature( LatexCommandRenameProvider, FeatureSpec { files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar = baz}")], main_file: "foo.bib", position: Position::new(0, 14), new_name: "qux", ..FeatureSpec::default() }, ); assert_eq!(edit, None); } }