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

fix(autocomplete): @datasourcename #1277

Merged
merged 4 commits into from
Nov 7, 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
26 changes: 26 additions & 0 deletions packages/language-server/src/__test__/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,10 @@ suite('Completions', function () {
label: '@ignore',
kind: CompletionItemKind.Property,
}
const fieldAttributeDatasourceName = {
label: '@db',
kind: CompletionItemKind.Property,
}

const functionCuid = {
label: 'cuid()',
Expand Down Expand Up @@ -1852,6 +1856,7 @@ suite('Completions', function () {
label: 'dbgenerated("")',
kind: CompletionItemKind.Function,
}

const staticValueEmptyList = {
label: '[]',
kind: CompletionItemKind.Value,
Expand Down Expand Up @@ -2024,6 +2029,26 @@ suite('Completions', function () {
items: [{ label: 'lastName', kind: CompletionItemKind.Field }],
},
})
assertCompletion({
provider: 'postgresql',
schema: /* Prisma */ `
model Post {
id Int @id @default()
email String? @unique
name String |
}`,
expected: {
isIncomplete: false,
items: [
fieldAttributeDatasourceName,
fieldAttributeUnique,
fieldAttributeMap,
fieldAttributeDefault,
fieldAttributeRelation,
fieldAttributeIgnore,
],
},
})
})

const enumUserTypeExpectedItems = [
Expand Down Expand Up @@ -2081,6 +2106,7 @@ suite('Completions', function () {
expected: {
isIncomplete: false,
items: [
fieldAttributeDatasourceName,
fieldAttributeUnique,
fieldAttributeMap,
// fieldAttributeDefault, is invalid
Expand Down
5 changes: 3 additions & 2 deletions packages/language-server/src/__test__/quickFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ suite('Quick Fixes', () => {

let character = 11

if (process.platform === "win32") {
if (process.platform === 'win32') {
character = 12
}

Expand All @@ -240,7 +240,8 @@ suite('Quick Fixes', () => {
},
},
severity: 1,
message: 'Error parsing attribute "@relation": The argument `references` must refer to a unique criteria in the related model. Consider adding an `@unique` attribute to the field `field` in the model `A`.',
message:
'Error parsing attribute "@relation": The argument `references` must refer to a unique criteria in the related model. Consider adding an `@unique` attribute to the field `field` in the model `A`.',
},
],
edit: {
Expand Down
1 change: 1 addition & 0 deletions packages/language-server/src/completion/completionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ export function getNativeTypes(document: TextDocument, prismaType: string): Comp
let nativeTypes: NativeTypeConstructors[] = nativeTypeConstructors(document.getText())

if (nativeTypes.length === 0) {
console.log('Did not receive any native type suggestions from prisma-fmt call.')
return []
}

Expand Down
43 changes: 28 additions & 15 deletions packages/language-server/src/completion/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ export function getSuggestionForNativeTypes(
}
}

/**
* Should suggest all field attributes for a given field
* EX: id Int |> @id, @default, @datasourceName, ...etc
*
* If `@datasourceName.` |> suggests nativeTypes
* @param block
* @param currentLine
* @param lines
* @param wordsBeforePosition
* @param document
* @returns
*/
export function getSuggestionForFieldAttribute(
block: Block,
currentLine: string,
Expand All @@ -127,36 +139,37 @@ export function getSuggestionForFieldAttribute(
}
let suggestions: CompletionItem[] = []

const enabledNativeTypes = declaredNativeTypes(document)
// Because @.?
if (enabledNativeTypes && wordsBeforePosition.length >= 2) {
if (wordsBeforePosition.length >= 2) {
const datasourceName = getFirstDatasourceName(lines)
const prismaType = wordsBeforePosition[1]
const nativeTypeSuggestions = getNativeTypes(document, prismaType)

if (datasourceName && nativeTypeSuggestions.length !== 0) {
if (!currentLine.includes('@' + datasourceName)) {
if (datasourceName) {
if (!currentLine.includes(`@${datasourceName}`)) {
suggestions.push({
// https://code.visualstudio.com/docs/editor/intellisense#_types-of-completions
kind: CompletionItemKind.Property,
label: '@' + datasourceName,
documentation:
'Defines a native database type that should be used for this field. See https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#native-types-mapping',
insertText: '@db.$0',
Jolg42 marked this conversation as resolved.
Show resolved Hide resolved
insertText: `@${datasourceName}$0`,
insertTextFormat: InsertTextFormat.Snippet,
})
} else if (
// Check that we are not separated by a space like `@db. |`
wordsBeforePosition[wordsBeforePosition.length - 1] === `@${datasourceName}`
) {
suggestions.push(...nativeTypeSuggestions)
return {
items: suggestions,
isIncomplete: false,
}

if (nativeTypeSuggestions.length !== 0) {
if (
// Check that we are not separated by a space like `@db. |`
wordsBeforePosition[wordsBeforePosition.length - 1] === `@${datasourceName}`
) {
suggestions.push(...nativeTypeSuggestions)
return {
items: suggestions,
isIncomplete: false,
}
}
}
} else {
console.log('Did not receive any native type suggestions from prisma-fmt call.')
}
}

Expand Down