summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/rename
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2021-06-04 03:02:48 +0000
committerNorbert Preining <norbert@preining.info>2021-06-04 03:02:48 +0000
commita5ae129f3aa3a3a0aeaa21c55b75a1d543fe4111 (patch)
treefe5e1e212aec203e668df8581a17374cdde352a1 /support/texlab/src/features/rename
parentd231c6331f53a048d29c2f847254a9f56d5ee2dd (diff)
CTAN sync 202106040302
Diffstat (limited to 'support/texlab/src/features/rename')
-rw-r--r--support/texlab/src/features/rename/entry.rs35
-rw-r--r--support/texlab/src/features/rename/label.rs37
2 files changed, 28 insertions, 44 deletions
diff --git a/support/texlab/src/features/rename/entry.rs b/support/texlab/src/features/rename/entry.rs
index 14fbfd42d7..35a77d926e 100644
--- a/support/texlab/src/features/rename/entry.rs
+++ b/support/texlab/src/features/rename/entry.rs
@@ -4,7 +4,7 @@ use cancellation::CancellationToken;
use lsp_types::{Range, RenameParams, TextEdit, WorkspaceEdit};
use crate::{
- features::cursor::{Cursor, CursorContext, HasPosition},
+ features::cursor::{CursorContext, HasPosition},
syntax::{bibtex, latex, CstNode},
DocumentData, LineIndexExt,
};
@@ -13,18 +13,9 @@ pub fn prepare_entry_rename<P: HasPosition>(
context: &CursorContext<P>,
_cancellation_token: &CancellationToken,
) -> Option<Range> {
- let range = match &context.cursor {
- Cursor::Latex(token) if token.kind() == latex::WORD => {
- let group = latex::CurlyGroupWordList::cast(token.parent())?;
- latex::Citation::cast(group.syntax().parent()?)?;
- token.text_range()
- }
- Cursor::Bibtex(token) if token.kind() == bibtex::WORD => {
- bibtex::Entry::cast(token.parent())?;
- token.text_range()
- }
- _ => return None,
- };
+ let (_, range) = context
+ .find_citation_key_word()
+ .or_else(|| context.find_entry_key())?;
Some(
context
@@ -41,11 +32,9 @@ pub fn rename_entry(
) -> Option<WorkspaceEdit> {
cancellation_token.result().ok()?;
prepare_entry_rename(context, cancellation_token)?;
- let key_text = context
- .cursor
- .as_latex()
- .map(|token| token.text())
- .or_else(|| context.cursor.as_bibtex().map(|token| token.text()))?;
+ 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 {
@@ -57,9 +46,9 @@ pub fn rename_entry(
.descendants()
.filter_map(latex::Citation::cast)
.filter_map(|citation| citation.key_list())
- .flat_map(|keys| keys.words())
- .filter(|key| key.text() == key_text)
- .map(|key| document.line_index.line_col_lsp_range(key.text_range()))
+ .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(|range| TextEdit::new(range, context.request.params.new_name.clone()))
.collect();
changes.insert(document.uri.as_ref().clone().into(), edits);
@@ -70,8 +59,8 @@ pub fn rename_entry(
.descendants()
.filter_map(bibtex::Entry::cast)
.filter_map(|entry| entry.key())
- .filter(|key| key.text() == key_text)
- .map(|key| document.line_index.line_col_lsp_range(key.text_range()))
+ .filter(|key| key.to_string() == key_text)
+ .map(|key| document.line_index.line_col_lsp_range(key.small_range()))
.map(|range| TextEdit::new(range, context.request.params.new_name.clone()))
.collect();
changes.insert(document.uri.as_ref().clone().into(), edits);
diff --git a/support/texlab/src/features/rename/label.rs b/support/texlab/src/features/rename/label.rs
index 11b414ccfa..a73a0064bf 100644
--- a/support/texlab/src/features/rename/label.rs
+++ b/support/texlab/src/features/rename/label.rs
@@ -13,20 +13,14 @@ pub fn prepare_label_rename<P: HasPosition>(
context: &CursorContext<P>,
_cancellation_token: &CancellationToken,
) -> Option<Range> {
- let name = context.cursor.as_latex()?;
- name.parent().parent().filter(|node| {
- matches!(
- node.kind(),
- latex::LABEL_DEFINITION | latex::LABEL_REFERENCE | latex::LABEL_REFERENCE_RANGE
- )
- })?;
+ let (_, range) = context.find_label_name_key()?;
Some(
context
.request
.main_document()
.line_index
- .line_col_lsp_range(name.text_range()),
+ .line_col_lsp_range(range),
)
}
@@ -35,7 +29,8 @@ pub fn rename_label(
cancellation_token: &CancellationToken,
) -> Option<WorkspaceEdit> {
prepare_label_rename(context, cancellation_token)?;
- let name_text = context.cursor.as_latex()?.text();
+ let (name_text, _) = context.find_label_name_key()?;
+
let mut changes = HashMap::new();
for document in &context.request.subset.documents {
cancellation_token.result().ok()?;
@@ -44,9 +39,9 @@ pub fn rename_label(
for node in data.root.descendants() {
if let Some(range) = latex::LabelDefinition::cast(node)
.and_then(|label| label.name())
- .and_then(|name| name.word())
- .filter(|name| name.text() == name_text)
- .map(|name| document.line_index.line_col_lsp_range(name.text_range()))
+ .and_then(|name| name.key())
+ .filter(|name| name.to_string() == name_text)
+ .map(|name| document.line_index.line_col_lsp_range(name.small_range()))
{
edits.push(TextEdit::new(
range,
@@ -57,9 +52,9 @@ pub fn rename_label(
latex::LabelReference::cast(node)
.and_then(|label| label.name_list())
.into_iter()
- .flat_map(|label| label.words())
- .filter(|name| name.text() == name_text)
- .map(|name| document.line_index.line_col_lsp_range(name.text_range()))
+ .flat_map(|label| label.keys())
+ .filter(|name| name.to_string() == name_text)
+ .map(|name| document.line_index.line_col_lsp_range(name.small_range()))
.for_each(|range| {
edits.push(TextEdit::new(
range,
@@ -70,22 +65,22 @@ pub fn rename_label(
if let Some(label) = latex::LabelReferenceRange::cast(node) {
if let Some(name1) = label
.from()
- .and_then(|name| name.word())
- .filter(|name| name.text() == name_text)
+ .and_then(|name| name.key())
+ .filter(|name| name.to_string() == name_text)
{
edits.push(TextEdit::new(
- document.line_index.line_col_lsp_range(name1.text_range()),
+ document.line_index.line_col_lsp_range(name1.small_range()),
context.request.params.new_name.clone(),
));
}
if let Some(name2) = label
.from()
- .and_then(|name| name.word())
- .filter(|name| name.text() == name_text)
+ .and_then(|name| name.key())
+ .filter(|name| name.to_string() == name_text)
{
edits.push(TextEdit::new(
- document.line_index.line_col_lsp_range(name2.text_range()),
+ document.line_index.line_col_lsp_range(name2.small_range()),
context.request.params.new_name.clone(),
));
}