Package 'prettifyAddins'

Title: 'RStudio' Addins to Prettify 'JavaScript', 'C++', 'Python', and More
Description: Provides 'RStudio' addins to prettify 'HTML', 'CSS', 'SCSS', 'JavaScript', 'JSX', 'Markdown', 'C(++)', 'LaTeX', 'Python', 'Julia', 'XML', 'Java', 'JSON', 'Ruby', and to reindent 'C(++)', 'Fortran', 'Java', 'Julia', 'Python', 'SAS', 'Scala', 'Shell', 'SQL' and "TypeScript". Two kinds of addins are provided: 'Prettify' and 'Indent'. The 'Indent' addins only reindent the code, while the 'Prettify' addins also modify the code, e.g. trailing semi-colons are added to 'JavaScript' code when they are missing.
Authors: Stéphane Laurent [aut, cre], James Long and contributors [cph] ('Prettier' library), Zeb Zhao [cph] ('indent.js' library), Marijn Haverbeke [cph] ('CodeMirror' library), George Leslie-Waksman and other contributors [cph] ('sql-formatter' library), Austin Cheney [cph] ('prettydiff' library), John Schlinkert [ctb, cph] (word-wrap)
Maintainer: Stéphane Laurent <[email protected]>
License: GPL-3
Version: 2.6.1
Built: 2024-10-24 03:58:36 UTC
Source: https://github.com/stla/prettifyaddins

Help Index


Prettifiable languages

Description

Returns the list of languages that are supported by this package.

Usage

getPrettifiableLanguages()

Prettify Java, JSON or Ruby

Description

Prettify Java code, JSON code or Ruby code.

Usage

prettify_FCA(contents = NA, language = NA)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "json"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

Value

The pretty code in a character string.

Note

This function requires a connection to Internet.

Examples

library(prettifyAddins)

code <- c(
  "{a: [0,1,  2   ],",
  "f:  function( x){return x+1}}" # this function will be prettified too
)

## Not run: 
cat(prettify_FCA(code, "json"))
## End(Not run)

Prettify code using Shiny

Description

Prettify some code using a Shiny app.

Usage

prettify_Shiny(contents = NA, language = NA, tabSize = NULL, themeInfo = NULL)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "javascript" or "JavaScript"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

themeInfo

this argument is not important, it controls the theme of the Shiny app; it must be NULL or a list with two fields: editor, the name of a theme, and dark, a logical value, which tells whether the theme is dark

Value

The pretty code in a character string.

Examples

library(prettifyAddins)

code <- c(
  "function f(x){",
  "return x+1",
  "}"
)
if(interactive()){
  cat(prettify_Shiny(code, "javascript"))
}

Prettify code using V8

Description

Prettify some code using the V8 package.

Usage

prettify_V8(contents = NA, language = NA, tabSize = NULL)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "javascript"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The pretty code in a character string.

Examples

library(prettifyAddins)

code <- c(
  "function f(x){",
  "return x+1",
  "}"
)
cat(prettify_V8(code, "JavaScript"))

Prettify Addins

Description

This package provides some RStudio addins: Prettify addins and Indent addins. To run an addin, select it from the Addins menu within RStudio. The Indent addins only reindent the code, while the Prettify addins also modify the code, e.g. they add trailing semi-colons to JavaScript code when they are missing.

Examples

# get the list of supported languages:
getPrettifiableLanguages()

Install PhantomJS

Description

This function is imported from the 'webdriver' package. Follow the link to its documentation: install_phantomjs


Prettify C, C++, Java

Description

Prettify some C, C++ or Java code.

Usage

prettifyCLANG(contents = NA, language = NA, tabSize = NULL)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code; when the contents is read from a file, this option is ignored, because the language is obtained from the extension of the file

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents is read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The pretty code in a character string.

Note

This function requires the command line utility clang-format.


Prettify HTML

Description

Prettify some HTML code. It works for big files.

Usage

prettifyHTML(contents = NA, tabSize = NULL)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents is read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The pretty code in a character string.

Note

This function requires the command line utility prettydiff, to install with npm.


Prettify Julia

Description

Prettify Julia code.

Usage

prettifyJulia(contents = NA, tabSize = NULL)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents is read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The pretty code in a character string.

Note

This function requires that Julia is installed on your system and that the Julia package JuliaFormatter is installed.


Prettify LaTeX

Description

Prettify LaTeX code, including Sweave code, sty files, cls files, and bib files.

Usage

prettifyLaTeX(contents = NA, tabSize = NULL, log = FALSE)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents is read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

log

logical, whether to generate a log file (it will be named indent.log)

Value

The pretty code in a character string.

Note

This function requires the command line utility latexindent.


Prettify Python

Description

Prettify Python code.

Usage

prettifyPython(contents = NA)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

Value

The pretty code in a character string.

Note

This function requires black.


Prettify XML

Description

Prettify some XML or SVG code.

Usage

prettifyXML(contents = NA, tabSize = NULL)

Arguments

contents

the code to be prettified; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Details

The code is prettified with the help of the command line utility xmllint if it is available, otherwise the xml2 is used.

Value

The pretty code in a character string.


Reindent code using chromote

Description

Reindent some code using chromote.

Usage

reindent_chromote(contents = NA, language = NA, tabSize = NULL)

Arguments

contents

the code to be reindented; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "python"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The reindented code in a character string.

Note

This function uses chromote::find_chrome() to find the executable of Google Chrome or another Chromium-based browser. If it is not found you will get an error. In this case set the environment variable CHROMOTE_CHROME to the path of such an executable (e.g. Sys.setenv(CHROMOTE_CHROME = "path/to/chrome.exe")).

Examples

library(prettifyAddins)

code <- c(
  'if test == 1:',
  'print "it is one"',
  'else:',
  'print "it is not one"'
)

if(Sys.which("chrome") != "") {
  cat(reindent_chromote(code, "python"))
}

Reindent code using PhantomJS

Description

Reindent some code using PhantomJS.

Usage

reindent_PhantomJS(contents = NA, language = NA, tabSize = NULL)

Arguments

contents

the code to be reindented; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "python"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The reindented code in a character string.

Note

This function requires the 'phantomjs' command-line utility.

Examples

library(prettifyAddins)

code <- c(
  'if test == 1:',
  'print "it is one"',
  'else:',
  'print "it is not one"'
)

if(Sys.which("phantomjs") != "") {
  cat(reindent_PhantomJS(code, "python"))
}

Reindent code using Shiny

Description

Reindent some code using a Shiny app.

Usage

reindent_Shiny(contents = NA, language = NA, tabSize = NULL, themeInfo = NULL)

Arguments

contents

the code to be reindented; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "javascript"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

themeInfo

this argument is not important, it controls the theme of the Shiny app; it must be NULL or a list with two fields: editor, the name of a theme, and dark, a logical value, which tells whether the theme is dark

Value

The reindented code in a character string.


Reindent code using V8

Description

Reindent some code using the V8 package.

Usage

reindent_V8(contents = NA, language = NA, tabSize = NULL)

Arguments

contents

the code to be reindented; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

language

the language of the code, such as "javascript"; see getPrettifiableLanguages; if the contents are read from a file and language=NA, then the language is guessed from the file extension

tabSize

number of spaces of the indentation (usually 2 or 4); if NULL (the default), there are two possibilities: if the contents are read from the current file in RStudio, then the number of spaces will be the one you use in RStudio; otherwise it is set to 2

Value

The reindented code in a character string.

Examples

library(prettifyAddins)

code <- c(
  "function f(x){",
  "return x+1",
  "}"
)
cat(reindent_V8(code, "javascript"))

Word wrap using V8

Description

Word wrap a text.

Usage

wordWrap(contents = NA, ncharacters = 80)

Arguments

contents

the text to be wrapped; there are three possibilities for this argument: NA (default), to use the file currently opened in RStudio; the path to a file; or the code given as a character vector

ncharacters

target number of characters per line

Value

The wrapped text in a character string.