Skip to content

Commit

Permalink
feat: update formatter to handle literal types
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Jul 11, 2023
1 parent 4a78c22 commit 6ae6923
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
31 changes: 28 additions & 3 deletions DSL/src/language-server/formatting/safe-ds-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AbstractFormatter, AstNode, CstNode, findCommentNode, Formatting, isAstNode } from 'langium';
import * as ast from '../generated/ast';
import { SdsImport, SdsImportAlias, SdsModule } from '../generated/ast';
import { annotationCallsOrEmpty, typeArgumentsOrEmpty } from '../helpers/astShortcuts';
import { annotationCallsOrEmpty, literalsOrEmpty, typeArgumentsOrEmpty } from '../helpers/astShortcuts';
import { FormattingAction, FormattingActionOptions } from 'langium/src/lsp/formatter';
import noSpace = Formatting.noSpace;
import newLine = Formatting.newLine;
Expand Down Expand Up @@ -149,6 +149,10 @@ export class SafeDSFormatter extends AbstractFormatter {
this.formatSdsMemberType(node);
} else if (ast.isSdsCallableType(node)) {
this.formatSdsCallableType(node);
} else if (ast.isSdsLiteralType(node)) {
this.formatSdsLiteralType(node);
} else if (ast.isSdsLiteralList(node)) {
this.formatSdsLiteralList(node);
} else if (ast.isSdsNamedType(node)) {
this.formatSdsNamedType(node);
} else if (ast.isSdsUnionType(node)) {
Expand Down Expand Up @@ -761,6 +765,25 @@ export class SafeDSFormatter extends AbstractFormatter {
formatter.keyword('->').surround(oneSpace());
}

private formatSdsLiteralType(node: ast.SdsLiteralType): void {
const formatter = this.getNodeFormatter(node);

formatter.keyword('literal').append(noSpace());
}

private formatSdsLiteralList(node: ast.SdsLiteralList): void {
const formatter = this.getNodeFormatter(node);
const literals = node.literals ?? [];

if (literals.length > 0) {
formatter.node(literals[0]).prepend(noSpace());
formatter.nodes(...literals.slice(1)).prepend(oneSpace());
}

formatter.keywords(',').prepend(noSpace());
formatter.keyword('>').prepend(noSpace());
}

private formatSdsNamedType(node: ast.SdsNamedType) {
const formatter = this.getNodeFormatter(node);

Expand Down Expand Up @@ -847,10 +870,12 @@ export class SafeDSFormatter extends AbstractFormatter {

if (ast.isSdsCallableType(node) || ast.isSdsMemberType(node)) {
return true;
} else if (ast.isSdsUnionType(node)) {
return typeArgumentsOrEmpty(node.typeArgumentList).length > 0;
} else if (ast.isSdsLiteralType(node)) {
return literalsOrEmpty(node).length > 0;
} else if (ast.isSdsNamedType(node)) {
return typeArgumentsOrEmpty(node.typeArgumentList).length > 0;
} else if (ast.isSdsUnionType(node)) {
return typeArgumentsOrEmpty(node.typeArgumentList).length > 0;
} else {
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions DSL/src/language-server/helpers/astShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
isSdsDeclaration,
SdsAnnotatedObject,
SdsAnnotationCall,
SdsClass,
SdsObject,
SdsLiteral,
SdsLiteralType,
SdsTypeArgument,
SdsTypeArgumentList,
} from '../generated/ast';
Expand All @@ -17,8 +17,8 @@ export const annotationCallsOrEmpty = function (node: SdsAnnotatedObject): SdsAn
}
};

export const classMembersOrEmpty = function (node: SdsClass): SdsObject[] {
return node.body?.members ?? [];
export const literalsOrEmpty = function (node: SdsLiteralType | undefined): SdsLiteral[] {
return node?.literalList?.literals ?? [];
};

export const typeArgumentsOrEmpty = function (node: SdsTypeArgumentList | undefined): SdsTypeArgument[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ segment mySegment(
// -----------------------------------------------------------------------------

segment mySegment(
x: union<>.InnerClass
x: literal<>.InnerClass
) {}

0 comments on commit 6ae6923

Please sign in to comment.