Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/ide/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,26 @@ fn test_fn() {
);
}

#[test]
fn test_fill_struct_fields_raw_ident() {
check_fix(
r#"
struct TestStruct { r#type: u8 }

fn test_fn() {
TestStruct { $0 };
}
"#,
r"
struct TestStruct { r#type: u8 }

fn test_fn() {
TestStruct { r#type: () };
}
",
);
}

#[test]
fn test_fill_struct_fields_no_diagnostic() {
check_no_diagnostics(
Expand Down
14 changes: 12 additions & 2 deletions crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ use stdx::format_to;
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};

pub fn name(text: &str) -> ast::Name {
ast_from_text(&format!("mod {};", text))
ast_from_text(&format!("mod {}{};", raw_ident_esc(text), text))
}

pub fn name_ref(text: &str) -> ast::NameRef {
ast_from_text(&format!("fn f() {{ {}; }}", text))
ast_from_text(&format!("fn f() {{ {}{}; }}", raw_ident_esc(text), text))
}

fn raw_ident_esc(ident: &str) -> &'static str {
let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some();
if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that Self is an odd one out here? (or from_keyword is really weird?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, Self is a keyword, TIL!

"r#"
} else {
""
}
}

// FIXME: replace stringly-typed constructor with a family of typed ctors, a-la
// `expr_xxx`.
pub fn ty(text: &str) -> ast::Type {
Expand Down