-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Put syntax highlighting into syntax highlighting #3349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,12 @@ use ra_ide_db::{ | |
| }; | ||
| use ra_prof::profile; | ||
| use ra_syntax::{ | ||
| ast, AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind::*, TextRange, WalkEvent, T, | ||
| ast, AstNode, AstToken, Direction, NodeOrToken, SyntaxElement, SyntaxKind::*, SyntaxToken, | ||
| TextRange, WalkEvent, T, | ||
| }; | ||
| use rustc_hash::FxHashMap; | ||
|
|
||
| use crate::{references::classify_name_ref, FileId}; | ||
| use crate::{call_info::call_info_for_token, references::classify_name_ref, Analysis, FileId}; | ||
|
|
||
| pub(crate) use html::highlight_as_html; | ||
| pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag}; | ||
|
|
@@ -94,11 +95,12 @@ pub(crate) fn highlight( | |
| WalkEvent::Enter(it) => it, | ||
| WalkEvent::Leave(_) => continue, | ||
| }; | ||
|
|
||
| let range = element.text_range(); | ||
|
|
||
| let element_to_highlight = if current_macro_call.is_some() { | ||
| // Inside a macro -- expand it first | ||
| let token = match element.into_token() { | ||
| let token = match element.clone().into_token() { | ||
| Some(it) if it.parent().kind() == TOKEN_TREE => it, | ||
| _ => continue, | ||
| }; | ||
|
|
@@ -110,9 +112,17 @@ pub(crate) fn highlight( | |
| _ => token.into(), | ||
| } | ||
| } else { | ||
| element | ||
| element.clone() | ||
| }; | ||
|
|
||
| if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) { | ||
| let expanded = element_to_highlight.as_token().unwrap().clone(); | ||
| if highlight_injection(&mut res, &sema, token, expanded).is_some() { | ||
| eprintln!("res = {:?}", res); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| if let Some((highlight, binding_hash)) = | ||
| highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight) | ||
| { | ||
|
|
@@ -281,3 +291,44 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { | |
| _ => default, | ||
| } | ||
| } | ||
|
|
||
| fn highlight_injection( | ||
| acc: &mut Vec<HighlightedRange>, | ||
| sema: &Semantics<RootDatabase>, | ||
| literal: ast::RawString, | ||
| expanded: SyntaxToken, | ||
| ) -> Option<()> { | ||
| let call_info = call_info_for_token(&sema, expanded)?; | ||
| let idx = call_info.active_parameter?; | ||
| let name = call_info.signature.parameter_names.get(idx)?; | ||
| if name != "ra_fixture" { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So to enable the syntax highlight inside string literal, the parameter name need to be called "ra_fixture", right ? |
||
| return None; | ||
| } | ||
| let value = literal.value()?; | ||
| let (analysis, tmp_file_id) = Analysis::from_single_file(value); | ||
|
|
||
| if let Some(range) = literal.open_quote_text_range() { | ||
| acc.push(HighlightedRange { | ||
| range, | ||
| highlight: HighlightTag::LiteralString.into(), | ||
| binding_hash: None, | ||
| }) | ||
| } | ||
|
|
||
| for mut h in analysis.highlight(tmp_file_id).unwrap() { | ||
| if let Some(r) = literal.map_range_up(h.range) { | ||
| h.range = r; | ||
| acc.push(h) | ||
| } | ||
| } | ||
|
|
||
| if let Some(range) = literal.close_quote_text_range() { | ||
| acc.push(HighlightedRange { | ||
| range, | ||
| highlight: HighlightTag::LiteralString.into(), | ||
| binding_hash: None, | ||
| }) | ||
| } | ||
|
|
||
| Some(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a leftover?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, I've removed this during the weekend I think