Skip to content

Commit

Permalink
fix:Support code action of generating relation fields for both sides (z…
Browse files Browse the repository at this point in the history
  • Loading branch information
jiashengguo authored Mar 21, 2023
1 parent c294a3a commit be0a88d
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions packages/schema/src/language-server/zmodel-code-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ export class ZModelCodeActionProvider implements CodeActionProvider {

const container = getContainerOfType(cstNode?.element, isDataModel) as DataModel;

const idField = container.fields.find((f) =>
f.attributes.find((attr) => attr.decl.ref?.name === '@id')
) as DataModelField;

if (container && container.$cstNode && idField) {
if (container && container.$cstNode) {
// indent
let indent = '\t';
const formatOptions = this.formatter.getFormatOptions();
Expand All @@ -79,17 +75,41 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
}
indent = indent.repeat(this.formatter.getIndent());

const typeName = container.name;
const fieldName = this.lowerCaseFirstLetter(typeName);

// might already exist
let referenceField = '';

const idFieldName = idField.name;
const referenceIdFieldName = fieldName + this.upperCaseFirstLetter(idFieldName);

if (!oppositeModel.fields.find((f) => f.name === referenceIdFieldName)) {
referenceField = '\n' + indent + `${referenceIdFieldName} ${idField.type.type}`;
let newText = '';
if (astNode.type.array) {
//post Post[]
const idField = container.fields.find((f) =>
f.attributes.find((attr) => attr.decl.ref?.name === '@id')
) as DataModelField;

// if no id field, we can't generate reference
if (!idField) {
return undefined;
}

const typeName = container.name;
const fieldName = this.lowerCaseFirstLetter(typeName);

// might already exist
let referenceField = '';

const idFieldName = idField.name;
const referenceIdFieldName = fieldName + this.upperCaseFirstLetter(idFieldName);

if (!oppositeModel.fields.find((f) => f.name === referenceIdFieldName)) {
referenceField = '\n' + indent + `${referenceIdFieldName} ${idField.type.type}`;
}

newText =
'\n' +
indent +
`${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` +
referenceField;
} else {
// user User @relation(fields: [userAbc], references: [id])
const typeName = container.name;
const fieldName = this.lowerCaseFirstLetter(typeName);
newText = '\n' + indent + `${fieldName} ${typeName}[]`;
}

return {
Expand All @@ -107,11 +127,7 @@ export class ZModelCodeActionProvider implements CodeActionProvider {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
end: lastField.$cstNode!.range.end,
},
newText:
'\n' +
indent +
`${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` +
referenceField,
newText,
},
],
},
Expand Down

0 comments on commit be0a88d

Please sign in to comment.