|
| 1 | +import { |
| 2 | + type AstReflection, |
| 3 | + type IndexManager, |
| 4 | + type LangiumDocument, |
| 5 | + type LangiumDocuments, |
| 6 | + type MaybePromise, |
| 7 | +} from 'langium'; |
| 8 | +import { DataField, DataModel, Model, isDataModel } from './ast'; |
| 9 | + |
| 10 | +import type { CodeActionProvider, LangiumServices } from 'langium/lsp'; |
| 11 | +import { CodeAction, CodeActionKind, type CodeActionParams, Command, Diagnostic } from 'vscode-languageserver'; |
| 12 | +import { IssueCodes } from './constants'; |
| 13 | +import { getAllFields, getDocument } from './utils'; |
| 14 | +import type { MissingOppositeRelationData } from './validators/datamodel-validator'; |
| 15 | +import { ZModelFormatter } from './zmodel-formatter'; |
| 16 | + |
| 17 | +export class ZModelCodeActionProvider implements CodeActionProvider { |
| 18 | + protected readonly reflection: AstReflection; |
| 19 | + protected readonly indexManager: IndexManager; |
| 20 | + protected readonly formatter: ZModelFormatter; |
| 21 | + protected readonly documents: LangiumDocuments; |
| 22 | + |
| 23 | + constructor(services: LangiumServices) { |
| 24 | + this.reflection = services.shared.AstReflection; |
| 25 | + this.indexManager = services.shared.workspace.IndexManager; |
| 26 | + this.formatter = services.lsp.Formatter as ZModelFormatter; |
| 27 | + this.documents = services.shared.workspace.LangiumDocuments; |
| 28 | + } |
| 29 | + |
| 30 | + getCodeActions( |
| 31 | + document: LangiumDocument, |
| 32 | + params: CodeActionParams, |
| 33 | + ): MaybePromise<Array<Command | CodeAction> | undefined> { |
| 34 | + const result: CodeAction[] = []; |
| 35 | + const acceptor = (ca: CodeAction | undefined) => ca && result.push(ca); |
| 36 | + for (const diagnostic of params.context.diagnostics) { |
| 37 | + this.createCodeActions(diagnostic, document, acceptor); |
| 38 | + } |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + private createCodeActions( |
| 43 | + diagnostic: Diagnostic, |
| 44 | + document: LangiumDocument, |
| 45 | + accept: (ca: CodeAction | undefined) => void, |
| 46 | + ) { |
| 47 | + switch (diagnostic.code) { |
| 48 | + case IssueCodes.MissingOppositeRelation: |
| 49 | + accept(this.fixMissingOppositeRelation(diagnostic, document)); |
| 50 | + } |
| 51 | + |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + |
| 55 | + private fixMissingOppositeRelation(diagnostic: Diagnostic, document: LangiumDocument): CodeAction | undefined { |
| 56 | + const data = diagnostic.data as MissingOppositeRelationData; |
| 57 | + |
| 58 | + const rootCst = |
| 59 | + data.relationFieldDocUri == document.textDocument.uri |
| 60 | + ? document.parseResult.value |
| 61 | + : this.documents.all.find((doc) => doc.textDocument.uri === data.relationFieldDocUri)?.parseResult |
| 62 | + .value; |
| 63 | + |
| 64 | + if (rootCst) { |
| 65 | + const fieldModel = rootCst as Model; |
| 66 | + const fieldAstNode = ( |
| 67 | + fieldModel.declarations.find( |
| 68 | + (x) => isDataModel(x) && x.name === data.relationDataModelName, |
| 69 | + ) as DataModel |
| 70 | + )?.fields.find((x) => x.name === data.relationFieldName) as DataField; |
| 71 | + |
| 72 | + if (!fieldAstNode) return undefined; |
| 73 | + |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 75 | + const oppositeModel = fieldAstNode.type.reference!.ref! as DataModel; |
| 76 | + |
| 77 | + const currentModel = document.parseResult.value as Model; |
| 78 | + |
| 79 | + const container = currentModel.declarations.find( |
| 80 | + (decl) => decl.name === data.dataModelName && isDataModel(decl), |
| 81 | + ) as DataModel; |
| 82 | + |
| 83 | + if (container && container.$cstNode) { |
| 84 | + // indent |
| 85 | + let indent = '\t'; |
| 86 | + const formatOptions = this.formatter.getFormatOptions(); |
| 87 | + if (formatOptions?.insertSpaces) { |
| 88 | + indent = ' '.repeat(formatOptions.tabSize); |
| 89 | + } |
| 90 | + indent = indent.repeat(this.formatter.getIndent()); |
| 91 | + |
| 92 | + let newText = ''; |
| 93 | + if (fieldAstNode.type.array) { |
| 94 | + // post Post[] |
| 95 | + const idField = getAllFields(container).find((f) => |
| 96 | + f.attributes.find((attr) => attr.decl.ref?.name === '@id'), |
| 97 | + ); |
| 98 | + |
| 99 | + // if no id field, we can't generate reference |
| 100 | + if (!idField) { |
| 101 | + return undefined; |
| 102 | + } |
| 103 | + |
| 104 | + const typeName = container.name; |
| 105 | + const fieldName = this.lowerCaseFirstLetter(typeName); |
| 106 | + |
| 107 | + // might already exist |
| 108 | + let referenceField = ''; |
| 109 | + |
| 110 | + const idFieldName = idField.name; |
| 111 | + const referenceIdFieldName = fieldName + this.upperCaseFirstLetter(idFieldName); |
| 112 | + |
| 113 | + if (!getAllFields(oppositeModel).find((f) => f.name === referenceIdFieldName)) { |
| 114 | + referenceField = '\n' + indent + `${referenceIdFieldName} ${idField.type.type}`; |
| 115 | + } |
| 116 | + |
| 117 | + newText = |
| 118 | + '\n' + |
| 119 | + indent + |
| 120 | + `${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` + |
| 121 | + referenceField + |
| 122 | + '\n'; |
| 123 | + } else { |
| 124 | + // user User @relation(fields: [userAbc], references: [id]) |
| 125 | + const typeName = container.name; |
| 126 | + const fieldName = this.lowerCaseFirstLetter(typeName); |
| 127 | + newText = '\n' + indent + `${fieldName} ${typeName}[]` + '\n'; |
| 128 | + } |
| 129 | + |
| 130 | + // the opposite model might be in the imported file |
| 131 | + const targetDocument = getDocument(oppositeModel); |
| 132 | + |
| 133 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 134 | + const endOffset = oppositeModel.$cstNode!.end - 1; |
| 135 | + const position = targetDocument.textDocument.positionAt(endOffset); |
| 136 | + |
| 137 | + return { |
| 138 | + title: `Add opposite relation fields on ${oppositeModel.name}`, |
| 139 | + kind: CodeActionKind.QuickFix, |
| 140 | + diagnostics: [diagnostic], |
| 141 | + isPreferred: false, |
| 142 | + edit: { |
| 143 | + changes: { |
| 144 | + [targetDocument.textDocument.uri]: [ |
| 145 | + { |
| 146 | + range: { |
| 147 | + start: position, |
| 148 | + end: position, |
| 149 | + }, |
| 150 | + newText, |
| 151 | + }, |
| 152 | + ], |
| 153 | + }, |
| 154 | + }, |
| 155 | + }; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return undefined; |
| 160 | + } |
| 161 | + |
| 162 | + private lowerCaseFirstLetter(str: string) { |
| 163 | + return str.charAt(0).toLowerCase() + str.slice(1); |
| 164 | + } |
| 165 | + |
| 166 | + private upperCaseFirstLetter(str: string) { |
| 167 | + return str.charAt(0).toUpperCase() + str.slice(1); |
| 168 | + } |
| 169 | +} |
0 commit comments