diff options
Diffstat (limited to 'support/texlab/src/features/formatting/bibtex_internal.rs')
-rw-r--r-- | support/texlab/src/features/formatting/bibtex_internal.rs | 132 |
1 files changed, 50 insertions, 82 deletions
diff --git a/support/texlab/src/features/formatting/bibtex_internal.rs b/support/texlab/src/features/formatting/bibtex_internal.rs index 641bee0cfe..58aaf0635d 100644 --- a/support/texlab/src/features/formatting/bibtex_internal.rs +++ b/support/texlab/src/features/formatting/bibtex_internal.rs @@ -1,19 +1,14 @@ -use cancellation::CancellationToken; -use cstree::NodeOrToken; use lsp_types::{DocumentFormattingParams, TextEdit}; +use rowan::{ast::AstNode, NodeOrToken}; use crate::{ features::FeatureRequest, - syntax::{ - bibtex::{self, HasType}, - CstNode, - }, + syntax::bibtex::{self, HasName, HasType, HasValue}, LineIndex, LineIndexExt, }; pub fn format_bibtex_internal( request: &FeatureRequest<DocumentFormattingParams>, - _cancellation_token: &CancellationToken, ) -> Option<Vec<TextEdit>> { let mut indent = String::new(); if request.params.options.insert_spaces { @@ -24,37 +19,34 @@ pub fn format_bibtex_internal( indent.push('\t'); } - let line_length = { - request - .context - .options - .read() - .unwrap() - .formatter_line_length - .map(|value| { - if value <= 0 { - usize::MAX - } else { - value as usize - } - }) - .unwrap_or(80) - }; + let line_length = request + .workspace + .environment + .options + .formatter_line_length + .map(|value| { + if value <= 0 { + usize::MAX + } else { + value as usize + } + }) + .unwrap_or(80); let document = request.main_document(); let data = document.data.as_bibtex()?; let mut edits = Vec::new(); - for node in data.root.children() { - let range = if let Some(entry) = bibtex::Entry::cast(node) { - entry.small_range() - } else if let Some(string) = bibtex::String::cast(node) { - string.small_range() - } else if let Some(preamble) = bibtex::Preamble::cast(node) { - preamble.small_range() - } else { - continue; - }; + for node in bibtex::SyntaxNode::new_root(data.green.clone()) + .children() + .filter(|node| { + matches!( + node.kind(), + bibtex::PREAMBLE | bibtex::STRING | bibtex::ENTRY + ) + }) + { + let range = node.text_range(); let mut formatter = Formatter::new( indent.clone(), @@ -115,13 +107,13 @@ impl<'a> Formatter<'a> { .count() } - fn visit_node(&mut self, parent: &bibtex::SyntaxNode) { + fn visit_node(&mut self, parent: bibtex::SyntaxNode) { match parent.kind() { bibtex::PREAMBLE => { let preamble = bibtex::Preamble::cast(parent).unwrap(); - self.visit_token_lowercase(preamble.ty().unwrap()); + self.visit_token_lowercase(&preamble.type_token().unwrap()); self.output.push('{'); - if preamble.syntax().arity() > 0 { + if preamble.syntax().children().next().is_some() { self.align.push(self.base_align()); for node in preamble.syntax().children() { self.visit_node(node); @@ -130,29 +122,29 @@ impl<'a> Formatter<'a> { } } bibtex::STRING => { - let string = bibtex::String::cast(parent).unwrap(); - self.visit_token_lowercase(string.ty().unwrap()); + let string = bibtex::StringDef::cast(parent).unwrap(); + self.visit_token_lowercase(&string.type_token().unwrap()); self.output.push('{'); - if let Some(name) = string.name() { + if let Some(name) = string.name_token() { self.output.push_str(name.text()); self.output.push_str(" = "); if let Some(value) = string.value() { self.align.push(self.base_align()); - self.visit_node(value.syntax()); + self.visit_node(value.syntax().clone()); self.output.push('}'); } } } bibtex::ENTRY => { let entry = bibtex::Entry::cast(parent).unwrap(); - self.visit_token_lowercase(entry.ty().unwrap()); + self.visit_token_lowercase(&entry.type_token().unwrap()); self.output.push('{'); - if let Some(key) = entry.key() { + if let Some(key) = entry.name_token() { self.output.push_str(&key.to_string()); self.output.push(','); self.output.push('\n'); for field in entry.fields() { - self.visit_node(field.syntax()); + self.visit_node(field.syntax().clone()); } self.output.push('}'); } @@ -160,18 +152,18 @@ impl<'a> Formatter<'a> { bibtex::FIELD => { let field = bibtex::Field::cast(parent).unwrap(); self.output.push_str(&self.indent); - let name = field.name().unwrap(); + let name = field.name_token().unwrap(); self.output.push_str(name.text()); self.output.push_str(" = "); if let Some(value) = field.value() { let count = name.text().chars().count(); self.align.push(self.tab_size as usize + count + 3); - self.visit_node(value.syntax()); + self.visit_node(value.syntax().clone()); self.output.push(','); self.output.push('\n'); } } - bibtex::VALUE => { + kind if bibtex::Value::can_cast(kind) => { let tokens: Vec<_> = parent .descendants_with_tokens() .filter_map(|element| element.into_token()) @@ -183,8 +175,8 @@ impl<'a> Formatter<'a> { let align = self.align.pop().unwrap_or_default(); let mut length = align + tokens[0].text().chars().count(); for i in 1..tokens.len() { - let previous = tokens[i - 1]; - let current = tokens[i]; + let previous = &tokens[i - 1]; + let current = &tokens[i]; let current_length = current.text().chars().count(); let insert_space = self.should_insert_space(previous, current); @@ -205,7 +197,7 @@ impl<'a> Formatter<'a> { length += current_length; } } - bibtex::ROOT | bibtex::JUNK | bibtex::COMMENT => { + bibtex::ROOT | bibtex::JUNK => { for element in parent.children_with_tokens() { match element { NodeOrToken::Token(token) => { @@ -241,10 +233,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -260,7 +249,7 @@ mod tests { .build() .formatting(); - let mut edits = format_bibtex_internal(&request, CancellationToken::none()).unwrap(); + let mut edits = format_bibtex_internal(&request).unwrap(); let edit2 = edits.pop().unwrap(); let edit1 = edits.pop().unwrap(); @@ -275,10 +264,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -291,10 +277,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -307,10 +290,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -323,10 +303,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -339,10 +316,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -355,10 +329,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } @@ -371,10 +342,7 @@ mod tests { .build() .formatting(); - let edit = format_bibtex_internal(&request, CancellationToken::none()) - .unwrap() - .pop() - .unwrap(); + let edit = format_bibtex_internal(&request).unwrap().pop().unwrap(); assert_display_snapshot!(edit.new_text); } |