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

Formatter break document with single line comments #1613

Closed
ydaveluy opened this issue Jul 29, 2024 · 2 comments
Closed

Formatter break document with single line comments #1613

ydaveluy opened this issue Jul 29, 2024 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@ydaveluy
Copy link
Contributor

Hello,

When the formatter prepend/append oneSpace or noSpace on a token, it break the document if a single line is between the 2 tokens

Langium version: 3.1.0
Package name: langium-lsp

Steps To Reproduce

  1. Simple HelloWorld grammar
grammar HelloWorld

entry Model:
    (persons+=Person | greetings+=Greeting)*;

Person:
    'person' name=ID;

Greeting:
    'Hello' person=[Person:ID] '!';

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;

hidden terminal ML_COMMENT: /\/\*[\s\S]*?\*\//;
hidden terminal SL_COMMENT: /\/\/[^\n\r]*/;
  1. one space between 'person' keyword and 'name' property
export class HelloFormatter extends AbstractFormatter {

    protected format(node: AstNode): void {
        if (ast.isPerson(node)) {
            const formatter = this.getNodeFormatter(node);
            formatter.property('name').prepend(Formatting.oneSpace())
        } 
    }

}
  1. simple hello file with a single line comment between person and name
person //comment
John

The current behavior

After the formatting the name is moved at the end of the single line comment

person //commentJohn

The expected behavior

person //comment
John

@ydaveluy ydaveluy added the bug Something isn't working label Jul 29, 2024
@ydaveluy ydaveluy changed the title Formatter break documentation with single line comments Formatter break document with single line comments Aug 13, 2024
@ydaveluy
Copy link
Contributor Author

Here is a temporary workaround that works only if rule name is SL_COMMENT:

const nodeEdits = this.createTextEdit(lastNode, node, prependFormatting, context);

Skip the prepend formatting if the lastNode rule is SL_COMMENT

if (!lastNode?.hidden || isLeafCstNode(lastNode) && lastNode.tokenType.name !== 'SL_COMMENT') {
    const nodeEdits = this.createTextEdit(lastNode, node, prependFormatting, context);
    for (const edit of nodeEdits) {
        if (edit && this.insideRange(edit.range, range)) {
            edits.push(edit);
        }
    }
}

@ydaveluy
Copy link
Contributor Author

fixed by #1628

@spoenemann spoenemann added this to the v3.2.0 milestone Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants