165 lines
5.0 KiB
Rust
Raw Normal View History

2019-01-13 19:54:28 +01:00
//! This crate provides those IDE features which use only a single file.
2019-01-08 22:30:32 +03:00
//!
2019-01-13 19:54:28 +01:00
//! This usually means functions which take syntax tree as an input and produce
//! an edit or some auxiliary info.
2019-01-08 22:30:32 +03:00
mod extend_selection;
mod folding_ranges;
mod line_index;
2018-12-25 21:26:36 +01:00
mod line_index_utils;
2019-01-02 16:29:08 +03:00
mod structure;
2018-08-28 14:47:12 +03:00
#[cfg(test)]
mod test_utils;
2019-01-10 17:50:49 +03:00
mod join_lines;
mod typing;
mod diagnostics;
2019-02-03 21:26:35 +03:00
#[derive(Debug)]
pub struct LocalEdit {
pub label: String,
pub edit: ra_text_edit::TextEdit,
pub cursor_position: Option<TextUnit>,
}
2018-08-01 10:40:07 +03:00
pub use self::{
extend_selection::extend_selection,
folding_ranges::{folding_ranges, Fold, FoldKind},
line_index::{LineCol, LineIndex},
line_index_utils::translate_offset_with_edit,
2019-01-02 16:29:08 +03:00
structure::{file_structure, StructureNode},
2019-01-10 17:50:49 +03:00
diagnostics::diagnostics,
join_lines::join_lines,
typing::{on_enter, on_dot_typed, on_eq_typed},
};
2019-01-03 15:08:32 +03:00
use ra_text_edit::TextEditBuilder;
2018-09-16 12:54:24 +03:00
use ra_syntax::{
2019-01-07 16:53:24 +03:00
SourceFile, SyntaxNode, TextRange, TextUnit, Direction,
2018-08-16 00:23:22 +03:00
SyntaxKind::{self, *},
2019-01-07 16:53:24 +03:00
ast::{self, AstNode},
algo::find_leaf_at_offset,
2018-08-11 12:28:59 +03:00
};
2018-12-28 17:39:12 +03:00
use rustc_hash::FxHashSet;
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,
}
#[derive(Debug, Copy, Clone)]
pub enum Severity {
Error,
2018-12-24 22:48:46 +08:00
WeakWarning,
}
2018-08-09 21:27:44 +03:00
#[derive(Debug)]
pub struct Diagnostic {
pub range: TextRange,
pub msg: String,
2018-12-24 22:48:46 +08:00
pub severity: Severity,
pub fix: Option<LocalEdit>,
2018-08-09 21:27:44 +03:00
}
2019-01-07 16:53:24 +03:00
pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
2019-02-08 14:49:43 +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];
2019-02-08 14:49:43 +03:00
let matching_node = parent.children().find(|node| node.kind() == matching_kind)?;
2018-08-16 00:23:22 +03:00
Some(matching_node.range().start())
}
2019-01-07 16:53:24 +03:00
pub fn highlight(root: &SyntaxNode) -> Vec<HighlightedRange> {
2018-12-28 17:39:12 +03:00
// Visited nodes to handle highlighting priorities
let mut highlighted = FxHashSet::default();
2018-08-10 15:07:43 +03:00
let mut res = Vec::new();
for node in root.descendants() {
2018-12-28 17:39:12 +03:00
if highlighted.contains(&node) {
continue;
}
2018-08-10 15:07:43 +03:00
let tag = match node.kind() {
2018-10-31 17:38:18 -04:00
COMMENT => "comment",
2018-08-10 15:07:43 +03:00
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",
2018-12-28 17:39:12 +03:00
_ => {
if let Some(macro_call) = ast::MacroCall::cast(node) {
if let Some(path) = macro_call.path() {
if let Some(segment) = path.segment() {
if let Some(name_ref) = segment.name_ref() {
highlighted.insert(name_ref.syntax());
let range_start = name_ref.syntax().range().start();
let mut range_end = name_ref.syntax().range().end();
for sibling in path.syntax().siblings(Direction::Next) {
match sibling.kind() {
EXCL | IDENT => range_end = sibling.range().end(),
_ => (),
}
}
res.push(HighlightedRange {
range: TextRange::from_to(range_start, range_end),
tag: "macro",
})
}
}
}
}
continue;
}
2018-08-10 15:07:43 +03:00
};
2019-02-08 14:49:43 +03:00
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-28 14:47:12 +03:00
#[cfg(test)]
mod tests {
2019-01-07 16:53:24 +03:00
use ra_syntax::AstNode;
2019-01-14 16:27:08 +03:00
use insta::assert_debug_snapshot_matches;
2019-01-07 16:53:24 +03:00
2019-01-14 16:27:08 +03:00
use crate::test_utils::{add_cursor, assert_eq_text, extract_offset};
2018-12-27 00:51:27 +08:00
2018-08-28 14:47:12 +03:00
use super::*;
#[test]
fn test_highlighting() {
2019-01-07 16:53:24 +03:00
let file = SourceFile::parse(
r#"
2018-08-28 14:47:12 +03:00
// comment
fn main() {}
println!("Hello, {}!", 92);
"#,
);
let hls = highlight(file.syntax());
2019-01-14 16:27:08 +03:00
assert_debug_snapshot_matches!("highlighting", hls);
2018-08-28 14:47:12 +03:00
}
#[test]
fn test_matching_brace() {
fn do_check(before: &str, after: &str) {
let (pos, before) = extract_offset(before);
2019-01-07 16:53:24 +03:00
let file = SourceFile::parse(&before);
2018-08-28 14:47:12 +03:00
let new_pos = match matching_brace(&file, pos) {
None => pos,
Some(pos) => pos,
};
let actual = add_cursor(&before, new_pos);
assert_eq_text!(after, &actual);
}
do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
2018-08-28 14:47:12 +03:00
}
2018-08-28 14:47:12 +03:00
}