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
Conversation
const firstWordBeforePosition = | ||
wordsBeforePosition[wordsBeforePosition.length - 1] | ||
const secondWordBeforePosition = | ||
wordsBeforePosition[wordsBeforePosition.length - 2] | ||
const wordBeforePosition = | ||
firstWordBeforePosition === '' | ||
? secondWordBeforePosition | ||
: firstWordBeforePosition |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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+/)
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, []) | ||
}) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
// 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, | ||
// } | ||
// } |
There was a problem hiding this comment.
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
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)