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: completion for referentialActions (Cascade,...) #847

Merged
merged 5 commits into from Aug 23, 2021

Conversation

Jolg42
Copy link
Member

@Jolg42 Jolg42 commented Aug 20, 2021

Closes #818

Adds reliable hardcoded auto-completion + tests for referential actions Cascade, Restrict, NoAction, SetNull, SetDefault

Issue: Restrict will be suggested for SQL server since it's hardcoded.

Hardcoding was the only option right now because prisma-format referential-actions subcommand returns an empty array [] most of the time (probably because it considers the schema invalid)

Comment on lines +882 to +889
const firstWordBeforePosition =
wordsBeforePosition[wordsBeforePosition.length - 1]
const secondWordBeforePosition =
wordsBeforePosition[wordsBeforePosition.length - 2]
const wordBeforePosition =
firstWordBeforePosition === ''
? secondWordBeforePosition
: firstWordBeforePosition
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Why can firstWordBeforePosition be an empty string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because onDelete: | if the cursor is here there first word is empty, which is what happens most of the time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that our logic that splits for the words or something built in? Feels super weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's custom code

const wordsBeforePosition: string[] = currentLineTillPosition.split(/\s+/)

Comment on lines +157 to +191
test('no datasource should return empty []', async () => {
const binPath = await util.getBinPath()

const schema = `
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialActions"]
}`

const actions = referentialActions(binPath, schema)

assert.deepStrictEqual(actions, [])
})

test('invalid schema should return empty []', async () => {
const binPath = await util.getBinPath()

const schema = `
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialActions"]
}

model { sss } // invalid model
`

const actions = referentialActions(binPath, schema)

assert.deepStrictEqual(actions, [])
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it really return an empty array or is that just what it does right now? I would probably expect it to return all options by default so the user can do _something - and when they ad more information to their schema (or fix it), the suggestions would be improved (and old autocompleted items fixed via validation).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It always returns an array either empty or not right now. It's not optimal.

Comment on lines +921 to +938
// Doesn't really work because prisma-fmt returns nothing when the schema is "invalid"
// but that also means that the schema is considered invalid when trying to autocomplete...
//
// if lastWord = onUpdate or onDelete
// then get suggestions by passing `referential-actions` arg to `prisma-fmt`
// if (definingReferentialAction(wordsBeforePosition)) {
// const suggestionsForReferentialActions: CompletionItem[] = referentialActions(
// binPath,
// document.getText(),
// ).map((action) => {
// return CompletionItem.create(action)
// })

// return {
// items: suggestionsForReferentialActions,
// isIncomplete: false,
// }
// }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this around because we could reuse after changing prisma-fmt behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

referentialActions: No auto complete for available actions
2 participants