7216: Highlighting improvements r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-01-09 13:09:15 +00:00 committed by GitHub
commit 939ca83b34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 334 additions and 394 deletions

View file

@ -76,8 +76,8 @@ pub use crate::{
references::{rename::RenameError, Declaration, ReferenceSearchResult}, references::{rename::RenameError, Declaration, ReferenceSearchResult},
runnables::{Runnable, RunnableKind, TestId}, runnables::{Runnable, RunnableKind, TestId},
syntax_highlighting::{ syntax_highlighting::{
tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}, tags::{Highlight, HlMod, HlMods, HlTag},
HighlightedRange, HlRange,
}, },
}; };
pub use assists::{Assist, AssistConfig, AssistId, AssistKind, InsertUseConfig}; pub use assists::{Assist, AssistConfig, AssistId, AssistKind, InsertUseConfig};
@ -449,12 +449,12 @@ impl Analysis {
} }
/// Computes syntax highlighting for the given file /// Computes syntax highlighting for the given file
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HlRange>> {
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false)) self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
} }
/// Computes syntax highlighting for the given file range. /// Computes syntax highlighting for the given file range.
pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HighlightedRange>> { pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HlRange>> {
self.with_db(|db| { self.with_db(|db| {
syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false) syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false)
}) })

View file

