posit-dev / posit-dev/air

More general handling of `{` blocks in argument lists

Open
#142 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
446
Forks
32
Avg merge
16h 48m
Merged PRs (30d)
1

Description

We currently have special support for trailing { blocks and trailing lambdas. The goal is to prevent multi-line { blocks from causing expansion of arguments in cases like these:

with(data, {
  x + y
})

map(xs, function(x) {
  x + 1
})

The implementation is currently borrowed from Biome which has to deal with similar cases such as JS lambda functions (https://github.com/posit-dev/air/pull/4). It is rather complicated and involves a best fitting algorithm. It supposes that the { blocks are either leading (unused in Air) or trailing. However it's been pointed out to us that the same behaviour would still be desirable in non-trailing case, for instance if optional arguments are trailing:

expect_snapshot({
  ...
}, error = TRUE)

And in fact there are important classes of functions that rely on trailing arguments in this way, e.g. the base higer order functions like Find() or Reduce().

Find(function(x) {
  ...
}, xs, right = FALSE)

We could leverage Biome's leading group feature to implement this. But I think it's worth exploring the feasibility of a simpler approach where we'd simply treat all { blocks as "resetting" the line width. This would handle all blocks in the same manner, whether they are leading, trailing, or not. E.g. it'd be allowed to write the following without causing an expansion:

f(1, {
  ...
}, 2, 3, {
  ...
}, 4)

Expansion would still be possible by leveraging magic line breaks, e.g. in the above example you'd add a newline before 1 to force expansion, or remove it to request the flat layout.

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 by examining the existing handling for trailing { blocks and trailing lambdas, including the Biome-inspired best-fitting algorithm described in the issue. Test the listed non-trailing and multiple-block examples, then verify that magic line breaks still control expansion and that all block positions receive the intended layout behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
r, rust
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.