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

Range formatting always adds indention when the range has initial indention #264

Closed
renkun-ken opened this issue May 25, 2020 · 4 comments · Fixed by #268
Closed

Range formatting always adds indention when the range has initial indention #264

renkun-ken opened this issue May 25, 2020 · 4 comments · Fixed by #268

Comments

@renkun-ken
Copy link
Member

Perform range formatting on the following code and it works well.

data <- dbQuery(
  con,
  "select name, group, max(update_time) as max_time
     from users
     where date = 20200525
     group by group"
)

However, it does not work well when the selection has initial indention like the following:

fun <- function(x, y) {
  data <- dbQuery(
    con,
    "select name, group, max(update_time) as max_time
     from users
     where date = 20200525
     group by group"
  )
}

image

Once:

fun <- function(x, y) {
  data <- dbQuery(
    con,
    "select name, group, max(update_time) as max_time
       from users
       where date = 20200525
       group by group"
  )
}

Twice:

fun <- function(x, y) {
  data <- dbQuery(
    con,
    "select name, group, max(update_time) as max_time
         from users
         where date = 20200525
         group by group"
  )
}

More times:

fun <- function(x, y) {
  data <- dbQuery(
    con,
    "select name, group, max(update_time) as max_time
               from users
               where date = 20200525
               group by group"
  )
}

This is particular annoying if on-type-formatting triggers range formatting inside the function like above.

@randy3k
Copy link
Member

randy3k commented May 25, 2020

It seems that we will need to check if a line is a part of a string and opt-out of the reindentation.

@renkun-ken
Copy link
Member Author

renkun-ken commented May 25, 2020

Looks like we could use a trick like the following:

style_fun <- function() {
  styler::tidyverse_style(indent_by = 4)
}

styler::style_text('{
  data <- dbQuery(
con,
"select name, group, max(update_time) as max_time
from users
where date = 20200525
group by group"
)
}', style = style_fun)

We put the code being formatted inside a {} block and use indent_by = n, and we extract the formatted code inside the {}.

@renkun-ken
Copy link
Member Author

renkun-ken commented May 25, 2020

Sorry, it won't work properly if there are nested blocks inside the expression since the indent_by = n will be applied to the whole expression.

@renkun-ken
Copy link
Member Author

Looks like we could tweak the style function like the following to apply initial indention to the expression

style_fun <- function() {
  s <- styler::tidyverse_style(indent_by = 2)
  s$indention$apply_initial_indention <- function(pd_nested) {
    print(pd_nested)
    # do something with pd_nested to add indention to expressions
    pd_nested
  }
  s
}

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