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
26 changes: 16 additions & 10 deletions src/language-server/universal-data-definition-language-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,29 @@ export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter
close.surround(Formatting.noSpace()).prepend(Formatting.newLine({allowMore: true})).append(Formatting.newLine({allowMore: true}));
}

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);
}
if(ast.isConceptualAssociation(node)){
this.formatConceptualAssociation(node)
}
// test passes with this commetned check
Copy link
Contributor

Choose a reason for hiding this comment

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

The test you have defined does not include a ConceptualAssociation, so commenting this method out will have no effect on test success/ failure.

// if(ast.isConceptualAssociation(node)){
// this.formatConceptualAssociation(node)
// }

if(ast.isConceptualEntity(node)){
this.formatConceptualEntity(node)
}

if(ast.isConceptualBasisEntity(node)){
this.formatConceptualBasisEntity(node)
}

// test passes with this commetned check
// if(ast.isConceptualBasisEntity(node)){
Copy link
Contributor

Choose a reason for hiding this comment

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

The test you have defined only has 1 ConceptualBasisEntity. The formatting you see around it is caused by 2 things: 1) the surrounding formatting is done by formatObj for the containing CDM; 2) A ConceptualBasisEntity is also a ConceptualElement. So when you comment out this method, the formatConceptualElement will still be called.

if you want to make sure you know the effect of a formatting method, you should have multiple instances of that type in your test. In this case, it means including multiple ConceptualBasisEntity instances. At a minimum, have 3. It may also make sense to have instances of different types intermingled where possible just to check on the effect of formatting one type on adjacent instances of a different type.

This brings up an important point: You can't just have if checks in the top level format(AstNode) method. If you do that, then every if is checked for each node and every if check that returns true will result in a format method being called - in the order they show up in format(AstNode). If multiple format methods are called for the same AstNode,not only will that be slower than necessary but also only the last one called will matter, because it will overwrite the effects of all format methods previously called for that AstNode.

Instead, you need to make sure to use if ... else if so that only a single formatting method is called for each AstNode. And you need to make sure to put the formatting methods in order from most specific to most general based on the hierarchy described in the .langium file, because you want the first test to pass to be the most specific formatter applicable.

// this.formatConceptualBasisEntity(node)
// }
}


Expand Down Expand Up @@ -102,9 +109,8 @@ export class UniversalDataDefinitionLanguageFormatter extends AbstractFormatter
}

protected formatConceptualBasisEntity(basis: ast.ConceptualBasisEntity): void {
const formatter = this.getNodeFormatter(basis);
formatter.property('name').prepend(Formatting.newLine()).surround(Formatting.oneSpace({allowMore: true}));
}
this.formatNode(basis)
}

protected formatConceptualAssociation(cassoc: ast.ConceptualAssociation): void {
this.formatContainer(cassoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ const universalDataDefinitionLanguageFormatting = expectFormatting(universalData
);

describe("Universal Data Definition Language Formatter", () => {
it("should format cdm ", async () => {
it("should format cdm array with conceptual elemnt", async () => {
await universalDataDefinitionLanguageFormatting({
before:
'dm PPT "Base data structures to support People, Places and Things"{cdm Conceptual "Need to start at Conceptual Level"{basis uddlBasis "Formating conceptual basis entity";}}',
'dm PPT "Base data structures to support People, Places and Things"{cdm Conceptual "Need to start at Conceptual Level"{basis uddlBasis "Formating conceptual basis entity"; domain cdmDomain "Entry for conceptual domain"; cdm anothercdm "A cdm with another data" {observable Information "Something a party can learn";}}}',
after: `dm PPT "Base data structures to support People, Places and Things"
{

cdm Conceptual "Need to start at Conceptual Level"
{

basis uddlBasis "Formating conceptual basis entity";
domain cdmDomain "Entry for conceptual domain";
cdm anothercdm "A cdm with another data"
{

observable Information "Something a party can learn";
}

}

}`
});
});

it("should format concptual element", async () => {
it("should format conceptual element", async () => {
await universalDataDefinitionLanguageFormatting({
before:
'dm PPT "Base data structures to support People, Places and Things"{cdm Conceptual "Need to start at Conceptual Level"{cassoc Training "Information delivered over time from one party to another " {};centity AddressableEntity "Any entity that is addressable in some way" {};}}',
Expand Down