Skip to content

Commit

Permalink
Refactor BibTeX AST
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 12, 2019
1 parent 13e8aad commit e2afe54
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/completion/quality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/folding/bibtex_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
32 changes: 16 additions & 16 deletions src/syntax/bibtex/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ 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>,
}

impl BibtexPreamble {
pub fn new(
kind: BibtexToken,
ty: BibtexToken,
left: Option<BibtexToken>,
content: Option<BibtexContent>,
right: Option<BibtexToken>,
Expand All @@ -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,
Expand All @@ -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>,
Expand All @@ -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>,
Expand All @@ -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,
Expand All @@ -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>,
Expand All @@ -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>,
Expand All @@ -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,
Expand All @@ -254,7 +254,7 @@ impl BibtexEntry {
}

pub fn is_comment(&self) -> bool {
self.kind.text().to_lowercase() == "@comment"
self.ty.text().to_lowercase() == "@comment"
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/syntax/bibtex/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down

0 comments on commit e2afe54

Please sign in to comment.