Skip to content
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

initial formatter schema #12

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
215 changes: 202 additions & 13 deletions src/language-server/universal-data-definition-language-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { AbstractFormatter, AstNode, Formatting, Module, PartialLangiumServices } from 'langium';
import { AbstractFormatter, AstNode, Formatting, Module, PartialLangiumServices, } from 'langium';
import * as ast from './generated/ast';
import { UniversalDataDefinitionLanguageServices } from './universal-data-definition-language-module';

export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter {

protected formatContainer(node: AstNode): void {
const formatter = this.getNodeFormatter(node);
const open = formatter.keyword('{');
Expand All @@ -22,11 +21,57 @@ export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter
close.surround(Formatting.noSpace()).prepend(Formatting.newLine({allowMore: true})).append(Formatting.newLine({allowMore: true}));
}

protected format(node: AstNode): void {
protected formatNode(node: AstNode): void {
const formatter = this.getNodeFormatter(node);
formatter.property('name').prepend(Formatting.newLine()).surround(Formatting.oneSpace({allowMore: true}))
}

protected format(node: AstNode): void {
// This method is called for every AstNode in a document
if (ast.isDataModel(node)) {
this.formatDataModel(node);

}
else if(ast.isConceptualAssociation(node)){
this.formatConceptualAssociation(node)
}
else if(ast.isConceptualEntity(node)){
this.formatConceptualEntity(node)
}
else if(ast.isLogicalEnumerated(node)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose the ordering of if-else if ... here? Make sure to document it. You don't want someone else to come along after you and make some change to the ordering that causes problems.

this.formatLogicalEnumerated(node)
}
else if(ast.isLogicalMeasurement(node)){
this.formatLogicalMeasurement(node)
}
else if(ast.isLogicalMeasurementSystemAxis(node)){
this.formatLogicalMeasurementSystemAxis(node)
}
else if(ast.isLogicalValueTypeUnit(node)){
this.formatLogicalValueTypeUnit(node)
}
else if(ast.isLogicalMeasurement(node)){
this.formatLogicalMeasurement(node)
}
else if(ast.isLogicalAssociation(node)){
this.formatLogicalAssociation(node)
}
else if(ast.isLogicalEntity(node)){
this.formatLogicalEntity(node)
}
else if(ast.isLogicalParticipantPathNode(node)){
this.formatLogicalParticipantPathNode(node)
}
else if(ast.isPlatformAssociation(node)){
this.formatPlatformAssociation(node);
}
else if(ast.isPlatformEntity(node)){
this.formatPlatformEntity(node)
}
else if(ast.isPlatformStruct(node)){
this.formatPlatformStruct(node)
}
else if(ast.isPlatformCompositeQuery(node)){
this.formatPlatformCompositeQuery(node);
}
}

Expand All @@ -50,10 +95,10 @@ export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter

protected formatConceptualDataModel(cdm: ast.ConceptualDataModel): void {
this.formatContainer(cdm);
cdm.cdm.forEach(dm => {
cdm.cdm.forEach(dm => {
this.formatConceptualDataModel(dm);
})
cdm.element.forEach( elem => {
cdm.element.forEach( elem => {
this.formatConceptualElement(elem);
})
}
Expand All @@ -64,7 +109,7 @@ export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter
this.formatLogicalDataModel(dm)
})
ldm.element.forEach( elem => {
this.formatElement(elem)
this.formatLogicalElement(elem)
})
}

Expand All @@ -74,21 +119,166 @@ export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter
this.formatPlatfornDataModel(dm)
})
pdm.element.forEach(elem => {
this.formatElement(elem)
this.formatPlatformElement(elem)
})
}

