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

Adds preservation of newlines between statements in formatter #405

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/language-support/src/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,16 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
throw new Error('Internal formatting error in findBottomChild');
};

preserveExplicitNewlineAfter = (ctx: ParserRuleContext) => {
preserveExplicitNewlineAfter = (ctx: ParserRuleContext | TerminalNode) => {
this.preserveExplicitNewline(ctx, 'after');
};

preserveExplicitNewlineBefore = (ctx: ParserRuleContext) => {
preserveExplicitNewlineBefore = (ctx: ParserRuleContext | TerminalNode) => {
this.preserveExplicitNewline(ctx, 'before');
};

preserveExplicitNewline = (
ctx: ParserRuleContext,
ctx: ParserRuleContext | TerminalNode,
side: 'before' | 'after',
) => {
const bottomChild = this.getBottomChild(ctx, side);
Expand Down Expand Up @@ -414,6 +414,7 @@ export class TreePrintVisitor extends CypherCmdParserVisitor<void> {
this.currentBuffer().pop();
}
this.visit(ctx.SEMICOLON(i));
this.preserveExplicitNewlineAfter(ctx.SEMICOLON(i));
Copy link
Contributor

Choose a reason for hiding this comment

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

Will the semicolon still be visited?

}
}
};
Expand Down
65 changes: 65 additions & 0 deletions packages/language-support/src/tests/formatting/linebreaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,4 +920,69 @@ LIMIT 10`.trimStart();
const expected = query;
verifyFormatting(query, expected);
});

test('two statements', () => {
const query = `
MATCH (n)

RETURN n;

MATCH (n)

RETURN m;`;
const expected = query.trimStart();
verifyFormatting(query, expected);
});

test('multiple statements with comments inbetween', () => {
const query = `
MATCH (n)

RETURN n;
// This is a comment

MATCH (n)

RETURN n;

// This is another comment

MATCH (n)
RETURN n;`;
const expected = query.trimStart();
verifyFormatting(query, expected);
});

test('end of statement with multiple newlines', () => {
const query = `
MATCH (n)
RETURN m;


`;
const expected = `MATCH (n)
RETURN m;`;
verifyFormatting(query, expected);
});

test('too many newlines between statements should truncate to one', () => {
const query = `
MATCH (n)
RETURN n;






MATCH (n)
RETURN m;`;
const expected = `
MATCH (n)
RETURN n;

MATCH (n)
RETURN m;`.trimStart();
verifyFormatting(query, expected);
});
});