Skip to content
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

feat(psl): add support for multi-line comments #5083

Merged
merged 1 commit into from
Dec 13, 2024
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
4 changes: 2 additions & 2 deletions psl/psl/tests/parsing/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ fn trailing_comments_allowed_in_configuration_blocks() {
datasource db {
provider = "postgres" // "mysql" | "sqlite" ...
url = env("TEST_POSTGRES_URI")
relationMode = "prisma" // = on or set to "foreignKeys" to turn off emulation
relationMode = "prisma" /* = on or set to "foreignKeys" to turn off emulation */
}

generator js {
provider = "prisma-client-js" // optional
previewFeatures = ["referentialIntegrity"] // []
previewFeatures = ["referentialIntegrity"] /* [] */
}
"#;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Models
With
Several
Lines
*/

model MyModel {
// Example Comment
field1 Int // Comment Field 1
Expand All @@ -11,6 +18,8 @@ model MyModel {
@@unique([field1, field3]) // there too
}

/* Types */

type YourType {
// Example Comment
field1 Int // Comment Field 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* Models
* With
* Several
* Lines
*/

model MyModel {
// Example Comment
field1 Int // Comment Field 1
Expand All @@ -10,6 +17,10 @@ model MyModel {
@@unique([field1, field3]) // there too
}

/**
* Types
*/

type YourType {
// Example Comment
field1 Int // Comment Field 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
datasource db {
provider = "postgres" // "mysql" | "sqlite" ...
url = env("TEST_POSTGRES_URI")
relationMode = "prisma" // = on or set to "foreignKeys" to turn off emulation
relationMode = "prisma" /* = on or set to "foreignKeys" to turn off emulation */
}

generator js {
provider = "prisma-client-js" // optional
previewFeatures = ["referentialIntegrity"] // []
previewFeatures = ["referentialIntegrity"] /* [] */
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
datasource db {
provider = "postgres" // "mysql" | "sqlite" ...
url = env("TEST_POSTGRES_URI")
relationMode = "prisma" // = on or set to "foreignKeys" to turn off emulation
relationMode = "prisma" /* = on or set to "foreignKeys" to turn off emulation */
}

generator js {
provider = "prisma-client-js" // optional
previewFeatures = ["referentialIntegrity"] // []
previewFeatures = ["referentialIntegrity"] /* [] */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Example Doc Comment
model MyModel {
pk Int @id

/*
* Multi-line comment
* There are several lines here.
*/

field1 Int // Comment Field 1
field2 Int // Comment Field 2

/**
* Multi-line comment for
* field3. This can contain **asterisks**.
*/
field3 Int // Comment Field 3
}
9 changes: 9 additions & 0 deletions psl/psl/tests/validation/comments/multi_line_block.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Comment MyModel
*/
model MyModel {
pk Int @id

field1 Int
field2 Int
}
9 changes: 9 additions & 0 deletions psl/psl/tests/validation/comments/muti_line_field.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
model MyModel {
pk Int @id

/**
* Multi-line comment for
* field1. This can contain **asterisks**.
*/
field1 Int
}
7 changes: 7 additions & 0 deletions psl/psl/tests/validation/comments/single_line_block.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Comment MyModel
model MyModel {
pk Int @id

field1 Int
field2 Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Example Doc Comment
model MyModel {
// Example Comment
pk Int @id

field1 Int // Comment Field 1
field2 Int // Comment Field 2
field3 Int // Comment Field 3
}
6 changes: 6 additions & 0 deletions psl/psl/tests/validation/comments/single_line_field.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
model MyModel {
pk Int @id

field1 Int // Comment Field 1
field2 Int // Comment Field 2
}
10 changes: 8 additions & 2 deletions psl/schema-ast/src/parser/datamodel.pest
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,17 @@ trailing_comma = @{ "," }
// ######################################
// Comments and Documentation Comments
// ######################################
comment_block = ${ ((doc_comment | comment) ~ NEWLINE?)+ }
trailing_comment = ${ doc_comment | comment }
comment_block = ${ ((doc_comment | comment | multi_line_comment) ~ NEWLINE?)+ }
trailing_comment = ${ doc_comment | comment | multi_line_comment }
doc_comment = { WHITESPACE* ~ "///" ~ doc_content }
comment = { WHITESPACE* ~ (!"///") ~ "//" ~ doc_content }
doc_content = @{ (!NEWLINE ~ ANY)* }
multi_line_comment = {
WHITESPACE*
~ "/*"
~ (!"*/" ~ ANY)* // Match any characters until the closing */
~ "*/"
}

// ######################################
// shared building blocks
Expand Down
9 changes: 9 additions & 0 deletions psl/schema-ast/src/parser/parse_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub(crate) fn parse_comment_block(token: Pair<'_>) -> Option<Comment> {
for comment in token.clone().into_inner() {
match comment.as_rule() {
Rule::doc_comment => lines.push(parse_doc_comment(comment)),
Rule::multi_line_comment => lines.push(parse_multi_line_comment(comment)),
Rule::comment | Rule::NEWLINE | Rule::WHITESPACE => {}
_ => parsing_catch_all(&comment, "comment block"),
}
Expand All @@ -28,6 +29,7 @@ pub(crate) fn parse_trailing_comment(pair: Pair<'_>) -> Option<Comment> {
for current in pair.into_inner() {
match current.as_rule() {
Rule::doc_comment => lines.push(parse_doc_comment(current)),
Rule::multi_line_comment => lines.push(parse_multi_line_comment(current)),
Rule::comment | Rule::NEWLINE | Rule::WHITESPACE => {}
_ => parsing_catch_all(&current, "trailing comment"),
}
Expand All @@ -52,3 +54,10 @@ pub(crate) fn parse_doc_comment(token: Pair<'_>) -> &str {
),
}
}

fn parse_multi_line_comment(token: Pair<'_>) -> &str {
// The matched string includes `/*` and `*/`. Strip them off.
let text = token.as_str();
// Safety check: multi_line_comment matches `/* ... */`, so length is at least 4.
text[2..text.len() - 2].trim()
}
26 changes: 26 additions & 0 deletions psl/schema-ast/src/reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,32 @@ fn reformat_comment_block(pair: Pair<'_>, table: &mut TableFormat) {
}
}
}
Rule::multi_line_comment => {
// Start the canonical multi-line comment block
table.start_new_line();
table.append_suffix_to_current_row("/**");

let content = current.as_str();
// Strip off `/*` and `*/`
let inner = &content[2..content.len() - 2];

// Normalize lines by removing leading `*` and extra whitespace.
for line in inner.lines() {
let line = line.trim();
// Remove a leading `*` if present, along with any following spaces.
let line = line.strip_prefix('*').map(str::trim_start).unwrap_or(line);

if !line.is_empty() {
table.start_new_line();
table.append_suffix_to_current_row(" * ");
table.append_suffix_to_current_row(line);
}
}

// Close the multi-line comment block
table.start_new_line();
table.append_suffix_to_current_row(" */");
}
_ => unreachable!(),
}
}
Expand Down
Loading