3998: Make add_function generate functions in other modules via qualified path r=matklad a=TimoFreiberg

Additional feature for #3639 

- [x] Add tests for paths with more segments
- [x] Make generating the function in another file work
- [x] Add `pub` or `pub(crate)` to the generated function if it's generated in a different module
- [x] Make the assist jump to the edited file
- [x] Enable file support in the `check_assist` helper

4006: Syntax highlighting for format strings r=matklad a=ltentrup

I have an implementation for syntax highlighting for format string modifiers `{}`.
The first commit refactors the changes in #3826 into a separate struct.
The second commit implements the highlighting: first we check in a macro call whether the macro is a format macro from `std`. In this case, we remember the format string node. If we encounter this node during syntax highlighting, we check for the format modifiers `{}` using regular expressions.

There are a few places which I am not quite sure:
- Is the way I extract the macro names correct?
- Is the `HighlightTag::Attribute` suitable for highlighting the `{}`?

Let me know what you think, any feedback is welcome!

Co-authored-by: Timo Freiberg <timo.freiberg@gmail.com>
Co-authored-by: Leander Tentrup <leander.tentrup@gmail.com>
Co-authored-by: Leander Tentrup <ltentrup@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-04-24 20:10:54 +00:00 committed by GitHub
commit 51a0058d4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 940 additions and 95 deletions

View file

