Skip to content

Commit

Permalink
fix: VsCode error textDocument/codeAction failed (zenstackhq#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashengguo authored Jan 1, 2024
1 parent e4aeee3 commit 3afe42f
Showing 1 changed file with 9 additions and 8 deletions.
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

0 comments on commit 3afe42f

Please sign in to comment.