GithubHelp home page GithubHelp logo

rlugojr / styler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from r-lib/styler

0.0 0.0 0.0 81 KB

Non-invasive pretty printing of R code

Home Page: https://github.com/rstats-gsoc/gsoc2017/wiki/Noninvasive-source-code-formatting

R 100.00%

styler's Introduction

styler

The goal of styler is to provide non-invasive pretty-printing of R source code while adhering to general formatting conventions.

Dirty source file

print_code <- function(x) {
  knitr::asis_output(
    paste(
      c("```r", "", "", "# ------", x, "# ------", "```"),
      collapse = "\n"))
}

dirty <- readLines("tests/testthat/example/in.R")

print_code(dirty)


# ------
test <- function() {
  "Double quotes remain as they are"
  'Single quotes are converted to double quotes'
  'even if the string contains an escaped \' single quote'
  'but not if it contains a "double quote'

  # Comments are always preserved

  function_calls(get_spaces=around_equal)

  no_space( after_opening( ), paren( (1 + 2)))
  no_space (before_opening (), paren ( (1 + 2)))
  no_space(before(closing ), paren((1 + 2) ) )
  multi(
    line,
    call
  )
  multi_line_empty_call(
  )

  one_space(after,comma("in","function",  args))

  {
    braced
    expression
  }

  braced("unnamed", {
    "function"
    call
  })

  braced(named = {
    "function"
    call
  })

  braced("unnamed reduces space",    {
  })

  braced("unnamed adds space space",{
  })

  braced(named_reduces_space =    {
  })

  braced(named_adds_space =    {
  })

  braced(  {
    empty_removes_space
  })

  a%/%b
  a%%b
  a&&b
  a||b
  a==b
  a!=b
  a<=b
  a>=b
  a<-b
  a->b
  a=b
  a<b
  a>b
  a*b
  a/b
  a^b
  a&b
  a|b
  a:=b

  a+b
  a-b
  a++b
  a+-b
  a++b
  a-+b
  a--b
  a+--b
  a--+b
  call( + a)
  call( - a)
  call(5, + a)
  call(5, - a)

  # Only with conservative settings:
  call(
    preserves, distance,
    after,     commas,
    given_has,one
  )
}
# ------

Tidied file

tidy <- style_text(dirty, get_transformers(strict = FALSE))

print_code(tidy)


# ------
test <- function() {
  "Double quotes remain as they are"
  "Single quotes are converted to double quotes"
  "even if the string contains an escaped ' single quote"
  'but not if it contains a "double quote'

  # Comments are always preserved

  function_calls(get_spaces = around_equal)

  no_space(after_opening(), paren((1 + 2)))
  no_space (before_opening (), paren ((1 + 2)))
  no_space(before(closing), paren((1 + 2)))
  multi(
    line,
    call
  )
  multi_line_empty_call(
  )

  one_space(after, comma("in", "function",  args))

  {
    braced
    expression
  }

  braced("unnamed", {
    "function"
    call
  })

  braced(named = {
    "function"
    call
  })

  braced("unnamed reduces space",    {
  })

  braced("unnamed adds space space", {
  })

  braced(named_reduces_space =    {
  })

  braced(named_adds_space =    {
  })

  braced({
    empty_removes_space
  })

  a %/% b
  a %% b
  a && b
  a || b
  a == b
  a != b
  a <= b
  a >= b
  a <- b
  a -> b
  a = b
  a < b
  a > b
  a * b
  a / b
  a ^ b
  a & b
  a | b
  a := b

  a + b
  a - b
  a + +b
  a + -b
  a + +b
  a - +b
  a - -b
  a + --b
  a - -+b
  call(+a)
  call(-a)
  call(5, +a)
  call(5, -a)

  # Only with conservative settings:
  call(
    preserves, distance,
    after,     commas,
    given_has, one
  )
}
# ------

Cleaned file

clean <- styler::style_text(dirty)

print_code(clean)


# ------
test <- function() {
  "Double quotes remain as they are"
  "Single quotes are converted to double quotes"
  "even if the string contains an escaped ' single quote"
  'but not if it contains a "double quote'

  # Comments are always preserved

  function_calls(get_spaces = around_equal)

  no_space(after_opening(), paren((1 + 2)))
  no_space(before_opening(), paren((1 + 2)))
  no_space(before(closing), paren((1 + 2)))
  multi(
    line,
    call
  )
  multi_line_empty_call(
  )

  one_space(after, comma("in", "function", args))

  {
    braced
    expression
  }

  braced("unnamed", {
    "function"
    call
  })

  braced(named = {
    "function"
    call
  })

  braced("unnamed reduces space", {
  })

  braced("unnamed adds space space", {
  })

  braced(named_reduces_space = {
  })

  braced(named_adds_space = {
  })

  braced({
    empty_removes_space
  })

  a %/% b
  a %% b
  a && b
  a || b
  a == b
  a != b
  a <= b
  a >= b
  a <- b
  a -> b
  a = b
  a < b
  a > b
  a * b
  a / b
  a ^ b
  a & b
  a | b
  a := b

  a + b
  a - b
  a + +b
  a + -b
  a + +b
  a - +b
  a - -b
  a + --b
  a - -+b
  call(+a)
  call(-a)
  call(5, +a)
  call(5, -a)

  # Only with conservative settings:
  call(
    preserves, distance,
    after, commas,
    given_has, one
  )
}
# ------

styler's People

Contributors

krlmlr avatar lorenzwalthert avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.