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 removes first option in alternation rules #1571

Closed
samir-abis opened this issue Jul 8, 2024 · 3 comments · Fixed by #1629
Closed

Formatter removes first option in alternation rules #1571

samir-abis opened this issue Jul 8, 2024 · 3 comments · Fixed by #1629
Labels
bug Something isn't working
Milestone

Comments

@samir-abis
Copy link

samir-abis commented Jul 8, 2024

Langium version: 3.1.1 (extension)
Package name: langium

Steps To Reproduce

  1. Create a .langium file with an alternation rule like:
NumberTypeSpecialization:
    {infer Int} 'Int' |
    {infer Float} 'Float' |
    {infer Decimal} 'Decimal';
  1. Activate the format on Langium files

The current behavior

The formatter removes the first option in alternation rules, resulting in:

NumberTypeSpecialization:
    
     |
    {infer Float} 'Float' |
    {infer Decimal} 'Decimal';

This occurs for any rule that starts with an alternation (|).

The expected behavior

The formatter should preserve all options in alternation rules, maintaining the original structure:

NumberTypeSpecialization:
    {infer Int} 'Int' |
    {infer Float} 'Float' |
    {infer Decimal} 'Decimal';
@samir-abis samir-abis added the bug Something isn't working label Jul 8, 2024
@ydaveluy
Copy link
Contributor

ydaveluy commented Aug 14, 2024

Hello,

I found where is the issue in the formatter code:
Sometime the between range has a start position after the end position that lead to incorrect edits :

const betweenRange: Range = {

Returning an empty edit in such case fix the issue.

// Ignore the edit if the previous node ends after the current node starts
if (a && (a.range.end.line > b.range.start.line ||
    (a.range.end.line === b.range.start.line && a.range.end.character > b.range.start.character))) {
    return [];
}
const betweenRange: Range = {
    start: a?.range.end ?? {
        character: 0,
        line: 0
    },
    end: b.range.start
};

@msujew I can provide a PR if you are interested. It can be included in #1628 or in a separate PR if you prefer.

@msujew
Copy link
Member

msujew commented Aug 14, 2024

@ydaveluy Thanks for looking into this! A separate PR would be appreciated :)

@ydaveluy
Copy link
Contributor

ok see PR #1629

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

Successfully merging a pull request may close this issue.

4 participants