Skip to content

Commit

Permalink
fixes #1326
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Sep 19, 2023
1 parent 9a956d4 commit 8051e33
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 6 deletions.
166 changes: 166 additions & 0 deletions packages/language-server/src/__test__/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ items.length`,

suite('Completions', function () {
// used in more than 1 suite
//#region types
const fieldProvider = {
label: 'provider',
kind: CompletionItemKind.Field,
Expand Down Expand Up @@ -145,6 +146,7 @@ suite('Completions', function () {
label: 'name',
kind: CompletionItemKind.Property,
}
//#endregion

suite('BASE BLOCKS', () => {
test('Diagnoses block type suggestions for empty file', () => {
Expand Down Expand Up @@ -503,6 +505,7 @@ suite('Completions', function () {

suite('GENERATOR BLOCK', () => {
// fieldProvider defined above already
//#region types
const fieldOutput = { label: 'output', kind: CompletionItemKind.Field }
const fieldBinaryTargets = {
label: 'binaryTargets',
Expand All @@ -516,6 +519,7 @@ suite('Completions', function () {
label: 'engineType',
kind: CompletionItemKind.Field,
}
//#endregion

test('Diagnoses generator field suggestions in empty block', () => {
assertCompletion({
Expand Down Expand Up @@ -596,6 +600,7 @@ suite('Completions', function () {
})

suite('BLOCK ATTRIBUTES', () => {
//#region types
const blockAttributeId = {
label: '@@id',
kind: CompletionItemKind.Property,
Expand Down Expand Up @@ -636,6 +641,7 @@ suite('Completions', function () {
label: 'two',
kind: CompletionItemKind.Property,
}
//#endregion

test('@@id([|])', () => {
assertCompletion({
Expand Down Expand Up @@ -1649,6 +1655,40 @@ suite('Completions', function () {
})
})
})

test('self-filter', () => {
assertCompletion({
schema: /* prisma */ `
generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema", "fullTextIndex"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
schemas = ["one"]
}
model A {
id Int
name String
@@id([id])
@@unique([id])
@@index([id])
@@fulltext([name])
@@map("hi")
@@ignore
@@schema("bas")
|
}
`,
expected: {
isIncomplete: false,
items: [blockAttributeUnique, blockAttributeIndex, blockAttributeFulltextIndex],
},
})
})
})

suite('TYPES', () => {
Expand Down Expand Up @@ -2067,6 +2107,7 @@ suite('Completions', function () {
})

suite('FIELD ATTRIBUTES', () => {
//#region types
const fieldAttributeId = {
label: '@id',
kind: CompletionItemKind.Property,
Expand Down Expand Up @@ -2197,6 +2238,7 @@ suite('Completions', function () {
label: 'Desc',
kind: CompletionItemKind.Enum,
}
//#endregion

test('Diagnoses field and block attribute suggestions', () => {
assertCompletion({
Expand Down Expand Up @@ -3442,6 +3484,130 @@ suite('Completions', function () {
})
})
})

suite('self-filter', () => {
test('Baseline', () => {
assertCompletion({
schema: /* prisma */ `
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model A {
id Int @id @unique @default(autoincrement()) @map("hi") @ignore @db.Integer |
}
`,
expected: {
isIncomplete: false,
items: [fieldAttributeRelation],
},
})
})

test('@relation', () => {
assertCompletion({
schema: /* prisma */ `
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model A {
id Int @id
b B @relation(fields: [bId], references: [id]) |
bId Int
}
model B {
id Int @id
A A[]
}
`,
expected: {
isIncomplete: false,
items: [
fieldAttributeDatasourceName,
fieldAttributeUnique,
fieldAttributeMap,
fieldAttributeDefault,
fieldAttributeIgnore,
],
},
})
})
test('@@ignore filters @ignore', () => {
assertCompletion({
schema: /* prisma */ `
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model A {
id Int @default(autoincrement()) @map("hi")
name String |
@@ignore
}
`,
expected: {
isIncomplete: false,
items: [
fieldAttributeDatasourceName,
fieldAttributeId,
fieldAttributeUnique,
fieldAttributeMap,
fieldAttributeDefault,
fieldAttributeRelation,
],
},
})
})
test('@@id filters @id', () => {
assertCompletion({
schema: /* prisma */ `
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model A {
id Int |
@@id([id])
}
`,
expected: {
isIncomplete: false,
items: [
fieldAttributeDatasourceName,
fieldAttributeUnique,
fieldAttributeMap,
fieldAttributeDefault,
fieldAttributeRelation,
fieldAttributeIgnore,
],
},
})
})
})
})

suite('SQL Server: clustered', () => {
Expand Down
28 changes: 22 additions & 6 deletions packages/language-server/src/completion/completionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ export function filterSuggestionsForBlock(
lines: string[],
): CompletionItem[] {
let reachedStartLine = false

for (const [key, item] of lines.entries()) {
if (key === block.range.start.line + 1) {
reachedStartLine = true
Expand All @@ -406,11 +407,23 @@ export function filterSuggestionsForBlock(
// Ignore commented lines
if (!item.startsWith('//')) {
// TODO we should also remove the other suggestions if used (default()...)
if (item.includes('@id')) {
suggestions = suggestions.filter((attribute) => !attribute.label.includes('id'))
}
// * Filter already-present attributes that can't be duplicated
;['@id', '@@map', '@@ignore', '@@schema'].forEach((label) => {
if (item.includes(label)) {
suggestions = suggestions.filter((suggestion) => suggestion.label !== label)

if (label === '@@ignore') {
suggestions = suggestions.filter((suggestion) => suggestion.label !== '@ignore')
}

if (label === '@id') {
suggestions = suggestions.filter((suggestion) => suggestion.label !== '@@id')
}
}
})
}
}

return suggestions
}

Expand Down Expand Up @@ -446,9 +459,12 @@ export function filterSuggestionsForLine(
suggestions = suggestions.filter((suggestion) => suggestion.label !== '@updatedAt')
}

if (currentLine.includes('@map')) {
suggestions = suggestions.filter((suggestion) => suggestion.label !== '@map')
}
// * Filter already-present attributes that can't be duplicated
fieldAttributes.forEach(({ label }) => {
if (currentLine.includes(label)) {
suggestions = suggestions.filter((suggestion) => suggestion.label !== label)
}
})

return suggestions
}
Expand Down

0 comments on commit 8051e33

Please sign in to comment.