Files
rust/crates/libeditor/src/lib.rs

156 lines
4.0 KiB
Rust
Raw Normal View History

2018-08-01 10:40:07 +03:00
extern crate libsyntax2;
2018-08-10 21:13:39 +03:00
extern crate superslice;
2018-08-12 18:50:16 +03:00
extern crate itertools;
2018-08-07 18:28:30 +03:00
mod extend_selection;
2018-08-11 12:28:59 +03:00
mod symbols;
2018-08-10 21:13:39 +03:00
mod line_index;
2018-08-12 18:50:16 +03:00
mod edit;
mod code_actions;
2018-08-23 20:55:23 +03:00
mod typing;
2018-08-26 09:12:18 +03:00
mod completion;
2018-08-01 10:40:07 +03:00
use libsyntax2::{
2018-08-26 09:12:18 +03:00
File, TextUnit, TextRange, SyntaxNodeRef,
2018-08-25 11:44:17 +03:00
ast::{AstNode, NameOwner},
2018-08-26 09:12:18 +03:00
algo::{walk, find_leaf_at_offset, ancestors},
2018-08-16 00:23:22 +03:00
SyntaxKind::{self, *},
2018-08-01 10:40:07 +03:00
};
2018-08-25 12:44:26 +03:00
pub use libsyntax2::AtomEdit;
2018-08-11 12:28:59 +03:00
pub use self::{
line_index::{LineIndex, LineCol},
extend_selection::extend_selection,
2018-08-14 11:20:09 +03:00
symbols::{StructureNode, file_structure, FileSymbol, file_symbols},
2018-08-25 12:44:26 +03:00
edit::{EditBuilder, Edit},
2018-08-16 12:51:40 +03:00
code_actions::{
2018-08-26 09:12:18 +03:00
ActionResult,
2018-08-22 18:05:43 +03:00
flip_comma, add_derive, add_impl,
2018-08-16 12:51:40 +03:00
},
2018-08-23 20:55:23 +03:00
typing::join_lines,
2018-08-26 09:12:18 +03:00
completion::scope_completion,
2018-08-11 12:28:59 +03:00
};
2018-08-01 10:40:07 +03:00
2018-08-09 12:47:34 +03:00
#[derive(Debug)]
2018-08-01 10:40:07 +03:00
pub struct HighlightedRange {
pub range: TextRange,
pub tag: &'static str,
}
2018-08-09 21:27:44 +03:00
#[derive(Debug)]
pub struct Diagnostic {
pub range: TextRange,
pub msg: String,
}
2018-08-09 16:03:21 +03:00
#[derive(Debug)]
pub struct Runnable {
pub range: TextRange,
pub kind: RunnableKind,
}
#[derive(Debug)]
pub enum RunnableKind {
Test { name: String },
Bin,
}
2018-08-25 11:44:58 +03:00
pub fn matching_brace(file: &File, offset: TextUnit) -> Option<TextUnit> {
2018-08-16 00:23:22 +03:00
const BRACES: &[SyntaxKind] = &[
L_CURLY, R_CURLY,
L_BRACK, R_BRACK,
L_PAREN, R_PAREN,
L_ANGLE, R_ANGLE,
];
2018-08-17 22:00:13 +03:00
let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset)
2018-08-16 00:23:22 +03:00
.filter_map(|node| {
let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
Some((node, idx))
})
.next()?;
let parent = brace_node.parent()?;
let matching_kind = BRACES[brace_idx ^ 1];
let matching_node = parent.children()
.find(|node| node.kind() == matching_kind)?;
Some(matching_node.range().start())
}
2018-08-25 11:44:58 +03:00
pub fn highlight(file: &File) -> Vec<HighlightedRange> {
2018-08-10 15:07:43 +03:00
let mut res = Vec::new();
2018-08-17 22:00:13 +03:00
for node in walk::preorder(file.syntax()) {
2018-08-10 15:07:43 +03:00
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,
})
2018-08-01 10:40:07 +03:00
}
2018-08-10 15:07:43 +03:00
res
}
2018-08-01 10:40:07 +03:00
2018-08-25 11:44:58 +03:00
pub fn diagnostics(file: &File) -> Vec<Diagnostic> {
2018-08-10 15:07:43 +03:00
let mut res = Vec::new();
2018-08-01 10:40:07 +03:00
2018-08-17 22:00:13 +03:00
for node in walk::preorder(file.syntax()) {
2018-08-10 15:07:43 +03:00
if node.kind() == ERROR {
res.push(Diagnostic {
range: node.range(),
msg: "Syntax Error".to_string(),
});
2018-08-09 21:27:44 +03:00
}
}
2018-08-10 15:07:43 +03:00
res.extend(file.errors().into_iter().map(|err| Diagnostic {
range: TextRange::offset_len(err.offset, 1.into()),
msg: err.msg,
}));
res
}
2018-08-09 21:27:44 +03:00
2018-08-25 11:44:58 +03:00
pub fn syntax_tree(file: &File) -> String {
2018-08-17 22:00:13 +03:00
::libsyntax2::utils::dump_tree(file.syntax())
2018-08-10 15:07:43 +03:00
}
2018-08-05 19:06:14 +03:00
2018-08-25 11:44:58 +03:00
pub fn runnables(file: &File) -> Vec<Runnable> {
2018-08-17 22:00:13 +03:00
file.ast()
2018-08-10 15:07:43 +03:00
.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,
2018-08-09 16:03:21 +03:00
})
2018-08-10 15:07:43 +03:00
})
.collect()
2018-08-05 19:06:14 +03:00
}
2018-08-26 09:12:18 +03:00
pub fn find_node_at_offset<'a, N: AstNode<'a>>(
syntax: SyntaxNodeRef<'a>,
offset: TextUnit,
) -> Option<N> {
let leaves = find_leaf_at_offset(syntax, offset);
let leaf = leaves.clone()
.find(|leaf| !leaf.kind().is_trivia())
.or_else(|| leaves.right_biased())?;
ancestors(leaf)
.filter_map(N::cast)
.next()
}