Skip to content

Support expression-level on-type-formatting #208

Closed

Description

@renkun-ken

The current on-type-formatting is an extremely simple one that only supports one-line formatting, i.e. it only tries to format the line under editing.

However, in many cases there's a need to format the current expression on \n or closing the brackets.

The on-type-formatting includes closing a bracket, and inserting a line break (\n).

The case of inserting a line break is somewhat special because the expression under editing is incomplete:

mtcars %>% # hit enter

then the formatting should add an indention to the new line and it becomes

mtcars %>%
    |

The case of closing brackets is typically handling complete expressions.

mtcars %>%
  select(a,b, c) %>%
  mutate(x=a + b, y = b - c) %>%
  select(a, b,c)

ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
  geom_point() +
  labs(title = "Scatterplot", x = "Carat", y = "Price")

if (x+1) {
  y+1
} else {
  z+1
}

Closing each bracket should result in formatting the current expression.

Here's my initial implementation: master...renkun-ken:on-type-formatting. The basic approach is backward expand the range line and line from the requested point and parse the content in range, and after an expression is found, and then the next parsing results in more expressions or none (no longer parse-able) the expression under editing is found. I'm still doing experiments with it.

The trick I use to format incomplete expression with \n inserted is to replace the new blank line with a dot . so that the . could make the expression complete. The formatted text with last dot removed could be used as a text edit of the original text.

But there are still some edge cases such as breaking a line in the middle of an expression. I'll do more experiment with it and hopefully I could create a use-able on-type-formatter.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions