-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix(lsp): get language server on par with v2 #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export { loadDocument } from './document'; | ||
| export * from './module'; | ||
| export { ZModelCodeGenerator } from './zmodel-code-generator'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { | ||
| type AstReflection, | ||
| type IndexManager, | ||
| type LangiumDocument, | ||
| type LangiumDocuments, | ||
| type MaybePromise, | ||
| } from 'langium'; | ||
| import { DataField, DataModel, Model, isDataModel } from './ast'; | ||
|
|
||
| import type { CodeActionProvider, LangiumServices } from 'langium/lsp'; | ||
| import { CodeAction, CodeActionKind, type CodeActionParams, Command, Diagnostic } from 'vscode-languageserver'; | ||
| import { IssueCodes } from './constants'; | ||
| import { getAllFields, getDocument } from './utils'; | ||
| import type { MissingOppositeRelationData } from './validators/datamodel-validator'; | ||
| import { ZModelFormatter } from './zmodel-formatter'; | ||
|
|
||
| export class ZModelCodeActionProvider implements CodeActionProvider { | ||
| protected readonly reflection: AstReflection; | ||
| protected readonly indexManager: IndexManager; | ||
| protected readonly formatter: ZModelFormatter; | ||
| protected readonly documents: LangiumDocuments; | ||
|
|
||
| constructor(services: LangiumServices) { | ||
| this.reflection = services.shared.AstReflection; | ||
| this.indexManager = services.shared.workspace.IndexManager; | ||
| this.formatter = services.lsp.Formatter as ZModelFormatter; | ||
| this.documents = services.shared.workspace.LangiumDocuments; | ||
| } | ||
|
|
||
| getCodeActions( | ||
| document: LangiumDocument, | ||
| params: CodeActionParams, | ||
| ): MaybePromise<Array<Command | CodeAction> | undefined> { | ||
| const result: CodeAction[] = []; | ||
| const acceptor = (ca: CodeAction | undefined) => ca && result.push(ca); | ||
| for (const diagnostic of params.context.diagnostics) { | ||
| this.createCodeActions(diagnostic, document, acceptor); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private createCodeActions( | ||
| diagnostic: Diagnostic, | ||
| document: LangiumDocument, | ||
| accept: (ca: CodeAction | undefined) => void, | ||
| ) { | ||
| switch (diagnostic.code) { | ||
| case IssueCodes.MissingOppositeRelation: | ||
| accept(this.fixMissingOppositeRelation(diagnostic, document)); | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| private fixMissingOppositeRelation(diagnostic: Diagnostic, document: LangiumDocument): CodeAction | undefined { | ||
| const data = diagnostic.data as MissingOppositeRelationData; | ||
|
|
||
| const rootCst = | ||
| data.relationFieldDocUri == document.textDocument.uri | ||
| ? document.parseResult.value | ||
| : this.documents.all.find((doc) => doc.textDocument.uri === data.relationFieldDocUri)?.parseResult | ||
| .value; | ||
|
|
||
| if (rootCst) { | ||
| const fieldModel = rootCst as Model; | ||
| const fieldAstNode = ( | ||
| fieldModel.declarations.find( | ||
| (x) => isDataModel(x) && x.name === data.relationDataModelName, | ||
| ) as DataModel | ||
| )?.fields.find((x) => x.name === data.relationFieldName) as DataField; | ||
|
|
||
| if (!fieldAstNode) return undefined; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
| const oppositeModel = fieldAstNode.type.reference!.ref! as DataModel; | ||
|
|
||
| const currentModel = document.parseResult.value as Model; | ||
|
|
||
| const container = currentModel.declarations.find( | ||
| (decl) => decl.name === data.dataModelName && isDataModel(decl), | ||
| ) as DataModel; | ||
|
|
||
| if (container && container.$cstNode) { | ||
| // indent | ||
| let indent = '\t'; | ||
| const formatOptions = this.formatter.getFormatOptions(); | ||
| if (formatOptions?.insertSpaces) { | ||
| indent = ' '.repeat(formatOptions.tabSize); | ||
| } | ||
| indent = indent.repeat(this.formatter.getIndent()); | ||
|
|
||
| let newText = ''; | ||
| if (fieldAstNode.type.array) { | ||
| // post Post[] | ||
| const idField = getAllFields(container).find((f) => | ||
| f.attributes.find((attr) => attr.decl.ref?.name === '@id'), | ||
| ); | ||
|
|
||
| // 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 (!getAllFields(oppositeModel).find((f) => f.name === referenceIdFieldName)) { | ||
| referenceField = '\n' + indent + `${referenceIdFieldName} ${idField.type.type}`; | ||
| } | ||
|
|
||
| newText = | ||
| '\n' + | ||
| indent + | ||
| `${fieldName} ${typeName} @relation(fields: [${referenceIdFieldName}], references: [${idFieldName}])` + | ||
| referenceField + | ||
| '\n'; | ||
| } else { | ||
| // user User @relation(fields: [userAbc], references: [id]) | ||
| const typeName = container.name; | ||
| const fieldName = this.lowerCaseFirstLetter(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 = targetDocument.textDocument.positionAt(endOffset); | ||
|
|
||
| return { | ||
| title: `Add opposite relation fields on ${oppositeModel.name}`, | ||
| kind: CodeActionKind.QuickFix, | ||
| diagnostics: [diagnostic], | ||
| isPreferred: false, | ||
| edit: { | ||
| changes: { | ||
| [targetDocument.textDocument.uri]: [ | ||
| { | ||
| range: { | ||
| start: position, | ||
| end: position, | ||
| }, | ||
| newText, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| private lowerCaseFirstLetter(str: string) { | ||
| return str.charAt(0).toLowerCase() + str.slice(1); | ||
| } | ||
|
|
||
| private upperCaseFirstLetter(str: string) { | ||
| return str.charAt(0).toUpperCase() + str.slice(1); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { DefaultCommentProvider, type AstNode } from 'langium'; | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { match } from 'ts-pattern'; | ||
| import { isDataField, isDataModel, isEnum, isEnumField, isFunctionDecl, isTypeDef } from './ast'; | ||
|
|
||
| export class ZModelCommentProvider extends DefaultCommentProvider { | ||
| override getComment(node: AstNode): string | undefined { | ||
| let comment = super.getComment(node); | ||
| if (!comment) { | ||
| // default comment | ||
| comment = match(node) | ||
| .when(isDataModel, (d) => `/**\n * Model *${d.name}*\n */`) | ||
| .when(isTypeDef, (d) => `/**\n * Type *${d.name}*\n */`) | ||
| .when(isEnum, (e) => `/**\n * Enum *${e.name}*\n */`) | ||
| .when(isEnumField, (f) => `/**\n * Value of enum *${f.$container?.name}*\n */`) | ||
| .when(isDataField, (f) => `/**\n * Field of *${f.$container?.name}*\n */`) | ||
| .when(isFunctionDecl, (f) => `/**\n * Function *${f.name}*\n */`) | ||
| .otherwise(() => ''); | ||
| } | ||
| return comment; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.