Skip to content

Commit 3afe42f

Browse files
authored
fix: VsCode error textDocument/codeAction failed (#915)
1 parent e4aeee3 commit 3afe42f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

packages/schema/src/language-server/zmodel-code-action.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
7575
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7676
const oppositeModel = fieldAstNode.type.reference!.ref! as DataModel;
7777

78-
const lastField = oppositeModel.fields[oppositeModel.fields.length - 1];
79-
8078
const currentModel = document.parseResult.value as Model;
8179

8280
const container = currentModel.declarations.find(
@@ -121,17 +119,22 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
121119
'\n' +
122120
indent +
123121
`${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` +
124-
referenceField;
122+
referenceField +
123+
'\n';
125124
} else {
126125
// user User @relation(fields: [userAbc], references: [id])
127126
const typeName = container.name;
128127
const fieldName = this.lowerCaseFirstLetter(typeName);
129-
newText = '\n' + indent + `${fieldName} ${typeName}[]`;
128+
newText = '\n' + indent + `${fieldName} ${typeName}[]` + '\n';
130129
}
131130

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

134+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
135+
const endOffset = oppositeModel.$cstNode!.end - 1;
136+
const position = document.textDocument.positionAt(endOffset);
137+
135138
return {
136139
title: `Add opposite relation fields on ${oppositeModel.name}`,
137140
kind: CodeActionKind.QuickFix,
@@ -142,10 +145,8 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
142145
[targetDocument.textDocument.uri]: [
143146
{
144147
range: {
145-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
146-
start: lastField.$cstNode!.range.end,
147-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
148-
end: lastField.$cstNode!.range.end,
148+
start: position,
149+
end: position,
149150
},
150151
newText,
151152
},

0 commit comments

Comments
 (0)