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

Discussion: Delimited softlines #175

Closed
Tracked by #204
Xophmeister opened this issue Jan 16, 2023 · 3 comments
Closed
Tracked by #204

Discussion: Delimited softlines #175

Xophmeister opened this issue Jan 16, 2023 · 3 comments

Comments

@Xophmeister
Copy link
Member

Is your feature request related to a problem? Please describe.
As described in #172 -- which is essentially the dual of this issue -- queries can't (AFAIK) be written that perform different actions in different contexts. The aforementioned issue discusses a situation where syntactic elements are added in multi-line contexts only; this issue is for inserting elements in single-line contexts only.

Consider the following, in Bash:

{
  foo
  bar
  quux
}

which is equivalent to:

{ foo; bar; quux; }

It's quite straightforward to write queries to format these compound statements:

(compound_statement) @prepend_hardline @append_hardline

(compound_statement
  .
  "{" @append_input_softline @append_indent_start
)

(compound_statement
  "}" @prepend_indent_end @prepend_spaced_softline
  .
)

(compound_statement
  (command) @prepend_spaced_softline
)

However, this will fail -- that is, "format unexpectedly" -- if the input is syntactically invalid. For example, a single-line input of {foo;bar;quux} (note the missing trailing semi-colon) will be formatted as:

{
  foo;
  bar;
  quux }

We could add ; delimiters in both single and multi-line contexts -- that would be valid -- but they'd be redundant in multi-line contexts.

(Aside: If redundant semi-colons exist in the original, multi-line input, they won't be removed by Topiary either.)

Describe the solution you'd like
Two new capture names: @{append,prepend}_delimited_softline.

To act as a generalisation of @{append,prepend}_spaced_softline, taking into account the local (#delimiter!) directive, in much the same way as @{append,prepend}_delimiter is a generalisation of @{append,prepend}_space.

Describe alternatives you've considered
Do nothing. That is, assume the input source is syntactically correct and format it appropriately. If the writer of the code has made mistakes, it's not Topiary's job to fix them.

TBH, I think "doing nothing" is probably the right thing to do in the example given. (In {foo;bar;quux}, the trailing semi-colon is considered an argument to quux; while unusual and bad style, that may be what's intended and it's no business of Topiary's to assume otherwise.)

Thus I'm posting this issue only to spur discussion, in case there are genuine uses for @{append,prepend}_delimited_softline.

@aspiwack
Copy link
Member

Redundant is fine, in general. For in languages that permit it, we tend to add trailing delimiters. The motivation for trailing delimiters is that they reduce the size of diffs if we add a line later.

That being said, I have to admit that in the case of Bash, having semicolon line terminators is pretty unidiomatic. So we should avoid this. And having no semicolons in the multiline case has the same diff-minimising property. So it does sound quite reasonable.

@nbacquey
Copy link
Member

nbacquey commented Feb 6, 2023

#205 introduces a predicate that is quite similar, in the sense that it outputs the #delimiter! only if the context is multiline. Here we would like the reverse, if I understand correctly.

We could summarize all of those use-cases with a single @XXXpend_context_delimiter which would take two predicates, like #delimiter_if_single_line! and #delimiter_if_multi_line!, and expand according to the context.
Maybe it's over-engineered for the moment, though.

@nbacquey
Copy link
Member

nbacquey commented Apr 6, 2023

#354 Solves this issue: appending a delimiter only in single-line context can now be done with

(
  (foo) @append_delimiter
  (#delimiter! ";")
  (#single_line_only!)
)

@nbacquey nbacquey closed this as completed Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants