Actions
Create dynamic fixes for your custom rules.
Overview
vale-ls
for an easy way to integrate Actions into your favorite text
editor.Actions provide a way for external tools to provide methods for correcting style issues.
While styles can use whatever value they want for actions and tools can implement the actions how they want, there are a series of standard actions that existing rules and tools use.
suggest
func suggest(match string) []string
suggest
returns an array of suggested replacements for the matched text.
script
action:
name: suggest
params:
- scriptName.tengo
The script
action allows you to define a custom suggestion script that will
be executed for each match. The script should return an array of strings called
suggestions
.
Scripts are written in Tengo and are stored in the
<StylesPath>/config/actions
directory. Here’s an example script:
text := import("text")
// `match` is provided by Vale and represents the rule's matched text.
made := text.re_replace(`([A-Z]\w+)([A-Z]\w+)`, match, `$1-$2`)
made = text.replace(made, "-", "_", 1)
made = text.to_lower(made)
// `suggestions` is required by Vale and represents the script's output.
suggestions := [made]
We would save this script as CamelToSnake.tengo
and then reference it in
our rule:
extends: existence
message: "'%s' should be in snake_case."
nonword: true
level: error
action:
name: suggest
params:
- CamelToSnake.tengo
tokens:
- '[A-Z]\w+[A-Z]\w+'
spellings
action:
name: suggest
params:
- spellings
spellings
returns the top 5 spelling suggestions for the matched text from
all active dictionaries. Suggestions are ordered by calculating the
Levenshtein distance between the matched text and the dictionary words.
replace
func replace(match string) []string
replace
returns an array of user-provided replacements.
action:
name: replace
params:
- option1
- option2
...
Rules that extend substitution
or capitalization
will automatically
populate the params
array, so you can simply provide the name
:
action:
name: replace
remove
func remove(match string)
remove
will remove the matched text of any rule.
action:
name: remove
edit
func edit(match string) string
edit
will perform an in-place edit on the match string according to the
provided parameters.
regex
Replace the provided regex pattern with the given string.
action:
name: edit
params:
- regex
- '([A-Z]\w+)([A-Z]\w+)' # pattern
- "$1-$2" # repl
This is equivalent to the following Go code:
match = pattern.ReplaceAllString(match, repl)
See Regexp.ReplaceAllString for more information.
trim_right
Trim the first parameter from the end of the matched text.
action:
name: edit
params:
- trim_right
- '.?!'
trim_left
Trim the first parameter from the start of the matched text.
action:
name: edit
params:
- trim_left
- ' '
trim
Trim the first parameter from the both start and end of the matched text.
action:
name: edit
params:
- trim
- ' '
split
Split the matched text on the first parameter at the index of the second parameter.
action:
name: edit
params:
- split
- ' ' # sub
- 1 # index
This is equivalent to the following Go code:
match = strings.Split(match, sub)[index]