From 4e62f4480cbd1509668509e81696f74f58aa66d3 Mon Sep 17 00:00:00 2001 From: hatoo Date: Tue, 12 Sep 2023 20:27:51 +0900 Subject: [PATCH] refactor --- src/main.rs | 14 +++++++------- src/src_tree.rs | 12 ++++++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7642a69..4cbbc7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -204,11 +204,11 @@ impl LanguageServer for Backend { .get(¶ms.text_document.uri) .ok_or_else(Error::internal_error)?; - let tree = &src_tree.tree; + let tree = src_tree.tree(); let root_node = tree.root_node(); let highlights = highlighter - .highlight(&language_config, src_tree.src.as_bytes(), None, |_| None) + .highlight(&language_config, src_tree.src().as_bytes(), None, |_| None) .map_err(|_| Error::internal_error())?; let mut current_hightlight: Option = None; @@ -273,7 +273,7 @@ impl LanguageServer for Backend { .ok_or_else(Error::internal_error)?; let fmt = src_tree.formatted(params.options.tab_size as usize); - let lines = src_tree.tree.root_node().end_position().row; + let lines = src_tree.tree().root_node().end_position().row; Ok(Some(vec![TextEdit { range: Range { @@ -324,7 +324,7 @@ impl LanguageServer for Backend { .get(&pos.text_document.uri) .ok_or_else(|| Error::invalid_params("unknown uri"))?; - let root = src_tree.tree.root_node(); + let root = src_tree.tree().root_node(); let posisiton = tree_sitter::Point { row: pos.position.line as _, @@ -338,14 +338,14 @@ impl LanguageServer for Backend { let mut markdown = String::new(); if let Some(definition) = src_tree.definition(node) { - let definition = definition.utf8_text(src_tree.src.as_bytes()).unwrap(); + let definition = definition.utf8_text(src_tree.src().as_bytes()).unwrap(); markdown.push_str(&format!( "#### Definition\n\n```egglog\n{}\n```\n", definition )); } - if let Some(desugar_result) = desugar_node(node, &src_tree.src) { + if let Some(desugar_result) = desugar_node(node, src_tree.src()) { markdown.push_str(&format!( "#### Desugar\n\n```egglog\n{}\n```", desugar_result @@ -401,7 +401,7 @@ impl LanguageServer for Backend { .get(¶ms.text_document_position_params.text_document.uri) .ok_or_else(|| Error::invalid_params("unknown uri"))?; - let root = src_tree.tree.root_node(); + let root = src_tree.tree().root_node(); let p = tree_sitter::Point { row: params.text_document_position_params.position.line as _, column: params.text_document_position_params.position.character as _, diff --git a/src/src_tree.rs b/src/src_tree.rs index 8ef4407..524e1f4 100644 --- a/src/src_tree.rs +++ b/src/src_tree.rs @@ -4,9 +4,9 @@ use tree_sitter_traversal::traverse; #[derive(Debug)] pub struct SrcTree { - pub src: String, + src: String, // Must be always matched to `src` - pub tree: Tree, + tree: Tree, } impl SrcTree { @@ -25,6 +25,14 @@ impl SrcTree { Self { src, tree } } + pub fn src(&self) -> &str { + &self.src + } + + pub fn tree(&self) -> &Tree { + &self.tree + } + pub fn global_types(&self) -> Vec { let queries = &[ Query::new(