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

Tabular Alignment #3614

Closed
treetime opened this issue Aug 18, 2023 · 6 comments · Fixed by #3616
Closed

Tabular Alignment #3614

treetime opened this issue Aug 18, 2023 · 6 comments · Fixed by #3616

Comments

@treetime
Copy link

Configuration (required)

#.scalafmt.conf
version = 3.7.12
runner.dialect = scala213

align.tokens = [{
  code = ")"
  owners = [{
    regex = "Defn\\.Def"
  }]
}]

Command-line parameters (required)

I did not attempt this using the CLI (since this is probably not a bug)

Steps

I want to end up with code like this:

object Test {

  def meeethod1(pram1      : AnyRef): Any = ???
  def methd2   (paaaaaram2 : Any   ): Any = ???
  def methh3   (param333333: Any   ): Any = ???
  def md4      (param4     : Any   ): Any = ???

}

Expectation

Something, anything would align in any way.

Notes

I don't think this is an issue with scalafmt per se, but I don't know whether parentheses are a valid alignment token. I am not well versed in scalafmt, but as far as I can tell, if I set a value for align.token, the regex section should enclose the area I want to align over, and these will be aligned over matching parents. For a colon I can make sense of it, Term.Param owns the colon, and the token there-after is aligned for every parent (Defn.Def).

align.tokens = [{
  code = ":"
  owners = [{
    regex = "Term\\.Param"
    parents = [ "Defn\\.Def" ]
  }]
}]

===>

object Test {

  def meeethod1(pram1:    AnyRef): Any = ???
  def methd2(paaaaaram2:  Any): Any = ???
  def methh3(param333333: Any): Any = ???
  def md4(param4:         Any): Any = ???

}

Thus, all the values to the right of the colon are aligned. No matter what I do, however, I cannot coax it into aligning over parentheses per the desired example. I've played with the AST explorer and tried a great many combinations of tokens to try achieve something, set values are wide and specific as I can - even inspected the source code a little (to no avail).

My best guess is that I would have to be able to set the alignment token to be a member of the AST itself to achieve what I'm trying to achieve, which does not seem to be a supported behaviour.

@treetime treetime changed the title Tabular Aligntmet Tabular Alignment Aug 18, 2023
@kitbellew
Copy link
Collaborator

align.tokens = [{
  code = ")"
  owners = [{
    regex = "Defn\\.Def"
  }]
}]

Quick comment: in

def methd2   (paaaaaram2 : Any   ): Any = ???

both ( and ) would need to be aligned on (although that's currently broken), and they are both owned by Term.ParamClause and not Defn.Def (perhaps you used an old scalameta version in AST explorer).

@treetime
Copy link
Author

Yes, I have been using the AST explorer (linked off the docs); the scalafiddle link never loads for me. I have only superficial knowledge of the names of the parts of the AST, so have been referencing that. For the record, the parser is [scalameta-4.4.17], and there is no explicit reference of a ParamClause.

@kitbellew
Copy link
Collaborator

I guess both are broken, fully or partially :( oh well.

@treetime
Copy link
Author

Ah, unluck :/
In reference to this and the linked issue 3615; in the test, one of the results is:

object a {
  def meeethod1( pram1:       AnyRef ): Any = ???
  def methd2(    paaaaaram2:  Any    ): Any = ???
  def meth3(     param333333: Any    ): Any = ???
  def md4(       param4:      Any    ): Any = ???
}

(which is great btw, thanks). The parameter names are aligned, but is it in any way possible to also align the physical parentheses themselves? I have it as "The thing after the alignment token" is the thing being aligned, am I correct in understanding the "alignment token" in my example should effectively be Term.Name?

@kitbellew
Copy link
Collaborator

Ah, unluck :/ In reference to this and the linked issue 3615; in the test, one of the results is:

#3615 is a bugfix uncovered by your case, but not a fix for your example. that would be a separate story, likely needing a
a new configuration parameter since it changes behaviour (currently, md4 and ( cannot be pulled apart if they had no space between them to begin with).

@treetime
Copy link
Author

Noted, but nevertheless, thank you, it takes me one step closer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants