diff --git a/DESCRIPTION b/DESCRIPTION index 6987654..ea5f5e0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,7 +37,8 @@ Suggests: urlchecker, readr, xopen, - spelling + spelling, + lintr Remotes: github::jmbarbone/fuj, github::jmbarbone/mark diff --git a/NAMESPACE b/NAMESPACE index ab4fd63..bc5416f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,6 +13,7 @@ export(.Fixmes) export(.FixmesHere) export(.GitBranchPrompt) export(.GitPrepareCommitMsg) +export(.LintFile) export(.NewsUrls) export(.NiceMessage) export(.OpenFile) diff --git a/NEWS.md b/NEWS.md index 620a148..31d0ead 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # Rprofile (development) +* Adds `.LintFile()` for selecting individual files to `lint` (defaults to currently opened file) [#21] * Adds `.GitPrepareCommitMsg()` to copy one of two templates to `.git/hook/prepare-commit-msg` [#10] * Adds `.OpenFile()` to open a file path or an object inside a file [#6] diff --git a/R/lint.R b/R/lint.R new file mode 100644 index 0000000..a3ffb8c --- /dev/null +++ b/R/lint.R @@ -0,0 +1,28 @@ +#' Lint a single file +#' +#' Use `lintr::lint()` to _lint_ a single file +#' +#' @param path The file path. Default (`NULL`) checks for the current file open +#' in the source editor in **RStudio**. +#' @param linters Passed to `linters` argument in `lintr::lint()`; however, +#' when passing a character vector, finds all linters with that tag. +#' @param ... Additional arguments passed to `lintr::lint()` +#' @returns See `lintr::lint()` +#' @export +.LintFile <- function(path = NULL, linters = "default", ...) { + fuj::require_namespace("lintr") + + if (is.null(path)) { + fuj::require_namespace("rstudioapi") + path <- rstudioapi::getSourceEditorContext()$path + } + + if (is.character(linters)) { + linters <- lintr::available_linters(tags = linters)$linter + linters <- fuj::set_names(linters) + linters <- lapply(linters, get, pos = asNamespace("lintr")) + } + + path <- normalizePath(path, .Platform$file.sep, mustWork = TRUE) + lintr::lint(path, linters = linters, ...) +} diff --git a/man/dot-LintFile.Rd b/man/dot-LintFile.Rd new file mode 100644 index 0000000..be98ecb --- /dev/null +++ b/man/dot-LintFile.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/lint.R +\name{.LintFile} +\alias{.LintFile} +\title{Lint a single file} +\usage{ +.LintFile(path = NULL, linters = "default", ...) +} +\arguments{ +\item{path}{The file path. Default (\code{NULL}) checks for the current file open +in the source editor in \strong{RStudio}.} + +\item{linters}{Passed to \code{linters} argument in \code{lintr::lint()}; however, +when passing a character vector, finds all linters with that tag.} + +\item{...}{Additional arguments passed to \code{lintr::lint()}} +} +\value{ +See \code{lintr::lint()} +} +\description{ +Use \code{lintr::lint()} to \emph{lint} a single file +}