-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fmt): quick-fix create new block (#4890)
* Something broke in the definition for these quickfixes in language-tools so this is an opportunistic to both fix and move them to engines. Reference: https://github.com/prisma/language-tools/blob/53e70077abbcf26c03c8aabb416521dba248804b/packages/language-server/src/lib/code-actions/index.ts#L137-L166 * Also added block qfs for composite types - enum - type
- Loading branch information
Showing
13 changed files
with
475 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use std::collections::HashMap; | ||
|
||
use lsp_types::{CodeAction, CodeActionKind, CodeActionOrCommand, Diagnostic, Range, TextEdit, Url, WorkspaceEdit}; | ||
use psl::{ | ||
diagnostics::Span, | ||
parser_database::walkers::{CompositeTypeWalker, ModelWalker}, | ||
schema_ast::ast::WithSpan, | ||
}; | ||
|
||
use super::CodeActionsContext; | ||
|
||
pub(super) fn create_missing_block_for_model( | ||
actions: &mut Vec<CodeActionOrCommand>, | ||
context: &CodeActionsContext<'_>, | ||
model: ModelWalker<'_>, | ||
) { | ||
let span_model = model.ast_model().span(); | ||
let diagnostics = context | ||
.diagnostics_for_span_with_message(span_model, "is neither a built-in type, nor refers to another model,"); | ||
|
||
if diagnostics.is_empty() { | ||
return; | ||
} | ||
|
||
let span = Span { | ||
start: span_model.start, | ||
end: span_model.end + 1, // * otherwise it's still not outside the closing brace | ||
file_id: span_model.file_id, | ||
}; | ||
|
||
let range = super::range_after_span(context.initiating_file_source(), span); | ||
|
||
diagnostics.iter().for_each(|diag| { | ||
push_missing_block( | ||
diag, | ||
context.lsp_params.text_document.uri.clone(), | ||
range, | ||
"model", | ||
actions, | ||
); | ||
push_missing_block( | ||
diag, | ||
context.lsp_params.text_document.uri.clone(), | ||
range, | ||
"enum", | ||
actions, | ||
); | ||
|
||
if let Some(ds) = context.datasource() { | ||
if ds.active_provider == "mongodb" { | ||
push_missing_block( | ||
diag, | ||
context.lsp_params.text_document.uri.clone(), | ||
range, | ||
"type", | ||
actions, | ||
); | ||
} | ||
} | ||
}) | ||
} | ||
|
||
pub(super) fn create_missing_block_for_type( | ||
actions: &mut Vec<CodeActionOrCommand>, | ||
context: &CodeActionsContext<'_>, | ||
composite_type: CompositeTypeWalker<'_>, | ||
) { | ||
let span_type = composite_type.ast_composite_type().span; | ||
|
||
let diagnostics = context | ||
.diagnostics_for_span_with_message(span_type, "is neither a built-in type, nor refers to another model,"); | ||
|
||
if diagnostics.is_empty() { | ||
return; | ||
} | ||
|
||
let span = Span { | ||
start: span_type.start, | ||
end: span_type.end + 1, // * otherwise it's still not outside the closing brace | ||
file_id: span_type.file_id, | ||
}; | ||
|
||
let range = super::range_after_span(context.initiating_file_source(), span); | ||
diagnostics.iter().for_each(|diag| { | ||
push_missing_block( | ||
diag, | ||
context.lsp_params.text_document.uri.clone(), | ||
range, | ||
"type", | ||
actions, | ||
); | ||
push_missing_block( | ||
diag, | ||
context.lsp_params.text_document.uri.clone(), | ||
range, | ||
"enum", | ||
actions, | ||
); | ||
}) | ||
} | ||
|
||
fn push_missing_block( | ||
diag: &Diagnostic, | ||
uri: Url, | ||
range: Range, | ||
block_type: &str, | ||
actions: &mut Vec<CodeActionOrCommand>, | ||
) { | ||
let name: &str = diag.message.split('\"').collect::<Vec<&str>>()[1]; | ||
let new_text = format!("\n{block_type} {name} {{\n\n}}\n"); | ||
let text = TextEdit { range, new_text }; | ||
|
||
let mut changes = HashMap::new(); | ||
changes.insert(uri, vec![text]); | ||
|
||
let edit = WorkspaceEdit { | ||
changes: Some(changes), | ||
..Default::default() | ||
}; | ||
|
||
let action = CodeAction { | ||
title: format!("Create new {block_type} '{name}'"), | ||
kind: Some(CodeActionKind::QUICKFIX), | ||
edit: Some(edit), | ||
diagnostics: Some(vec![diag.clone()]), | ||
..Default::default() | ||
}; | ||
|
||
actions.push(CodeActionOrCommand::CodeAction(action)) | ||
} |
80 changes: 80 additions & 0 deletions
80
prisma-fmt/tests/code_actions/scenarios/create_missing_block/result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
[ | ||
{ | ||
"title": "Create new model 'Animal'", | ||
"kind": "quickfix", | ||
"diagnostics": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 11, | ||
"character": 11 | ||
}, | ||
"end": { | ||
"line": 11, | ||
"character": 17 | ||
} | ||
}, | ||
"severity": 1, | ||
"message": "Type \"Animal\" is neither a built-in type, nor refers to another model, composite type, or enum." | ||
} | ||
], | ||
"edit": { | ||
"changes": { | ||
"file:///path/to/schema.prisma": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 12, | ||
"character": 1 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": "\nmodel Animal {\n\n}\n" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"title": "Create new enum 'Animal'", | ||
"kind": "quickfix", | ||
"diagnostics": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 11, | ||
"character": 11 | ||
}, | ||
"end": { | ||
"line": 11, | ||
"character": 17 | ||
} | ||
}, | ||
"severity": 1, | ||
"message": "Type \"Animal\" is neither a built-in type, nor refers to another model, composite type, or enum." | ||
} | ||
], | ||
"edit": { | ||
"changes": { | ||
"file:///path/to/schema.prisma": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 12, | ||
"character": 1 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": "\nenum Animal {\n\n}\n" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] |
13 changes: 13 additions & 0 deletions
13
prisma-fmt/tests/code_actions/scenarios/create_missing_block/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgres" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model Kattbjorn { | ||
id String @id | ||
friend Animal | ||
} |
80 changes: 80 additions & 0 deletions
80
prisma-fmt/tests/code_actions/scenarios/create_missing_block_composite_type/result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
[ | ||
{ | ||
"title": "Create new type 'Animal'", | ||
"kind": "quickfix", | ||
"diagnostics": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 11, | ||
"character": 11 | ||
}, | ||
"end": { | ||
"line": 11, | ||
"character": 17 | ||
} | ||
}, | ||
"severity": 1, | ||
"message": "Type \"Animal\" is neither a built-in type, nor refers to another model, composite type, or enum." | ||
} | ||
], | ||
"edit": { | ||
"changes": { | ||
"file:///path/to/schema.prisma": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 12, | ||
"character": 1 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": "\ntype Animal {\n\n}\n" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
"title": "Create new enum 'Animal'", | ||
"kind": "quickfix", | ||
"diagnostics": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 11, | ||
"character": 11 | ||
}, | ||
"end": { | ||
"line": 11, | ||
"character": 17 | ||
} | ||
}, | ||
"severity": 1, | ||
"message": "Type \"Animal\" is neither a built-in type, nor refers to another model, composite type, or enum." | ||
} | ||
], | ||
"edit": { | ||
"changes": { | ||
"file:///path/to/schema.prisma": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 12, | ||
"character": 1 | ||
}, | ||
"end": { | ||
"line": 13, | ||
"character": 0 | ||
} | ||
}, | ||
"newText": "\nenum Animal {\n\n}\n" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
] |
13 changes: 13 additions & 0 deletions
13
prisma-fmt/tests/code_actions/scenarios/create_missing_block_composite_type/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "mongodb" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
type Kattbjorn { | ||
name String | ||
friend Animal | ||
} |
Oops, something went wrong.