mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 23:24:29 +00:00
5776: Fix eslint errors on .eslintrc.js and rollup.config.js r=matklad a=fuafa Eslint complains if these two files does not include in the `tsconfig.json`. ``` Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: .eslintrc.js. The file must be included in at least one of the projects provided.eslint ``` ![image](https://user-images.githubusercontent.com/20750310/90338269-176d4f80-e01b-11ea-8710-3ea817b235d2.png) 5780: Fixup whitespace when adding missing impl items r=matklad a=jDomantas Generate properly formatted whitespace when adding impl items - with an empty line between items and removing extra whitespace that often appears at the end. This is my first time working on rust analyzer so I'm not very familiar with its internal APIs. If there's a better way to do such syntax tree editing I'd be glad to hear it. Co-authored-by: xiaofa <xiaofalzx@gmail.com> Co-authored-by: jDomantas <djadenkus@gmail.com>
This commit is contained in:
commit
7d95a8447c
4 changed files with 89 additions and 9 deletions
|
@ -48,7 +48,6 @@ enum AddMissingImplMembersMode {
|
|||
// fn foo(&self) -> u32 {
|
||||
// ${0:todo!()}
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
|
@ -89,8 +88,8 @@ pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -
|
|||
// impl Trait for () {
|
||||
// Type X = ();
|
||||
// fn foo(&self) {}
|
||||
// $0fn bar(&self) {}
|
||||
//
|
||||
// $0fn bar(&self) {}
|
||||
// }
|
||||
// ```
|
||||
pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||
|
@ -240,15 +239,18 @@ struct S;
|
|||
|
||||
impl Foo for S {
|
||||
fn bar(&self) {}
|
||||
|
||||
$0type Output;
|
||||
|
||||
const CONST: usize = 42;
|
||||
|
||||
fn foo(&self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn baz(&self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
@ -281,10 +283,10 @@ struct S;
|
|||
|
||||
impl Foo for S {
|
||||
fn bar(&self) {}
|
||||
|
||||
fn foo(&self) {
|
||||
${0:todo!()}
|
||||
}
|
||||
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
@ -599,6 +601,7 @@ trait Foo {
|
|||
struct S;
|
||||
impl Foo for S {
|
||||
$0type Output;
|
||||
|
||||
fn foo(&self) {
|
||||
todo!()
|
||||
}
|
||||
|
@ -705,6 +708,58 @@ trait Tr {
|
|||
|
||||
impl Tr for () {
|
||||
$0type Ty;
|
||||
}"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_whitespace_fixup_preserves_bad_tokens() {
|
||||
check_assist(
|
||||
add_missing_impl_members,
|
||||
r#"
|
||||
trait Tr {
|
||||
fn foo();
|
||||
}
|
||||
|
||||
impl Tr for ()<|> {
|
||||
+++
|
||||
}"#,
|
||||
r#"
|
||||
trait Tr {
|
||||
fn foo();
|
||||
}
|
||||
|
||||
impl Tr for () {
|
||||
fn foo() {
|
||||
${0:todo!()}
|
||||
}
|
||||
+++
|
||||
}"#,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_whitespace_fixup_preserves_comments() {
|
||||
check_assist(
|
||||
add_missing_impl_members,
|
||||
r#"
|
||||
trait Tr {
|
||||
fn foo();
|
||||
}
|
||||
|
||||
impl Tr for ()<|> {
|
||||
// very important
|
||||
}"#,
|
||||
r#"
|
||||
trait Tr {
|
||||
fn foo();
|
||||
}
|
||||
|
||||
impl Tr for () {
|
||||
fn foo() {
|
||||
${0:todo!()}
|
||||
}
|
||||
// very important
|
||||
}"#,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -82,8 +82,8 @@ trait Trait {
|
|||
impl Trait for () {
|
||||
Type X = ();
|
||||
fn foo(&self) {}
|
||||
$0fn bar(&self) {}
|
||||
|
||||
$0fn bar(&self) {}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
|
@ -115,7 +115,6 @@ impl Trait<u32> for () {
|
|||
fn foo(&self) -> u32 {
|
||||
${0:todo!()}
|
||||
}
|
||||
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
|
|
|
@ -91,29 +91,52 @@ impl ast::AssocItemList {
|
|||
res = make_multiline(res);
|
||||
}
|
||||
items.into_iter().for_each(|it| res = res.append_item(it));
|
||||
res
|
||||
res.fixup_trailing_whitespace().unwrap_or(res)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn append_item(&self, item: ast::AssocItem) -> ast::AssocItemList {
|
||||
let (indent, position) = match self.assoc_items().last() {
|
||||
let (indent, position, whitespace) = match self.assoc_items().last() {
|
||||
Some(it) => (
|
||||
leading_indent(it.syntax()).unwrap_or_default().to_string(),
|
||||
InsertPosition::After(it.syntax().clone().into()),
|
||||
"\n\n",
|
||||
),
|
||||
None => match self.l_curly_token() {
|
||||
Some(it) => (
|
||||
" ".to_string() + &leading_indent(self.syntax()).unwrap_or_default(),
|
||||
InsertPosition::After(it.into()),
|
||||
"\n",
|
||||
),
|
||||
None => return self.clone(),
|
||||
},
|
||||
};
|
||||
let ws = tokens::WsBuilder::new(&format!("\n{}", indent));
|
||||
let ws = tokens::WsBuilder::new(&format!("{}{}", whitespace, indent));
|
||||
let to_insert: ArrayVec<[SyntaxElement; 2]> =
|
||||
[ws.ws().into(), item.syntax().clone().into()].into();
|
||||
self.insert_children(position, to_insert)
|
||||
}
|
||||
|
||||
/// Remove extra whitespace between last item and closing curly brace.
|
||||
fn fixup_trailing_whitespace(&self) -> Option<ast::AssocItemList> {
|
||||
let first_token_after_items =
|
||||
self.assoc_items().last()?.syntax().next_sibling_or_token()?;
|
||||
let last_token_before_curly = self.r_curly_token()?.prev_sibling_or_token()?;
|
||||
if last_token_before_curly != first_token_after_items {
|
||||
// there is something more between last item and
|
||||
// right curly than just whitespace - bail out
|
||||
return None;
|
||||
}
|
||||
let whitespace =
|
||||
last_token_before_curly.clone().into_token().and_then(ast::Whitespace::cast)?;
|
||||
let text = whitespace.syntax().text();
|
||||
let newline = text.rfind("\n")?;
|
||||
let keep = tokens::WsBuilder::new(&text[newline..]);
|
||||
Some(self.replace_children(
|
||||
first_token_after_items..=last_token_before_curly,
|
||||
std::iter::once(keep.ws().into()),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl ast::RecordExprFieldList {
|
||||
|
|
3
editors/code/.eslintignore
Normal file
3
editors/code/.eslintignore
Normal file
|
@ -0,0 +1,3 @@
|
|||
node_modules
|
||||
.eslintrc.js
|
||||
rollup.config.js
|
Loading…
Reference in a new issue