mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Add support for marking doctest items as distinct from normal code, add default tag to all doctest elements
This commit is contained in:
parent
0c12c4f960
commit
351bba9bee
9 changed files with 47 additions and 34 deletions
|
@ -443,13 +443,13 @@ 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<HighlightedRange>> {
|
||||||
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false))
|
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None, false, None))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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<HighlightedRange>> {
|
||||||
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, None)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,6 @@ use crate::{FileId, RootDatabase};
|
||||||
|
|
||||||
pub(crate) fn prime_caches(db: &RootDatabase, files: Vec<FileId>) {
|
pub(crate) fn prime_caches(db: &RootDatabase, files: Vec<FileId>) {
|
||||||
for file in files {
|
for file in files {
|
||||||
let _ = crate::syntax_highlighting::highlight(db, file, None, false);
|
let _ = crate::syntax_highlighting::highlight(db, file, None, false, None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,9 +47,9 @@ 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="attribute">#![</span><span class="function attribute">allow</span><span class="attribute">(unused_mut)]</span>
|
<span class="comment documentation">/// #</span> <span class="operator injected">#</span><span class="operator injected">!</span><span class="operator injected">[</span><span class="operator injected">allow</span><span class="operator injected">(</span><span class="operator injected">unused_mut</span><span class="operator injected">)</span><span class="operator injected">]</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </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> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="operator injected"> </span><span class="keyword injected">mut</span><span class="operator injected"> </span><span class="operator injected">foo</span><span class="operator injected">:</span><span class="operator injected"> </span><span class="operator injected">Foo</span><span class="operator injected"> </span><span class="operator injected">=</span><span class="operator injected"> </span><span class="operator injected">Foo</span><span class="operator injected">::</span><span class="operator injected">new</span><span class="operator injected">(</span><span class="operator injected">)</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// ```</span>
|
</span><span class="operator injected"> </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">new</span>() -> <span class="struct">Foo</span> {
|
<span class="keyword">pub</span> <span class="keyword">const</span> <span class="keyword">fn</span> <span class="function declaration">new</span>() -> <span class="struct">Foo</span> {
|
||||||
<span class="struct">Foo</span> { <span class="field">bar</span>: <span class="bool_literal">true</span> }
|
<span class="struct">Foo</span> { <span class="field">bar</span>: <span class="bool_literal">true</span> }
|
||||||
}
|
}
|
||||||
|
@ -59,27 +59,27 @@ 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">use</span> <span class="module">x</span>::<span class="module">y</span>;
|
<span class="comment documentation">/// </span><span class="keyword injected">use</span><span class="operator injected"> </span><span class="operator injected">x</span><span class="operator injected">::</span><span class="operator injected">y</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">///</span>
|
</span><span class="comment documentation">///</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </span><span class="keyword">let</span> <span class="variable declaration">foo</span> = <span class="struct">Foo</span>::<span class="function">new</span>();
|
</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="operator injected"> </span><span class="operator injected">foo</span><span class="operator injected"> </span><span class="operator injected">=</span><span class="operator injected"> </span><span class="operator injected">Foo</span><span class="operator injected">::</span><span class="operator injected">new</span><span class="operator injected">(</span><span class="operator injected">)</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">///</span>
|
</span><span class="comment documentation">///</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </span><span class="comment">// calls bar on foo</span>
|
</span> <span class="comment documentation">/// </span><span class="comment injected">// calls bar on foo</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </span><span class="macro">assert!</span>(foo.bar());
|
</span> <span class="comment documentation">/// </span><span class="macro injected">assert!</span><span class="operator injected">(</span><span class="operator injected">foo</span><span class="operator injected">.</span><span class="operator injected">bar</span><span class="operator injected">(</span><span class="operator injected">)</span><span class="operator injected">)</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">///</span>
|
</span><span class="comment documentation">///</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </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> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="operator injected"> </span><span class="operator injected">bar</span><span class="operator injected"> </span><span class="operator injected">=</span><span class="operator injected"> </span><span class="operator injected">foo</span><span class="operator injected">.</span><span class="operator injected">bar</span><span class="operator injected"> </span><span class="operator injected">||</span><span class="operator injected"> </span><span class="operator injected">Foo</span><span class="operator injected">::</span><span class="operator injected">bar</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">///</span>
|
</span><span class="comment documentation">///</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </span><span class="comment">/* multi-line
|
</span> <span class="comment documentation">/// </span><span class="comment injected">/* multi-line
|
||||||
</span><span class="comment documentation">/// </span><span class="comment"> comment */</span>
|
</span><span class="comment documentation">/// </span><span class="comment injected"> comment */</span><span class="operator injected">
|
||||||
<span class="comment documentation">///</span>
|
</span><span class="comment documentation">///</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// </span><span class="keyword">let</span> <span class="variable declaration">multi_line_string</span> = <span class="string_literal">"Foo
|
</span> <span class="comment documentation">/// </span><span class="keyword injected">let</span><span class="operator injected"> </span><span class="operator injected">multi_line_string</span><span class="operator injected"> </span><span class="operator injected">=</span><span class="operator injected"> </span><span class="string_literal injected">"Foo
|
||||||
</span><span class="comment documentation">/// </span><span class="string_literal"> bar
|
</span><span class="comment documentation">/// </span><span class="string_literal injected"> bar
|
||||||
</span><span class="comment documentation">/// </span><span class="string_literal"> "</span>;
|
</span><span class="comment documentation">/// </span><span class="string_literal injected"> "</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">///</span>
|
</span><span class="comment documentation">///</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// ```</span>
|
</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">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 documentation">/// </span><span class="keyword injected">let</span><span class="operator injected"> </span><span class="operator injected">foobar</span><span class="operator injected"> </span><span class="operator injected">=</span><span class="operator injected"> </span><span class="operator injected">Foo</span><span class="operator injected">::</span><span class="operator injected">new</span><span class="operator injected">(</span><span class="operator injected">)</span><span class="operator injected">.</span><span class="operator injected">bar</span><span class="operator injected">(</span><span class="operator injected">)</span><span class="operator injected">;</span><span class="operator injected">
|
||||||
<span class="comment documentation">/// ```</span>
|
</span><span class="operator injected"> </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>
|
||||||
<span class="comment documentation">/// echo 1</span>
|
<span class="comment documentation">/// echo 1</span>
|
||||||
|
|
|
@ -45,6 +45,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,
|
||||||
|
default_tag: Option<HighlightTag>,
|
||||||
) -> Vec<HighlightedRange> {
|
) -> Vec<HighlightedRange> {
|
||||||
let _p = profile("highlight");
|
let _p = profile("highlight");
|
||||||
let sema = Semantics::new(db);
|
let sema = Semantics::new(db);
|
||||||
|
@ -107,6 +108,7 @@ pub(crate) fn highlight(
|
||||||
&mut bindings_shadow_count,
|
&mut bindings_shadow_count,
|
||||||
syntactic_name_ref_highlighting,
|
syntactic_name_ref_highlighting,
|
||||||
name.syntax().clone().into(),
|
name.syntax().clone().into(),
|
||||||
|
default_tag,
|
||||||
) {
|
) {
|
||||||
stack.add(HighlightedRange {
|
stack.add(HighlightedRange {
|
||||||
range: name.syntax().text_range(),
|
range: name.syntax().text_range(),
|
||||||
|
@ -206,6 +208,7 @@ pub(crate) fn highlight(
|
||||||
&mut bindings_shadow_count,
|
&mut bindings_shadow_count,
|
||||||
syntactic_name_ref_highlighting,
|
syntactic_name_ref_highlighting,
|
||||||
element_to_highlight.clone(),
|
element_to_highlight.clone(),
|
||||||
|
default_tag,
|
||||||
) {
|
) {
|
||||||
stack.add(HighlightedRange { range, highlight, binding_hash });
|
stack.add(HighlightedRange { range, highlight, binding_hash });
|
||||||
if let Some(string) =
|
if let Some(string) =
|
||||||
|
@ -430,13 +433,14 @@ fn highlight_element(
|
||||||
bindings_shadow_count: &mut FxHashMap<Name, u32>,
|
bindings_shadow_count: &mut FxHashMap<Name, u32>,
|
||||||
syntactic_name_ref_highlighting: bool,
|
syntactic_name_ref_highlighting: bool,
|
||||||
element: SyntaxElement,
|
element: SyntaxElement,
|
||||||
|
default_tag: Option<HighlightTag>,
|
||||||
) -> Option<(Highlight, Option<u64>)> {
|
) -> Option<(Highlight, Option<u64>)> {
|
||||||
let db = sema.db;
|
let db = sema.db;
|
||||||
let mut binding_hash = None;
|
let mut binding_hash = None;
|
||||||
let highlight: Highlight = match element.kind() {
|
let highlight: Highlight = match element.kind() {
|
||||||
FN_DEF => {
|
FN_DEF => {
|
||||||
bindings_shadow_count.clear();
|
bindings_shadow_count.clear();
|
||||||
return None;
|
default_tag?.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Highlight definitions depending on the "type" of the definition.
|
// Highlight definitions depending on the "type" of the definition.
|
||||||
|
@ -515,12 +519,12 @@ 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() {
|
||||||
return None;
|
default_tag?.into()
|
||||||
|
} else {
|
||||||
|
let mut h = Highlight::new(HighlightTag::Operator);
|
||||||
|
h |= HighlightModifier::Unsafe;
|
||||||
|
h
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut h = Highlight::new(HighlightTag::Operator);
|
|
||||||
h |= HighlightModifier::Unsafe;
|
|
||||||
h
|
|
||||||
}
|
}
|
||||||
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
|
T![!] if element.parent().and_then(ast::MacroCall::cast).is_some() => {
|
||||||
Highlight::new(HighlightTag::Macro)
|
Highlight::new(HighlightTag::Macro)
|
||||||
|
@ -546,7 +550,7 @@ fn highlight_element(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => return None,
|
_ => default_tag?.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return Some((highlight, binding_hash));
|
return Some((highlight, binding_hash));
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let ranges = highlight(db, file_id, None, false);
|
let ranges = highlight(db, file_id, None, false, None);
|
||||||
let text = parse.tree().syntax().to_string();
|
let text = parse.tree().syntax().to_string();
|
||||||
let mut prev_pos = TextSize::from(0);
|
let mut prev_pos = TextSize::from(0);
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
|
@ -150,7 +150,10 @@ pub(super) fn highlight_doc_comment(
|
||||||
let (analysis, tmp_file_id) = Analysis::from_single_file(text);
|
let (analysis, tmp_file_id) = Analysis::from_single_file(text);
|
||||||
|
|
||||||
stack.push();
|
stack.push();
|
||||||
for mut h in analysis.with_db(|db| super::highlight(db, tmp_file_id, None, true)).unwrap() {
|
for mut h in analysis
|
||||||
|
.with_db(|db| super::highlight(db, tmp_file_id, None, true, Some(HighlightTag::Operator)))
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
// Determine start offset and end offset in case of multi-line ranges
|
// Determine start offset and end offset in case of multi-line ranges
|
||||||
let mut start_offset = None;
|
let mut start_offset = None;
|
||||||
let mut end_offset = None;
|
let mut end_offset = None;
|
||||||
|
@ -172,6 +175,7 @@ pub(super) fn highlight_doc_comment(
|
||||||
h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(),
|
h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
h.highlight |= HighlightModifier::Injected;
|
||||||
stack.add(h);
|
stack.add(h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ pub enum HighlightModifier {
|
||||||
/// not.
|
/// not.
|
||||||
Definition,
|
Definition,
|
||||||
Documentation,
|
Documentation,
|
||||||
|
Injected,
|
||||||
Mutable,
|
Mutable,
|
||||||
Unsafe,
|
Unsafe,
|
||||||
}
|
}
|
||||||
|
@ -110,6 +111,7 @@ impl HighlightModifier {
|
||||||
HighlightModifier::ControlFlow,
|
HighlightModifier::ControlFlow,
|
||||||
HighlightModifier::Definition,
|
HighlightModifier::Definition,
|
||||||
HighlightModifier::Documentation,
|
HighlightModifier::Documentation,
|
||||||
|
HighlightModifier::Injected,
|
||||||
HighlightModifier::Mutable,
|
HighlightModifier::Mutable,
|
||||||
HighlightModifier::Unsafe,
|
HighlightModifier::Unsafe,
|
||||||
];
|
];
|
||||||
|
@ -120,6 +122,7 @@ impl HighlightModifier {
|
||||||
HighlightModifier::ControlFlow => "control",
|
HighlightModifier::ControlFlow => "control",
|
||||||
HighlightModifier::Definition => "declaration",
|
HighlightModifier::Definition => "declaration",
|
||||||
HighlightModifier::Documentation => "documentation",
|
HighlightModifier::Documentation => "documentation",
|
||||||
|
HighlightModifier::Injected => "injected",
|
||||||
HighlightModifier::Mutable => "mutable",
|
HighlightModifier::Mutable => "mutable",
|
||||||
HighlightModifier::Unsafe => "unsafe",
|
HighlightModifier::Unsafe => "unsafe",
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,7 @@ macro_rules! define_semantic_token_modifiers {
|
||||||
define_semantic_token_modifiers![
|
define_semantic_token_modifiers![
|
||||||
(CONSTANT, "constant"),
|
(CONSTANT, "constant"),
|
||||||
(CONTROL_FLOW, "controlFlow"),
|
(CONTROL_FLOW, "controlFlow"),
|
||||||
|
(INJECTED, "injected"),
|
||||||
(MUTABLE, "mutable"),
|
(MUTABLE, "mutable"),
|
||||||
(UNSAFE, "unsafe"),
|
(UNSAFE, "unsafe"),
|
||||||
(ATTRIBUTE_MODIFIER, "attribute"),
|
(ATTRIBUTE_MODIFIER, "attribute"),
|
||||||
|
|
|
@ -331,6 +331,7 @@ fn semantic_token_type_and_modifiers(
|
||||||
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
|
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
|
||||||
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
|
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
|
||||||
HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION,
|
HighlightModifier::Documentation => lsp_types::SemanticTokenModifier::DOCUMENTATION,
|
||||||
|
HighlightModifier::Injected => semantic_tokens::INJECTED,
|
||||||
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW,
|
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW,
|
||||||
HighlightModifier::Mutable => semantic_tokens::MUTABLE,
|
HighlightModifier::Mutable => semantic_tokens::MUTABLE,
|
||||||
HighlightModifier::Unsafe => semantic_tokens::UNSAFE,
|
HighlightModifier::Unsafe => semantic_tokens::UNSAFE,
|
||||||
|
|
Loading…
Reference in a new issue