Refactor BibTeX AST

This commit is contained in:
Patrick Förster 2019-05-12 12:38:41 +02:00
parent 13e8aaddd5
commit e2afe54802
5 changed files with 40 additions and 40 deletions

View file

@ -51,9 +51,9 @@ impl OrderByQualityCompletionProvider {
finder.visit_root(&tree.root);
match finder.results.pop()? {
BibtexNode::Root(_) => Some(""),
BibtexNode::Preamble(preamble) => get_type_query(&preamble.kind, position),
BibtexNode::String(string) => get_type_query(&string.kind, position),
BibtexNode::Entry(entry) => get_type_query(&entry.kind, position),
BibtexNode::Preamble(preamble) => get_type_query(&preamble.ty, position),
BibtexNode::String(string) => get_type_query(&string.ty, position),
BibtexNode::Entry(entry) => get_type_query(&entry.ty, position),
BibtexNode::Comment(comment) => Some(comment.token.text()),
BibtexNode::Field(field) => {
if field.name.range().contains(position) {

View file

@ -18,9 +18,9 @@ impl BibtexDeclarationFoldingProvider {
fn fold(declaration: &BibtexDeclaration) -> Option<FoldingRange> {
let kind = match declaration {
BibtexDeclaration::Comment(_) => None,
BibtexDeclaration::Preamble(preamble) => Some(&preamble.kind),
BibtexDeclaration::String(string) => Some(&string.kind),
BibtexDeclaration::Entry(entry) => Some(&entry.kind),
BibtexDeclaration::Preamble(preamble) => Some(&preamble.ty),
BibtexDeclaration::String(string) => Some(&string.ty),
BibtexDeclaration::Entry(entry) => Some(&entry.ty),
}?;
let right = match declaration {

View file

@ -68,7 +68,7 @@ impl BibtexFormatter {
}
pub fn format_preamble(&mut self, preamble: &BibtexPreamble) {
self.format_token(&preamble.kind);
self.format_token(&preamble.ty);
self.output.push('{');
if let Some(ref content) = preamble.content {
self.format_content(content, self.output.chars().count());
@ -77,7 +77,7 @@ impl BibtexFormatter {
}
pub fn format_string(&mut self, string: &BibtexString) {
self.format_token(&string.kind);
self.format_token(&string.ty);
self.output.push('{');
if let Some(ref name) = string.name {
self.output.push_str(name.text());
@ -90,7 +90,7 @@ impl BibtexFormatter {
}
pub fn format_entry(&mut self, entry: &BibtexEntry) {
self.format_token(&entry.kind);
self.format_token(&entry.ty);
self.output.push('{');
if let Some(ref key) = entry.key {
self.output.push_str(key.text());

View file

@ -118,7 +118,7 @@ impl SyntaxNode for BibtexComment {
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BibtexPreamble {
pub range: Range,
pub kind: BibtexToken,
pub ty: BibtexToken,
pub left: Option<BibtexToken>,
pub content: Option<BibtexContent>,
pub right: Option<BibtexToken>,
@ -126,7 +126,7 @@ pub struct BibtexPreamble {
impl BibtexPreamble {
pub fn new(
kind: BibtexToken,
ty: BibtexToken,
left: Option<BibtexToken>,
content: Option<BibtexContent>,
right: Option<BibtexToken>,
@ -138,11 +138,11 @@ impl BibtexPreamble {
} else if let Some(ref left) = left {
left.end()
} else {
kind.end()
ty.end()
};
BibtexPreamble {
range: Range::new(kind.start(), end),
kind,
range: Range::new(ty.start(), end),
ty,
left,
content,
right,
@ -159,7 +159,7 @@ impl SyntaxNode for BibtexPreamble {
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BibtexString {
pub range: Range,
pub kind: BibtexToken,
pub ty: BibtexToken,
pub left: Option<BibtexToken>,
pub name: Option<BibtexToken>,
pub assign: Option<BibtexToken>,
@ -169,7 +169,7 @@ pub struct BibtexString {
impl BibtexString {
pub fn new(
kind: BibtexToken,
ty: BibtexToken,
left: Option<BibtexToken>,
name: Option<BibtexToken>,
assign: Option<BibtexToken>,
@ -187,12 +187,12 @@ impl BibtexString {
} else if let Some(ref left) = left {
left.end()
} else {
kind.end()
ty.end()
};
BibtexString {
range: Range::new(kind.start(), end),
kind,
range: Range::new(ty.start(), end),
ty,
left,
name,
assign,
@ -211,7 +211,7 @@ impl SyntaxNode for BibtexString {
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BibtexEntry {
pub range: Range,
pub kind: BibtexToken,
pub ty: BibtexToken,
pub left: Option<BibtexToken>,
pub key: Option<BibtexToken>,
pub comma: Option<BibtexToken>,
@ -221,7 +221,7 @@ pub struct BibtexEntry {
impl BibtexEntry {
pub fn new(
kind: BibtexToken,
ty: BibtexToken,
left: Option<BibtexToken>,
key: Option<BibtexToken>,
comma: Option<BibtexToken>,
@ -239,12 +239,12 @@ impl BibtexEntry {
} else if let Some(ref left) = left {
left.end()
} else {
kind.end()
ty.end()
};
BibtexEntry {
range: Range::new(kind.start(), end),
kind,
range: Range::new(ty.start(), end),
ty,
left,
key,
comma,
@ -254,7 +254,7 @@ impl BibtexEntry {
}
pub fn is_comment(&self) -> bool {
self.kind.text().to_lowercase() == "@comment"
self.ty.text().to_lowercase() == "@comment"
}
}

View file

@ -38,65 +38,65 @@ impl<I: Iterator<Item = BibtexToken>> BibtexParser<I> {
}
fn preamble(&mut self) -> BibtexPreamble {
let kind = self.tokens.next().unwrap();
let ty = self.tokens.next().unwrap();
let left = self.expect2(BibtexTokenKind::BeginBrace, BibtexTokenKind::BeginParen);
if left.is_none() {
return BibtexPreamble::new(kind, None, None, None);
return BibtexPreamble::new(ty, None, None, None);
}
if !self.can_match_content() {
return BibtexPreamble::new(kind, left, None, None);
return BibtexPreamble::new(ty, left, None, None);
}
let content = self.content();
let right = self.expect2(BibtexTokenKind::EndBrace, BibtexTokenKind::EndParen);
BibtexPreamble::new(kind, left, Some(content), right)
BibtexPreamble::new(ty, left, Some(content), right)
}
fn string(&mut self) -> BibtexString {
let kind = self.tokens.next().unwrap();
let ty = self.tokens.next().unwrap();
let left = self.expect2(BibtexTokenKind::BeginBrace, BibtexTokenKind::BeginParen);
if left.is_none() {
return BibtexString::new(kind, None, None, None, None, None);
return BibtexString::new(ty, None, None, None, None, None);
}
let name = self.expect1(BibtexTokenKind::Word);
if name.is_none() {
return BibtexString::new(kind, left, None, None, None, None);
return BibtexString::new(ty, left, None, None, None, None);
}
let assign = self.expect1(BibtexTokenKind::Assign);
if assign.is_none() {
return BibtexString::new(kind, left, name, None, None, None);
return BibtexString::new(ty, left, name, None, None, None);
}
if !self.can_match_content() {
return BibtexString::new(kind, left, name, assign, None, None);
return BibtexString::new(ty, left, name, assign, None, None);
}
let value = self.content();
let right = self.expect2(BibtexTokenKind::EndBrace, BibtexTokenKind::EndParen);
BibtexString::new(kind, left, name, assign, Some(value), right)
BibtexString::new(ty, left, name, assign, Some(value), right)
}
fn entry(&mut self) -> BibtexEntry {
let kind = self.tokens.next().unwrap();
let ty = self.tokens.next().unwrap();
let left = self.expect2(BibtexTokenKind::BeginBrace, BibtexTokenKind::BeginParen);
if left.is_none() {
return BibtexEntry::new(kind, None, None, None, Vec::new(), None);
return BibtexEntry::new(ty, None, None, None, Vec::new(), None);
}
let name = self.expect1(BibtexTokenKind::Word);
if name.is_none() {
return BibtexEntry::new(kind, left, None, None, Vec::new(), None);
return BibtexEntry::new(ty, left, None, None, Vec::new(), None);
}
let comma = self.expect1(BibtexTokenKind::Comma);
if comma.is_none() {
return BibtexEntry::new(kind, left, name, None, Vec::new(), None);
return BibtexEntry::new(ty, left, name, None, Vec::new(), None);
}
let mut fields = Vec::new();
@ -105,7 +105,7 @@ impl<I: Iterator<Item = BibtexToken>> BibtexParser<I> {
}
let right = self.expect2(BibtexTokenKind::EndBrace, BibtexTokenKind::EndParen);
BibtexEntry::new(kind, left, name, comma, fields, right)
BibtexEntry::new(ty, left, name, comma, fields, right)
}
fn field(&mut self) -> BibtexField {