Skip to content

Commit

Permalink
fix(completion): suggest @id for enum and ignore comments
Browse files Browse the repository at this point in the history
closes #1084
  • Loading branch information
Jolg42 committed Jun 30, 2022
1 parent 3a6dee5 commit bc7ce0a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
25 changes: 25 additions & 0 deletions packages/language-server/src/__test__/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,31 @@ suite('Completions', function () {
},
})
})
test('enum UserType |', () => {
assertCompletion({
schema: /* Prisma */ `
model DateTest {
// id Int @id @default()
enum UserType |
}
enum UserType {
ADMIN
NORMAL
}`,
expected: {
isIncomplete: false,
items: [
fieldAttributeId,
fieldAttributeUnique,
fieldAttributeMap,
fieldAttributeDefault,
fieldAttributeRelation,
fieldAttributeIgnore,
],
},
})
})

suite('@default()', function () {
test('Scalar lists', () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/language-server/src/completion/completionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,12 @@ export function removeInvalidAttributeSuggestions(
break
}

// TODO we should also remove the other suggestions if used (default()...)
if (item.includes('@id')) {
supportedAttributes = supportedAttributes.filter((attribute) => !attribute.label.includes('id'))
// Ingore commented lines
if (!item.startsWith('//')) {
// TODO we should also remove the other suggestions if used (default()...)
if (item.includes('@id')) {
supportedAttributes = supportedAttributes.filter((attribute) => !attribute.label.includes('id'))
}
}
}
return supportedAttributes
Expand Down
13 changes: 11 additions & 2 deletions packages/language-server/src/completion/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,20 @@ export function getSuggestionForFieldAttribute(

suggestions.push(...fieldAttributes)

if (!(currentLine.includes('Int') || currentLine.includes('String'))) {
const fieldType = getFieldType(currentLine)
if (!fieldType) {
return
}

const modelOrEnum = getModelOrTypeOrEnumBlock(fieldType, lines)
const isAtIdAllowed = fieldType === 'Int' || fieldType === 'String' || modelOrEnum?.type === 'enum'
if (!isAtIdAllowed) {
// id not allowed
suggestions = suggestions.filter((sugg) => sugg.label !== '@id')
}
if (!currentLine.includes('DateTime')) {

const isUpdatedAtAllowed = fieldType === 'DateTime'
if (!isUpdatedAtAllowed) {
// updatedAt not allowed
suggestions = suggestions.filter((sugg) => sugg.label !== '@updatedAt')
}
Expand Down

0 comments on commit bc7ce0a

Please sign in to comment.