-
Notifications
You must be signed in to change notification settings - Fork 27
/
generate-patterns.nu
executable file
·80 lines (72 loc) · 2.19 KB
/
generate-patterns.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env nu
# builtin sort functions only sort alphabetically
def sort-by-length [
column: string
] {
insert length {get $column|str length}
|sort-by -r length
|reject length
}
# generate combined regex for all single word commands
def match-for-single [
commands:record
] {
'\b(' + ($commands|where ($it.subcommands|length) == 1|sort-by-length name|get name|str collect '|'|str replace -a -s '?' '\?') + ')\b'
}
# generate list of regexes for every two word command name
def match-for-double [
commands: record
] {
$commands
|where ($it.subcommands|length) > 0 and ($it.subcommands.second-word|all $it != '')|each {|x|
'\b' + $x.name + '(\s' + ($x.subcommands.second-word|compact|str collect '|\s') + ')\b'
}
}
# returns regexes for all commands, both single and double word single-word append is conditional because some letters only have two word commands e.g 'q'
def generate-matches [
category: record
] {
if ($category | get category) ends-with '_sub' {
match-for-double $category.commands
} else {
match-for-single $category.commands
}
}
let patterns = (
$nu.scope.commands
|where is_builtin == true and is_extern == false
|get name
|split column ' ' first-word second-word
|default '' second-word
|uniq
|upsert category {|x|
let first_letter = ($x.first-word|split chars|get 0)
if $x.second-word == '' {
$"($first_letter)"
} else {
$"($first_letter)_sub"
}
}
|group-by category
|transpose category commands
|upsert commands {
get commands
|group-by first-word
|transpose name subcommands
}
|reverse
|each {|category|
generate-matches $category
|each {|match|
{name: $"keyword.other.($category.category)", match: $match}
}
}
|flatten
)
open syntaxes/nushell.tmLanguage.json
|update repository.keywords.patterns $patterns
|save syntaxes/nushell.tmLanguage.json
# TODO: don't allow keywords that have their own definition to be included keyword.other.blah
# like:
# alias, let, let-env, for, def, def-env, extern, module, overlay
# probably some others should be added