summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/rename
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/src/features/rename')
-rw-r--r--support/texlab/src/features/rename/command.rs32
-rw-r--r--support/texlab/src/features/rename/entry.rs57
-rw-r--r--support/texlab/src/features/rename/label.rs55
3 files changed, 68 insertions, 76 deletions
diff --git a/support/texlab/src/features/rename/command.rs b/support/texlab/src/features/rename/command.rs
index 402783f3ae..e0677f443e 100644
--- a/support/texlab/src/features/rename/command.rs
+++ b/support/texlab/src/features/rename/command.rs
@@ -1,18 +1,15 @@
use std::collections::HashMap;
-use cancellation::CancellationToken;
-use cstree::{TextRange, TextSize};
use lsp_types::{Range, RenameParams, TextEdit, WorkspaceEdit};
+use rowan::{TextRange, TextSize};
use crate::{
features::cursor::{CursorContext, HasPosition},
+ syntax::latex,
LineIndexExt,
};
-pub fn prepare_command_rename<P: HasPosition>(
- context: &CursorContext<P>,
- _cancellation_token: &CancellationToken,
-) -> Option<Range> {
+pub fn prepare_command_rename<P: HasPosition>(context: &CursorContext<P>) -> Option<Range> {
Some(
context
.request
@@ -22,20 +19,13 @@ pub fn prepare_command_rename<P: HasPosition>(
)
}
-pub fn rename_command(
- context: &CursorContext<RenameParams>,
- cancellation_token: &CancellationToken,
-) -> Option<WorkspaceEdit> {
- cancellation_token.result().ok()?;
- prepare_command_rename(context, cancellation_token)?;
+pub fn rename_command(context: &CursorContext<RenameParams>) -> Option<WorkspaceEdit> {
+ prepare_command_rename(context)?;
let name = context.cursor.as_latex()?.text();
let mut changes = HashMap::new();
- for document in &context.request.subset.documents {
- cancellation_token.result().ok()?;
-
+ for document in context.request.workspace.documents_by_uri.values() {
if let Some(data) = document.data.as_latex() {
- let edits = data
- .root
+ let edits = latex::SyntaxNode::new_root(data.green.clone())
.descendants_with_tokens()
.filter_map(|element| element.into_token())
.filter(|token| token.kind().is_command_name() && token.text() == name)
@@ -49,7 +39,7 @@ pub fn rename_command(
})
.collect();
- changes.insert(document.uri.as_ref().clone().into(), edits);
+ changes.insert(document.uri.as_ref().clone(), edits);
}
}
@@ -80,15 +70,15 @@ mod tests {
let req = tester.rename();
let context = CursorContext::new(req);
- let actual_edit = rename_command(&context, CancellationToken::none()).unwrap();
+ let actual_edit = rename_command(&context).unwrap();
let mut expected_changes = HashMap::new();
expected_changes.insert(
- uri1.as_ref().clone().into(),
+ uri1.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(0, 1, 0, 4), "qux".into())],
);
expected_changes.insert(
- uri2.as_ref().clone().into(),
+ uri2.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(0, 1, 0, 4), "qux".into())],
);
let expected_edit = WorkspaceEdit::new(expected_changes);
diff --git a/support/texlab/src/features/rename/entry.rs b/support/texlab/src/features/rename/entry.rs
index 35a77d926e..4861b63570 100644
--- a/support/texlab/src/features/rename/entry.rs
+++ b/support/texlab/src/features/rename/entry.rs
@@ -1,18 +1,18 @@
use std::collections::HashMap;
-use cancellation::CancellationToken;
use lsp_types::{Range, RenameParams, TextEdit, WorkspaceEdit};
+use rowan::ast::AstNode;
use crate::{
features::cursor::{CursorContext, HasPosition},
- syntax::{bibtex, latex, CstNode},
+ syntax::{
+ bibtex::{self, HasName},
+ latex,
+ },
DocumentData, LineIndexExt,
};
-pub fn prepare_entry_rename<P: HasPosition>(
- context: &CursorContext<P>,
- _cancellation_token: &CancellationToken,
-) -> Option<Range> {
+pub fn prepare_entry_rename<P: HasPosition>(context: &CursorContext<P>) -> Option<Range> {
let (_, range) = context
.find_citation_key_word()
.or_else(|| context.find_entry_key())?;
@@ -26,44 +26,41 @@ pub fn prepare_entry_rename<P: HasPosition>(
)
}
-pub fn rename_entry(
- context: &CursorContext<RenameParams>,
- cancellation_token: &CancellationToken,
-) -> Option<WorkspaceEdit> {
- cancellation_token.result().ok()?;
- prepare_entry_rename(context, cancellation_token)?;
+pub fn rename_entry(context: &CursorContext<RenameParams>) -> Option<WorkspaceEdit> {
+ prepare_entry_rename(context)?;
let (key_text, _) = context
.find_citation_key_word()
.or_else(|| context.find_entry_key())?;
let mut changes = HashMap::new();
- for document in &context.request.subset.documents {
- cancellation_token.result().ok()?;
+ for document in context.request.workspace.documents_by_uri.values() {
match &document.data {
DocumentData::Latex(data) => {
- let edits: Vec<_> = data
- .root
+ let edits: Vec<_> = latex::SyntaxNode::new_root(data.green.clone())
.descendants()
.filter_map(latex::Citation::cast)
.filter_map(|citation| citation.key_list())
.flat_map(|keys| keys.keys())
.filter(|key| key.to_string() == key_text)
- .map(|key| document.line_index.line_col_lsp_range(key.small_range()))
+ .map(|key| {
+ document
+ .line_index
+ .line_col_lsp_range(latex::small_range(&key))
+ })
.map(|range| TextEdit::new(range, context.request.params.new_name.clone()))
.collect();
- changes.insert(document.uri.as_ref().clone().into(), edits);
+ changes.insert(document.uri.as_ref().clone(), edits);
}
DocumentData::Bibtex(data) => {
- let edits: Vec<_> = data
- .root
+ let edits: Vec<_> = bibtex::SyntaxNode::new_root(data.green.clone())
.descendants()
.filter_map(bibtex::Entry::cast)
- .filter_map(|entry| entry.key())
- .filter(|key| key.to_string() == key_text)
- .map(|key| document.line_index.line_col_lsp_range(key.small_range()))
+ .filter_map(|entry| entry.name_token())
+ .filter(|key| key.text() == key_text)
+ .map(|key| document.line_index.line_col_lsp_range(key.text_range()))
.map(|range| TextEdit::new(range, context.request.params.new_name.clone()))
.collect();
- changes.insert(document.uri.as_ref().clone().into(), edits);
+ changes.insert(document.uri.as_ref().clone(), edits);
}
DocumentData::BuildLog(_) => {}
}
@@ -98,15 +95,15 @@ mod tests {
let request = tester.rename();
let context = CursorContext::new(request);
- let actual_edit = rename_entry(&context, CancellationToken::none()).unwrap();
+ let actual_edit = rename_entry(&context).unwrap();
let mut expected_changes = HashMap::new();
expected_changes.insert(
- uri1.as_ref().clone().into(),
+ uri1.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(0, 9, 0, 12), "qux".into())],
);
expected_changes.insert(
- uri2.as_ref().clone().into(),
+ uri2.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(1, 6, 1, 9), "qux".into())],
);
let expected_edit = WorkspaceEdit::new(expected_changes);
@@ -132,15 +129,15 @@ mod tests {
let request = tester.rename();
let context = CursorContext::new(request);
- let actual_edit = rename_entry(&context, CancellationToken::none()).unwrap();
+ let actual_edit = rename_entry(&context).unwrap();
let mut expected_changes = HashMap::new();
expected_changes.insert(
- uri1.as_ref().clone().into(),
+ uri1.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(0, 9, 0, 12), "qux".into())],
);
expected_changes.insert(
- uri2.as_ref().clone().into(),
+ uri2.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(1, 6, 1, 9), "qux".into())],
);
let expected_edit = WorkspaceEdit::new(expected_changes);
diff --git a/support/texlab/src/features/rename/label.rs b/support/texlab/src/features/rename/label.rs
index a73a0064bf..f24b86810b 100644
--- a/support/texlab/src/features/rename/label.rs
+++ b/support/texlab/src/features/rename/label.rs
@@ -1,18 +1,15 @@
use std::collections::HashMap;
-use cancellation::CancellationToken;
use lsp_types::{Range, RenameParams, TextEdit, WorkspaceEdit};
+use rowan::ast::AstNode;
use crate::{
features::cursor::{CursorContext, HasPosition},
- syntax::{latex, CstNode},
+ syntax::latex,
LineIndexExt,
};
-pub fn prepare_label_rename<P: HasPosition>(
- context: &CursorContext<P>,
- _cancellation_token: &CancellationToken,
-) -> Option<Range> {
+pub fn prepare_label_rename<P: HasPosition>(context: &CursorContext<P>) -> Option<Range> {
let (_, range) = context.find_label_name_key()?;
Some(
@@ -24,24 +21,24 @@ pub fn prepare_label_rename<P: HasPosition>(
)
}
-pub fn rename_label(
- context: &CursorContext<RenameParams>,
- cancellation_token: &CancellationToken,
-) -> Option<WorkspaceEdit> {
- prepare_label_rename(context, cancellation_token)?;
+pub fn rename_label(context: &CursorContext<RenameParams>) -> Option<WorkspaceEdit> {
+ prepare_label_rename(context)?;
let (name_text, _) = context.find_label_name_key()?;
let mut changes = HashMap::new();
- for document in &context.request.subset.documents {
- cancellation_token.result().ok()?;
+ for document in context.request.workspace.documents_by_uri.values() {
if let Some(data) = document.data.as_latex() {
let mut edits = Vec::new();
- for node in data.root.descendants() {
- if let Some(range) = latex::LabelDefinition::cast(node)
+ for node in latex::SyntaxNode::new_root(data.green.clone()).descendants() {
+ if let Some(range) = latex::LabelDefinition::cast(node.clone())
.and_then(|label| label.name())
.and_then(|name| name.key())
.filter(|name| name.to_string() == name_text)
- .map(|name| document.line_index.line_col_lsp_range(name.small_range()))
+ .map(|name| {
+ document
+ .line_index
+ .line_col_lsp_range(latex::small_range(&name))
+ })
{
edits.push(TextEdit::new(
range,
@@ -49,12 +46,16 @@ pub fn rename_label(
));
}
- latex::LabelReference::cast(node)
+ latex::LabelReference::cast(node.clone())
.and_then(|label| label.name_list())
.into_iter()
.flat_map(|label| label.keys())
.filter(|name| name.to_string() == name_text)
- .map(|name| document.line_index.line_col_lsp_range(name.small_range()))
+ .map(|name| {
+ document
+ .line_index
+ .line_col_lsp_range(latex::small_range(&name))
+ })
.for_each(|range| {
edits.push(TextEdit::new(
range,
@@ -62,14 +63,16 @@ pub fn rename_label(
));
});
- if let Some(label) = latex::LabelReferenceRange::cast(node) {
+ if let Some(label) = latex::LabelReferenceRange::cast(node.clone()) {
if let Some(name1) = label
.from()
.and_then(|name| name.key())
.filter(|name| name.to_string() == name_text)
{
edits.push(TextEdit::new(
- document.line_index.line_col_lsp_range(name1.small_range()),
+ document
+ .line_index
+ .line_col_lsp_range(latex::small_range(&name1)),
context.request.params.new_name.clone(),
));
}
@@ -80,14 +83,16 @@ pub fn rename_label(
.filter(|name| name.to_string() == name_text)
{
edits.push(TextEdit::new(
- document.line_index.line_col_lsp_range(name2.small_range()),
+ document
+ .line_index
+ .line_col_lsp_range(latex::small_range(&name2)),
context.request.params.new_name.clone(),
));
}
}
}
- changes.insert(document.uri.as_ref().clone().into(), edits);
+ changes.insert(document.uri.as_ref().clone(), edits);
}
}
@@ -119,15 +124,15 @@ mod tests {
let request = tester.rename();
let context = CursorContext::new(request);
- let actual_edit = rename_label(&context, CancellationToken::none()).unwrap();
+ let actual_edit = rename_label(&context).unwrap();
let mut expected_changes = HashMap::new();
expected_changes.insert(
- uri1.as_ref().clone().into(),
+ uri1.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(0, 7, 0, 10), "bar".into())],
);
expected_changes.insert(
- uri2.as_ref().clone().into(),
+ uri2.as_ref().clone(),
vec![TextEdit::new(Range::new_simple(0, 5, 0, 8), "bar".into())],
);
let expected_edit = WorkspaceEdit::new(expected_changes);