@ -12,7 +12,7 @@ use ra_ide_db::{
};
use ra_prof::profile;
use ra_syntax::{
ast::{self, HasQuotes, HasStringValue},
ast::{self, HasFormatSpecifier, HasQuotes, HasStringValue},
AstNode, AstToken, Direction, NodeOrToken, SyntaxElement,
SyntaxKind::*,
SyntaxToken, TextRange, WalkEvent, T,
@ -21,6 +21,7 @@ use rustc_hash::FxHashMap;
use crate::{call_info::ActiveParameter, Analysis, FileId};
use ast::FormatSpecifier;
pub(crate) use html::highlight_as_html;
pub use tags::{Highlight, HighlightModifier, HighlightModifiers, HighlightTag};
@ -31,6 +32,81 @@ pub struct HighlightedRange {
pub binding_hash: Option<u64>,
}
#[derive(Debug)]
struct HighlightedRangeStack {
stack: Vec<Vec<HighlightedRange>>,
}
/// We use a stack to implement the flattening logic for the highlighted
/// syntax ranges.
impl HighlightedRangeStack {
fn new() -> Self {
Self { stack: vec![Vec::new()] }
}
fn push(&mut self) {
self.stack.push(Vec::new());
}
/// Flattens the highlighted ranges.
///
/// For example `#[cfg(feature = "foo")]` contains the nested ranges:
/// 1) parent-range: Attribute [0, 23)
/// 2) child-range: String [16, 21)
///
/// The following code implements the flattening, for our example this results to:
/// `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
fn pop(&mut self) {
let children = self.stack.pop().unwrap();
let prev = self.stack.last_mut().unwrap();
let needs_flattening = !children.is_empty()
&& !prev.is_empty()
&& children.first().unwrap().range.is_subrange(&prev.last().unwrap().range);
if !needs_flattening {
prev.extend(children);
} else {
let mut parent = prev.pop().unwrap();
for ele in children {
assert!(ele.range.is_subrange(&parent.range));
let mut cloned = parent.clone();
parent.range = TextRange::from_to(parent.range.start(), ele.range.start());
cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end());
if !parent.range.is_empty() {
prev.push(parent);
}
prev.push(ele);
parent = cloned;
}
if !parent.range.is_empty() {
prev.push(parent);
}
}
}
fn add(&mut self, range: HighlightedRange) {
self.stack
.last_mut()
.expect("during DFS traversal, the stack must not be empty")
.push(range)
}
fn flattened(mut self) -> Vec<HighlightedRange> {
assert_eq!(
self.stack.len(),
1,
"after DFS traversal, the stack should only contain a single element"
);
let mut res = self.stack.pop().unwrap();
res.sort_by_key(|range| range.range.start());
// Check that ranges are sorted and disjoint
assert!(res
.iter()
.zip(res.iter().skip(1))
.all(|(left, right)| left.range.end() <= right.range.start()));
res
}
}
pub(crate) fn highlight(
db: &RootDatabase,
file_id: FileId,
@ -57,52 +133,18 @@ pub(crate) fn highlight(
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
// We use a stack for the DFS traversal below.
// When we leave a node, the we use it to flatten the highlighted ranges.
let mut res: Vec<Vec<HighlightedRange>> = vec![Vec::new()];
let mut stack = HighlightedRangeStack::new();
let mut current_macro_call: Option<ast::MacroCall> = None;
let mut format_string: Option<SyntaxElement> = None;
// Walk all nodes, keeping track of whether we are inside a macro or not.
// If in macro, expand it first and highlight the expanded code.
for event in root.preorder_with_tokens() {
match &event {
WalkEvent::Enter(_) => res.push(Vec::new()),
WalkEvent::Leave(_) => {
/* Flattens the highlighted ranges.
*
* For example `#[cfg(feature = "foo")]` contains the nested ranges:
* 1) parent-range: Attribute [0, 23)
* 2) child-range: String [16, 21)
*
* The following code implements the flattening, for our example this results to:
* `[Attribute [0, 16), String [16, 21), Attribute [21, 23)]`
*/
let children = res.pop().unwrap();
let prev = res.last_mut().unwrap();
let needs_flattening = !children.is_empty()
&& !prev.is_empty()
&& children.first().unwrap().range.is_subrange(&prev.last().unwrap().range);
if !needs_flattening {
prev.extend(children);
} else {
let mut parent = prev.pop().unwrap();
for ele in children {
assert!(ele.range.is_subrange(&parent.range));
let mut cloned = parent.clone();
parent.range = TextRange::from_to(parent.range.start(), ele.range.start());
cloned.range = TextRange::from_to(ele.range.end(), cloned.range.end());
if !parent.range.is_empty() {
prev.push(parent);
}
prev.push(ele);
parent = cloned;
}
if !parent.range.is_empty() {
prev.push(parent);
}
}
}
WalkEvent::Enter(_) => stack.push(),
WalkEvent::Leave(_) => stack.pop(),
};
let current = res.last_mut().expect("during DFS traversal, the stack must not be empty");
let event_range = match &event {
WalkEvent::Enter(it) => it.text_range(),
@ -119,7 +161,7 @@ pub(crate) fn highlight(
WalkEvent::Enter(Some(mc)) => {
current_macro_call = Some(mc.clone());
if let Some(range) = macro_call_range(&mc) {
current.push(HighlightedRange {
stack.add(HighlightedRange {
range,
highlight: HighlightTag::Macro.into(),
binding_hash: None,
@ -130,6 +172,7 @@ pub(crate) fn highlight(
WalkEvent::Leave(Some(mc)) => {
assert!(current_macro_call == Some(mc));
current_macro_call = None;
format_string = None;
continue;
}
_ => (),
@ -150,6 +193,30 @@ pub(crate) fn highlight(
};
let token = sema.descend_into_macros(token.clone());
let parent = token.parent();
// Check if macro takes a format string and remember it for highlighting later.
// The macros that accept a format string expand to a compiler builtin macros
// `format_args` and `format_args_nl`.
if let Some(fmt_macro_call) = parent.parent().and_then(ast::MacroCall::cast) {
if let Some(name) =
fmt_macro_call.path().and_then(|p| p.segment()).and_then(|s| s.name_ref())
{
match name.text().as_str() {
"format_args" | "format_args_nl" => {
format_string = parent
.children_with_tokens()
.filter(|t| t.kind() != WHITESPACE)
.nth(1)
.filter(|e| {
ast::String::can_cast(e.kind())
|| ast::RawString::can_cast(e.kind())
})
}
_ => {}
}
}
}
// We only care Name and Name_ref
match (token.kind(), parent.kind()) {
(IDENT, NAME) | (IDENT, NAME_REF) => parent.into(),
@ -161,27 +228,72 @@ pub(crate) fn highlight(
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(current, &sema, token, expanded).is_some() {
if highlight_injection(&mut stack, &sema, token, expanded).is_some() {
continue;
}
}
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)
highlight_element(&sema, &mut bindings_shadow_count, element_to_highlight.clone())
{
current.push(HighlightedRange { range, highlight, binding_hash });
stack.add(HighlightedRange { range, highlight, binding_hash });
if let Some(string) =
element_to_highlight.as_token().cloned().and_then(ast::String::cast)
{
stack.push();
if is_format_string {
string.lex_format_specifier(|piece_range, kind| {
if let Some(highlight) = highlight_format_specifier(kind) {
stack.add(HighlightedRange {
range: piece_range + range.start(),
highlight: highlight.into(),
binding_hash: None,
});
}
});
}
stack.pop();
} else if let Some(string) =
element_to_highlight.as_token().cloned().and_then(ast::RawString::cast)
{
stack.push();
if is_format_string {
string.lex_format_specifier(|piece_range, kind| {
if let Some(highlight) = highlight_format_specifier(kind) {
stack.add(HighlightedRange {
range: piece_range + range.start(),
highlight: highlight.into(),
binding_hash: None,
});
}
});
}
stack.pop();
}
}
}
assert_eq!(res.len(), 1, "after DFS traversal, the stack should only contain a single element");
let mut res = res.pop().unwrap();
res.sort_by_key(|range| range.range.start());
// Check that ranges are sorted and disjoint
assert!(res
.iter()
.zip(res.iter().skip(1))
.all(|(left, right)| left.range.end() <= right.range.start()));
res
stack.flattened()
}
fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
Some(match kind {
FormatSpecifier::Open
| FormatSpecifier::Close
| FormatSpecifier::Colon
| FormatSpecifier::Fill
| FormatSpecifier::Align
| FormatSpecifier::Sign
| FormatSpecifier::NumberSign
| FormatSpecifier::DollarSign
| FormatSpecifier::Dot
| FormatSpecifier::Asterisk
| FormatSpecifier::QuestionMark => HighlightTag::Attribute,
FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral,
FormatSpecifier::Identifier => HighlightTag::Local,
})
}
fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
@ -359,7 +471,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
}
fn highlight_injection(
acc: &mut Vec<HighlightedRange>,
acc: &mut HighlightedRangeStack,
sema: &Semantics<RootDatabase>,
literal: ast::RawString,
expanded: SyntaxToken,
@ -372,7 +484,7 @@ fn highlight_injection(
let (analysis, tmp_file_id) = Analysis::from_single_file(value);
if let Some(range) = literal.open_quote_text_range() {
acc.push(HighlightedRange {
acc.add(HighlightedRange {
range,
highlight: HighlightTag::StringLiteral.into(),
binding_hash: None,
@ -382,12 +494,12 @@ fn highlight_injection(
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)
acc.add(h)
}
}
if let Some(range) = literal.close_quote_text_range() {
acc.push(HighlightedRange {
acc.add(HighlightedRange {
range,
highlight: HighlightTag::StringLiteral.into(),
binding_hash: None,