Skip to content

Commit

Permalink
fix: don't pull first module entry into comment
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Jul 4, 2023
1 parent 5fb3c57 commit 2a1f91f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
57 changes: 52 additions & 5 deletions DSL/src/language-server/formatting/safe-ds-formatter.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -191,15 +191,26 @@ 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));
}
});

// 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));
}
Expand All @@ -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));
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test

class C

// -----------------------------------------------------------------------------

// test
class C
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test

import test

// -----------------------------------------------------------------------------

// test
import test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test

@Annotation

// -----------------------------------------------------------------------------

// test
@Annotation
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// test

package test

// -----------------------------------------------------------------------------

// test
package test

0 comments on commit 2a1f91f

Please sign in to comment.