Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/ra_ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,14 @@ impl Analysis {

/// Computes syntax highlighting for the given file
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> {
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None))
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
}

/// Computes syntax highlighting for the given file range.
pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HighlightedRange>> {
self.with_db(|db| syntax_highlighting::highlight(db, frange.file_id, Some(frange.range)))
self.with_db(|db| {
syntax_highlighting::highlight(db, frange.file_id, Some(frange.range), false)
})
}

/// Computes syntax highlighting for the given file.
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_ide/src/prime_caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use crate::{FileId, RootDatabase};

pub(crate) fn prime_caches(db: &RootDatabase, files: Vec<FileId>) {
for file in files {
let _ = crate::syntax_highlighting::highlight(db, file, None);
let _ = crate::syntax_highlighting::highlight(db, file, None, false);
}
}
23 changes: 17 additions & 6 deletions crates/ra_ide/src/snapshots/highlight_doctest.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,45 @@
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
</style>
<pre><code><span class="keyword">impl</span> <span class="unresolved_reference">Foo</span> {
<pre><code><span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
<span class="field declaration">bar</span>: <span class="builtin_type">bool</span>,
}

<span class="keyword">impl</span> <span class="struct">Foo</span> {
<span class="keyword">pub</span> <span class="keyword">const</span> <span class="constant declaration">bar</span>: <span class="builtin_type">bool</span> = <span class="bool_literal">true</span>;

<span class="comment">/// Constructs a new `Foo`.</span>
<span class="comment">///</span>
<span class="comment">/// # Examples</span>
<span class="comment">///</span>
<span class="comment">/// ```</span>
<span class="comment">/// #</span> <span class="attribute">#![</span><span class="function attribute">allow</span><span class="attribute">(unused_mut)]</span>
<span class="comment">/// </span><span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">foo</span>: <span class="unresolved_reference">Foo</span> = <span class="unresolved_reference">Foo</span>::<span class="unresolved_reference">new</span>();
<span class="comment">/// </span><span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">foo</span>: <span class="struct">Foo</span> = <span class="struct">Foo</span>::<span class="function">new</span>();
<span class="comment">/// ```</span>
<span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration">new</span>() -&gt; <span class="unresolved_reference">Foo</span> {
<span class="unresolved_reference">Foo</span> { }
<span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration">new</span>() -&gt; <span class="struct">Foo</span> {
<span class="struct">Foo</span> { <span class="field">bar</span>: <span class="bool_literal">true</span> }
}