protected formatConceptualElement(elem: ast.ConceptualElement): void {
/**Formatting conceptuals */
protected formatConceptualElement(elem: ast.ConceptualElement): void {
const formatter = this.getNodeFormatter(elem);
formatter.property('name').prepend(Formatting.newLine()).surround(Formatting.oneSpace({allowMore: true}));
formatter.property('name').prepend(Formatting.newLine({allowMore: true})).surround(Formatting.oneSpace({allowMore: true}));
this.formatObj(elem);
}

protected formatElement(elem: ast.LogicalElement | ast.PlatformElement): void {
protected formatConceptualEntity(entity: ast.ConceptualEntity): void {
this.formatContainer(entity)
entity.composition.forEach(comp => {
this.formatConceptualComposition(comp)
})
}

protected formatConceptualAssociation(cassoc: ast.ConceptualAssociation): void {
this.formatContainer(cassoc)
cassoc.composition.forEach(comp => {
this.formatConceptualComposition(comp)
})
}

protected formatConceptualComposition(comp: ast.ConceptualComposition ): void {
this.formatContainer(comp)
}

protected formatConceptualParticipant(participant: ast.ConceptualParticipant): void {
this.formatContainer(participant)
if(participant?.type){
this.formatConceptualEntity(participant.type.ref!)
}
}

/**Formatting logicals */
protected formatLogicalElement(elem: ast.LogicalElement): void {
const formatter = this.getNodeFormatter(elem);
formatter.property('name').prepend(Formatting.newLine()).surround(Formatting.oneSpace({allowMore: true}));
this.formatObj(elem);
}

protected formatLogicalEnumerated(lenum: ast.LogicalEnumerated): void {
this.formatContainer(lenum)
lenum.label.forEach(label => {
this.formatContainer(label)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you calling formatContainer on label? It's not a container.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The label is an array of LogicalEnumeratedSet | LogicalEnumerationLabel which are both containers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a LogicalEnumeratedSet, which is a container, or a LogicalEnumerationLabel, which is not a container. In these circumstances, remember that the top level format method will figure out the type of label - so all you need to do is call format, which will then call the appropriate formatter for either LogicalEnumerationLabel or LogicalEnumeratedSet. Your current approach will not handle multiple levels of nesting appropriately.

})
}

protected formatLogicalMeasurementSystem(sys: ast.LogicalMeasurementSystem): void {
this.formatContainer(sys);
sys.constraint.forEach(sysConst => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

measurementSystemAxis is also a collection.

this.formatContainer(sysConst)
})
sys.referencePoint.forEach(ref => {
this.formatLogicalReferencePoint(ref);
})
}

protected formatLogicalMeasurementSystemAxis(sysAxis: ast.LogicalMeasurementSystemAxis): void {
this.formatContainer(sysAxis);
sysAxis.constraint.forEach(sysConst => {
this.formatContainer(sysConst);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Individual constraints are not containers.

})
}

protected formatLogicalReferencePoint(refPoint: ast.LogicalReferencePoint): void {
this.formatContainer(refPoint);
refPoint.referencePointPart.forEach(pointPart => {
this.formatContainer(pointPart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An individual ReferencePointPart is not a container

})
}

protected formatLogicalValueTypeUnit(typeUnit: ast.LogicalValueTypeUnit):void {
this.formatContainer(typeUnit);
if(typeUnit.constraint){
this.formatContainer(typeUnit);
}
}

protected formatLogicalMeasurement(measure: ast.LogicalMeasurement): void {
this.formatContainer(measure);
measure.attribute.forEach(attr => {
this.formatContainer(attr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Individual attributes are not containers

})
measure.constraint.forEach(mesConst => {
this.formatContainer(mesConst)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

individual constraints are not containers

})
}

protected formatLogicalEntity(entity: ast.LogicalEntity): void {
this.formatContainer(entity);
entity.composition.forEach(comp => {
this.formatContainer(comp);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Individual composition elements are not containers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up, will make the changes and revert. I will get back if I am stuck.

})
}

protected formatLogicalAssociation(asso: ast.LogicalAssociation): void {
this.formatContainer(asso);
asso.composition.forEach(comp => {
this.formatContainer(comp);
})
asso.participant.forEach(part => {
this.formatLogicalParticipant(part);
})
}

protected formatLogicalParticipant(part: ast.LogicalParticipant): void {
this.formatContainer(part)
}

protected formatLogicalParticipantPathNode(pathBode: ast.LogicalParticipantPathNode): void {
this.formatContainer(pathBode)
}

protected formatLogicalCompositeQuery(query: ast.LogicalCompositeQuery): void {
this.formatContainer(query)
query.composition.forEach(comp => {
this.formatContainer(query)
})
}

protected formatPlatformElement(elem: ast.PlatformElement): void {
const formatter = this.getNodeFormatter(elem);
formatter.property('name').prepend(Formatting.newLine()).surround(Formatting.oneSpace({allowMore: true}));
this.formatObj(elem);
}

protected formatPlatformEntity(entity: ast.PlatformEntity): void {
this.formatContainer(entity)
entity.composition.forEach(comp => {
this.formatContainer(comp)
})
}

protected formatPlatformStruct(str: ast.PlatformStruct): void {
this.formatContainer(str)
str.member.forEach(mem => {
this.formatContainer(mem)
})
}

protected formatPlatformAssociation(asso: ast.PlatformAssociation): void {
this.formatContainer(asso)
asso.participant.forEach(part => {
this.formatPlatformParticipant(part);
})
}

protected formatPlatformParticipant(part: ast.PlatformParticipant): void {
this.formatContainer(part)
}

protected formatPlatformCompositeQuery(query: ast.PlatformCompositeQuery): void {
this.formatContainer(query)
query.composition.forEach(comp => {
this.formatContainer(comp);
})
}
}


Expand All @@ -97,5 +287,4 @@ export const UniversalDataDefinitionLanguageModule: Module<UniversalDataDefiniti
lsp: {
Formatter: () => new UniversalDataDefinitionLanguageFormatter()
}
};

};
Loading