Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/schema/src/language-server/zmodel-code-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const oppositeModel = fieldAstNode.type.reference!.ref! as DataModel;

const lastField = oppositeModel.fields[oppositeModel.fields.length - 1];

const currentModel = document.parseResult.value as Model;

const container = currentModel.declarations.find(
Expand Down Expand Up @@ -121,17 +119,22 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
'\n' +
indent +
`${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` +
referenceField;
referenceField +
'\n';
} else {
// user User @relation(fields: [userAbc], references: [id])
const typeName = container.name;
const fieldName = this.lowerCaseFirstLetter(typeName);
newText = '\n' + indent + `${fieldName} ${typeName}[]`;
newText = '\n' + indent + `${fieldName} ${typeName}[]` + '\n';
}

// the opposite model might be in the imported file
const targetDocument = getDocument(oppositeModel);

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const endOffset = oppositeModel.$cstNode!.end - 1;
const position = document.textDocument.positionAt(endOffset);

return {
title: `Add opposite relation fields on ${oppositeModel.name}`,
kind: CodeActionKind.QuickFix,
Expand All @@ -142,10 +145,8 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
[targetDocument.textDocument.uri]: [
{
range: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
start: lastField.$cstNode!.range.end,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
end: lastField.$cstNode!.range.end,
start: position,
end: position,
},
newText,
},
Expand Down