Skip to content

Commit

Permalink
fix(autocomplete): @datasourcename (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Nov 7, 2022
1 parent 5e69a09 commit c6ca8e0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
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',
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

0 comments on commit c6ca8e0

Please sign in to comment.