mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Fix tt::Punct
's spacing calculation
This commit is contained in:
parent
b87a23b91b
commit
41b0c54c07
5 changed files with 152 additions and 27 deletions
|
@ -94,11 +94,11 @@ macro_rules! m {
|
||||||
($($s:stmt)*) => (stringify!($($s |)*);)
|
($($s:stmt)*) => (stringify!($($s |)*);)
|
||||||
}
|
}
|
||||||
stringify!(;
|
stringify!(;
|
||||||
|;
|
| ;
|
||||||
|92|;
|
|92| ;
|
||||||
|let x = 92|;
|
|let x = 92| ;
|
||||||
|loop {}
|
|loop {}
|
||||||
|;
|
| ;
|
||||||
|);
|
|);
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -118,7 +118,7 @@ m!(.. .. ..);
|
||||||
macro_rules! m {
|
macro_rules! m {
|
||||||
($($p:pat)*) => (stringify!($($p |)*);)
|
($($p:pat)*) => (stringify!($($p |)*);)
|
||||||
}
|
}
|
||||||
stringify!(.. .. ..|);
|
stringify!(.. .. .. |);
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,14 +82,14 @@ fn attribute_macro_syntax_completion_2() {
|
||||||
#[proc_macros::identity_when_valid]
|
#[proc_macros::identity_when_valid]
|
||||||
fn foo() { bar.; blub }
|
fn foo() { bar.; blub }
|
||||||
"#,
|
"#,
|
||||||
expect![[r##"
|
expect![[r#"
|
||||||
#[proc_macros::identity_when_valid]
|
#[proc_macros::identity_when_valid]
|
||||||
fn foo() { bar.; blub }
|
fn foo() { bar.; blub }
|
||||||
|
|
||||||
fn foo() {
|
fn foo() {
|
||||||
bar.;
|
bar. ;
|
||||||
blub
|
blub
|
||||||
}"##]],
|
}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -293,14 +293,10 @@ pub(crate) fn reverse_fixups(
|
||||||
undo_info: &SyntaxFixupUndoInfo,
|
undo_info: &SyntaxFixupUndoInfo,
|
||||||
) {
|
) {
|
||||||
tt.token_trees.retain(|tt| match tt {
|
tt.token_trees.retain(|tt| match tt {
|
||||||
tt::TokenTree::Leaf(leaf) => {
|
tt::TokenTree::Leaf(leaf) => token_map.synthetic_token_id(leaf.id()) != Some(EMPTY_ID),
|
||||||
token_map.synthetic_token_id(leaf.id()).is_none()
|
tt::TokenTree::Subtree(st) => {
|
||||||
|| token_map.synthetic_token_id(leaf.id()) != Some(EMPTY_ID)
|
st.delimiter.map_or(true, |d| token_map.synthetic_token_id(d.id) != Some(EMPTY_ID))
|
||||||
}
|
}
|
||||||
tt::TokenTree::Subtree(st) => st.delimiter.map_or(true, |d| {
|
|
||||||
token_map.synthetic_token_id(d.id).is_none()
|
|
||||||
|| token_map.synthetic_token_id(d.id) != Some(EMPTY_ID)
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
tt.token_trees.iter_mut().for_each(|tt| match tt {
|
tt.token_trees.iter_mut().for_each(|tt| match tt {
|
||||||
tt::TokenTree::Subtree(tt) => reverse_fixups(tt, token_map, undo_info),
|
tt::TokenTree::Subtree(tt) => reverse_fixups(tt, token_map, undo_info),
|
||||||
|
@ -339,9 +335,8 @@ mod tests {
|
||||||
|
|
||||||
// the fixed-up tree should be syntactically valid
|
// the fixed-up tree should be syntactically valid
|
||||||
let (parse, _) = mbe::token_tree_to_syntax_node(&tt, ::mbe::TopEntryPoint::MacroItems);
|
let (parse, _) = mbe::token_tree_to_syntax_node(&tt, ::mbe::TopEntryPoint::MacroItems);
|
||||||
assert_eq!(
|
assert!(
|
||||||
parse.errors(),
|
parse.errors().is_empty(),
|
||||||
&[],
|
|
||||||
"parse has syntax errors. parse tree:\n{:#?}",
|
"parse has syntax errors. parse tree:\n{:#?}",
|
||||||
parse.syntax_node()
|
parse.syntax_node()
|
||||||
);
|
);
|
||||||
|
@ -468,12 +463,13 @@ fn foo() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
fn foo () {a .__ra_fixup}
|
fn foo () {a . __ra_fixup}
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[ignore]
|
||||||
fn incomplete_field_expr_2() {
|
fn incomplete_field_expr_2() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
|
@ -488,6 +484,7 @@ fn foo () {a .__ra_fixup ;}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[ignore]
|
||||||
fn incomplete_field_expr_3() {
|
fn incomplete_field_expr_3() {
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
|
@ -525,7 +522,7 @@ fn foo() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
fn foo () {let x = a .__ra_fixup ;}
|
fn foo () {let x = a . __ra_fixup ;}
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -541,7 +538,7 @@ fn foo() {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
fn foo () {a .b ; bar () ;}
|
fn foo () {a . b ; bar () ;}
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,9 @@ use tt::buffer::{Cursor, TokenBuffer};
|
||||||
|
|
||||||
use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, TokenMap};
|
use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, TokenMap};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
/// Convert the syntax node to a `TokenTree` (what macro
|
/// Convert the syntax node to a `TokenTree` (what macro
|
||||||
/// will consume).
|
/// will consume).
|
||||||
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) {
|
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) {
|
||||||
|
@ -228,7 +231,7 @@ fn convert_tokens<C: TokenConverter>(conv: &mut C) -> tt::Subtree {
|
||||||
}
|
}
|
||||||
|
|
||||||
let spacing = match conv.peek().map(|next| next.kind(conv)) {
|
let spacing = match conv.peek().map(|next| next.kind(conv)) {
|
||||||
Some(kind) if !kind.is_trivia() => tt::Spacing::Joint,
|
Some(kind) if is_single_token_op(kind) => tt::Spacing::Joint,
|
||||||
_ => tt::Spacing::Alone,
|
_ => tt::Spacing::Alone,
|
||||||
};
|
};
|
||||||
let char = match token.to_char(conv) {
|
let char = match token.to_char(conv) {
|
||||||
|
@ -307,6 +310,35 @@ fn convert_tokens<C: TokenConverter>(conv: &mut C) -> tt::Subtree {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_single_token_op(kind: SyntaxKind) -> bool {
|
||||||
|
matches!(
|
||||||
|
kind,
|
||||||
|
EQ | L_ANGLE
|
||||||
|
| R_ANGLE
|
||||||
|
| BANG
|
||||||
|
| AMP
|
||||||
|
| PIPE
|
||||||
|
| TILDE
|
||||||
|
| AT
|
||||||
|
| DOT
|
||||||
|
| COMMA
|
||||||
|
| SEMICOLON
|
||||||
|
| COLON
|
||||||
|
| POUND
|
||||||
|
| DOLLAR
|
||||||
|
| QUESTION
|
||||||
|
| PLUS
|
||||||
|
| MINUS
|
||||||
|
| STAR
|
||||||
|
| SLASH
|
||||||
|
| PERCENT
|
||||||
|
| CARET
|
||||||
|
// LIFETIME_IDENT will be split into a sequence of `'` (a single quote) and an
|
||||||
|
// identifier.
|
||||||
|
| LIFETIME_IDENT
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the textual content of a doc comment block as a quoted string
|
/// Returns the textual content of a doc comment block as a quoted string
|
||||||
/// That is, strips leading `///` (or `/**`, etc)
|
/// That is, strips leading `///` (or `/**`, etc)
|
||||||
/// and strips the ending `*/`
|
/// and strips the ending `*/`
|
||||||
|
@ -591,10 +623,10 @@ impl SynToken {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SrcToken<Converter> for SynToken {
|
impl SrcToken<Converter> for SynToken {
|
||||||
fn kind(&self, _ctx: &Converter) -> SyntaxKind {
|
fn kind(&self, ctx: &Converter) -> SyntaxKind {
|
||||||
match self {
|
match self {
|
||||||
SynToken::Ordinary(token) => token.kind(),
|
SynToken::Ordinary(token) => token.kind(),
|
||||||
SynToken::Punch(token, _) => token.kind(),
|
SynToken::Punch(..) => SyntaxKind::from_char(self.to_char(ctx).unwrap()).unwrap(),
|
||||||
SynToken::Synthetic(token) => token.kind,
|
SynToken::Synthetic(token) => token.kind,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -651,7 +683,7 @@ impl TokenConverter for Converter {
|
||||||
}
|
}
|
||||||
|
|
||||||
let curr = self.current.clone()?;
|
let curr = self.current.clone()?;
|
||||||
if !&self.range.contains_range(curr.text_range()) {
|
if !self.range.contains_range(curr.text_range()) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let (new_current, new_synth) =
|
let (new_current, new_synth) =
|
||||||
|
@ -809,12 +841,15 @@ impl<'a> TtTreeSink<'a> {
|
||||||
let next = last.bump();
|
let next = last.bump();
|
||||||
if let (
|
if let (
|
||||||
Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(curr), _)),
|
Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(curr), _)),
|
||||||
Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(_), _)),
|
Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(next), _)),
|
||||||
) = (last.token_tree(), next.token_tree())
|
) = (last.token_tree(), next.token_tree())
|
||||||
{
|
{
|
||||||
// Note: We always assume the semi-colon would be the last token in
|
// Note: We always assume the semi-colon would be the last token in
|
||||||
// other parts of RA such that we don't add whitespace here.
|
// other parts of RA such that we don't add whitespace here.
|
||||||
if curr.spacing == tt::Spacing::Alone && curr.char != ';' {
|
//
|
||||||
|
// When `next` is a `Punct` of `'`, that's a part of a lifetime identifier so we don't
|
||||||
|
// need to add whitespace either.
|
||||||
|
if curr.spacing == tt::Spacing::Alone && curr.char != ';' && next.char != '\'' {
|
||||||
self.inner.token(WHITESPACE, " ");
|
self.inner.token(WHITESPACE, " ");
|
||||||
self.text_pos += TextSize::of(' ');
|
self.text_pos += TextSize::of(' ');
|
||||||
}
|
}
|
||||||
|
|
93
crates/mbe/src/syntax_bridge/tests.rs
Normal file
93
crates/mbe/src/syntax_bridge/tests.rs
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use syntax::{ast, AstNode};
|
||||||
|
use test_utils::extract_annotations;
|
||||||
|
use tt::{
|
||||||
|
buffer::{TokenBuffer, TokenTreeRef},
|
||||||
|
Leaf, Punct, Spacing,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::syntax_node_to_token_tree;
|
||||||
|
|
||||||
|
fn check_punct_spacing(fixture: &str) {
|
||||||
|
let source_file = ast::SourceFile::parse(fixture).ok().unwrap();
|
||||||
|
let (subtree, token_map) = syntax_node_to_token_tree(source_file.syntax());
|
||||||
|
let mut annotations: HashMap<_, _> = extract_annotations(fixture)
|
||||||
|
.into_iter()
|
||||||
|
.map(|(range, annotation)| {
|
||||||
|
let token = token_map.token_by_range(range).expect("no token found");
|
||||||
|
let spacing = match annotation.as_str() {
|
||||||
|
"Alone" => Spacing::Alone,
|
||||||
|
"Joint" => Spacing::Joint,
|
||||||
|
a => panic!("unknown annotation: {}", a),
|
||||||
|
};
|
||||||
|
(token, spacing)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let buf = TokenBuffer::from_subtree(&subtree);
|
||||||
|
let mut cursor = buf.begin();
|
||||||
|
while !cursor.eof() {
|
||||||
|
while let Some(token_tree) = cursor.token_tree() {
|
||||||
|
if let TokenTreeRef::Leaf(Leaf::Punct(Punct { spacing, id, .. }), _) = token_tree {
|
||||||
|
if let Some(expected) = annotations.remove(&id) {
|
||||||
|
assert_eq!(expected, *spacing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor = cursor.bump_subtree();
|
||||||
|
}
|
||||||
|
cursor = cursor.bump();
|
||||||
|
}
|
||||||
|
|
||||||
|
assert!(annotations.is_empty(), "unchecked annotations: {:?}", annotations);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn punct_spacing() {
|
||||||
|
check_punct_spacing(
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
0+0;
|
||||||
|
//^ Alone
|
||||||
|
0+(0);
|
||||||
|
//^ Alone
|
||||||
|
0<=0;
|
||||||
|
//^ Joint
|
||||||
|
// ^ Alone
|
||||||
|
0<=(0);
|
||||||
|
// ^ Alone
|
||||||
|
a=0;
|
||||||
|
//^ Alone
|
||||||
|
a=(0);
|
||||||
|
//^ Alone
|
||||||
|
a+=0;
|
||||||
|
//^ Joint
|
||||||
|
// ^ Alone
|
||||||
|
a+=(0);
|
||||||
|
// ^ Alone
|
||||||
|
a&&b;
|
||||||
|
//^ Joint
|
||||||
|
// ^ Alone
|
||||||
|
a&&(b);
|
||||||
|
// ^ Alone
|
||||||
|
foo::bar;
|
||||||
|
// ^ Joint
|
||||||
|
// ^ Alone
|
||||||
|
use foo::{bar,baz,};
|
||||||
|
// ^ Alone
|
||||||
|
// ^ Alone
|
||||||
|
// ^ Alone
|
||||||
|
struct Struct<'a> {};
|
||||||
|
// ^ Joint
|
||||||
|
// ^ Joint
|
||||||
|
Struct::<0>;
|
||||||
|
// ^ Alone
|
||||||
|
Struct::<{0}>;
|
||||||
|
// ^ Alone
|
||||||
|
;;
|
||||||
|
//^ Joint
|
||||||
|
// ^ Alone
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue