Reformat Code: Does an exclude block directive exist?

When doing a reformat: Does anyone know a way to stop this...

if (level < 6) {
    1.0
  } else if (item_tier == ITEM$TIER$GREEN) {
    if (durability == TRUE) {
      c( 0.7, 0.5128, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0375,
         0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )[level - 5]
    } else {
      c( 0.7, 0.5128, 0.3448, 0.2352, 0.1538, 0.1250, 0.1, 0.08, 0.075,
         0.06, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )[level - 5]
    }
  } else {
    # White, Blue
    c( 0.7, 0.2564, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0286,
       0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )[level - 5]
  }

from becoming this...

if (level < 6) {
  1.0
} else if (item_tier == ITEM$TIER$GREEN) {
  if (durability == TRUE) {
    c(
      0.7,
      0.5128,
      0.1724,
      0.1176,
      0.0769,
      0.0625,
      0.05,
      0.04,
      0.0375,
      0.02,
      0.1176,
      0.0769,
      0.0625,
      0.02,
      0.003
    )[level - 5]
  } else {
    c(
      0.7,
      0.5128,
      0.3448,
      0.2352,
      0.1538,
      0.1250,
      0.1,
      0.08,
      0.075,
      0.06,
      0.1176,
      0.0769,
      0.0625,
      0.02,
      0.003
    )[level - 5]
  }
} else {
  # White, Blue
  c(
    0.7,
    0.2564,
    0.1724,
    0.1176,
    0.0769,
    0.0625,
    0.05,
    0.04,
    0.0286,
    0.02,
    0.1176,
    0.0769,
    0.0625,
    0.02,
    0.003
  )[level - 5]
}

Of course, writing it to look nicer helps but then even something like this...

RATE_SET1 <- c( 0.7, 0.5128, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0375,
                0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )
RATE_SET2 <- c( 0.7, 0.5128, 0.3448, 0.2352, 0.1538, 0.1250, 0.1, 0.08, 0.075,
                0.06, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )
RATE_SET3 <- c( 0.7, 0.2564, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0286,
                0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )

if (level < 6) {
  1.0
} else if (item_tier == ITEM$TIER$GREEN) {
  if (durability == TRUE) {
    RATE_SET1[level - 5]
  } else {
    RATE_SET2[level - 5]
  }
} else {
  # White, Blue
  RATE_SET3[level - 5]
}

turns into...

RATE_SET1 <-
  c(
    0.7,
    0.5128,
    0.1724,
    0.1176,
    0.0769,
    0.0625,
    0.05,
    0.04,
    0.0375,
    0.02,
    0.1176,
    0.0769,
    0.0625,
    0.02,
    0.003
  )
RATE_SET2 <-
  c(
    0.7,
    0.5128,
    0.3448,
    0.2352,
    0.1538,
    0.1250,
    0.1,
    0.08,
    0.075,
    0.06,
    0.1176,
    0.0769,
    0.0625,
    0.02,
    0.003
  )
RATE_SET3 <-
  c(
    0.7,
    0.2564,
    0.1724,
    0.1176,
    0.0769,
    0.0625,
    0.05,
    0.04,
    0.0286,
    0.02,
    0.1176,
    0.0769,
    0.0625,
    0.02,
    0.003
  )

if (level < 6) {
  1.0
} else if (item_tier == ITEM$TIER$GREEN) {
  if (durability == TRUE) {
    RATE_SET1[level - 5]
  } else {
    RATE_SET2[level - 5]
  }
} else {
  # White, Blue
  RATE_SET3[level - 5]
}

but at least the logic structure is discernible.

I couldn't find anything but perhaps there are comment base directives? Maybe like...

## {{ RStudioApi::styler(Style = off) }}

RATE_SET1 <- c( 0.7, 0.5128, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0375,
                0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )
RATE_SET2 <- c( 0.7, 0.5128, 0.3448, 0.2352, 0.1538, 0.1250, 0.1, 0.08, 0.075,
                0.06, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )
RATE_SET3 <- c( 0.7, 0.2564, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0286,
                0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )

## {{ RStudioApi::styler(Style = on) }}

if (level < 6) {
  1.0
} else if (item_tier == ITEM$TIER$GREEN) {
  if (durability == TRUE) {
    RATE_SET1[level - 5]
  } else {
    RATE_SET2[level - 5]
  }
} else {
  # White, Blue
  RATE_SET3[level - 5]
}

After a bit more searching it seems I should be thinking of these as Magic Comments, instead of compiler directives, and that some exist within the RStudio editor...

Diagnostics has some

# !diagnostics off -- disable diagnostics within this document.

# !diagnostics style=[true/false] -- toggle style diagnostics for this document.

# !diagnostics level=[syntax/core/all] -- toggle the level, or severity, of diagnostics reported.

# !diagnostics suppress=<comma-separated list of variables> -- suppress diagnostic warnings for particular variables

And there is one for sourcing

# !source altparsers::src -- use altparsers::src() instead of source() when you invoke the Source command.

But nothing mentioned for Reformat?

Perhaps it doesn't exist and I should be adding this as a feature request or learning how the TextEditingTargetReformatHelper.java works?

Instead of the RStudio formatter, you can also use {styler}, which is non-invasive by design, There is a RStudioAddin to style the active file and other APIs for styling files or text like this:

styler::style_text("
                   if (level < 6) {
    1.0
  } else if (item_tier == ITEM$TIER$GREEN) {
    if (durability == TRUE) {
      c( 0.7, 0.5128, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0375,
         0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )[level - 5]
    } else {
      c( 0.7, 0.5128, 0.3448, 0.2352, 0.1538, 0.1250, 0.1, 0.08, 0.075,
         0.06, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )[level - 5]
    }
  } else {
    # White, Blue
    c( 0.7, 0.2564, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0286,
       0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003 )[level - 5]
  }")
#> 
#> if (level < 6) {
#>   1.0
#> } else if (item_tier == ITEM$TIER$GREEN) {
#>   if (durability == TRUE) {
#>     c(
#>       0.7, 0.5128, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0375,
#>       0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003
#>     )[level - 5]
#>   } else {
#>     c(
#>       0.7, 0.5128, 0.3448, 0.2352, 0.1538, 0.1250, 0.1, 0.08, 0.075,
#>       0.06, 0.1176, 0.0769, 0.0625, 0.02, 0.003
#>     )[level - 5]
#>   }
#> } else {
#>   # White, Blue
#>   c(
#>     0.7, 0.2564, 0.1724, 0.1176, 0.0769, 0.0625, 0.05, 0.04, 0.0286,
#>     0.02, 0.1176, 0.0769, 0.0625, 0.02, 0.003
#>   )[level - 5]
#> }

Created on 2020-08-13 by the reprex package (v0.3.0)

Note that as of r-lib/styler#247, there is no support for breaking long lines (yet),

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.