eclipse-xtext / eclipse-xtext/xtext

[Formatter] newLine + spaces can't be achieved

Open
#2,532 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
831
Forks
330
Avg merge
3d 7h
Merged PRs (30d)
12

Description

I am trying to indent a list based on its header and I can't achieve this using the current formatter.

Further explanation:

Grammar:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

DSL returns XTDSL:
	models+=Model*
;

Model returns XTModel:
	{XTModel} 'startList' '(' greetings+=Greeting*')'';';
	
Greeting returns XTGreeting:
	'list' ids_list=identifier_list ':' 'endlist' ';';

identifier_list returns XTIdentifierList:
	ids+=identifier (',' ids+=identifier)*
;

identifier returns XTIdentifier:
	{XTIdentifier} ID
;

Formatter code:

	def dispatch void format(XTDSL xtdsl, extension IFormattableDocument document) {
		xtdsl.models.forEach[format]
	}

	def dispatch void format(XTModel xtmodel, extension IFormattableDocument document) {
		xtmodel.greetings.forEach[format; append[newLine]]
		xtmodel.regionFor.keyword("startList").append[oneSpace]
		xtmodel.regionFor.keyword("(").append[newLine]
		xtmodel.regionFor.keyword(")").prepend[noSpace]
		xtmodel.regionFor.keyword(";").prepend[noSpace]
		xtmodel.append[newLine]
		alignGreetingsOnOpenBracket(xtmodel.greetings, document)
	}

	def void alignGreetingsOnOpenBracket(EList<XTGreeting> greetings, extension IFormattableDocument document) {
		val precedingString = "startList ("
		for (greeting : greetings) {
			greeting.prepend[space = Strings.repeat(" ", precedingString .length); highPriority]
		}
	}

After applying the formatter:

startList (
list A : endlist;
list B : endlist;
list C : endlist;
);

Expected output after applying the formatter:

startList (
           list A : endlist;
           list B : endlist;
           list C : endlist;
);

Basically what I am trying to achieve is having spaces before list A : endlist; with the width of startList ( to have the list A, list B and list C all aligned with the opening bracket found after startList.

The formatter implementation does not allow something like this:

var precedingString = "startList ("
greeting.prepend[space = Strings.repeat(" ", precedingString.length); newLine]

If a newLine exists then spaces specified will always be ignored. The only way to have spaces with a newLine is to use indent which can be of whatever width specified by tabWidth EditorsPlugin preference.

I assume that the formatter should allow having spaces along with newLines or allow the specification of the indentation width as an override to the tabWidth preference if needed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the formatter implementation shown, especially format(XTModel) and alignGreetingsOnOpenBracket, and reproduce the grammar example's current and expected output. Done means the formatter has a defined way to retain the requested leading spaces after a newline or provide an explicit indentation width for this case.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.