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

First draft #1

Merged
merged 38 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4de443a
initial
lorenzwalthert Apr 8, 2021
fd25393
Add GitHub links to DESCRIPTION
lorenzwalthert Apr 8, 2021
bb817b8
re-export
lorenzwalthert Apr 8, 2021
47b6004
on top level, always add a line break to last expression
lorenzwalthert Apr 8, 2021
ce1abb7
update readme
lorenzwalthert Apr 8, 2021
af02943
multi line round brace expression should not trigger semi-colon
lorenzwalthert Apr 8, 2021
c1b46aa
add tests, fix wile case
lorenzwalthert Apr 8, 2021
d01f734
infra
lorenzwalthert Apr 8, 2021
46704d6
add gh actions
lorenzwalthert Apr 8, 2021
a900ef5
add magrittr
lorenzwalthert Apr 8, 2021
9efe799
add more tests
lorenzwalthert Apr 8, 2021
6251348
exclude tests
lorenzwalthert Apr 8, 2021
7c64060
fix braces
lorenzwalthert Apr 8, 2021
f011061
document params
lorenzwalthert Apr 8, 2021
c21b7a7
fix another note
lorenzwalthert Apr 8, 2021
0e8bd8d
lazy evaluation for speed
lorenzwalthert Apr 8, 2021
3d0f520
improve speed
lorenzwalthert Apr 8, 2021
5c3b90b
add MIT licence
lorenzwalthert Apr 8, 2021
5ef3ef6
add some comments
lorenzwalthert Apr 8, 2021
066cccf
replace tests, core function, naming
lorenzwalthert Apr 8, 2021
533ccff
line break manipulator is temporarily turned off
lorenzwalthert Apr 8, 2021
cfbfa0e
fix indention
lorenzwalthert Apr 8, 2021
025c88f
re-add tests
lorenzwalthert Apr 8, 2021
4fde3c7
ensure to use the mlr style guide
lorenzwalthert Apr 8, 2021
28972a6
add line break for long function dec
lorenzwalthert Apr 8, 2021
28c17af
more name change
lorenzwalthert Apr 8, 2021
d503b67
remove redundant licence
lorenzwalthert Apr 9, 2021
d4d5d4c
make sure behaviour is exactly as with styler. current solution was n…
lorenzwalthert Apr 9, 2021
2fc6bb3
make sure behaviour is exactly as with styler. current solution was n…
lorenzwalthert Apr 9, 2021
23bd589
fix references
lorenzwalthert Apr 9, 2021
ea59e5f
fix description
lorenzwalthert Apr 9, 2021
2adb8ff
fix re-export, add api tests
lorenzwalthert Apr 9, 2021
4a5dc65
more styling
lorenzwalthert Apr 9, 2021
29fa54c
ensure latest styler version
lorenzwalthert Apr 9, 2021
affe38f
revert random change in gitignore
lorenzwalthert Apr 9, 2021
1914b8f
random fixes
lorenzwalthert Apr 9, 2021
f7582a4
new scope definition
lorenzwalthert Apr 9, 2021
24fbc66
support length 0 code styling
lorenzwalthert Apr 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
^styler.mlr\.Rproj$
^\.Rproj\.user$
^README\.Rmd$
^\.pre-commit-config\.yaml$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
86 changes: 86 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@v1
with:
r-version: ${{ matrix.config.r }}

- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Restore R package cache
if: runner.os != 'Windows'
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')

- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
59 changes: 59 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# All available hooks: https://pre-commit.com/hooks.html
# R specific hooks: https://github.com/lorenzwalthert/precommit
repos:
- repo: https://github.com/lorenzwalthert/precommit
rev: v0.1.3
hooks:
- id: style-files
args: [--style_pkg=styler.mlr, --style_fun=mlr_style]
exclude: 'tests/testthat/.*/.*\.R'
- id: roxygenize
# codemeta must be above use-tidy-description when both are used
# - id: codemeta-description-updated
- id: use-tidy-description
- id: spell-check
exclude: >
(?x)^(
data/.*|
\.github/workflows/.+|
tests/testthat/.*/.*-in_tree|
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)renv\.lock|
(.*/|)renv/settings\.dcf|
(.*/|)\.gitignore|
(.*/|)NAMESPACE|
(.*/|)WORDLIST|
(.*/|)\.travis.yml|
(.*/|)appveyor.yml|
(.*/|)\.Rbuildignore|
(.*/|)\.pre-commit-.*|
.*\.[rR]|
.*\.Rproj|
.*\.py|
.*\.feather|
.*\.rds|
.*\.Rds|
.*\.sh|
.*\.RData
)$
- id: readme-rmd-rendered
- id: parsable-R
- id: no-browser-statement
- id: deps-in-desc
args: [--allow_private_imports]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: check-added-large-files
args: ['--maxkb=200']
- id: end-of-file-fixer
exclude: '\.Rd'
- repo: local
hooks:
- id: forbid-to-commit
name: Don't commit common R artifacts
entry: Cannot commit .Rhistory, .RData, .Rds or .rds.
language: fail
files: '\.Rhistory|\.RData|\.Rds|\.rds$'
# `exclude: <regex>` to allow committing specific files.
27 changes: 27 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Package: styler.mlr
Title: Third-party Style Guide For 'mlr'
Version: 0.0.3
Authors@R:
person(given = "Lorenz",
family = "Walthert",
role = c("aut", "cre"),
email = "[email protected]")
Description: styles code according to the 'mlr style guide' using
'styler'.
License: MIT + file LICENSE
Imports:
magrittr,
purrr,
styler (>= 1.4.1),
tibble
Suggests:
fs,
rlang,
testthat (>= 3.0.0),
usethis,
withr
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
21 changes: 21 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by roxygen2: do not edit by hand

export(cache_activate)
export(cache_clear)
export(cache_deactivate)
export(cache_info)
export(create_style_guide)
export(default_style_guide_attributes)
export(mlr_style)
export(specify_math_token_spacing)
export(specify_reindention)
export(specify_transformers_drop)
export(style_dir)
export(style_file)
export(style_pkg)
export(style_text)
export(tidyverse_math_token_spacing)
export(tidyverse_reindention)
export(tidyverse_style)
import(styler)
importFrom(purrr,partial)
189 changes: 189 additions & 0 deletions R/core.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
version = unlist(unname(read.dcf("DESCRIPTION")[, "Version"]))

#' The mlr style
#'
#' Style code according to the mlr style guide. For more details and docs, see
#' the [styler::tidyverse_style()].
#' @inheritParams styler::tidyverse_style
#' @param min_lines_for_break The minimal number of lines required within a
#' function declaration to make styler adding a blank line between header and
#' body.
#' @details
#' The following levels for `scope` are available:
#'
#' * "none": Performs no transformation at all.
#' * "spaces": Manipulates spacing between token on the same line.
#' * "indention": Manipulates the indention, i.e. number of spaces at the
#' beginning of each line.
#' * "line_breaks": Manipulates line breaks between tokens.
#' * "tokens": manipulates tokens.
#'
#' `scope` can be specified in two ways:
#'
#' - As a string: In this case all less invasive scope levels are implied, e.g.
#' "line_breaks" includes "indention", "spaces". This is brief and what most
#' users need.
#' - As vector of class `AsIs`: Each level has to be listed explicitly by
#' wrapping one ore more levels of the scope in [I()]. This offers more
#' granular control at the expense of more verbosity.
#'
#' @family obtain transformers
#' @family style_guides
#' @examples
#' style_text("call( 1)", style = mlr_style, scope = "spaces")
#' style_text("call( 1)", transformers = mlr_style(strict = TRUE))
#' @importFrom purrr partial
#' @export
mlr_style = function(scope = "tokens",
strict = TRUE,
indent_by = 2,
start_comments_with_one_space = FALSE,
reindention = tidyverse_reindention(),
math_token_spacing = tidyverse_math_token_spacing(),
min_lines_for_break = 10) {

args = as.list(environment())
scope = styler:::scope_normalize(scope)
indention_manipulators = if ("indention" %in% scope) {
list(
indent_braces = partial(styler:::indent_braces, indent_by = indent_by),
indent_op = partial(styler:::indent_op, indent_by = indent_by),
indent_eq_sub = partial(styler:::indent_eq_sub, indent_by = indent_by),
indent_without_paren = partial(styler:::indent_without_paren,
indent_by = indent_by
)
)
}
space_manipulators = if ("spaces" %in% scope) {
list(
remove_space_before_closing_paren = styler:::remove_space_before_closing_paren,
remove_space_before_opening_paren = if (strict) {
styler:::remove_space_before_opening_paren
},
add_space_after_for_if_while = styler:::add_space_after_for_if_while,
remove_space_before_comma = styler:::remove_space_before_comma,
style_space_around_math_token = partial(
styler:::style_space_around_math_token, strict,
math_token_spacing$zero,
math_token_spacing$one
),
style_space_around_tilde = partial(
styler:::style_space_around_tilde,
strict = strict
),
spacing_around_op = purrr::partial(
styler:::set_space_around_op,
strict = strict
),
# already called in spacing_around_op
# spacing_around_comma = if (strict) {
# # set_space_after_comma
# styler:::add_space_after_comma
# } else {
# styler:::add_space_after_comma
# },
remove_space_after_opening_paren = styler:::remove_space_after_opening_paren,
remove_space_after_excl = styler:::remove_space_after_excl,
set_space_after_bang_bang = styler:::set_space_after_bang_bang,
remove_space_before_dollar = styler:::set_space_after_bang_bang,
remove_space_after_fun_dec = styler:::set_space_after_bang_bang,
remove_space_around_colons = styler:::set_space_after_bang_bang,
start_comments_with_space = partial(
styler:::start_comments_with_space,
force_one = start_comments_with_one_space
),
remove_space_after_unary_pm_nested = styler:::remove_space_after_unary_pm_nested,
spacing_before_comments = if (strict) {
styler:::set_space_before_comments
} else {
styler:::add_space_before_comments
},
set_space_between_levels = styler:::set_space_between_levels,
set_space_between_eq_sub_and_comma = styler:::set_space_between_eq_sub_and_comma
# set_space_in_curly_curly = styler:::set_space_in_curly_curly
)
}

use_raw_indention = !("indention" %in% scope)

line_break_manipulators = if ("line_breaks" %in% scope) {
list(
# set_line_break_around_comma_and_or = styler:::set_line_break_around_comma_and_or,
# set_line_break_after_assignment = set_line_break_after_assignment,
# set_line_break_before_curly_opening = set_line_break_before_curly_opening,
remove_line_break_before_round_closing_after_curly =
if (strict) styler:::remove_line_break_before_round_closing_after_curly,
style_line_break_around_curly = partial(
styler:::style_line_break_around_curly,
strict = strict
),
# remove_line_breaks_in_fun_dec =
# if (strict) remove_line_breaks_in_fun_dec,
# set_line_break_around_curly_curly = styler:::set_line_break_around_curly_curly,
# set_line_break_before_closing_call = if (strict) {
# partial(
# styler:::set_line_break_before_closing_call,
# except_token_before = "COMMENT"
# )
# },
# set_line_break_after_opening_if_call_is_multi_line = if (strict) {
# partial(
# styler:::set_line_break_after_opening_if_call_is_multi_line,
# except_token_after = "COMMENT",
# except_text_before = c("ifelse", "if_else"), # don't modify line break here
# force_text_before = c("switch") # force line break after first token
# )
# },
remove_line_break_in_fun_call = purrr::partial(
styler:::remove_line_break_in_fun_call,
strict = strict
),
add_line_break_after_pipe = styler:::add_line_break_after_pipe,
set_line_break_after_opening_if_call_is_multi_line = if (strict) {
partial(
styler:::set_line_break_after_opening_if_call_is_multi_line,
except_token_after = "COMMENT",
except_text_before = c("switch", "ifelse", "if_else")
)
},
# # this breaks }) into separate lines, see https://github.com/r-lib/styler/issues/514#issue-443293104
# # add_line_break_before_round_closing_after_curly,
# add_line_break_after_pipe = if (strict) add_line_break_after_pipe,
# set_linebreak_after_ggplot2_plus = if (strict) set_linebreak_after_ggplot2_plus
# should be last because it depends on other line breaks via n_lines()
set_line_break_after_fun_dec_header = partial(
set_line_break_after_fun_dec_header,
min_lines_for_break = min_lines_for_break
)
)
}

token_manipulators = if ("tokens" %in% scope) {
list(
fix_quotes = styler:::fix_quotes,
force_assignment_eq = force_assignment_eq,
resolve_semicolon = styler:::resolve_semicolon,
add_brackets_in_pipe = styler:::add_brackets_in_pipe,
remove_terminal_token_before_and_after = styler:::remove_terminal_token_before_and_after,
wrap_if_else_while_for_fun_multi_line_in_curly =
if (strict) styler:::wrap_if_else_while_for_fun_multi_line_in_curly
)
}



create_style_guide(
# transformer functions
initialize = default_style_guide_attributes,
line_break = line_break_manipulators,
space = space_manipulators,
indention = indention_manipulators,
token = token_manipulators,
# transformer options
use_raw_indention = use_raw_indention,
reindention = reindention,
style_guide_name = "styler.mlr::mlr_style@https://github.com/mlr-org/styler.mlr/",
style_guide_version = version,
more_specs_style_guide = args
)
}
Loading