Skip to content

Instantly share code, notes, and snippets.

@powercode
Last active April 10, 2017 18:40
Show Gist options
  • Save powercode/f6284c9a947bc807c4769a7ca517a433 to your computer and use it in GitHub Desktop.
Save powercode/f6284c9a947bc807c4769a7ca517a433 to your computer and use it in GitHub Desktop.
Argument completer for ripgrep (rg)
using namespace System.Collections.Generic
using namespace System.Management.Automation
class RGCompleter {
static [bool] ShouldAdd([string] $completionText, [string] $wordToComplete, [string[]] $previousCommand, [string[]] $multiOpt) {
if ($completionText.StartsWith($wordToComplete, [StringComparison]::OrdinalIgnoreCase)){
if ($completionText -in $previousCommand -and $completionText -notin $multiOpt) {
return $false
}
return $true
}
return $false
}
static [string[]] GetPreviousOptions([Language.CommandAst] $ast, [int] $cursorColumn) {
if ($ast.CommandElements.Count -eq 1) {
return [string[]]::new(0)
}
return $ast.CommandElements.Foreach{$_.Extent.Text}
}
static [List[CompletionResult]] CompleteArgument([string] $command, [string] $previousCommand, [string]$wordToComplete) {
$result = [List[CompletionResult]]::new(10)
switch -regex -CaseSensitive ($previousCommand){
'-(A|B|C)' {
(1..10).Where{"$_".StartsWith($wordToComplete)}.Foreach{
$result.Add([CompletionResult]::new($_,$_, [CompletionResultType]::ParameterValue, $_))
}
}
'--color(?!s)' {
('never','auto','always','ansi').Where{"$_".StartsWith($wordToComplete)}.Foreach{
$result.Add([CompletionResult]::new($_,$_, [CompletionResultType]::ParameterValue, $_))
}
}
'--colors' {
$type = 'path','line','match'
$attrib = 'fg','bg','style','none'
$style ='nobold','bold','nointense','intense'
$color = 'red','blue','green','cyan','magenta','yellow','white','black'
$t,$a,$v = $wordToComplete.Trim().Split(':')
$completions = $type.Foreach{
$t=$_
"${t}:none"
('fg','bg').Foreach{$a = $_; $color.Foreach{"${t}:${a}:$_"}}
$style.Foreach{"${t}:style:$_"}
}.Where{
$_.StartsWith($wordToComplete, [Stringcomparison]::OrdinalIgnoreCase)
}.Foreach{$result.Add([CompletionResult]::new($_, $_, [CompletionResultType]::ParameterValue, $_))}
}
'-(T|t)' {
(& $command --type-list).Foreach{
$c, $t = $_.Split(":",2)
if ($c.StartsWith($wordToComplete, [StringComparison]::OrdinalIgnoreCase)) {
$r = [CompletionResult]::new($c,$c, [CompletionResultType]::ParameterValue, $t)
$result.Add($r)
}
}
}
}
return $result;
}
static [List[CompletionResult]] CompleteInput([string] $wordToComplete, [Language.CommandAst] $ast, [int] $cursorColumn){
$hasArgOpts = '-A','--after-context','-B','--before-context','--color','--colors','-C','--context','--context-separator','-E','--encoding','-f','--file','-g','--glob','--ignore-file','-M','--max-columns','-m','--max-count','--maxdepth','--path-separator','-e','--regexp','-r','--replace','-j','--threads','-t','--type','--type-add','--type-clear','-T','--type-not'
$multiOpts = '--colors','-f','--file','-g','--glob','--ignore-file','-e','--regexp','-t','--type','--type-add','-T','--type-not'
$prevOpts = [RGCompleter]::GetPreviousOptions($ast, $cursorColumn)
$command = $ast.CommandElements[0].Extent.Text
if ($prevOpts.Length -gt 1 -and $wordToComplete) {
$previous = $prevOpts[-2]
}
else {
$previous = $prevOpts[-1]
}
if ($previous -in $hasArgOpts) {
return [RGCompleter]::CompleteArgument($command, $previous, $wordToComplete)
}
$result = [List[CompletionResult]]::new(60)
if ([RGCompleter]::ShouldAdd("-A", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-A", "-A <NUM>", [CompletionResultType]::ParameterName, 'Show NUM lines after each match.'))
}
if ([RGCompleter]::ShouldAdd("-B", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-B", "-B <NUM>", [CompletionResultType]::ParameterName, 'Show NUM lines before each match.'))
}
if ([RGCompleter]::ShouldAdd("-s", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-s", "-s", [CompletionResultType]::ParameterName, 'Search case sensitively. This overrides -i/--ignore-case and -S/--smart-case.'))
}
if ([RGCompleter]::ShouldAdd("--color", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--color", "--color <WHEN>", [CompletionResultType]::ParameterName, @"
When to use color in the output. The possible values are never, auto, always or ansi.
The default is auto. When always is used, coloring is attempted based on your
environment. When ansi used, coloring is forcefully done using ANSI escape color codes.
"@
))
}
if ([RGCompleter]::ShouldAdd("--colors", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--colors", "--colors <SPEC>", [CompletionResultType]::ParameterName, @"
This flag specifies color settings for use in the output. This flag may be provided
multiple times. Settings are applied iteratively. Colors are limited to one of eight
choices: red, blue, green, cyan, magenta, yellow, white and black. Styles are limited to
nobold, bold, nointense or intense.
"@
))
}
if ([RGCompleter]::ShouldAdd("--column", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--column", "--column", [CompletionResultType]::ParameterName, @"
Show column numbers (1-based). This only shows the column numbers for the first match on
each line. This does not try to account for Unicode. One byte is equal to one column.
This implies --line-number.
"@
))
}
if ([RGCompleter]::ShouldAdd("-C", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-C", "-C <NUM>", [CompletionResultType]::ParameterName, 'Show NUM lines before and after each match.'))
}
if ([RGCompleter]::ShouldAdd("--context-separator", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--context-separator", "--context-separator <SEPARATOR>", [CompletionResultType]::ParameterName, @"
The string used to separate non-contiguous context lines in the output. Escape sequences
like \x7F or \t may be used. The default value is --.
"@
))
}
if ([RGCompleter]::ShouldAdd("-c", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-c", "-c", [CompletionResultType]::ParameterName, 'Only show count of matches for each file.'))
}
if ([RGCompleter]::ShouldAdd("--debug", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--debug", "--debug", [CompletionResultType]::ParameterName, 'Show debug messages. Please use this when filing a bug report.'))
}
if ([RGCompleter]::ShouldAdd("-E", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-E", "-E <ENCODING>", [CompletionResultType]::ParameterName, @"
Specify the text encoding that ripgrep will use on all files searched. The default value
is ''auto'', which will cause ripgrep to do a best effort automatic detection of encoding
on a per-file basis. Other supported values can be found in the list of labels here:
https://encoding.spec.whatwg.org/#concept-encoding-get
"@
))
}
if ([RGCompleter]::ShouldAdd("-f", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-f", "-f <FILE>", [CompletionResultType]::ParameterName, @"
Search for patterns from the given file, with one pattern per line. When this flag is
used or multiple times or in combination with the -e/--regexp flag, then all patterns
provided are searched. Empty pattern lines will match all input lines, and the newline
is not counted as part of the pattern.
"@
))
}
if ([RGCompleter]::ShouldAdd("--files", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--files", "--files", [CompletionResultType]::ParameterName, @"
Print each file that would be searched without actually performing the search. This is
useful to determine whether a particular file is being searched or not.
"@
))
}
if ([RGCompleter]::ShouldAdd("-l", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-l", "-l", [CompletionResultType]::ParameterName, 'Only show the paths with at least one match.'))
}
if ([RGCompleter]::ShouldAdd("--files-without-match", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--files-without-match", "--files-without-match", [CompletionResultType]::ParameterName, 'Only show the paths that contains zero matches.'))
}
if ([RGCompleter]::ShouldAdd("-F", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-F", "-F", [CompletionResultType]::ParameterName, @"
Treat the pattern as a literal string instead of a regular expression. When this flag is
used, special regular expression meta characters such as (){}*+. do not need to be
escaped.
"@
))
}
if ([RGCompleter]::ShouldAdd("-L", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-L", "-L", [CompletionResultType]::ParameterName, 'Follow symbolic links.'))
}
if ([RGCompleter]::ShouldAdd("-g", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-g", "-g <GLOB>", [CompletionResultType]::ParameterName, @"
Include or exclude files/directories for searching that match the given glob. This
always overrides any other ignore logic. Multiple glob flags may be used. Globbing rules
match .gitignore globs. Precede a glob with a ! to exclude it.
"@
))
}
if ([RGCompleter]::ShouldAdd("-h", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-h", "-h", [CompletionResultType]::ParameterName, 'Prints help information. Use --help for more details.'))
}
if ([RGCompleter]::ShouldAdd("--heading", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--heading", "--heading", [CompletionResultType]::ParameterName, @"
This shows the file name above clusters of matches from each file instead of showing the
file name for every match. This is the default mode at a tty.
"@
))
}
if ([RGCompleter]::ShouldAdd("--hidden", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--hidden", "--hidden", [CompletionResultType]::ParameterName, @"
Search hidden files and directories. By default, hidden files and directories are
skipped.
"@
))
}
if ([RGCompleter]::ShouldAdd("-i", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-i", "-i", [CompletionResultType]::ParameterName, 'Case insensitive search. This is overridden by --case-sensitive.'))
}
if ([RGCompleter]::ShouldAdd("--ignore-file", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--ignore-file", "--ignore-file <FILE>", [CompletionResultType]::ParameterName, @"
Specify additional ignore files for filtering file paths. Ignore files should be in the
gitignore format and are matched relative to the current working directory. These ignore
files have lower precedence than all other ignore files. When specifying multiple ignore
files, earlier files have lower precedence than later files.
"@
))
}
if ([RGCompleter]::ShouldAdd("-v", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-v", "-v", [CompletionResultType]::ParameterName, 'Invert matching. Show lines that don''t match given patterns.'))
}
if ([RGCompleter]::ShouldAdd("-n", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-n", "-n", [CompletionResultType]::ParameterName, 'Show line numbers (1-based). This is enabled by default when searching in a tty.'))
}
if ([RGCompleter]::ShouldAdd("-M", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-M", "-M <NUM>", [CompletionResultType]::ParameterName, @"
Don''t print lines longer than this limit in bytes. Longer lines are omitted, and only
the number of matches in that line is printed.
"@
))
}
if ([RGCompleter]::ShouldAdd("-m", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-m", "-m <NUM>", [CompletionResultType]::ParameterName, 'Limit the number of matching lines per file searched to NUM.'))
}
if ([RGCompleter]::ShouldAdd("--max-filesize", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--max-filesize", "--max-filesize", [CompletionResultType]::ParameterName, 'Ignore files larger than NUM in size. Does not ignore directories.'))
}
if ([RGCompleter]::ShouldAdd("--maxdepth", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--maxdepth", "--maxdepth <NUM>", [CompletionResultType]::ParameterName, @"
Limit the depth of directory traversal to NUM levels beyond the paths given. A value of
zero only searches the starting-points themselves.
"@
))
}
if ([RGCompleter]::ShouldAdd("--mmap", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--mmap", "--mmap", [CompletionResultType]::ParameterName, @"
Search using memory maps when possible. This is enabled by default when ripgrep thinks
it will be faster. Note that memory map searching doesn''t currently support all options,
so if an incompatible option (e.g., --context) is given with --mmap, then memory maps
will not be used.
"@
))
}
if ([RGCompleter]::ShouldAdd("--no-filename", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-filename", "--no-filename", [CompletionResultType]::ParameterName, 'Never show the file name for a match. This is the default when one file is searched.'))
}
if ([RGCompleter]::ShouldAdd("--no-heading", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-heading", "--no-heading", [CompletionResultType]::ParameterName, @"
Don''t group matches by each file. If -H/--with-filename is enabled, then file names will
be shown for every line matched. This is the default mode when not at a tty.
"@
))
}
if ([RGCompleter]::ShouldAdd("--no-ignore", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-ignore", "--no-ignore", [CompletionResultType]::ParameterName, @"
Don''t respect ignore files (.gitignore, .ignore, etc.). This implies --no-ignore-parent
and --no-ignore-vcs.
"@
))
}
if ([RGCompleter]::ShouldAdd("--no-ignore-parent", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-ignore-parent", "--no-ignore-parent", [CompletionResultType]::ParameterName, 'Don''t respect ignore files (.gitignore, .ignore, etc.) in parent directories.'))
}
if ([RGCompleter]::ShouldAdd("--no-ignore-vcs", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-ignore-vcs", "--no-ignore-vcs", [CompletionResultType]::ParameterName, @"
Don''t respect version control ignore files (.gitignore, etc.). This implies
--no-ignore-parent. Note that .ignore files will continue to be respected.
"@
))
}
if ([RGCompleter]::ShouldAdd("-N", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-N", "-N", [CompletionResultType]::ParameterName, 'Suppress line numbers. This is enabled by default when NOT searching in a tty.'))
}
if ([RGCompleter]::ShouldAdd("--no-messages", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-messages", "--no-messages", [CompletionResultType]::ParameterName, 'Suppress all error messages. This is equivalent to redirecting stderr to /dev/null.'))
}
if ([RGCompleter]::ShouldAdd("--no-mmap", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--no-mmap", "--no-mmap", [CompletionResultType]::ParameterName, 'Never use memory maps, even when they might be faster.'))
}
if ([RGCompleter]::ShouldAdd("-0", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-0", "-0", [CompletionResultType]::ParameterName, @"
Whenever a file name is printed, follow it with a NUL byte. This includes printing file
names before matches, and when printing a list of matching files such as with --count,
--files-with-matches and --files. This option is useful for use with xargs.
"@
))
}
if ([RGCompleter]::ShouldAdd("--path-separator", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--path-separator", "--path-separator <SEPARATOR>", [CompletionResultType]::ParameterName, @"
The path separator to use when printing file paths. This defaults to your platform''s
path separator, which is / on Unix and \ on Windows. This flag is intended for
overriding the default when the environment demands it (e.g., cygwin). A path separator
is limited to a single byte.
"@
))
}
if ([RGCompleter]::ShouldAdd("-p", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-p", "-p", [CompletionResultType]::ParameterName, 'Alias for --color always --heading -n.'))
}
if ([RGCompleter]::ShouldAdd("-q", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-q", "-q", [CompletionResultType]::ParameterName, @"
Do not print anything to stdout. If a match is found in a file, stop searching. This is
useful when ripgrep is used only for its exit code.
"@
))
}
if ([RGCompleter]::ShouldAdd("-e", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-e", "-e <pattern>", [CompletionResultType]::ParameterName, @"
A regular expression used for searching. Multiple patterns may be given. To match a
pattern beginning with a -, use [-].
"@
))
}
if ([RGCompleter]::ShouldAdd("-r", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-r", "-r <ARG>", [CompletionResultType]::ParameterName, @"
Replace every match with the string given when printing results. Neither this flag nor
any other flag will modify your files.
"@
))
}
if ([RGCompleter]::ShouldAdd("-S", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-S", "-S", [CompletionResultType]::ParameterName, @"
Searches case insensitively if the pattern is all lowercase. Search case sensitively
otherwise. This is overridden by either -s/--case-sensitive or -i/--ignore-case.
"@
))
}
if ([RGCompleter]::ShouldAdd("--sort-files", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--sort-files", "--sort-files", [CompletionResultType]::ParameterName, @"
Sort results by file path. Note that this currently disables all parallelism and runs
search in a single thread.
"@
))
}
if ([RGCompleter]::ShouldAdd("-a", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-a", "-a", [CompletionResultType]::ParameterName, 'Search binary files as if they were text.'))
}
if ([RGCompleter]::ShouldAdd("-j", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-j", "-j <ARG>", [CompletionResultType]::ParameterName, @"
The approximate number of threads to use. A value of 0 (which is the default) causes
ripgrep to choose the thread count using heuristics.
"@
))
}
if ([RGCompleter]::ShouldAdd("-t", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-t", "-t <TYPE>", [CompletionResultType]::ParameterName, @"
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
"@
))
}
if ([RGCompleter]::ShouldAdd("--type-add", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--type-add", "--type-add <TYPE>", [CompletionResultType]::ParameterName, @"
Add a new glob for a particular file type. Only one glob can be added at a time.
Multiple --type-add flags can be provided. Unless --type-clear is used, globs are added
to any existing globs defined inside of ripgrep.
"@
))
}
if ([RGCompleter]::ShouldAdd("--type-clear", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--type-clear", "--type-clear <TYPE>", [CompletionResultType]::ParameterName, @"
Clear the file type globs previously defined for TYPE. This only clears the default type
definitions that are found inside of ripgrep.
"@
))
}
if ([RGCompleter]::ShouldAdd("--type-list", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--type-list", "--type-list", [CompletionResultType]::ParameterName, 'Show all supported file types and their corresponding globs.'))
}
if ([RGCompleter]::ShouldAdd("-T", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-T", "-T <TYPE>", [CompletionResultType]::ParameterName, @"
Do not search files matching TYPE. Multiple type-not flags may be provided. Use the
--type-list flag to list all available types.
"@
))
}
if ([RGCompleter]::ShouldAdd("-u", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-u", "-u", [CompletionResultType]::ParameterName, @"
Reduce the level of "smart" searching. A single -u won''t respect .gitignore (etc.)
files. Two -u flags will additionally search hidden files and directories. Three -u
flags will additionally search binary files. -uu is roughly equivalent to grep -r and
-uuu is roughly equivalent to grep -a -r.
"@
))
}
if ([RGCompleter]::ShouldAdd("-V", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-V", "-V", [CompletionResultType]::ParameterName, 'Prints version information'))
}
if ([RGCompleter]::ShouldAdd("--vimgrep", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("--vimgrep", "--vimgrep", [CompletionResultType]::ParameterName, @"
Show results with every match on its own line, including line numbers and column
numbers. With this option, a line with more than one match will be printed more than
once.
"@
))
}
if ([RGCompleter]::ShouldAdd("-H", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-H", "-H", [CompletionResultType]::ParameterName, @"
Prefix each match with the file name that contains it. This is the default when more
than one file is searched.
"@
))
}
if ([RGCompleter]::ShouldAdd("-w", $wordToComplete, $prevOpts, $multiOpts)) {
$result.Add([CompletionResult]::new("-w", "-w", [CompletionResultType]::ParameterName, @"
Only show matches surrounded by word boundaries. This is equivalent to putting \b before
and after all of the search patterns.
"@
))
}
return $result
}
}
Register-ArgumentCompleter -native -CommandName 'rg.exe' -scriptblock {
param([string] $wordToComplete, [Language.CommandAst] $ast, [int] $cursorPosition)
return [RGCompleter]::CompleteInput($wordToComplete, $ast, $cursorPosition)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment