From d7c5a6f3081c2e7266620779d3c32067f947b959 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Aug 2018 15:07:43 +0300 Subject: Start lang server --- libeditor/src/lib.rs | 165 ++++++++++++++++++++++++--------------------------- 1 file changed, 77 insertions(+), 88 deletions(-) (limited to 'libeditor/src/lib.rs') diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs index 5ebb49139..817a2d15b 100644 --- a/libeditor/src/lib.rs +++ b/libeditor/src/lib.rs @@ -6,13 +6,10 @@ use libsyntax2::{ SyntaxNodeRef, AstNode, algo::walk, SyntaxKind::*, + ast, }; pub use libsyntax2::{TextRange, TextUnit}; -pub struct File { - inner: libsyntax2::File -} - #[derive(Debug)] pub struct HighlightedRange { pub range: TextRange, @@ -44,103 +41,95 @@ pub enum RunnableKind { Bin, } -impl File { - pub fn new(text: &str) -> File { - File { - inner: libsyntax2::File::parse(text) - } +pub fn highlight(file: &ast::File) -> Vec { + let syntax = file.syntax(); + let mut res = Vec::new(); + for node in walk::preorder(syntax.as_ref()) { + let tag = match node.kind() { + ERROR => "error", + COMMENT | DOC_COMMENT => "comment", + STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", + ATTR => "attribute", + NAME_REF => "text", + NAME => "function", + INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", + LIFETIME => "parameter", + k if k.is_keyword() => "keyword", + _ => continue, + }; + res.push(HighlightedRange { + range: node.range(), + tag, + }) } + res +} - pub fn highlight(&self) -> Vec { - let syntax = self.inner.syntax(); - let mut res = Vec::new(); - for node in walk::preorder(syntax.as_ref()) { - let tag = match node.kind() { - ERROR => "error", - COMMENT | DOC_COMMENT => "comment", - STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", - ATTR => "attribute", - NAME_REF => "text", - NAME => "function", - INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", - LIFETIME => "parameter", - k if k.is_keyword() => "keyword", - _ => continue, - }; - res.push(HighlightedRange { - range: node.range(), - tag, - }) - } - res - } +pub fn diagnostics(file: &ast::File) -> Vec { + let syntax = file.syntax(); + let mut res = Vec::new(); - pub fn diagnostics(&self) -> Vec { - let syntax = self.inner.syntax(); - let mut res = Vec::new(); - - for node in walk::preorder(syntax.as_ref()) { - if node.kind() == ERROR { - res.push(Diagnostic { - range: node.range(), - msg: "Syntax Error".to_string(), - }); - } + for node in walk::preorder(syntax.as_ref()) { + if node.kind() == ERROR { + res.push(Diagnostic { + range: node.range(), + msg: "Syntax Error".to_string(), + }); } - res.extend(self.inner.errors().into_iter().map(|err| Diagnostic { - range: TextRange::offset_len(err.offset, 1.into()), - msg: err.msg, - })); - res } + res.extend(file.errors().into_iter().map(|err| Diagnostic { + range: TextRange::offset_len(err.offset, 1.into()), + msg: err.msg, + })); + res +} - pub fn syntax_tree(&self) -> String { - ::libsyntax2::utils::dump_tree(&self.inner.syntax()) - } +pub fn syntax_tree(file: &ast::File) -> String { + ::libsyntax2::utils::dump_tree(&file.syntax()) +} - pub fn symbols(&self) -> Vec { - let syntax = self.inner.syntax(); - let res: Vec = walk::preorder(syntax.as_ref()) - .filter_map(Declaration::cast) - .filter_map(|decl| { - let name = decl.name()?; - let range = decl.range(); - Some(Symbol { name, range }) - }) - .collect(); - res // NLL :-( - } +pub fn symbols(file: &ast::File) -> Vec { + let syntax = file.syntax(); + let res: Vec = walk::preorder(syntax.as_ref()) + .filter_map(Declaration::cast) + .filter_map(|decl| { + let name = decl.name()?; + let range = decl.range(); + Some(Symbol { name, range }) + }) + .collect(); + res // NLL :-( +} - pub fn extend_selection(&self, range: TextRange) -> Option { - let syntax = self.inner.syntax(); - extend_selection::extend_selection(syntax.as_ref(), range) - } +pub fn extend_selection(file: &ast::File, range: TextRange) -> Option { + let syntax = file.syntax(); + extend_selection::extend_selection(syntax.as_ref(), range) +} - pub fn runnables(&self) -> Vec { - self.inner - .functions() - .filter_map(|f| { - let name = f.name()?.text(); - let kind = if name == "main" { - RunnableKind::Bin - } else if f.has_atom_attr("test") { - RunnableKind::Test { - name: name.to_string() - } - } else { - return None; - }; - Some(Runnable { - range: f.syntax().range(), - kind, - }) +pub fn runnables(file: &ast::File) -> Vec { + file + .functions() + .filter_map(|f| { + let name = f.name()?.text(); + let kind = if name == "main" { + RunnableKind::Bin + } else if f.has_atom_attr("test") { + RunnableKind::Test { + name: name.to_string() + } + } else { + return None; + }; + Some(Runnable { + range: f.syntax().range(), + kind, }) - .collect() - } + }) + .collect() } -struct Declaration<'f>(SyntaxNodeRef<'f>); +struct Declaration<'f> (SyntaxNodeRef<'f>); impl<'f> Declaration<'f> { fn cast(node: SyntaxNodeRef<'f>) -> Option> { -- cgit v1.2.3