<span class="comment">/// `bar` method on `Foo`.</span>
<span class="comment">///</span>
<span class="comment">/// # Examples</span>
<span class="comment">///</span>
<span class="comment">/// ```</span>
<span class="comment">/// </span><span class="keyword">let</span> <span class="variable declaration">foo</span> = <span class="unresolved_reference">Foo</span>::<span class="unresolved_reference">new</span>();
<span class="comment">/// </span><span class="keyword">use</span> <span class="module">x</span>::<span class="module">y</span>;
<span class="comment">///</span>
<span class="comment">/// </span><span class="keyword">let</span> <span class="variable declaration">foo</span> = <span class="struct">Foo</span>::<span class="function">new</span>();
<span class="comment">///</span>
<span class="comment">/// </span><span class="comment">// calls bar on foo</span>
<span class="comment">/// </span><span class="macro">assert!</span>(foo.bar());
<span class="comment">///</span>
<span class="comment">/// </span><span class="keyword">let</span> <span class="variable declaration">bar</span> = <span class="variable">foo</span>.<span class="field">bar</span> || <span class="struct">Foo</span>::<span class="constant">bar</span>;
<span class="comment">///</span>
<span class="comment">/// </span><span class="comment">/* multi-line
</span><span class="comment">/// </span><span class="comment"> comment */</span>
<span class="comment">///</span>
Expand All @@ -63,7 +74,7 @@
<span class="comment">/// ```</span>
<span class="comment">///</span>
<span class="comment">/// ```</span>
<span class="comment">/// </span><span class="keyword">let</span> <span class="variable declaration">foobar</span> = <span class="unresolved_reference">Foo</span>::<span class="unresolved_reference">new</span>().<span class="unresolved_reference">bar</span>();
<span class="comment">/// </span><span class="keyword">let</span> <span class="variable declaration">foobar</span> = <span class="struct">Foo</span>::<span class="function">new</span>().<span class="function">bar</span>();
<span class="comment">/// ```</span>
<span class="keyword">pub</span> <span class="keyword">fn</span> <span class="function declaration">foo</span>(&<span class="self_keyword">self</span>) -&gt; <span class="builtin_type">bool</span> {
<span class="bool_literal">true</span>
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide/src/snapshots/highlight_injection.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide/src/snapshots/highlight_strings.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide/src/snapshots/highlight_unsafe.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide/src/snapshots/highlighting.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
Expand Down
1 change: 1 addition & 0 deletions crates/ra_ide/src/snapshots/rainbow_highlighting.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
Expand Down
63 changes: 60 additions & 3 deletions crates/ra_ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(crate) fn highlight(
db: &RootDatabase,
file_id: FileId,
range_to_highlight: Option<TextRange>,
syntactic_name_ref_highlighting: bool,
) -> Vec<HighlightedRange> {
let _p = profile("highlight");
let sema = Semantics::new(db);
Expand Down Expand Up @@ -104,6 +105,7 @@ pub(crate) fn highlight(
if let Some((highlight, binding_hash)) = highlight_element(
&sema,
&mut bindings_shadow_count,
syntactic_name_ref_highlighting,
name.syntax().clone().into(),
) {
stack.add(HighlightedRange {
Expand Down Expand Up @@ -200,9 +202,12 @@ pub(crate) fn highlight(

let is_format_string = format_string.as_ref() == Some(&element_to_highlight);

if let Some((highlight, binding_hash)) =
highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone())
{
if let Some((highlight, binding_hash)) = highlight_element(
&sema,
&mut bindings_shadow_count,
syntactic_name_ref_highlighting,
element_to_highlight.clone(),
) {
stack.add(HighlightedRange { range, highlight, binding_hash });
if let Some(string) =
element_to_highlight.as_token().cloned().and_then(ast::String::cast)
Expand Down Expand Up @@ -410,6 +415,7 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
fn highlight_element(
sema: &Semantics<RootDatabase>,
bindings_shadow_count: &mut FxHashMap<Name, u32>,
syntactic_name_ref_highlighting: bool,
element: SyntaxElement,
) -> Option<(Highlight, Option<u64>)> {
let db = sema.db;
Expand Down Expand Up @@ -463,6 +469,7 @@ fn highlight_element(
}
NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(),
},
None if syntactic_name_ref_highlighting => highlight_name_ref_by_syntax(name_ref),
None => HighlightTag::UnresolvedReference.into(),
}
}
Expand Down Expand Up @@ -614,3 +621,53 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {

tag.into()
}

fn highlight_name_ref_by_syntax(name: ast::NameRef) -> Highlight {
let default = HighlightTag::UnresolvedReference;

let parent = match name.syntax().parent() {
Some(it) => it,
_ => return default.into(),
};

let tag = match parent.kind() {
METHOD_CALL_EXPR => HighlightTag::Function,
FIELD_EXPR => HighlightTag::Field,
PATH_SEGMENT => {
let path = match parent.parent().and_then(ast::Path::cast) {
Some(it) => it,
_ => return default.into(),
};
let expr = match path.syntax().parent().and_then(ast::PathExpr::cast) {
Some(it) => it,
_ => {
// 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() {
HighlightTag::Struct
} else {
HighlightTag::Module
}
.into();
}
};
let parent = match expr.syntax().parent() {
Some(it) => it,
None => return default.into(),
};

match parent.kind() {
CALL_EXPR => HighlightTag::Function,
_ => {
if name.text().chars().next().unwrap_or_default().is_uppercase() {
HighlightTag::Struct
} else {
HighlightTag::Constant
}
}
}
}
_ => default,
};

tag.into()
}
3 changes: 2 additions & 1 deletion crates/ra_ide/src/syntax_highlighting/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
)
}

let ranges = highlight(db, file_id, None);
let ranges = highlight(db, file_id, None, false);
let text = parse.tree().syntax().to_string();
let mut prev_pos = TextSize::from(0);
let mut buf = String::new();
Expand Down Expand Up @@ -84,6 +84,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.variable { color: #DCDCCC; }
.format_specifier { color: #CC696B; }
.mutable { text-decoration: underline; }
.unresolved_reference { color: #FC5555; }

.keyword { color: #F0DFAF; font-weight: bold; }
.keyword.unsafe { color: #BC8383; font-weight: bold; }
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_ide/src/syntax_highlighting/injection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub(super) fn highlight_doc_comment(
let (analysis, tmp_file_id) = Analysis::from_single_file(text);

stack.push();
for mut h in analysis.highlight(tmp_file_id).unwrap() {
for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
// Determine start offset and end offset in case of multi-line ranges
let mut start_offset = None;
let mut end_offset = None;
Expand Down
14 changes: 12 additions & 2 deletions crates/ra_ide/src/syntax_highlighting/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,13 @@ fn main() {
fn test_highlight_doctest() {
check_highlighting(
r#"
struct Foo {
bar: bool,
}

impl Foo {
pub const bar: bool = true;

/// Constructs a new `Foo`.
///
/// # Examples
Expand All @@ -297,19 +303,23 @@ impl Foo {
/// let mut foo: Foo = Foo::new();
/// ```
pub const fn new() -> Foo {
Foo { }
Foo { bar: true }
}

/// `bar` method on `Foo`.
///
/// # Examples
///
/// ```
/// use x::y;
///
/// let foo = Foo::new();
///
/// // calls bar on foo
/// assert!(foo.bar());
///
/// let bar = foo.bar || Foo::bar;
///
/// /* multi-line
/// comment */
///
Expand All @@ -330,7 +340,7 @@ impl Foo {
.trim(),
"crates/ra_ide/src/snapshots/highlight_doctest.html",
false,
)
);
}

/// Highlights the code given by the `ra_fixture` argument, renders the
Expand Down