@ -1,11 +1,13 @@
pub(crate) mod tags;
mod highlights; mod highlights;
mod injector; mod injector;
mod format; mod format;
mod html;
mod injection; mod injection;
mod macro_rules; mod macro_rules;
pub(crate) mod tags;
mod html;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -26,13 +28,13 @@ use crate::{
syntax_highlighting::{ syntax_highlighting::{
format::FormatStringHighlighter, macro_rules::MacroRulesHighlighter, tags::Highlight, format::FormatStringHighlighter, macro_rules::MacroRulesHighlighter, tags::Highlight,
}, },
FileId, HighlightModifier, HighlightTag, SymbolKind, FileId, HlMod, HlTag, SymbolKind,
}; };
pub(crate) use html::highlight_as_html; pub(crate) use html::highlight_as_html;
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub struct HighlightedRange { pub struct HlRange {
pub range: TextRange, pub range: TextRange,
pub highlight: Highlight, pub highlight: Highlight,
pub binding_hash: Option<u64>, pub binding_hash: Option<u64>,
@ -52,7 +54,7 @@ pub(crate) fn highlight(
file_id: FileId, file_id: FileId,
range_to_highlight: Option<TextRange>, range_to_highlight: Option<TextRange>,
syntactic_name_ref_highlighting: bool, syntactic_name_ref_highlighting: bool,
) -> Vec<HighlightedRange> { ) -> Vec<HlRange> {
let _p = profile::span("highlight"); let _p = profile::span("highlight");
let sema = Semantics::new(db); let sema = Semantics::new(db);
@ -72,7 +74,7 @@ pub(crate) fn highlight(
}; };
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
let mut stack = highlights::Highlights::new(range_to_highlight); let mut hl = highlights::Highlights::new(range_to_highlight);
let mut current_macro_call: Option<ast::MacroCall> = None; let mut current_macro_call: Option<ast::MacroCall> = None;
let mut current_macro_rules: Option<ast::MacroRules> = None; let mut current_macro_rules: Option<ast::MacroRules> = None;
@ -96,9 +98,9 @@ pub(crate) fn highlight(
match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) { match event.clone().map(|it| it.into_node().and_then(ast::MacroCall::cast)) {
WalkEvent::Enter(Some(mc)) => { WalkEvent::Enter(Some(mc)) => {
if let Some(range) = macro_call_range(&mc) { if let Some(range) = macro_call_range(&mc) {
stack.add(HighlightedRange { hl.add(HlRange {
range, range,
highlight: HighlightTag::Symbol(SymbolKind::Macro).into(), highlight: HlTag::Symbol(SymbolKind::Macro).into(),
binding_hash: None, binding_hash: None,
}); });
} }
@ -134,7 +136,7 @@ pub(crate) fn highlight(
inside_attribute = false inside_attribute = false
} }
if let Some((new_comments, inj)) = injection::extract_doc_comments(node) { if let Some((new_comments, inj)) = injection::extract_doc_comments(node) {
injection::highlight_doc_comment(new_comments, inj, &mut stack); injection::highlight_doc_comment(new_comments, inj, &mut hl);
} }
} }
WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => { WalkEvent::Enter(NodeOrToken::Node(node)) if ast::Attr::can_cast(node.kind()) => {
@ -179,7 +181,7 @@ pub(crate) fn highlight(
if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) { if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) {
if token.is_raw() { if token.is_raw() {
let expanded = element_to_highlight.as_token().unwrap().clone(); let expanded = element_to_highlight.as_token().unwrap().clone();
if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() { if injection::highlight_injection(&mut hl, &sema, token, expanded).is_some() {
continue; continue;
} }
} }
@ -192,24 +194,24 @@ pub(crate) fn highlight(
element_to_highlight.clone(), element_to_highlight.clone(),
) { ) {
if inside_attribute { if inside_attribute {
highlight = highlight | HighlightModifier::Attribute; highlight = highlight | HlMod::Attribute;
} }
if macro_rules_highlighter.highlight(element_to_highlight.clone()).is_none() { if macro_rules_highlighter.highlight(element_to_highlight.clone()).is_none() {
stack.add(HighlightedRange { range, highlight, binding_hash }); hl.add(HlRange { range, highlight, binding_hash });
} }
if let Some(string) = if let Some(string) =
element_to_highlight.as_token().cloned().and_then(ast::String::cast) element_to_highlight.as_token().cloned().and_then(ast::String::cast)
{ {
format_string_highlighter.highlight_format_string(&mut stack, &string, range); format_string_highlighter.highlight_format_string(&mut hl, &string, range);
// Highlight escape sequences // Highlight escape sequences
if let Some(char_ranges) = string.char_ranges() { if let Some(char_ranges) = string.char_ranges() {
for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) { for (piece_range, _) in char_ranges.iter().filter(|(_, char)| char.is_ok()) {
if string.text()[piece_range.start().into()..].starts_with('\\') { if string.text()[piece_range.start().into()..].starts_with('\\') {
stack.add(HighlightedRange { hl.add(HlRange {
range: piece_range + range.start(), range: piece_range + range.start(),
highlight: HighlightTag::EscapeSequence.into(), highlight: HlTag::EscapeSequence.into(),
binding_hash: None, binding_hash: None,
}); });
} }
@ -219,7 +221,7 @@ pub(crate) fn highlight(
} }
} }
stack.to_vec() hl.to_vec()
} }
fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
@ -292,22 +294,20 @@ fn highlight_element(
}; };
match name_kind { match name_kind {
Some(NameClass::ExternCrate(_)) => HighlightTag::Symbol(SymbolKind::Module).into(), Some(NameClass::ExternCrate(_)) => HlTag::Symbol(SymbolKind::Module).into(),
Some(NameClass::Definition(def)) => { Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
highlight_def(db, def) | HighlightModifier::Definition
}
Some(NameClass::ConstReference(def)) => highlight_def(db, def), Some(NameClass::ConstReference(def)) => highlight_def(db, def),
Some(NameClass::PatFieldShorthand { field_ref, .. }) => { Some(NameClass::PatFieldShorthand { field_ref, .. }) => {
let mut h = HighlightTag::Symbol(SymbolKind::Field).into(); let mut h = HlTag::Symbol(SymbolKind::Field).into();
if let Definition::Field(field) = field_ref { if let Definition::Field(field) = field_ref {
if let VariantDef::Union(_) = field.parent_def(db) { if let VariantDef::Union(_) = field.parent_def(db) {
h |= HighlightModifier::Unsafe; h |= HlMod::Unsafe;
} }
} }
h h
} }
None => highlight_name_by_syntax(name) | HighlightModifier::Definition, None => highlight_name_by_syntax(name) | HlMod::Definition,
} }
} }
@ -315,16 +315,14 @@ fn highlight_element(
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => { NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
// even though we track whether we are in an attribute or not we still need this special case // even though we track whether we are in an attribute or not we still need this special case
// as otherwise we would emit unresolved references for name refs inside attributes // as otherwise we would emit unresolved references for name refs inside attributes
Highlight::from(HighlightTag::Symbol(SymbolKind::Function)) Highlight::from(HlTag::Symbol(SymbolKind::Function))
} }
NAME_REF => { NAME_REF => {
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| { highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| {
match NameRefClass::classify(sema, &name_ref) { match NameRefClass::classify(sema, &name_ref) {
Some(name_kind) => match name_kind { Some(name_kind) => match name_kind {
NameRefClass::ExternCrate(_) => { NameRefClass::ExternCrate(_) => HlTag::Symbol(SymbolKind::Module).into(),
HighlightTag::Symbol(SymbolKind::Module).into()
}
NameRefClass::Definition(def) => { NameRefClass::Definition(def) => {
if let Definition::Local(local) = &def { if let Definition::Local(local) = &def {
if let Some(name) = local.name(db) { if let Some(name) = local.name(db) {
@ -338,7 +336,7 @@ fn highlight_element(
if let Definition::Local(local) = &def { if let Definition::Local(local) = &def {
if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) { if is_consumed_lvalue(name_ref.syntax().clone().into(), local, db) {
h |= HighlightModifier::Consuming; h |= HlMod::Consuming;
} }
} }
@ -346,7 +344,7 @@ fn highlight_element(
if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) { if matches!(parent.kind(), FIELD_EXPR | RECORD_PAT_FIELD) {
if let Definition::Field(field) = def { if let Definition::Field(field) = def {
if let VariantDef::Union(_) = field.parent_def(db) { if let VariantDef::Union(_) = field.parent_def(db) {
h |= HighlightModifier::Unsafe; h |= HlMod::Unsafe;
} }
} }
} }
@ -355,13 +353,13 @@ fn highlight_element(
h h
} }
NameRefClass::FieldShorthand { .. } => { NameRefClass::FieldShorthand { .. } => {
HighlightTag::Symbol(SymbolKind::Field).into() HlTag::Symbol(SymbolKind::Field).into()
} }
}, },
None if syntactic_name_ref_highlighting => { None if syntactic_name_ref_highlighting => {
highlight_name_ref_by_syntax(name_ref, sema) highlight_name_ref_by_syntax(name_ref, sema)
} }
None => HighlightTag::UnresolvedReference.into(), None => HlTag::UnresolvedReference.into(),
} }
}) })
} }
@ -369,60 +367,53 @@ fn highlight_element(
// Simple token-based highlighting // Simple token-based highlighting
COMMENT => { COMMENT => {
let comment = element.into_token().and_then(ast::Comment::cast)?; let comment = element.into_token().and_then(ast::Comment::cast)?;
let h = HighlightTag::Comment; let h = HlTag::Comment;
match comment.kind().doc { match comment.kind().doc {
Some(_) => h | HighlightModifier::Documentation, Some(_) => h | HlMod::Documentation,
None => h.into(), None => h.into(),
} }
} }
STRING | BYTE_STRING => HighlightTag::StringLiteral.into(), STRING | BYTE_STRING => HlTag::StringLiteral.into(),
ATTR => HighlightTag::Attribute.into(), ATTR => HlTag::Attribute.into(),
INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(), INT_NUMBER | FLOAT_NUMBER => HlTag::NumericLiteral.into(),
BYTE => HighlightTag::ByteLiteral.into(), BYTE => HlTag::ByteLiteral.into(),
CHAR => HighlightTag::CharLiteral.into(), CHAR => HlTag::CharLiteral.into(),
QUESTION => Highlight::new(HighlightTag::Operator) | HighlightModifier::ControlFlow, QUESTION => Highlight::new(HlTag::Operator) | HlMod::ControlFlow,
LIFETIME => { LIFETIME => {
let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap(); let lifetime = element.into_node().and_then(ast::Lifetime::cast).unwrap();
match NameClass::classify_lifetime(sema, &lifetime) { match NameClass::classify_lifetime(sema, &lifetime) {
Some(NameClass::Definition(def)) => { Some(NameClass::Definition(def)) => highlight_def(db, def) | HlMod::Definition,
highlight_def(db, def) | HighlightModifier::Definition
}
None => match NameRefClass::classify_lifetime(sema, &lifetime) { None => match NameRefClass::classify_lifetime(sema, &lifetime) {
Some(NameRefClass::Definition(def)) => highlight_def(db, def), Some(NameRefClass::Definition(def)) => highlight_def(db, def),
_ => Highlight::new(HighlightTag::Symbol(SymbolKind::LifetimeParam)), _ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)),
}, },
_ => { _ => Highlight::new(HlTag::Symbol(SymbolKind::LifetimeParam)) | HlMod::Definition,
Highlight::new(HighlightTag::Symbol(SymbolKind::LifetimeParam))
| HighlightModifier::Definition
}
} }
} }
p if p.is_punct() => match p { p if p.is_punct() => match p {
T![&] => { T![&] => {
let h = HighlightTag::Operator.into(); let h = HlTag::Operator.into();
let is_unsafe = element let is_unsafe = element
.parent() .parent()
.and_then(ast::RefExpr::cast) .and_then(ast::RefExpr::cast)
.map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr)) .map(|ref_expr| sema.is_unsafe_ref_expr(&ref_expr))
.unwrap_or(false); .unwrap_or(false);
if is_unsafe { if is_unsafe {
h | HighlightModifier::Unsafe h | HlMod::Unsafe
} else { } else {
h h
} }
} }
T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => { T![::] | T![->] | T![=>] | T![..] | T![=] | T![@] | T![.] => HlTag::Operator.into(),
HighlightTag::Operator.into()
}
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => { T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
HighlightTag::Symbol(SymbolKind::Macro).into() HlTag::Symbol(SymbolKind::Macro).into()
} }
T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => { T![!] if element.parent().and_then(ast::NeverType::cast).is_some() => {
HighlightTag::BuiltinType.into() HlTag::BuiltinType.into()
} }
T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => { T![*] if element.parent().and_then(ast::PtrType::cast).is_some() => {
HighlightTag::Keyword.into() HlTag::Keyword.into()
} }
T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { T![*] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?; let prefix_expr = element.parent().and_then(ast::PrefixExpr::cast)?;
@ -430,11 +421,11 @@ fn highlight_element(
let expr = prefix_expr.expr()?; let expr = prefix_expr.expr()?;
let ty = sema.type_of_expr(&expr)?; let ty = sema.type_of_expr(&expr)?;
if ty.is_raw_ptr() { if ty.is_raw_ptr() {
HighlightTag::Operator | HighlightModifier::Unsafe HlTag::Operator | HlMod::Unsafe
} else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() { } else if let Some(ast::PrefixOp::Deref) = prefix_expr.op_kind() {
HighlightTag::Operator.into() HlTag::Operator.into()
} else { } else {
HighlightTag::Punctuation.into() HlTag::Punctuation.into()
} }
} }
T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { T![-] if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
@ -442,34 +433,26 @@ fn highlight_element(
let expr = prefix_expr.expr()?; let expr = prefix_expr.expr()?;
match expr { match expr {
ast::Expr::Literal(_) => HighlightTag::NumericLiteral, ast::Expr::Literal(_) => HlTag::NumericLiteral,
_ => HighlightTag::Operator, _ => HlTag::Operator,
} }
.into() .into()
} }
_ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => { _ if element.parent().and_then(ast::PrefixExpr::cast).is_some() => {
HighlightTag::Operator.into() HlTag::Operator.into()
}
_ if element.parent().and_then(ast::BinExpr::cast).is_some() => {
HighlightTag::Operator.into()
} }
_ if element.parent().and_then(ast::BinExpr::cast).is_some() => HlTag::Operator.into(),
_ if element.parent().and_then(ast::RangeExpr::cast).is_some() => { _ if element.parent().and_then(ast::RangeExpr::cast).is_some() => {
HighlightTag::Operator.into() HlTag::Operator.into()
} }
_ if element.parent().and_then(ast::RangePat::cast).is_some() => { _ if element.parent().and_then(ast::RangePat::cast).is_some() => HlTag::Operator.into(),
HighlightTag::Operator.into() _ if element.parent().and_then(ast::RestPat::cast).is_some() => HlTag::Operator.into(),
} _ if element.parent().and_then(ast::Attr::cast).is_some() => HlTag::Attribute.into(),
_ if element.parent().and_then(ast::RestPat::cast).is_some() => { _ => HlTag::Punctuation.into(),
HighlightTag::Operator.into()
}
_ if element.parent().and_then(ast::Attr::cast).is_some() => {
HighlightTag::Attribute.into()
}
_ => HighlightTag::Punctuation.into(),
}, },
k if k.is_keyword() => { k if k.is_keyword() => {
let h = Highlight::new(HighlightTag::Keyword); let h = Highlight::new(HlTag::Keyword);
match k { match k {
T![break] T![break]
| T![continue] | T![continue]
@ -479,10 +462,10 @@ fn highlight_element(
| T![match] | T![match]
| T![return] | T![return]
| T![while] | T![while]
| T![in] => h | HighlightModifier::ControlFlow, | T![in] => h | HlMod::ControlFlow,
T![for] if !is_child_of_impl(&element) => h | HighlightModifier::ControlFlow, T![for] if !is_child_of_impl(&element) => h | HlMod::ControlFlow,
T![unsafe] => h | HighlightModifier::Unsafe, T![unsafe] => h | HlMod::Unsafe,
T![true] | T![false] => HighlightTag::BoolLiteral.into(), T![true] | T![false] => HlTag::BoolLiteral.into(),
T![self] => { T![self] => {
let self_param_is_mut = element let self_param_is_mut = element
.parent() .parent()
@ -495,7 +478,7 @@ fn highlight_element(
.and_then(SyntaxNode::parent) .and_then(SyntaxNode::parent)
.and_then(ast::Path::cast) .and_then(ast::Path::cast)
.and_then(|p| sema.resolve_path(&p)); .and_then(|p| sema.resolve_path(&p));
let mut h = HighlightTag::Symbol(SymbolKind::SelfParam).into(); let mut h = HlTag::Symbol(SymbolKind::SelfParam).into();
if self_param_is_mut if self_param_is_mut
|| matches!(self_path, || matches!(self_path,
Some(hir::PathResolution::Local(local)) Some(hir::PathResolution::Local(local))
@ -503,12 +486,12 @@ fn highlight_element(
&& (local.is_mut(db) || local.ty(db).is_mutable_reference()) && (local.is_mut(db) || local.ty(db).is_mutable_reference())
) )
{ {
h |= HighlightModifier::Mutable h |= HlMod::Mutable
} }
if let Some(hir::PathResolution::Local(local)) = self_path { if let Some(hir::PathResolution::Local(local)) = self_path {
if is_consumed_lvalue(element, &local, db) { if is_consumed_lvalue(element, &local, db) {
h |= HighlightModifier::Consuming; h |= HlMod::Consuming;
} }
} }
@ -519,7 +502,7 @@ fn highlight_element(
.and_then(ast::IdentPat::cast) .and_then(ast::IdentPat::cast)
.and_then(|ident_pat| { .and_then(|ident_pat| {
if sema.is_unsafe_ident_pat(&ident_pat) { if sema.is_unsafe_ident_pat(&ident_pat) {
Some(HighlightModifier::Unsafe) Some(HlMod::Unsafe)
} else { } else {
None None
} }
@ -568,21 +551,21 @@ fn highlight_method_call(
method_call: &ast::MethodCallExpr, method_call: &ast::MethodCallExpr,
) -> Option<Highlight> { ) -> Option<Highlight> {
let func = sema.resolve_method_call(&method_call)?; let func = sema.resolve_method_call(&method_call)?;
let mut h = HighlightTag::Symbol(SymbolKind::Function).into(); let mut h = HlTag::Symbol(SymbolKind::Function).into();
h |= HighlightModifier::Associated; h |= HlMod::Associated;
if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
h |= HighlightModifier::Unsafe; h |= HlMod::Unsafe;
} }
if let Some(self_param) = func.self_param(sema.db) { if let Some(self_param) = func.self_param(sema.db) {
match self_param.access(sema.db) { match self_param.access(sema.db) {
hir::Access::Shared => (), hir::Access::Shared => (),
hir::Access::Exclusive => h |= HighlightModifier::Mutable, hir::Access::Exclusive => h |= HlMod::Mutable,
hir::Access::Owned => { hir::Access::Owned => {
if let Some(receiver_ty) = if let Some(receiver_ty) =
method_call.receiver().and_then(|it| sema.type_of_expr(&it)) method_call.receiver().and_then(|it| sema.type_of_expr(&it))
{ {
if !receiver_ty.is_copy(sema.db) { if !receiver_ty.is_copy(sema.db) {
h |= HighlightModifier::Consuming h |= HlMod::Consuming
} }
} }
} }
@ -593,78 +576,78 @@ fn highlight_method_call(
fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight { fn highlight_def(db: &RootDatabase, def: Definition) -> Highlight {
match def { match def {
Definition::Macro(_) => HighlightTag::Symbol(SymbolKind::Macro), Definition::Macro(_) => HlTag::Symbol(SymbolKind::Macro),
Definition::Field(_) => HighlightTag::Symbol(SymbolKind::Field), Definition::Field(_) => HlTag::Symbol(SymbolKind::Field),
Definition::ModuleDef(def) => match def { Definition::ModuleDef(def) => match def {
hir::ModuleDef::Module(_) => HighlightTag::Symbol(SymbolKind::Module), hir::ModuleDef::Module(_) => HlTag::Symbol(SymbolKind::Module),
hir::ModuleDef::Function(func) => { hir::ModuleDef::Function(func) => {
let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Function)); let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Function));
if func.as_assoc_item(db).is_some() { if func.as_assoc_item(db).is_some() {
h |= HighlightModifier::Associated; h |= HlMod::Associated;
if func.self_param(db).is_none() { if func.self_param(db).is_none() {
h |= HighlightModifier::Static h |= HlMod::Static
} }
} }
if func.is_unsafe(db) { if func.is_unsafe(db) {
h |= HighlightModifier::Unsafe; h |= HlMod::Unsafe;
} }
return h; return h;
} }
hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HighlightTag::Symbol(SymbolKind::Struct), hir::ModuleDef::Adt(hir::Adt::Struct(_)) => HlTag::Symbol(SymbolKind::Struct),
hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HighlightTag::Symbol(SymbolKind::Enum), hir::ModuleDef::Adt(hir::Adt::Enum(_)) => HlTag::Symbol(SymbolKind::Enum),
hir::ModuleDef::Adt(hir::Adt::Union(_)) => HighlightTag::Symbol(SymbolKind::Union), hir::ModuleDef::Adt(hir::Adt::Union(_)) => HlTag::Symbol(SymbolKind::Union),
hir::ModuleDef::Variant(_) => HighlightTag::Symbol(SymbolKind::Variant), hir::ModuleDef::Variant(_) => HlTag::Symbol(SymbolKind::Variant),
hir::ModuleDef::Const(konst) => { hir::ModuleDef::Const(konst) => {
let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Const)); let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Const));
if konst.as_assoc_item(db).is_some() { if konst.as_assoc_item(db).is_some() {
h |= HighlightModifier::Associated h |= HlMod::Associated
} }
return h; return h;
} }
hir::ModuleDef::Trait(_) => HighlightTag::Symbol(SymbolKind::Trait), hir::ModuleDef::Trait(_) => HlTag::Symbol(SymbolKind::Trait),
hir::ModuleDef::TypeAlias(type_) => { hir::ModuleDef::TypeAlias(type_) => {
let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::TypeAlias)); let mut h = Highlight::new(HlTag::Symbol(SymbolKind::TypeAlias));
if type_.as_assoc_item(db).is_some() { if type_.as_assoc_item(db).is_some() {
h |= HighlightModifier::Associated h |= HlMod::Associated
} }
return h; return h;
} }
hir::ModuleDef::BuiltinType(_) => HighlightTag::BuiltinType, hir::ModuleDef::BuiltinType(_) => HlTag::BuiltinType,
hir::ModuleDef::Static(s) => { hir::ModuleDef::Static(s) => {
let mut h = Highlight::new(HighlightTag::Symbol(SymbolKind::Static)); let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Static));
if s.is_mut(db) { if s.is_mut(db) {
h |= HighlightModifier::Mutable; h |= HlMod::Mutable;
h |= HighlightModifier::Unsafe; h |= HlMod::Unsafe;
} }
return h; return h;
} }
}, },
Definition::SelfType(_) => HighlightTag::Symbol(SymbolKind::Impl), Definition::SelfType(_) => HlTag::Symbol(SymbolKind::Impl),
Definition::TypeParam(_) => HighlightTag::Symbol(SymbolKind::TypeParam), Definition::TypeParam(_) => HlTag::Symbol(SymbolKind::TypeParam),
Definition::ConstParam(_) => HighlightTag::Symbol(SymbolKind::ConstParam), Definition::ConstParam(_) => HlTag::Symbol(SymbolKind::ConstParam),
Definition::Local(local) => { Definition::Local(local) => {
let tag = if local.is_param(db) { let tag = if local.is_param(db) {
HighlightTag::Symbol(SymbolKind::ValueParam) HlTag::Symbol(SymbolKind::ValueParam)
} else { } else {
HighlightTag::Symbol(SymbolKind::Local) HlTag::Symbol(SymbolKind::Local)
}; };
let mut h = Highlight::new(tag); let mut h = Highlight::new(tag);
if local.is_mut(db) || local.ty(db).is_mutable_reference() { if local.is_mut(db) || local.ty(db).is_mutable_reference() {
h |= HighlightModifier::Mutable; h |= HlMod::Mutable;
} }
if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) { if local.ty(db).as_callable(db).is_some() || local.ty(db).impls_fnonce(db) {
h |= HighlightModifier::Callable; h |= HlMod::Callable;
} }
return h; return h;
} }
Definition::LifetimeParam(_) => HighlightTag::Symbol(SymbolKind::LifetimeParam), Definition::LifetimeParam(_) => HlTag::Symbol(SymbolKind::LifetimeParam),
Definition::Label(_) => HighlightTag::Symbol(SymbolKind::Label), Definition::Label(_) => HlTag::Symbol(SymbolKind::Label),
} }
.into() .into()
} }
fn highlight_name_by_syntax(name: ast::Name) -> Highlight { fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
let default = HighlightTag::UnresolvedReference; let default = HlTag::UnresolvedReference;
let parent = match name.syntax().parent() { let parent = match name.syntax().parent() {
Some(it) => it, Some(it) => it,
@ -672,19 +655,19 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
}; };
let tag = match parent.kind() { let tag = match parent.kind() {
STRUCT => HighlightTag::Symbol(SymbolKind::Struct), STRUCT => HlTag::Symbol(SymbolKind::Struct),
ENUM => HighlightTag::Symbol(SymbolKind::Enum), ENUM => HlTag::Symbol(SymbolKind::Enum),
VARIANT => HighlightTag::Symbol(SymbolKind::Variant), VARIANT => HlTag::Symbol(SymbolKind::Variant),
UNION => HighlightTag::Symbol(SymbolKind::Union), UNION => HlTag::Symbol(SymbolKind::Union),
TRAIT => HighlightTag::Symbol(SymbolKind::Trait), TRAIT => HlTag::Symbol(SymbolKind::Trait),
TYPE_ALIAS => HighlightTag::Symbol(SymbolKind::TypeAlias), TYPE_ALIAS => HlTag::Symbol(SymbolKind::TypeAlias),
TYPE_PARAM => HighlightTag::Symbol(SymbolKind::TypeParam), TYPE_PARAM => HlTag::Symbol(SymbolKind::TypeParam),
RECORD_FIELD => HighlightTag::Symbol(SymbolKind::Field), RECORD_FIELD => HlTag::Symbol(SymbolKind::Field),
MODULE => HighlightTag::Symbol(SymbolKind::Module), MODULE => HlTag::Symbol(SymbolKind::Module),
FN => HighlightTag::Symbol(SymbolKind::Function), FN => HlTag::Symbol(SymbolKind::Function),
CONST => HighlightTag::Symbol(SymbolKind::Const), CONST => HlTag::Symbol(SymbolKind::Const),
STATIC => HighlightTag::Symbol(SymbolKind::Static), STATIC => HlTag::Symbol(SymbolKind::Static),
IDENT_PAT => HighlightTag::Symbol(SymbolKind::Local), IDENT_PAT => HlTag::Symbol(SymbolKind::Local),
_ => default, _ => default,
}; };
@ -692,7 +675,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
} }
fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight { fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight {
let default = HighlightTag::UnresolvedReference; let default = HlTag::UnresolvedReference;
let parent = match name.syntax().parent() { let parent = match name.syntax().parent() {
Some(it) => it, Some(it) => it,
@ -703,10 +686,10 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
METHOD_CALL_EXPR => { METHOD_CALL_EXPR => {
return ast::MethodCallExpr::cast(parent) return ast::MethodCallExpr::cast(parent)
.and_then(|method_call| highlight_method_call(sema, &method_call)) .and_then(|method_call| highlight_method_call(sema, &method_call))
.unwrap_or_else(|| HighlightTag::Symbol(SymbolKind::Function).into()); .unwrap_or_else(|| HlTag::Symbol(SymbolKind::Function).into());
} }
FIELD_EXPR => { FIELD_EXPR => {
let h = HighlightTag::Symbol(SymbolKind::Field); let h = HlTag::Symbol(SymbolKind::Field);
let is_union = ast::FieldExpr::cast(parent) let is_union = ast::FieldExpr::cast(parent)
.and_then(|field_expr| { .and_then(|field_expr| {
let field = sema.resolve_field(&field_expr)?; let field = sema.resolve_field(&field_expr)?;
@ -718,7 +701,7 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
}) })
.unwrap_or(false); .unwrap_or(false);
if is_union { if is_union {
h | HighlightModifier::Unsafe h | HlMod::Unsafe
} else { } else {
h.into() h.into()
} }
@ -733,9 +716,9 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
_ => { _ => {
// within path, decide whether it is module or adt by checking for uppercase name // within path, decide whether it is module or adt by checking for uppercase name
return if name.text().chars().next().unwrap_or_default().is_uppercase() { return if name.text().chars().next().unwrap_or_default().is_uppercase() {
HighlightTag::Symbol(SymbolKind::Struct) HlTag::Symbol(SymbolKind::Struct)
} else { } else {
HighlightTag::Symbol(SymbolKind::Module) HlTag::Symbol(SymbolKind::Module)
} }
.into(); .into();
} }
@ -746,11 +729,11 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas
}; };
match parent.kind() { match parent.kind() {
CALL_EXPR => HighlightTag::Symbol(SymbolKind::Function).into(), CALL_EXPR => HlTag::Symbol(SymbolKind::Function).into(),
_ => if name.text().chars().next().unwrap_or_default().is_uppercase() { _ => if name.text().chars().next().unwrap_or_default().is_uppercase() {
HighlightTag::Symbol(SymbolKind::Struct) HlTag::Symbol(SymbolKind::Struct)
} else { } else {
HighlightTag::Symbol(SymbolKind::Const) HlTag::Symbol(SymbolKind::Const)
} }
.into(), .into(),
} }

View file

@ -4,7 +4,7 @@ use syntax::{
AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
}; };
use crate::{HighlightTag, HighlightedRange, SymbolKind}; use crate::{HlRange, HlTag, SymbolKind};
use super::highlights::Highlights; use super::highlights::Highlights;
@ -46,7 +46,7 @@ impl FormatStringHighlighter {
if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) { if self.format_string.as_ref() == Some(&SyntaxElement::from(string.syntax().clone())) {
string.lex_format_specifier(|piece_range, kind| { string.lex_format_specifier(|piece_range, kind| {
if let Some(highlight) = highlight_format_specifier(kind) { if let Some(highlight) = highlight_format_specifier(kind) {
stack.add(HighlightedRange { stack.add(HlRange {
range: piece_range + range.start(), range: piece_range + range.start(),
highlight: highlight.into(), highlight: highlight.into(),
binding_hash: None, binding_hash: None,
@ -57,7 +57,7 @@ impl FormatStringHighlighter {
} }
} }
fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> { fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HlTag> {
Some(match kind { Some(match kind {
FormatSpecifier::Open FormatSpecifier::Open
| FormatSpecifier::Close | FormatSpecifier::Close
@ -69,8 +69,8 @@ fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
| FormatSpecifier::DollarSign | FormatSpecifier::DollarSign
| FormatSpecifier::Dot | FormatSpecifier::Dot
| FormatSpecifier::Asterisk | FormatSpecifier::Asterisk
| FormatSpecifier::QuestionMark => HighlightTag::FormatSpecifier, | FormatSpecifier::QuestionMark => HlTag::FormatSpecifier,
FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral, FormatSpecifier::Integer | FormatSpecifier::Zero => HlTag::NumericLiteral,
FormatSpecifier::Identifier => HighlightTag::Symbol(SymbolKind::Local), FormatSpecifier::Identifier => HlTag::Symbol(SymbolKind::Local),
}) })
} }

View file

@ -4,33 +4,29 @@ use std::{cmp::Ordering, iter};
use stdx::equal_range_by; use stdx::equal_range_by;
use syntax::TextRange; use syntax::TextRange;
use crate::{HighlightTag, HighlightedRange}; use crate::{HlRange, HlTag};
pub(super) struct Highlights { pub(super) struct Highlights {
root: Node, root: Node,
} }
struct Node { struct Node {
highlighted_range: HighlightedRange, hl_range: HlRange,
nested: Vec<Node>, nested: Vec<Node>,
} }
impl Highlights { impl Highlights {
pub(super) fn new(range: TextRange) -> Highlights { pub(super) fn new(range: TextRange) -> Highlights {
Highlights { Highlights {
root: Node::new(HighlightedRange { root: Node::new(HlRange { range, highlight: HlTag::None.into(), binding_hash: None }),
range,
highlight: HighlightTag::Dummy.into(),
binding_hash: None,
}),
} }
} }
pub(super) fn add(&mut self, highlighted_range: HighlightedRange) { pub(super) fn add(&mut self, hl_range: HlRange) {
self.root.add(highlighted_range); self.root.add(hl_range);
} }
pub(super) fn to_vec(self) -> Vec<HighlightedRange> { pub(super) fn to_vec(self) -> Vec<HlRange> {
let mut res = Vec::new(); let mut res = Vec::new();
self.root.flatten(&mut res); self.root.flatten(&mut res);
res res
@ -38,59 +34,54 @@ impl Highlights {
} }
impl Node { impl Node {
fn new(highlighted_range: HighlightedRange) -> Node { fn new(hl_range: HlRange) -> Node {
Node { highlighted_range, nested: Vec::new() } Node { hl_range, nested: Vec::new() }
} }
fn add(&mut self, highlighted_range: HighlightedRange) { fn add(&mut self, hl_range: HlRange) {
assert!(self.highlighted_range.range.contains_range(highlighted_range.range)); assert!(self.hl_range.range.contains_range(hl_range.range));
// Fast path // Fast path
if let Some(last) = self.nested.last_mut() { if let Some(last) = self.nested.last_mut() {
if last.highlighted_range.range.contains_range(highlighted_range.range) { if last.hl_range.range.contains_range(hl_range.range) {
return last.add(highlighted_range); return last.add(hl_range);
} }
if last.highlighted_range.range.end() <= highlighted_range.range.start() { if last.hl_range.range.end() <= hl_range.range.start() {
return self.nested.push(Node::new(highlighted_range)); return self.nested.push(Node::new(hl_range));
} }
} }
let (start, len) = equal_range_by(&self.nested, |n| { let (start, len) =
ordering(n.highlighted_range.range, highlighted_range.range) equal_range_by(&self.nested, |n| ordering(n.hl_range.range, hl_range.range));
});
if len == 1 if len == 1 && self.nested[start].hl_range.range.contains_range(hl_range.range) {
&& self.nested[start].highlighted_range.range.contains_range(highlighted_range.range) return self.nested[start].add(hl_range);
{
return self.nested[start].add(highlighted_range);
} }
let nested = self let nested = self
.nested .nested
.splice(start..start + len, iter::once(Node::new(highlighted_range))) .splice(start..start + len, iter::once(Node::new(hl_range)))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
self.nested[start].nested = nested; self.nested[start].nested = nested;
} }
fn flatten(&self, acc: &mut Vec<HighlightedRange>) { fn flatten(&self, acc: &mut Vec<HlRange>) {
let mut start = self.highlighted_range.range.start(); let mut start = self.hl_range.range.start();
let mut nested = self.nested.iter(); let mut nested = self.nested.iter();
loop { loop {
let next = nested.next(); let next = nested.next();
let end = next.map_or(self.highlighted_range.range.end(), |it| { let end = next.map_or(self.hl_range.range.end(), |it| it.hl_range.range.start());
it.highlighted_range.range.start()
});
if start < end { if start < end {
acc.push(HighlightedRange { acc.push(HlRange {
range: TextRange::new(start, end), range: TextRange::new(start, end),
highlight: self.highlighted_range.highlight, highlight: self.hl_range.highlight,
binding_hash: self.highlighted_range.binding_hash, binding_hash: self.hl_range.binding_hash,
}); });
} }
start = match next { start = match next {
Some(child) => { Some(child) => {
child.flatten(acc); child.flatten(acc);
child.highlighted_range.range.end() child.hl_range.range.end()
} }
None => break, None => break,
} }

View file

@ -20,26 +20,26 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
) )
} }
let ranges = highlight(db, file_id, None, false); let hl_ranges = highlight(db, file_id, None, false);
let text = parse.tree().syntax().to_string(); let text = parse.tree().syntax().to_string();
let mut buf = String::new(); let mut buf = String::new();
buf.push_str(&STYLE); buf.push_str(&STYLE);
buf.push_str("<pre><code>"); buf.push_str("<pre><code>");
for range in &ranges { for r in &hl_ranges {
let curr = &text[range.range]; let chunk = html_escape(&text[r.range]);
if range.highlight.is_empty() { if r.highlight.is_empty() {
format_to!(buf, "{}", html_escape(curr)); format_to!(buf, "{}", chunk);
continue; continue;
} }
let class = range.highlight.to_string().replace('.', " "); let class = r.highlight.to_string().replace('.', " ");
let color = match (rainbow, range.binding_hash) { let color = match (rainbow, r.binding_hash) {
(true, Some(hash)) => { (true, Some(hash)) => {
format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash)) format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash))
} }
_ => "".into(), _ => "".into(),
}; };
format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, html_escape(curr)); format_to!(buf, "<span class=\"{}\"{}>{}</span>", class, color, chunk);
} }
buf.push_str("</code></pre>"); buf.push_str("</code></pre>");
buf buf

View file

@ -7,12 +7,12 @@ use ide_db::call_info::ActiveParameter;
use itertools::Itertools; use itertools::Itertools;
use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; use syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize};
use crate::{Analysis, HighlightModifier, HighlightTag, HighlightedRange, RootDatabase}; use crate::{Analysis, HlMod, HlRange, HlTag, RootDatabase};
use super::{highlights::Highlights, injector::Injector}; use super::{highlights::Highlights, injector::Injector};
pub(super) fn highlight_injection( pub(super) fn highlight_injection(
acc: &mut Highlights, hl: &mut Highlights,
sema: &Semantics<RootDatabase>, sema: &Semantics<RootDatabase>,
literal: ast::String, literal: ast::String,
expanded: SyntaxToken, expanded: SyntaxToken,
@ -22,82 +22,55 @@ pub(super) fn highlight_injection(
return None; return None;
} }
let value = literal.value()?; let value = literal.value()?;
let marker_info = MarkerInfo::new(&*value);
let (analysis, tmp_file_id) = Analysis::from_single_file(marker_info.cleaned_text.clone());
if let Some(range) = literal.open_quote_text_range() { if let Some(range) = literal.open_quote_text_range() {
acc.add(HighlightedRange { hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
range,
highlight: HighlightTag::StringLiteral.into(),
binding_hash: None,
})
} }
for mut h in analysis.highlight(tmp_file_id).unwrap() { let mut inj = Injector::default();
let range = marker_info.map_range_up(h.range);
let mut text = &*value;
let mut offset: TextSize = 0.into();
while !text.is_empty() {
let marker = "$0";
let idx = text.find(marker).unwrap_or(text.len());
let (chunk, next) = text.split_at(idx);
inj.add(chunk, TextRange::at(offset, TextSize::of(chunk)));
text = next;
offset += TextSize::of(chunk);
if let Some(next) = text.strip_prefix(marker) {
if let Some(range) = literal.map_range_up(TextRange::at(offset, TextSize::of(marker))) {
hl.add(HlRange { range, highlight: HlTag::Keyword.into(), binding_hash: None });
}
text = next;
let marker_len = TextSize::of(marker);
offset += marker_len;
}
}
let (analysis, tmp_file_id) = Analysis::from_single_file(inj.text().to_string());
for mut hl_range in analysis.highlight(tmp_file_id).unwrap() {
for range in inj.map_range_up(hl_range.range) {
if let Some(range) = literal.map_range_up(range) { if let Some(range) = literal.map_range_up(range) {
h.range = range; hl_range.range = range;
acc.add(h); hl.add(hl_range.clone());
}
} }
} }
if let Some(range) = literal.close_quote_text_range() { if let Some(range) = literal.close_quote_text_range() {
acc.add(HighlightedRange { hl.add(HlRange { range, highlight: HlTag::StringLiteral.into(), binding_hash: None })
range,
highlight: HighlightTag::StringLiteral.into(),
binding_hash: None,
})
} }
Some(()) Some(())
} }
/// Data to remove `$0` from string and map ranges
#[derive(Default, Debug)]
struct MarkerInfo {
cleaned_text: String,
markers: Vec<TextRange>,
}
impl MarkerInfo {
fn new(mut text: &str) -> Self {
let marker = "$0";
let mut res = MarkerInfo::default();
let mut offset: TextSize = 0.into();
while !text.is_empty() {
let idx = text.find(marker).unwrap_or(text.len());
let (chunk, next) = text.split_at(idx);
text = next;
res.cleaned_text.push_str(chunk);
offset += TextSize::of(chunk);
if let Some(next) = text.strip_prefix(marker) {
text = next;
let marker_len = TextSize::of(marker);
res.markers.push(TextRange::at(offset, marker_len));
offset += marker_len;
}
}
res
}
fn map_range_up(&self, range: TextRange) -> TextRange {
TextRange::new(
self.map_offset_up(range.start(), true),
self.map_offset_up(range.end(), false),
)
}
fn map_offset_up(&self, mut offset: TextSize, start: bool) -> TextSize {
for r in &self.markers {
if r.start() < offset || (start && r.start() == offset) {
offset += r.len()
}
}
offset
}
}
const RUSTDOC_FENCE: &'static str = "```"; const RUSTDOC_FENCE: &'static str = "```";
const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
"", "",
@ -116,7 +89,7 @@ const RUSTDOC_FENCE_TOKENS: &[&'static str] = &[
/// Lastly, a vector of new comment highlight ranges (spanning only the /// Lastly, a vector of new comment highlight ranges (spanning only the
/// comment prefix) is returned which is used in the syntax highlighting /// comment prefix) is returned which is used in the syntax highlighting
/// injection to replace the previous (line-spanning) comment ranges. /// injection to replace the previous (line-spanning) comment ranges.
pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec<HighlightedRange>, Injector)> { pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec<HlRange>, Injector)> {
let mut inj = Injector::default(); let mut inj = Injector::default();
// wrap the doctest into function body to get correct syntax highlighting // wrap the doctest into function body to get correct syntax highlighting
let prefix = "fn doctest() {\n"; let prefix = "fn doctest() {\n";
@ -166,12 +139,12 @@ pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec<Highlighted
pos pos
}; };
new_comments.push(HighlightedRange { new_comments.push(HlRange {
range: TextRange::new( range: TextRange::new(
range.start(), range.start(),
range.start() + TextSize::try_from(pos).unwrap(), range.start() + TextSize::try_from(pos).unwrap(),
), ),
highlight: HighlightTag::Comment | HighlightModifier::Documentation, highlight: HlTag::Comment | HlMod::Documentation,
binding_hash: None, binding_hash: None,
}); });
line_start += range.len() - TextSize::try_from(pos).unwrap(); line_start += range.len() - TextSize::try_from(pos).unwrap();
@ -196,7 +169,7 @@ pub(super) fn extract_doc_comments(node: &SyntaxNode) -> Option<(Vec<Highlighted
/// Injection of syntax highlighting of doctests. /// Injection of syntax highlighting of doctests.
pub(super) fn highlight_doc_comment( pub(super) fn highlight_doc_comment(
new_comments: Vec<HighlightedRange>, new_comments: Vec<HlRange>,
inj: Injector, inj: Injector,
stack: &mut Highlights, stack: &mut Highlights,
) { ) {
@ -207,9 +180,9 @@ pub(super) fn highlight_doc_comment(
for h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() { for h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
for r in inj.map_range_up(h.range) { for r in inj.map_range_up(h.range) {
stack.add(HighlightedRange { stack.add(HlRange {
range: r, range: r,
highlight: h.highlight | HighlightModifier::Injected, highlight: h.highlight | HlMod::Injected,
binding_hash: h.binding_hash, binding_hash: h.binding_hash,
}); });
} }

View file

@ -17,17 +17,15 @@ impl Injector {
pub(super) fn add(&mut self, text: &str, source_range: TextRange) { pub(super) fn add(&mut self, text: &str, source_range: TextRange) {
let len = TextSize::of(text); let len = TextSize::of(text);
assert_eq!(len, source_range.len()); assert_eq!(len, source_range.len());
self.add_impl(text, Some(source_range.start()));
let target_range = TextRange::at(TextSize::of(&self.buf), len);
self.ranges
.push((target_range, Some(Delta::new(target_range.start(), source_range.start()))));
self.buf.push_str(text);
} }
pub(super) fn add_unmapped(&mut self, text: &str) { pub(super) fn add_unmapped(&mut self, text: &str) {
self.add_impl(text, None);
}
fn add_impl(&mut self, text: &str, source: Option<TextSize>) {
let len = TextSize::of(text); let len = TextSize::of(text);
let target_range = TextRange::at(TextSize::of(&self.buf), len); let target_range = TextRange::at(TextSize::of(&self.buf), len);
self.ranges.push((target_range, None)); self.ranges.push((target_range, source.map(|it| Delta::new(target_range.start(), it))));
self.buf.push_str(text); self.buf.push_str(text);
} }

View file

@ -1,7 +1,7 @@
//! Syntax highlighting for macro_rules!. //! Syntax highlighting for macro_rules!.
use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T}; use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
use crate::{HighlightTag, HighlightedRange}; use crate::{HlRange, HlTag};
#[derive(Default)] #[derive(Default)]
pub(super) struct MacroRulesHighlighter { pub(super) struct MacroRulesHighlighter {
@ -19,13 +19,13 @@ impl MacroRulesHighlighter {
} }
} }
pub(super) fn highlight(&self, element: SyntaxElement) -> Option<HighlightedRange> { pub(super) fn highlight(&self, element: SyntaxElement) -> Option<HlRange> {
if let Some(state) = self.state.as_ref() { if let Some(state) = self.state.as_ref() {
if matches!(state.rule_state, RuleState::Matcher | RuleState::Expander) { if matches!(state.rule_state, RuleState::Matcher | RuleState::Expander) {
if let Some(range) = is_metavariable(element) { if let Some(range) = is_metavariable(element) {
return Some(HighlightedRange { return Some(HlRange {
range, range,
highlight: HighlightTag::UnresolvedReference.into(), highlight: HlTag::UnresolvedReference.into(),
binding_hash: None, binding_hash: None,
}); });
} }

View file

@ -7,15 +7,15 @@ use crate::SymbolKind;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Highlight { pub struct Highlight {
pub tag: HighlightTag, pub tag: HlTag,
pub modifiers: HighlightModifiers, pub mods: HlMods,
} }
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct HighlightModifiers(u32); pub struct HlMods(u32);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum HighlightTag { pub enum HlTag {
Symbol(SymbolKind), Symbol(SymbolKind),
BoolLiteral, BoolLiteral,
@ -33,13 +33,13 @@ pub enum HighlightTag {
Operator, Operator,
UnresolvedReference, UnresolvedReference,
// For things which don't have proper Tag, but want to use modifiers. // For things which don't have a specific highlight.
Dummy, None,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)] #[repr(u8)]
pub enum HighlightModifier { pub enum HlMod {
/// Used to differentiate individual elements within attributes. /// Used to differentiate individual elements within attributes.
Attribute = 0, Attribute = 0,
/// Used with keywords like `if` and `break`. /// Used with keywords like `if` and `break`.
@ -61,10 +61,10 @@ pub enum HighlightModifier {
Unsafe, Unsafe,
} }
impl HighlightTag { impl HlTag {
fn as_str(self) -> &'static str { fn as_str(self) -> &'static str {
match self { match self {
HighlightTag::Symbol(symbol) => match symbol { HlTag::Symbol(symbol) => match symbol {
SymbolKind::Const => "constant", SymbolKind::Const => "constant",
SymbolKind::Static => "static", SymbolKind::Static => "static",
SymbolKind::Enum => "enum", SymbolKind::Enum => "enum",
@ -86,59 +86,59 @@ impl HighlightTag {
SymbolKind::SelfParam => "self_keyword", SymbolKind::SelfParam => "self_keyword",
SymbolKind::Impl => "self_type", SymbolKind::Impl => "self_type",
}, },
HighlightTag::Attribute => "attribute", HlTag::Attribute => "attribute",
HighlightTag::BoolLiteral => "bool_literal", HlTag::BoolLiteral => "bool_literal",
HighlightTag::BuiltinType => "builtin_type", HlTag::BuiltinType => "builtin_type",
HighlightTag::ByteLiteral => "byte_literal", HlTag::ByteLiteral => "byte_literal",
HighlightTag::CharLiteral => "char_literal", HlTag::CharLiteral => "char_literal",
HighlightTag::Comment => "comment", HlTag::Comment => "comment",
HighlightTag::EscapeSequence => "escape_sequence", HlTag::EscapeSequence => "escape_sequence",
HighlightTag::FormatSpecifier => "format_specifier", HlTag::FormatSpecifier => "format_specifier",
HighlightTag::Keyword => "keyword", HlTag::Keyword => "keyword",
HighlightTag::Punctuation => "punctuation", HlTag::Punctuation => "punctuation",
HighlightTag::NumericLiteral => "numeric_literal", HlTag::NumericLiteral => "numeric_literal",
HighlightTag::Operator => "operator", HlTag::Operator => "operator",
HighlightTag::StringLiteral => "string_literal", HlTag::StringLiteral => "string_literal",
HighlightTag::UnresolvedReference => "unresolved_reference", HlTag::UnresolvedReference => "unresolved_reference",
HighlightTag::Dummy => "dummy", HlTag::None => "none",
} }
} }
} }
impl fmt::Display for HighlightTag { impl fmt::Display for HlTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f) fmt::Display::fmt(self.as_str(), f)
} }
} }
impl HighlightModifier { impl HlMod {
const ALL: &'static [HighlightModifier; HighlightModifier::Unsafe as u8 as usize + 1] = &[ const ALL: &'static [HlMod; HlMod::Unsafe as u8 as usize + 1] = &[
HighlightModifier::Attribute, HlMod::Attribute,
HighlightModifier::ControlFlow, HlMod::ControlFlow,
HighlightModifier::Definition, HlMod::Definition,
HighlightModifier::Documentation, HlMod::Documentation,
HighlightModifier::Injected, HlMod::Injected,
HighlightModifier::Mutable, HlMod::Mutable,
HighlightModifier::Consuming, HlMod::Consuming,
HighlightModifier::Callable, HlMod::Callable,
HighlightModifier::Static, HlMod::Static,
HighlightModifier::Associated, HlMod::Associated,
HighlightModifier::Unsafe, HlMod::Unsafe,
]; ];
fn as_str(self) -> &'static str { fn as_str(self) -> &'static str {
match self { match self {
HighlightModifier::Attribute => "attribute", HlMod::Attribute => "attribute",
HighlightModifier::ControlFlow => "control", HlMod::ControlFlow => "control",
HighlightModifier::Definition => "declaration", HlMod::Definition => "declaration",
HighlightModifier::Documentation => "documentation", HlMod::Documentation => "documentation",
HighlightModifier::Injected => "injected", HlMod::Injected => "injected",
HighlightModifier::Mutable => "mutable", HlMod::Mutable => "mutable",
HighlightModifier::Consuming => "consuming", HlMod::Consuming => "consuming",
HighlightModifier::Unsafe => "unsafe", HlMod::Unsafe => "unsafe",
HighlightModifier::Callable => "callable", HlMod::Callable => "callable",
HighlightModifier::Static => "static", HlMod::Static => "static",
HighlightModifier::Associated => "associated", HlMod::Associated => "associated",
} }
} }
@ -147,7 +147,7 @@ impl HighlightModifier {
} }
} }
impl fmt::Display for HighlightModifier { impl fmt::Display for HlMod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f) fmt::Display::fmt(self.as_str(), f)
} }
@ -156,63 +156,63 @@ impl fmt::Display for HighlightModifier {
impl fmt::Display for Highlight { impl fmt::Display for Highlight {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.tag)?; write!(f, "{}", self.tag)?;
for modifier in self.modifiers.iter() { for modifier in self.mods.iter() {
write!(f, ".{}", modifier)? write!(f, ".{}", modifier)?
} }
Ok(()) Ok(())
} }
} }
impl From<HighlightTag> for Highlight { impl From<HlTag> for Highlight {
fn from(tag: HighlightTag) -> Highlight { fn from(tag: HlTag) -> Highlight {
Highlight::new(tag) Highlight::new(tag)
} }
} }
impl Highlight { impl Highlight {
pub(crate) fn new(tag: HighlightTag) -> Highlight { pub(crate) fn new(tag: HlTag) -> Highlight {
Highlight { tag, modifiers: HighlightModifiers::default() } Highlight { tag, mods: HlMods::default() }
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.tag == HighlightTag::Dummy && self.modifiers == HighlightModifiers::default() self.tag == HlTag::None && self.mods == HlMods::default()
} }
} }
impl ops::BitOr<HighlightModifier> for HighlightTag { impl ops::BitOr<HlMod> for HlTag {
type Output = Highlight; type Output = Highlight;
fn bitor(self, rhs: HighlightModifier) -> Highlight { fn bitor(self, rhs: HlMod) -> Highlight {
Highlight::new(self) | rhs Highlight::new(self) | rhs
} }
} }
impl ops::BitOrAssign<HighlightModifier> for HighlightModifiers { impl ops::BitOrAssign<HlMod> for HlMods {
fn bitor_assign(&mut self, rhs: HighlightModifier) { fn bitor_assign(&mut self, rhs: HlMod) {
self.0 |= rhs.mask(); self.0 |= rhs.mask();
} }
} }
impl ops::BitOrAssign<HighlightModifier> for Highlight { impl ops::BitOrAssign<HlMod> for Highlight {
fn bitor_assign(&mut self, rhs: HighlightModifier) { fn bitor_assign(&mut self, rhs: HlMod) {
self.modifiers |= rhs; self.mods |= rhs;
} }
} }
impl ops::BitOr<HighlightModifier> for Highlight { impl ops::BitOr<HlMod> for Highlight {
type Output = Highlight; type Output = Highlight;
fn bitor(mut self, rhs: HighlightModifier) -> Highlight { fn bitor(mut self, rhs: HlMod) -> Highlight {
self |= rhs; self |= rhs;
self self
} }
} }
impl HighlightModifiers { impl HlMods {
pub fn contains(self, m: HighlightModifier) -> bool { pub fn contains(self, m: HlMod) -> bool {
self.0 & m.mask() == m.mask() self.0 & m.mask() == m.mask()
} }
pub fn iter(self) -> impl Iterator<Item = HighlightModifier> { pub fn iter(self) -> impl Iterator<Item = HlMod> {
HighlightModifier::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask()) HlMod::ALL.iter().copied().filter(move |it| self.0 & it.mask() == it.mask())
} }
} }

View file

@ -37,7 +37,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style> </style>
<pre><code><span class="comment documentation">/// ```</span> <pre><code><span class="comment documentation">/// ```</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="punctuation injected">_</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"early doctests should not go boom"</span><span class="punctuation injected">;</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="punctuation">{</span> <span class="keyword">struct</span> <span class="struct declaration">Foo</span> <span class="punctuation">{</span>
<span class="field declaration">bar</span><span class="punctuation">:</span> <span class="builtin_type">bool</span><span class="punctuation">,</span> <span class="field declaration">bar</span><span class="punctuation">:</span> <span class="builtin_type">bool</span><span class="punctuation">,</span>
@ -45,7 +45,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="keyword">impl</span> <span class="struct">Foo</span> <span class="punctuation">{</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="punctuation injected">_</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"Call me</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="punctuation injected">_</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Call me</span>
<span class="comment">// KILLER WHALE</span> <span class="comment">// KILLER WHALE</span>
<span class="comment documentation">/// </span><span class="string_literal injected"> Ishmael."</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="string_literal injected"> Ishmael."</span><span class="punctuation injected">;</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
@ -56,8 +56,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="comment documentation">/// # Examples</span> <span class="comment documentation">/// # Examples</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="comment documentation">/// #</span><span class="dummy injected"> </span><span class="attribute attribute injected">#</span><span class="attribute attribute injected">!</span><span class="attribute attribute injected">[</span><span class="function attribute injected">allow</span><span class="punctuation attribute injected">(</span><span class="attribute attribute injected">unused_mut</span><span class="punctuation attribute injected">)</span><span class="attribute attribute injected">]</span> <span class="comment documentation">/// #</span><span class="none injected"> </span><span class="attribute attribute injected">#</span><span class="attribute attribute injected">!</span><span class="attribute attribute injected">[</span><span class="function attribute injected">allow</span><span class="punctuation attribute injected">(</span><span class="attribute attribute injected">unused_mut</span><span class="punctuation attribute injected">)</span><span class="attribute attribute injected">]</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="keyword injected">mut</span><span class="dummy injected"> </span><span class="variable declaration injected mutable">foo</span><span class="punctuation injected">:</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="keyword injected">mut</span><span class="none injected"> </span><span class="variable declaration injected mutable">foo</span><span class="punctuation injected">:</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration static associated">new</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-&gt;</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration static associated">new</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="operator">-&gt;</span> <span class="struct">Foo</span> <span class="punctuation">{</span>
<span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">bar</span><span class="punctuation">:</span> <span class="bool_literal">true</span> <span class="punctuation">}</span> <span class="struct">Foo</span> <span class="punctuation">{</span> <span class="field">bar</span><span class="punctuation">:</span> <span class="bool_literal">true</span> <span class="punctuation">}</span>
@ -68,26 +68,26 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="comment documentation">/// # Examples</span> <span class="comment documentation">/// # Examples</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="comment documentation">/// </span><span class="keyword injected">use</span><span class="dummy injected"> </span><span class="module injected">x</span><span class="operator injected">::</span><span class="module injected">y</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="keyword injected">use</span><span class="none injected"> </span><span class="module injected">x</span><span class="operator injected">::</span><span class="module injected">y</span><span class="punctuation injected">;</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">foo</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">foo</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// </span><span class="comment injected">// calls bar on foo</span> <span class="comment documentation">/// </span><span class="comment injected">// calls bar on foo</span>
<span class="comment documentation">/// </span><span class="macro injected">assert!</span><span class="punctuation injected">(</span><span class="dummy injected">foo</span><span class="operator injected">.</span><span class="dummy injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="macro injected">assert!</span><span class="punctuation injected">(</span><span class="none injected">foo</span><span class="operator injected">.</span><span class="none injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">bar</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="variable injected">foo</span><span class="operator injected">.</span><span class="field injected">bar</span><span class="dummy injected"> </span><span class="operator injected">||</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="constant injected">bar</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">bar</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="variable injected">foo</span><span class="operator injected">.</span><span class="field injected">bar</span><span class="none injected"> </span><span class="operator injected">||</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="constant injected">bar</span><span class="punctuation injected">;</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// </span><span class="comment injected">/* multi-line</span> <span class="comment documentation">/// </span><span class="comment injected">/* multi-line</span>
<span class="comment documentation">/// </span><span class="comment injected"> comment */</span> <span class="comment documentation">/// </span><span class="comment injected"> comment */</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">multi_line_string</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="string_literal injected">"Foo</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">multi_line_string</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="string_literal injected">"Foo</span>
<span class="comment documentation">/// </span><span class="string_literal injected"> bar</span> <span class="comment documentation">/// </span><span class="string_literal injected"> bar</span>
<span class="comment documentation">/// </span><span class="string_literal injected"> "</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="string_literal injected"> "</span><span class="punctuation injected">;</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// ```rust,no_run</span> <span class="comment documentation">/// ```rust,no_run</span>
<span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="dummy injected"> </span><span class="variable declaration injected">foobar</span><span class="dummy injected"> </span><span class="operator injected">=</span><span class="dummy injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="operator injected">.</span><span class="function injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="none injected"> </span><span class="variable declaration injected">foobar</span><span class="none injected"> </span><span class="operator injected">=</span><span class="none injected"> </span><span class="struct injected">Foo</span><span class="operator injected">::</span><span class="function injected">new</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="operator injected">.</span><span class="function injected">bar</span><span class="punctuation injected">(</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span>
<span class="comment documentation">/// ```</span> <span class="comment documentation">/// ```</span>
<span class="comment documentation">///</span> <span class="comment documentation">///</span>
<span class="comment documentation">/// ```sh</span> <span class="comment documentation">/// ```sh</span>

View file

@ -40,9 +40,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span> <span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
<span class="function">f</span><span class="punctuation">(</span><span class="string_literal">r"</span> <span class="function">f</span><span class="punctuation">(</span><span class="string_literal">r"</span>
<span class="keyword">fn</span> <span class="function declaration">foo</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span> <span class="keyword">fn</span> <span class="function declaration">foo</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
<span class="function">foo</span><span class="punctuation">(</span>$0<span class="punctuation">{</span> <span class="function">foo</span><span class="punctuation">(</span><span class="keyword">$0</span><span class="punctuation">{</span>
<span class="numeric_literal">92</span> <span class="numeric_literal">92</span>
<span class="punctuation">}</span>$0<span class="punctuation">)</span> <span class="punctuation">}</span><span class="keyword">$0</span><span class="punctuation">)</span>
<span class="punctuation">}</span><span class="string_literal">"</span><span class="punctuation">)</span><span class="punctuation">;</span> <span class="punctuation">}</span><span class="string_literal">"</span><span class="punctuation">)</span><span class="punctuation">;</span>
<span class="punctuation">}</span> <span class="punctuation">}</span>
</code></pre> </code></pre>

View file

@ -6,10 +6,9 @@ use std::{
use ide::{ use ide::{
Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, Documentation, FileId, Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, Documentation, FileId,
FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag, FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HlMod, HlRange, HlTag, Indel, InlayHint,
HighlightedRange, Indel, InlayHint, InlayKind, InsertTextFormat, LineIndex, Markup, InlayKind, InsertTextFormat, LineIndex, Markup, NavigationTarget, ReferenceAccess, Runnable,
NavigationTarget, ReferenceAccess, Runnable, Severity, SourceChange, SourceFileEdit, Severity, SourceChange, SourceFileEdit, SymbolKind, TextEdit, TextRange, TextSize,
SymbolKind, TextEdit, TextRange, TextSize,
}; };
use itertools::Itertools; use itertools::Itertools;
@ -337,7 +336,7 @@ static TOKEN_RESULT_COUNTER: AtomicU32 = AtomicU32::new(1);
pub(crate) fn semantic_tokens( pub(crate) fn semantic_tokens(
text: &str, text: &str,
line_index: &LineIndex, line_index: &LineIndex,
highlights: Vec<HighlightedRange>, highlights: Vec<HlRange>,
) -> lsp_types::SemanticTokens { ) -> lsp_types::SemanticTokens {
let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string(); let id = TOKEN_RESULT_COUNTER.fetch_add(1, Ordering::SeqCst).to_string();
let mut builder = semantic_tokens::SemanticTokensBuilder::new(id); let mut builder = semantic_tokens::SemanticTokensBuilder::new(id);
@ -377,7 +376,7 @@ fn semantic_token_type_and_modifiers(
) -> (lsp_types::SemanticTokenType, semantic_tokens::ModifierSet) { ) -> (lsp_types::SemanticTokenType, semantic_tokens::ModifierSet) {
let mut mods = semantic_tokens::ModifierSet::default(); let mut mods = semantic_tokens::ModifierSet::default();
let type_ = match highlight.tag { let type_ = match highlight.tag {
HighlightTag::Symbol(symbol) => match symbol { HlTag::Symbol(symbol) => match symbol {
SymbolKind::Module => lsp_types::SemanticTokenType::NAMESPACE, SymbolKind::Module => lsp_types::SemanticTokenType::NAMESPACE,
SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE, SymbolKind::Impl => lsp_types::SemanticTokenType::TYPE,
SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY, SymbolKind::Field => lsp_types::SemanticTokenType::PROPERTY,
@ -389,7 +388,7 @@ fn semantic_token_type_and_modifiers(
SymbolKind::SelfParam => semantic_tokens::SELF_KEYWORD, SymbolKind::SelfParam => semantic_tokens::SELF_KEYWORD,
SymbolKind::Local => lsp_types::SemanticTokenType::VARIABLE, SymbolKind::Local => lsp_types::SemanticTokenType::VARIABLE,
SymbolKind::Function => { SymbolKind::Function => {
if highlight.modifiers.contains(HighlightModifier::Associated) { if highlight.mods.contains(HlMod::Associated) {
lsp_types::SemanticTokenType::METHOD lsp_types::SemanticTokenType::METHOD
} else { } else {
lsp_types::SemanticTokenType::FUNCTION lsp_types::SemanticTokenType::FUNCTION
@ -412,38 +411,34 @@ fn semantic_token_type_and_modifiers(
SymbolKind::Trait => lsp_types::SemanticTokenType::INTERFACE, SymbolKind::Trait => lsp_types::SemanticTokenType::INTERFACE,
SymbolKind::Macro => lsp_types::SemanticTokenType::MACRO, SymbolKind::Macro => lsp_types::SemanticTokenType::MACRO,
}, },
HighlightTag::BuiltinType => semantic_tokens::BUILTIN_TYPE, HlTag::BuiltinType => semantic_tokens::BUILTIN_TYPE,
HighlightTag::Dummy => semantic_tokens::GENERIC, HlTag::None => semantic_tokens::GENERIC,
HighlightTag::ByteLiteral | HighlightTag::NumericLiteral => { HlTag::ByteLiteral | HlTag::NumericLiteral => lsp_types::SemanticTokenType::NUMBER,
lsp_types::SemanticTokenType::NUMBER HlTag::BoolLiteral => semantic_tokens::BOOLEAN,
} HlTag::CharLiteral | HlTag::StringLiteral => lsp_types::SemanticTokenType::STRING,
HighlightTag::BoolLiteral => semantic_tokens::BOOLEAN, HlTag::Comment => lsp_types::SemanticTokenType::COMMENT,
HighlightTag::CharLiteral | HighlightTag::StringLiteral => { HlTag::Attribute => semantic_tokens::ATTRIBUTE,
lsp_types::SemanticTokenType::STRING HlTag::Keyword => lsp_types::SemanticTokenType::KEYWORD,
} HlTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE,
HighlightTag::Comment => lsp_types::SemanticTokenType::COMMENT, HlTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
HighlightTag::Attribute => semantic_tokens::ATTRIBUTE, HlTag::Operator => lsp_types::SemanticTokenType::OPERATOR,
HighlightTag::Keyword => lsp_types::SemanticTokenType::KEYWORD, HlTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
HighlightTag::UnresolvedReference => semantic_tokens::UNRESOLVED_REFERENCE, HlTag::Punctuation => semantic_tokens::PUNCTUATION,
HighlightTag::FormatSpecifier => semantic_tokens::FORMAT_SPECIFIER,
HighlightTag::Operator => lsp_types::SemanticTokenType::OPERATOR,
HighlightTag::EscapeSequence => semantic_tokens::ESCAPE_SEQUENCE,
HighlightTag::Punctuation => semantic_tokens::PUNCTUATION,
}; };
for modifier in highlight.modifiers.iter() { for modifier in highlight.mods.iter() {
let modifier = match modifier { let modifier = match modifier {
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER, HlMod::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION, HlMod::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION, HlMod::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION,
HighlightModifier::Injected => semantic_tokens::INJECTED, HlMod::Injected => semantic_tokens::INJECTED,
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW, HlMod::ControlFlow => semantic_tokens::CONTROL_FLOW,
HighlightModifier::Mutable => semantic_tokens::MUTABLE, HlMod::Mutable => semantic_tokens::MUTABLE,
HighlightModifier::Consuming => semantic_tokens::CONSUMING, HlMod::Consuming => semantic_tokens::CONSUMING,
HighlightModifier::Unsafe => semantic_tokens::UNSAFE, HlMod::Unsafe => semantic_tokens::UNSAFE,
HighlightModifier::Callable => semantic_tokens::CALLABLE, HlMod::Callable => semantic_tokens::CALLABLE,
HighlightModifier::Static => lsp_types::SemanticTokenModifier::STATIC, HlMod::Static => lsp_types::SemanticTokenModifier::STATIC,
HighlightModifier::Associated => continue, HlMod::Associated => continue,
}; };
mods |= modifier; mods |= modifier;
} }