summaryrefslogtreecommitdiff
path: root/support/texlab/src/syntax/latex/printer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'support/texlab/src/syntax/latex/printer.rs')
-rw-r--r--support/texlab/src/syntax/latex/printer.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/support/texlab/src/syntax/latex/printer.rs b/support/texlab/src/syntax/latex/printer.rs
new file mode 100644
index 0000000000..ce03778033
--- /dev/null
+++ b/support/texlab/src/syntax/latex/printer.rs
@@ -0,0 +1,77 @@
+use super::ast::*;
+use crate::syntax::text::*;
+use lsp_types::Position;
+use std::sync::Arc;
+
+#[derive(Debug)]
+pub struct LatexPrinter {
+ pub output: String,
+ position: Position,
+}
+
+impl LatexPrinter {
+ pub fn new(start_position: Position) -> Self {
+ Self {
+ output: String::new(),
+ position: start_position,
+ }
+ }
+
+ fn synchronize(&mut self, position: Position) {
+ while self.position.line < position.line {
+ self.output.push('\n');
+ self.position.line += 1;
+ }
+
+ while self.position.character < position.character {
+ self.output.push(' ');
+ self.position.character += 1;
+ }
+ }
+
+ fn print_token(&mut self, token: &LatexToken) {
+ self.synchronize(token.start());
+ self.output.push_str(token.text());
+ self.position.character += token.end().character - token.start().character;
+ self.synchronize(token.end());
+ }
+}
+
+impl LatexVisitor for LatexPrinter {
+ fn visit_root(&mut self, root: Arc<LatexRoot>) {
+ for child in &root.children {
+ child.accept(self);
+ }
+ }
+
+ fn visit_group(&mut self, group: Arc<LatexGroup>) {
+ self.print_token(&group.left);
+ for child in &group.children {
+ child.accept(self);
+ }
+ if let Some(right) = &group.right {
+ self.print_token(right);
+ }
+ }
+
+ fn visit_command(&mut self, command: Arc<LatexCommand>) {
+ self.print_token(&command.name);
+ for group in &command.groups {
+ self.visit_group(Arc::clone(&group));
+ }
+ }
+
+ fn visit_text(&mut self, text: Arc<LatexText>) {
+ for word in &text.words {
+ self.print_token(word);
+ }
+ }
+
+ fn visit_comma(&mut self, comma: Arc<LatexComma>) {
+ self.print_token(&comma.token);
+ }
+
+ fn visit_math(&mut self, math: Arc<LatexMath>) {
+ self.print_token(&math.token)
+ }
+}