|
| 1 | +import { DataModel, DataModelField, isDataModel } from '@zenstackhq/language/ast'; |
| 2 | +import { |
| 3 | + AstReflection, |
| 4 | + CodeActionProvider, |
| 5 | + findDeclarationNodeAtOffset, |
| 6 | + getContainerOfType, |
| 7 | + IndexManager, |
| 8 | + LangiumDocument, |
| 9 | + LangiumServices, |
| 10 | + MaybePromise, |
| 11 | +} from 'langium'; |
| 12 | + |
| 13 | +import { CodeAction, CodeActionKind, CodeActionParams, Command, Diagnostic } from 'vscode-languageserver'; |
| 14 | +import { IssueCodes } from './constants'; |
| 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 | + |
| 22 | + constructor(services: LangiumServices) { |
| 23 | + this.reflection = services.shared.AstReflection; |
| 24 | + this.indexManager = services.shared.workspace.IndexManager; |
| 25 | + this.formatter = services.lsp.Formatter as ZModelFormatter; |
| 26 | + } |
| 27 | + |
| 28 | + getCodeActions( |
| 29 | + document: LangiumDocument, |
| 30 | + params: CodeActionParams |
| 31 | + ): MaybePromise<Array<Command | CodeAction> | undefined> { |
| 32 | + const result: CodeAction[] = []; |
| 33 | + const acceptor = (ca: CodeAction | undefined) => ca && result.push(ca); |
| 34 | + for (const diagnostic of params.context.diagnostics) { |
| 35 | + this.createCodeActions(diagnostic, document, acceptor); |
| 36 | + } |
| 37 | + return result; |
| 38 | + } |
| 39 | + |
| 40 | + private createCodeActions( |
| 41 | + diagnostic: Diagnostic, |
| 42 | + document: LangiumDocument, |
| 43 | + accept: (ca: CodeAction | undefined) => void |
| 44 | + ) { |
| 45 | + switch (diagnostic.code) { |
| 46 | + case IssueCodes.MissingOppositeRelation: |
| 47 | + accept(this.fixMissingOppositeRelation(diagnostic, document)); |
| 48 | + } |
| 49 | + |
| 50 | + return undefined; |
| 51 | + } |
| 52 | + |
| 53 | + private fixMissingOppositeRelation(diagnostic: Diagnostic, document: LangiumDocument): CodeAction | undefined { |
| 54 | + const offset = document.textDocument.offsetAt(diagnostic.range.start); |
| 55 | + const rootCst = document.parseResult.value.$cstNode; |
| 56 | + |
| 57 | + if (rootCst) { |
| 58 | + const cstNode = findDeclarationNodeAtOffset(rootCst, offset); |
| 59 | + |
| 60 | + const astNode = cstNode?.element as DataModelField; |
| 61 | + |
| 62 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 63 | + const oppositeModel = astNode.type.reference!.ref! as DataModel; |
| 64 | + |
| 65 | + const lastField = oppositeModel.fields[oppositeModel.fields.length - 1]; |
| 66 | + |
| 67 | + const container = getContainerOfType(cstNode?.element, isDataModel) as DataModel; |
| 68 | + |
| 69 | + const idField = container.fields.find((f) => |
| 70 | + f.attributes.find((attr) => attr.decl.ref?.name === '@id') |
| 71 | + ) as DataModelField; |
| 72 | + |
| 73 | + if (container && container.$cstNode && idField) { |
| 74 | + // indent |
| 75 | + let indent = '\t'; |
| 76 | + const formatOptions = this.formatter.getFormatOptions(); |
| 77 | + if (formatOptions?.insertSpaces) { |
| 78 | + indent = ' '.repeat(formatOptions.tabSize); |
| 79 | + } |
| 80 | + indent = indent.repeat(this.formatter.getIndent()); |
| 81 | + |
| 82 | + const typeName = container.name; |
| 83 | + const fieldName = this.lowerCaseFirstLetter(typeName); |
| 84 | + |
| 85 | + // might already exist |
| 86 | + let referenceField = ''; |
| 87 | + |
| 88 | + const idFieldName = idField.name; |
| 89 | + const referenceIdFieldName = fieldName + this.upperCaseFirstLetter(idFieldName); |
| 90 | + |
| 91 | + if (!oppositeModel.fields.find((f) => f.name === referenceIdFieldName)) { |
| 92 | + referenceField = '\n' + indent + `${referenceIdFieldName} ${idField.type.type}`; |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + title: `Add opposite relation fields on ${oppositeModel.name}`, |
| 97 | + kind: CodeActionKind.QuickFix, |
| 98 | + diagnostics: [diagnostic], |
| 99 | + isPreferred: false, |
| 100 | + edit: { |
| 101 | + changes: { |
| 102 | + [document.textDocument.uri]: [ |
| 103 | + { |
| 104 | + range: { |
| 105 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 106 | + start: lastField.$cstNode!.range.end, |
| 107 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 108 | + end: lastField.$cstNode!.range.end, |
| 109 | + }, |
| 110 | + newText: |
| 111 | + '\n' + |
| 112 | + indent + |
| 113 | + `${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` + |
| 114 | + referenceField, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + }, |
| 119 | + }; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return undefined; |
| 124 | + } |
| 125 | + |
| 126 | + private lowerCaseFirstLetter(str: string) { |
| 127 | + return str.charAt(0).toLowerCase() + str.slice(1); |
| 128 | + } |
| 129 | + |
| 130 | + private upperCaseFirstLetter(str: string) { |
| 131 | + return str.charAt(0).toUpperCase() + str.slice(1); |
| 132 | + } |
| 133 | +} |
0 commit comments