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(ls): Autocomplete datasource field "extensions" #1303

Merged
merged 1 commit into from
Nov 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/language-server/src/__test__/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ suite('Completions', function () {
label: 'relationMode',
kind: CompletionItemKind.Field,
}
const fieldPostgresqlExtensions = {
label: 'extensions',
kind: CompletionItemKind.Field,
}

const sqlite = { label: 'sqlite', kind: CompletionItemKind.Constant }
const mysql = { label: 'mysql', kind: CompletionItemKind.Constant }
const postgresql = {
Expand Down Expand Up @@ -324,6 +329,25 @@ suite('Completions', function () {
})
})

test('Diagnoses field extension availability', () => {
assertCompletion({
schema: /* Prisma */ `
generator client {
previewFeatures = ["postgresqlExtensions"]
}

datasource db {
provider = "postgresql"
|
}
`,
expected: {
isIncomplete: false,
items: [fieldUrl, fieldShadowDatabaseUrl, fieldRelationMode, fieldPostgresqlExtensions],
},
})
})

test('provider = "|"', () => {
assertCompletion({
schema: /* Prisma */ `
Expand Down
7 changes: 6 additions & 1 deletion packages/language-server/src/completion/completions.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
"insertText": "relationMode = $0",
"documentation": "Set the global relation mode for all relations. Values can be either `\"foreignKeys\"` (Default), or `\"prisma\"`. [learn more](https://pris.ly/d/relationMode)",
"fullSignature": "relationMode = \"foreignKeys\" | \"prisma\")"
},
{
"label": "extensions",
"insertText": "extensions = [$0]",
"documentation": "Enable PostgreSQL extensions. [Learn More](https://pris.ly/d/postgresql-extensions)"
}
],
"generatorFields": [
Expand Down Expand Up @@ -489,4 +494,4 @@
"documentation": "The relation will not use Foreign Keys from the databse. Prisma Client will emulate their behavior for update and delete queries. [learn more](https://pris.ly/d/relationMode)"
}
]
}
}
10 changes: 8 additions & 2 deletions packages/language-server/src/completion/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,14 @@ export function getSuggestionsForFieldTypes(

function getSuggestionForDataSourceField(block: Block, lines: string[], position: Position): CompletionItem[] {
// create deep copy
const suggestions: CompletionItem[] = klona(supportedDataSourceFields)
let suggestions: CompletionItem[] = klona(supportedDataSourceFields)

const postgresExtensionsEnabled = getAllPreviewFeaturesFromGenerators(lines)?.includes('postgresqlextensions')
const isPostgres = getFirstDatasourceProvider(lines)?.includes('postgres')

if (!(postgresExtensionsEnabled && isPostgres)) {
suggestions = suggestions.filter((item) => item.label !== 'extensions')
}

const labels: string[] = removeInvalidFieldSuggestions(
suggestions.map((item) => item.label),
Expand Down Expand Up @@ -476,7 +483,6 @@ export function getSuggestionForSupportedFields(
}
}
}
break
}

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/previewFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type PreviewFeatures =
// value must be lowercase
Lowercase<'fullTextIndex'>
Lowercase<'fullTextIndex'> | Lowercase<'postgresqlExtensions'>