-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Datasource Spans & Completions (#3703)
* Added datasource completions from LT to prisma-fmt * Completions for DS now available at start of line * ds completions no longer available outside of the closing curly-brace * Updated the rest of the grammar and parsing logic This follows the changes for config blocks Co-authored-by: Tom Houlé <13155277+tomhoule@users.noreply.github.com> * Updated reformatter to handle inner_contents Update test expects Co-authored-by: Tom Houlé <13155277+tomhoule@users.noreply.github.com> * fix prisma-fmt tests to reflect span changes * added tests for default ds completions and multischema * Added: sub-completions for xyz_url new datasource positions * Update: config values now Optional Added: new diagnostic for props without value Co-authored-by: Tom Houlé <13155277+tomhoule@users.noreply.github.com> * Added tests for url argument completions * Updated get_config test snapshots * Added support for params to pretty_doc * Added XYZ_URL completion for `env()` Added tests * Updated completion test snapshots due to params * Remove engines tag from completion labels * removed commented line * update `env` doc to mention db pull * extensions completion * validation to only offer completions for what's not already there * `add_quotes` for envar name completions * Updated Tests: env -> env() per what was in LT XYZ_URL kind -> 21, per what was in LT db gen > db pull completions no longer re-show-up formatting * missed change in merge * no fancy features allowed * revert direct_url span back to just the arg * Clippy complaining about unused inner_span for models and views - this will need to be re-added later when models and views are updated * formatting * Update ds and connector to check prop definitions * cleanup unused * Added connector specific completion capabilities. Moved extensions and schemas (only postgres). Co-authored-by: Julius de Bruijn <bruijn@prisma.io> * Add schemas completions to other connectors - mssql, mysql, cockroachdb Moved connector completions to builtin-connectors crate * rename push_completions -> datamodel_completions formatting --------- Co-authored-by: Tom Houlé <13155277+tomhoule@users.noreply.github.com> Co-authored-by: Julius de Bruijn <bruijn@prisma.io>
- Loading branch information
1 parent
1c2b092
commit 3b9f029
Showing
56 changed files
with
1,208 additions
and
291 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
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,160 @@ | ||
use std::collections::HashMap; | ||
|
||
use lsp_types::{ | ||
CompletionItem, CompletionItemKind, CompletionList, Documentation, InsertTextFormat, MarkupContent, MarkupKind, | ||
}; | ||
use psl::datamodel_connector::format_completion_docs; | ||
|
||
use super::{add_quotes, CompletionContext}; | ||
|
||
pub(super) fn relation_mode_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: "relationMode".to_owned(), | ||
insert_text: Some(r#"relationmode = $0"#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::FIELD), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#"relationMode = "foreignKeys" | "prisma""#, | ||
r#"Set the global relation mode for all relations. Values can be either "foreignKeys" (Default), or "prisma". [Learn more](https://pris.ly/d/relation-mode)"#, | ||
None, | ||
), | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn direct_url_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: "directUrl".to_owned(), | ||
insert_text: Some(r#"directUrl = $0"#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::FIELD), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#"directUrl = "String" | env("ENVIRONMENT_VARIABLE")"#, | ||
r#"Connection URL for direct connection to the database. [Learn more](https://pris.ly/d/data-proxy-cli)."#, | ||
None, | ||
) | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn shadow_db_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: "shadowDatabaseUrl".to_owned(), | ||
insert_text: Some(r#"shadowDatabaseUrl = $0"#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::FIELD), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#"shadowDatabaseUrl = "String" | env("ENVIRONMENT_VARIABLE")"#, | ||
r#"Connection URL including authentication info to use for Migrate's [shadow database](https://pris.ly/d/migrate-shadow)."#, | ||
None, | ||
), | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn url_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: "url".to_owned(), | ||
insert_text: Some(r#"url = $0"#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::FIELD), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#"url = "String" | env("ENVIRONMENT_VARIABLE")"#, | ||
r#"Connection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/connection-strings)."#, | ||
None, | ||
), | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn provider_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: "provider".to_owned(), | ||
insert_text: Some(r#"provider = $0"#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::FIELD), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#"provider = "foo""#, | ||
r#"Describes which datasource connector to use. Can be one of the following datasource providers: `postgresql`, `mysql`, `sqlserver`, `sqlite`, `mongodb` or `cockroachdb`."#, | ||
None, | ||
), | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn url_env_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: "env()".to_owned(), | ||
insert_text: Some(r#"env($0)"#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::PROPERTY), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#"env(_ environmentVariable: string)"#, | ||
r#"Specifies a datasource via an environment variable. When running a Prisma CLI command that needs the database connection URL (e.g. `prisma db pull`), you need to make sure that the `DATABASE_URL` environment variable is set. One way to do so is by creating a `.env` file. Note that the file must be in the same directory as your schema.prisma file to automatically be picked up by the Prisma CLI.""#, | ||
Some(HashMap::from([( | ||
"environmentVariable", | ||
"The environment variable in which the database connection URL is stored.", | ||
)])), | ||
), | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn url_quotes_completion(completion_list: &mut CompletionList) { | ||
completion_list.items.push(CompletionItem { | ||
label: r#""""#.to_owned(), | ||
insert_text: Some(r#""$0""#.to_owned()), | ||
insert_text_format: Some(InsertTextFormat::SNIPPET), | ||
kind: Some(CompletionItemKind::PROPERTY), | ||
documentation: Some(Documentation::MarkupContent(MarkupContent { | ||
kind: MarkupKind::Markdown, | ||
value: format_completion_docs( | ||
r#""connectionString""#, | ||
r#"Connection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/prisma-schema)."#, | ||
None, | ||
), | ||
})), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
pub(super) fn url_env_db_completion(completion_list: &mut CompletionList, kind: &str, ctx: CompletionContext<'_>) { | ||
let text = match kind { | ||
"url" => "DATABASE_URL", | ||
"directUrl" => "DIRECT_URL", | ||
"shadowDatabaseUrl" => "SHADOW_DATABASE_URL", | ||
_ => unreachable!(), | ||
}; | ||
|
||
let insert_text = if add_quotes(ctx.params, ctx.db.source()) { | ||
format!(r#""{text}""#) | ||
} else { | ||
text.to_owned() | ||
}; | ||
|
||
completion_list.items.push(CompletionItem { | ||
label: text.to_owned(), | ||
insert_text: Some(insert_text), | ||
insert_text_format: Some(InsertTextFormat::PLAIN_TEXT), | ||
kind: Some(CompletionItemKind::CONSTANT), | ||
..Default::default() | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -10,8 +10,8 @@ | |
"character": 4 | ||
}, | ||
"end": { | ||
"line": 4, | ||
"character": 0 | ||
"line": 3, | ||
"character": 35 | ||
} | ||
}, | ||
"severity": 2, | ||
|
55 changes: 55 additions & 0 deletions
55
...a-fmt/tests/text_document_completion/scenarios/datasource_default_completions/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,55 @@ | ||
{ | ||
"isIncomplete": false, | ||
"items": [ | ||
{ | ||
"label": "provider", | ||
"kind": 5, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\nprovider = \"foo\"\n```\n___\nDescribes which datasource connector to use. Can be one of the following datasource providers: `postgresql`, `mysql`, `sqlserver`, `sqlite`, `mongodb` or `cockroachdb`.\n\n" | ||
}, | ||
"insertText": "provider = $0", | ||
"insertTextFormat": 2 | ||
}, | ||
{ | ||
"label": "url", | ||
"kind": 5, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\nurl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/connection-strings).\n\n" | ||
}, | ||
"insertText": "url = $0", | ||
"insertTextFormat": 2 | ||
}, | ||
{ | ||
"label": "shadowDatabaseUrl", | ||
"kind": 5, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\nshadowDatabaseUrl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL including authentication info to use for Migrate's [shadow database](https://pris.ly/d/migrate-shadow).\n\n" | ||
}, | ||
"insertText": "shadowDatabaseUrl = $0", | ||
"insertTextFormat": 2 | ||
}, | ||
{ | ||
"label": "directUrl", | ||
"kind": 5, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\ndirectUrl = \"String\" | env(\"ENVIRONMENT_VARIABLE\")\n```\n___\nConnection URL for direct connection to the database. [Learn more](https://pris.ly/d/data-proxy-cli).\n\n" | ||
}, | ||
"insertText": "directUrl = $0", | ||
"insertTextFormat": 2 | ||
}, | ||
{ | ||
"label": "relationMode", | ||
"kind": 5, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\nrelationMode = \"foreignKeys\" | \"prisma\"\n```\n___\nSet the global relation mode for all relations. Values can be either \"foreignKeys\" (Default), or \"prisma\". [Learn more](https://pris.ly/d/relation-mode)\n\n" | ||
}, | ||
"insertText": "relationmode = $0", | ||
"insertTextFormat": 2 | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
...fmt/tests/text_document_completion/scenarios/datasource_default_completions/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,7 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
<|> | ||
} |
25 changes: 25 additions & 0 deletions
25
...-fmt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/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,25 @@ | ||
{ | ||
"isIncomplete": false, | ||
"items": [ | ||
{ | ||
"label": "env()", | ||
"kind": 10, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\nenv(_ environmentVariable: string)\n```\n___\nSpecifies a datasource via an environment variable. When running a Prisma CLI command that needs the database connection URL (e.g. `prisma db pull`), you need to make sure that the `DATABASE_URL` environment variable is set. One way to do so is by creating a `.env` file. Note that the file must be in the same directory as your schema.prisma file to automatically be picked up by the Prisma CLI.\"\n\n_@param_ environmentVariable The environment variable in which the database connection URL is stored." | ||
}, | ||
"insertText": "env($0)", | ||
"insertTextFormat": 2 | ||
}, | ||
{ | ||
"label": "\"\"", | ||
"kind": 10, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "```prisma\n\"connectionString\"\n```\n___\nConnection URL including authentication info. Each datasource provider documents the URL syntax. Most providers use the syntax provided by the database. [Learn more](https://pris.ly/d/prisma-schema).\n\n" | ||
}, | ||
"insertText": "\"$0\"", | ||
"insertTextFormat": 2 | ||
} | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
...mt/tests/text_document_completion/scenarios/datasource_direct_url_arguments/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,10 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["multischema"] | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = "" | ||
directUrl = <|> | ||
} |
11 changes: 11 additions & 0 deletions
11
prisma-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/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,11 @@ | ||
{ | ||
"isIncomplete": false, | ||
"items": [ | ||
{ | ||
"label": "DIRECT_URL", | ||
"kind": 21, | ||
"insertText": "DIRECT_URL", | ||
"insertTextFormat": 1 | ||
} | ||
] | ||
} |
10 changes: 10 additions & 0 deletions
10
...a-fmt/tests/text_document_completion/scenarios/datasource_env_db_direct_url/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,10 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["multischema"] | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("") | ||
directUrl = env("<|>") | ||
} |
Oops, something went wrong.