2019-03-23 11:34:49 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
|
2019-05-21 06:04:54 -05:00
|
|
|
use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind, SyntaxKind::*, SyntaxElement, T};
|
2019-01-26 02:20:30 -06:00
|
|
|
use ra_db::SourceDatabase;
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-03-23 11:34:49 -05:00
|
|
|
use crate::{FileId, db::RootDatabase};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct HighlightedRange {
|
|
|
|
pub range: TextRange,
|
|
|
|
pub tag: &'static str,
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-05-21 06:04:54 -05:00
|
|
|
fn is_control_keyword(kind: SyntaxKind) -> bool {
|
|
|
|
match kind {
|
2019-05-21 08:28:10 -05:00
|
|
|
T![for]
|
|
|
|
| T![loop]
|
|
|
|
| T![while]
|
|
|
|
| T![continue]
|
|
|
|
| T![break]
|
|
|
|
| T![if]
|
|
|
|
| T![else]
|
|
|
|
| T![match]
|
|
|
|
| T![return] => true,
|
2019-05-21 06:04:54 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:17:10 -06:00
|
|
|
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
|
2019-01-26 02:51:36 -06:00
|
|
|
let source_file = db.parse(file_id);
|
2019-03-23 11:34:49 -05:00
|
|
|
|
|
|
|
// Visited nodes to handle highlighting priorities
|
2019-03-30 05:25:53 -05:00
|
|
|
let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default();
|
2019-03-23 11:34:49 -05:00
|
|
|
let mut res = Vec::new();
|
2019-03-30 05:25:53 -05:00
|
|
|
for node in source_file.syntax().descendants_with_tokens() {
|
2019-03-23 11:34:49 -05:00
|
|
|
if highlighted.contains(&node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let tag = match node.kind() {
|
|
|
|
COMMENT => "comment",
|
|
|
|
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
|
|
|
|
ATTR => "attribute",
|
2019-05-23 05:26:38 -05:00
|
|
|
NAME_REF => {
|
|
|
|
if let Some(name_ref) = node.as_node().and_then(|n| ast::NameRef::cast(n)) {
|
|
|
|
use crate::name_ref_kind::{classify_name_ref, NameRefKind::*};
|
|
|
|
use hir::{ModuleDef, ImplItem};
|
|
|
|
|
|
|
|
// FIXME: try to reuse the SourceAnalyzers
|
|
|
|
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
|
|
|
match classify_name_ref(db, &analyzer, name_ref) {
|
|
|
|
Some(Method(_)) => "function",
|
|
|
|
Some(Macro(_)) => "macro",
|
|
|
|
Some(FieldAccess(_)) => "field",
|
|
|
|
Some(AssocItem(ImplItem::Method(_))) => "function",
|
|
|
|
Some(AssocItem(ImplItem::Const(_))) => "constant",
|
|
|
|
Some(AssocItem(ImplItem::TypeAlias(_))) => "type",
|
|
|
|
Some(Def(ModuleDef::Module(_))) => "module",
|
|
|
|
Some(Def(ModuleDef::Function(_))) => "function",
|
|
|
|
Some(Def(ModuleDef::Struct(_))) => "type",
|
|
|
|
Some(Def(ModuleDef::Enum(_))) => "type",
|
|
|
|
Some(Def(ModuleDef::EnumVariant(_))) => "constant",
|
|
|
|
Some(Def(ModuleDef::Const(_))) => "constant",
|
|
|
|
Some(Def(ModuleDef::Static(_))) => "constant",
|
|
|
|
Some(Def(ModuleDef::Trait(_))) => "type",
|
|
|
|
Some(Def(ModuleDef::TypeAlias(_))) => "type",
|
|
|
|
Some(SelfType(_)) => "type",
|
|
|
|
Some(Pat(_)) => "text",
|
|
|
|
Some(SelfParam(_)) => "type",
|
|
|
|
Some(GenericParam(_)) => "type",
|
|
|
|
None => "text",
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
"text"
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 11:34:49 -05:00
|
|
|
NAME => "function",
|
2019-05-23 05:26:38 -05:00
|
|
|
TYPE_ALIAS_DEF | TYPE_ARG | TYPE_PARAM => "type",
|
2019-03-23 11:34:49 -05:00
|
|
|
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
|
|
|
|
LIFETIME => "parameter",
|
2019-05-21 08:28:10 -05:00
|
|
|
T![unsafe] => "keyword.unsafe",
|
|
|
|
k if is_control_keyword(k) => "keyword.control",
|
2019-03-23 11:34:49 -05:00
|
|
|
k if k.is_keyword() => "keyword",
|
|
|
|
_ => {
|
2019-03-30 05:25:53 -05:00
|
|
|
if let Some(macro_call) = node.as_node().and_then(ast::MacroCall::cast) {
|
2019-03-23 11:34:49 -05:00
|
|
|
if let Some(path) = macro_call.path() {
|
|
|
|
if let Some(segment) = path.segment() {
|
|
|
|
if let Some(name_ref) = segment.name_ref() {
|
2019-03-30 05:25:53 -05:00
|
|
|
highlighted.insert(name_ref.syntax().into());
|
2019-03-23 11:34:49 -05:00
|
|
|
let range_start = name_ref.syntax().range().start();
|
|
|
|
let mut range_end = name_ref.syntax().range().end();
|
2019-03-30 05:25:53 -05:00
|
|
|
for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
|
2019-03-23 11:34:49 -05:00
|
|
|
match sibling.kind() {
|
2019-05-15 07:35:47 -05:00
|
|
|
T![!] | IDENT => range_end = sibling.range().end(),
|
2019-03-23 11:34:49 -05:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.push(HighlightedRange {
|
|
|
|
range: TextRange::from_to(range_start, range_end),
|
|
|
|
tag: "macro",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
res.push(HighlightedRange { range: node.range(), tag })
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use insta::assert_debug_snapshot_matches;
|
|
|
|
|
|
|
|
use crate::mock_analysis::single_file;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_highlighting() {
|
|
|
|
let (analysis, file_id) = single_file(
|
|
|
|
r#"
|
2019-05-23 05:26:38 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
struct Foo {
|
|
|
|
pub x: i32,
|
|
|
|
pub y: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo<T>() -> T {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2019-03-23 11:34:49 -05:00
|
|
|
// comment
|
|
|
|
fn main() {}
|
|
|
|
println!("Hello, {}!", 92);
|
2019-05-23 05:26:38 -05:00
|
|
|
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
vec.push(Foo { x: 0, y: 1 });
|
|
|
|
unsafe { vec.set_len(0); }
|
2019-03-23 11:34:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
let result = analysis.highlight(file_id);
|
|
|
|
assert_debug_snapshot_matches!("highlighting", result);
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|