From 2a1f91fd73768a768a4c1f71bebe0799d768c1ab Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Tue, 4 Jul 2023 18:21:50 +0200 Subject: [PATCH] fix: don't pull first module entry into comment --- .../formatting/safe-ds-formatter.ts | 57 +++++++++++++++++-- .../comments/before declarations.sdstest | 8 +++ .../comments/before imports.sdstest | 8 +++ .../before module annotations.sdstest | 8 +++ .../comments/before module name.sdstest | 8 +++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 DSL/tests/resources/formatting/comments/before declarations.sdstest create mode 100644 DSL/tests/resources/formatting/comments/before imports.sdstest create mode 100644 DSL/tests/resources/formatting/comments/before module annotations.sdstest create mode 100644 DSL/tests/resources/formatting/comments/before module name.sdstest diff --git a/DSL/src/language-server/formatting/safe-ds-formatter.ts b/DSL/src/language-server/formatting/safe-ds-formatter.ts index cb28836db..fdbc47b6f 100644 --- a/DSL/src/language-server/formatting/safe-ds-formatter.ts +++ b/DSL/src/language-server/formatting/safe-ds-formatter.ts @@ -1,4 +1,4 @@ -import { AbstractFormatter, AstNode, Formatting } from 'langium'; +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'; @@ -191,7 +191,11 @@ export class SafeDSFormatter extends AbstractFormatter { // Annotations annotations.forEach((value, index) => { if (index === 0) { - formatter.node(value).prepend(noSpace()); + if (this.hasComment(value)) { + formatter.node(value).prepend(newLine()); + } else { + formatter.node(value).prepend(noSpace()); + } } else { formatter.node(value).prepend(newLines(1)); } @@ -199,7 +203,14 @@ export class SafeDSFormatter extends AbstractFormatter { // Package if (annotations.length === 0) { - formatter.keyword('package').prepend(noSpace()); + const packageKeyword = formatter.keyword('package'); + const cstNodes = packageKeyword.nodes + + if (cstNodes.length > 0 && this.hasComment(cstNodes[0])) { + packageKeyword.prepend(newLine()); + } else { + packageKeyword.prepend(noSpace()); + } } else { formatter.keyword('package').prepend(newLines(2)); } @@ -210,7 +221,11 @@ export class SafeDSFormatter extends AbstractFormatter { imports.forEach((value, index) => { if (index === 0) { if (annotations.length === 0 && !name) { - formatter.node(value).prepend(noSpace()); + if (this.hasComment(value)) { + formatter.node(value).prepend(newLine()); + } else { + formatter.node(value).prepend(noSpace()); + } } else { formatter.node(value).prepend(newLines(2)); } @@ -223,7 +238,11 @@ export class SafeDSFormatter extends AbstractFormatter { members.forEach((value, index) => { if (index === 0) { if (annotations.length === 0 && !name && imports.length === 0) { - formatter.node(value).prepend(noSpace()); + if (this.hasComment(value)) { + formatter.node(value).prepend(newLine()); + } else { + formatter.node(value).prepend(noSpace()); + } } else { const valueAnnotations = annotationCallsOrEmpty(value); if (valueAnnotations.length > 0) { @@ -871,4 +890,32 @@ export class SafeDSFormatter extends AbstractFormatter { formatter.keyword(':').prepend(noSpace()).append(oneSpace()); } + + // ----------------------------------------------------------------------------- + // Helpers + // ----------------------------------------------------------------------------- + + /** + * Returns whether the given node has a comment associated with it. + * + * @param node The node to check. + */ + private hasComment(node: AstNode | CstNode | undefined): boolean { + return Boolean(this.getCommentNode(node)); + } + + /** + * Returns the comment associated with the given node. + * + * @param node The node to get the comment for. + */ + private getCommentNode(node: AstNode | CstNode | undefined): CstNode | undefined { + const commentNames = ["ML_COMMENT", "SL_COMMENT"] + + if (isAstNode(node)) { + return findCommentNode(node.$cstNode, commentNames) + } else { + return findCommentNode(node, commentNames) + } + } } diff --git a/DSL/tests/resources/formatting/comments/before declarations.sdstest b/DSL/tests/resources/formatting/comments/before declarations.sdstest new file mode 100644 index 000000000..48f6b4814 --- /dev/null +++ b/DSL/tests/resources/formatting/comments/before declarations.sdstest @@ -0,0 +1,8 @@ +// test + +class C + +// ----------------------------------------------------------------------------- + +// test +class C diff --git a/DSL/tests/resources/formatting/comments/before imports.sdstest b/DSL/tests/resources/formatting/comments/before imports.sdstest new file mode 100644 index 000000000..e3f919432 --- /dev/null +++ b/DSL/tests/resources/formatting/comments/before imports.sdstest @@ -0,0 +1,8 @@ +// test + +import test + +// ----------------------------------------------------------------------------- + +// test +import test diff --git a/DSL/tests/resources/formatting/comments/before module annotations.sdstest b/DSL/tests/resources/formatting/comments/before module annotations.sdstest new file mode 100644 index 000000000..df48ef4d6 --- /dev/null +++ b/DSL/tests/resources/formatting/comments/before module annotations.sdstest @@ -0,0 +1,8 @@ +// test + +@Annotation + +// ----------------------------------------------------------------------------- + +// test +@Annotation diff --git a/DSL/tests/resources/formatting/comments/before module name.sdstest b/DSL/tests/resources/formatting/comments/before module name.sdstest new file mode 100644 index 000000000..375fe08f7 --- /dev/null +++ b/DSL/tests/resources/formatting/comments/before module name.sdstest @@ -0,0 +1,8 @@ +// test + +package test + +// ----------------------------------------------------------------------------- + +// test +package test