| 1 |
package action |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"sort" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 9 |
"github.com/zyedidia/micro/v2/internal/config" |
| 10 |
"github.com/zyedidia/micro/v2/internal/util" |
| 11 |
"github.com/zyedidia/micro/v2/pkg/highlight" |
| 12 |
) |
| 13 |
|
| 14 |
// This file is meant (for now) for autocompletion in command mode, not |
| 15 |
// while coding. This helps micro autocomplete commands and then filenames |
| 16 |
// for example with `vsplit filename`. |
| 17 |
|
| 18 |
// CommandComplete autocompletes commands |
| 19 |
func CommandComplete(b *buffer.Buffer) ([]string, []string) { |
| 20 |
c := b.GetActiveCursor() |
| 21 |
input, argstart := b.GetArg() |
| 22 |
|
| 23 |
var suggestions []string |
| 24 |
for cmd := range commands { |
| 25 |
if strings.HasPrefix(cmd, input) { |
| 26 |
suggestions = append(suggestions, cmd) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
sort.Strings(suggestions) |
| 31 |
completions := make([]string, len(suggestions)) |
| 32 |
for i := range suggestions { |
| 33 |
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 34 |
} |
| 35 |
|
| 36 |
return completions, suggestions |
| 37 |
} |
| 38 |
|
| 39 |
// HelpComplete autocompletes help topics |
| 40 |
func HelpComplete(b *buffer.Buffer) ([]string, []string) { |
| 41 |
c := b.GetActiveCursor() |
| 42 |
input, argstart := b.GetArg() |
| 43 |
|
| 44 |
var suggestions []string |
| 45 |
|
| 46 |
for _, file := range config.ListRuntimeFiles(config.RTHelp) { |
| 47 |
topic := file.Name() |
| 48 |
if strings.HasPrefix(topic, input) { |
| 49 |
suggestions = append(suggestions, topic) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
sort.Strings(suggestions) |
| 54 |
completions := make([]string, len(suggestions)) |
| 55 |
for i := range suggestions { |
| 56 |
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 57 |
} |
| 58 |
return completions, suggestions |
| 59 |
} |
| 60 |
|
| 61 |
// colorschemeComplete tab-completes names of colorschemes. |
| 62 |
// This is just a heper value for OptionValueComplete |
| 63 |
func colorschemeComplete(input string) (string, []string) { |
| 64 |
var suggestions []string |
| 65 |
files := config.ListRuntimeFiles(config.RTColorscheme) |
| 66 |
|
| 67 |
for _, f := range files { |
| 68 |
if strings.HasPrefix(f.Name(), input) { |
| 69 |
suggestions = append(suggestions, f.Name()) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
var chosen string |
| 74 |
if len(suggestions) == 1 { |
| 75 |
chosen = suggestions[0] |
| 76 |
} |
| 77 |
|
| 78 |
return chosen, suggestions |
| 79 |
} |
| 80 |
|
| 81 |
// filetypeComplete autocompletes filetype |
| 82 |
func filetypeComplete(input string) (string, []string) { |
| 83 |
var suggestions []string |
| 84 |
|
| 85 |
// We cannot match filetypes just by names of syntax files, |
| 86 |
// since those names may be different from the actual filetype values |
| 87 |
// specified inside syntax files (e.g. "c++" filetype in cpp.yaml). |
| 88 |
// So we need to parse filetype values out of those files. |
| 89 |
for _, f := range config.ListRealRuntimeFiles(config.RTSyntax) { |
| 90 |
data, err := f.Data() |
| 91 |
if err != nil { |
| 92 |
continue |
| 93 |
} |
| 94 |
header, err := highlight.MakeHeaderYaml(data) |
| 95 |
if err != nil { |
| 96 |
continue |
| 97 |
} |
| 98 |
// Prevent duplicated defaults |
| 99 |
if header.FileType == "off" || header.FileType == "unknown" { |
| 100 |
continue |
| 101 |
} |
| 102 |
if strings.HasPrefix(header.FileType, input) { |
| 103 |
suggestions = append(suggestions, header.FileType) |
| 104 |
} |
| 105 |
} |
| 106 |
headerLoop: |
| 107 |
for _, f := range config.ListRuntimeFiles(config.RTSyntaxHeader) { |
| 108 |
data, err := f.Data() |
| 109 |
if err != nil { |
| 110 |
continue |
| 111 |
} |
| 112 |
header, err := highlight.MakeHeader(data) |
| 113 |
if err != nil { |
| 114 |
continue |
| 115 |
} |
| 116 |
for _, v := range suggestions { |
| 117 |
if v == header.FileType { |
| 118 |
continue headerLoop |
| 119 |
} |
| 120 |
} |
| 121 |
if strings.HasPrefix(header.FileType, input) { |
| 122 |
suggestions = append(suggestions, header.FileType) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if strings.HasPrefix("off", input) { |
| 127 |
suggestions = append(suggestions, "off") |
| 128 |
} |
| 129 |
|
| 130 |
var chosen string |
| 131 |
if len(suggestions) == 1 { |
| 132 |
chosen = suggestions[0] |
| 133 |
} |
| 134 |
|
| 135 |
return chosen, suggestions |
| 136 |
} |
| 137 |
|
| 138 |
// OptionComplete autocompletes options |
| 139 |
func OptionComplete(b *buffer.Buffer) ([]string, []string) { |
| 140 |
c := b.GetActiveCursor() |
| 141 |
input, argstart := b.GetArg() |
| 142 |
|
| 143 |
var suggestions []string |
| 144 |
for option := range config.GlobalSettings { |
| 145 |
if strings.HasPrefix(option, input) { |
| 146 |
suggestions = append(suggestions, option) |
| 147 |
} |
| 148 |
} |
| 149 |
// for option := range localSettings { |
| 150 |
// if strings.HasPrefix(option, input) && !contains(suggestions, option) { |
| 151 |
// suggestions = append(suggestions, option) |
| 152 |
// } |
| 153 |
// } |
| 154 |
|
| 155 |
sort.Strings(suggestions) |
| 156 |
completions := make([]string, len(suggestions)) |
| 157 |
for i := range suggestions { |
| 158 |
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 159 |
} |
| 160 |
return completions, suggestions |
| 161 |
} |
| 162 |
|
| 163 |
// OptionValueComplete completes values for various options |
| 164 |
func OptionValueComplete(b *buffer.Buffer) ([]string, []string) { |
| 165 |
c := b.GetActiveCursor() |
| 166 |
l := b.LineBytes(c.Y) |
| 167 |
l = util.SliceStart(l, c.X) |
| 168 |
input, argstart := b.GetArg() |
| 169 |
|
| 170 |
completeValue := false |
| 171 |
args := bytes.Split(l, []byte{' '}) |
| 172 |
if len(args) >= 2 { |
| 173 |
// localSettings := config.DefaultLocalSettings() |
| 174 |
for option := range config.GlobalSettings { |
| 175 |
if option == string(args[len(args)-2]) { |
| 176 |
completeValue = true |
| 177 |
break |
| 178 |
} |
| 179 |
} |
| 180 |
// for option := range localSettings { |
| 181 |
// if option == string(args[len(args)-2]) { |
| 182 |
// completeValue = true |
| 183 |
// break |
| 184 |
// } |
| 185 |
// } |
| 186 |
} |
| 187 |
if !completeValue { |
| 188 |
return OptionComplete(b) |
| 189 |
} |
| 190 |
|
| 191 |
inputOpt := string(args[len(args)-2]) |
| 192 |
|
| 193 |
inputOpt = strings.TrimSpace(inputOpt) |
| 194 |
var suggestions []string |
| 195 |
// localSettings := config.DefaultLocalSettings() |
| 196 |
var optionVal any |
| 197 |
for k, option := range config.GlobalSettings { |
| 198 |
if k == inputOpt { |
| 199 |
optionVal = option |
| 200 |
} |
| 201 |
} |
| 202 |
// for k, option := range localSettings { |
| 203 |
// if k == inputOpt { |
| 204 |
// optionVal = option |
| 205 |
// } |
| 206 |
// } |
| 207 |
|
| 208 |
switch optionVal.(type) { |
| 209 |
case bool: |
| 210 |
if strings.HasPrefix("on", input) { |
| 211 |
suggestions = append(suggestions, "on") |
| 212 |
} else if strings.HasPrefix("true", input) { |
| 213 |
suggestions = append(suggestions, "true") |
| 214 |
} |
| 215 |
if strings.HasPrefix("off", input) { |
| 216 |
suggestions = append(suggestions, "off") |
| 217 |
} else if strings.HasPrefix("false", input) { |
| 218 |
suggestions = append(suggestions, "false") |
| 219 |
} |
| 220 |
case string: |
| 221 |
switch inputOpt { |
| 222 |
case "colorscheme": |
| 223 |
_, suggestions = colorschemeComplete(input) |
| 224 |
case "filetype": |
| 225 |
_, suggestions = filetypeComplete(input) |
| 226 |
case "sucmd": |
| 227 |
if strings.HasPrefix("sudo", input) { |
| 228 |
suggestions = append(suggestions, "sudo") |
| 229 |
} |
| 230 |
if strings.HasPrefix("doas", input) { |
| 231 |
suggestions = append(suggestions, "doas") |
| 232 |
} |
| 233 |
default: |
| 234 |
if choices, ok := config.OptionChoices[inputOpt]; ok { |
| 235 |
for _, choice := range choices { |
| 236 |
if strings.HasPrefix(choice, input) { |
| 237 |
suggestions = append(suggestions, choice) |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
sort.Strings(suggestions) |
| 244 |
|
| 245 |
completions := make([]string, len(suggestions)) |
| 246 |
for i := range suggestions { |
| 247 |
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 248 |
} |
| 249 |
return completions, suggestions |
| 250 |
} |
| 251 |
|
| 252 |
// PluginCmdComplete autocompletes the plugin command |
| 253 |
func PluginCmdComplete(b *buffer.Buffer) ([]string, []string) { |
| 254 |
c := b.GetActiveCursor() |
| 255 |
input, argstart := b.GetArg() |
| 256 |
|
| 257 |
var suggestions []string |
| 258 |
for _, cmd := range PluginCmds { |
| 259 |
if strings.HasPrefix(cmd, input) { |
| 260 |
suggestions = append(suggestions, cmd) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
sort.Strings(suggestions) |
| 265 |
completions := make([]string, len(suggestions)) |
| 266 |
for i := range suggestions { |
| 267 |
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 268 |
} |
| 269 |
return completions, suggestions |
| 270 |
} |
| 271 |
|
| 272 |
// PluginComplete completes values for the plugin command |
| 273 |
func PluginComplete(b *buffer.Buffer) ([]string, []string) { |
| 274 |
c := b.GetActiveCursor() |
| 275 |
l := b.LineBytes(c.Y) |
| 276 |
l = util.SliceStart(l, c.X) |
| 277 |
input, argstart := b.GetArg() |
| 278 |
|
| 279 |
completeValue := false |
| 280 |
args := bytes.Split(l, []byte{' '}) |
| 281 |
if len(args) >= 2 { |
| 282 |
for _, cmd := range PluginCmds { |
| 283 |
if cmd == string(args[len(args)-2]) { |
| 284 |
completeValue = true |
| 285 |
break |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
if !completeValue { |
| 290 |
return PluginCmdComplete(b) |
| 291 |
} |
| 292 |
|
| 293 |
var suggestions []string |
| 294 |
for _, pl := range config.Plugins { |
| 295 |
if strings.HasPrefix(pl.Name, input) { |
| 296 |
suggestions = append(suggestions, pl.Name) |
| 297 |
} |
| 298 |
} |
| 299 |
sort.Strings(suggestions) |
| 300 |
|
| 301 |
completions := make([]string, len(suggestions)) |
| 302 |
for i := range suggestions { |
| 303 |
completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 304 |
} |
| 305 |
return completions, suggestions |
| 306 |
} |
| 307 |
|
| 308 |
// PluginNameComplete completes with the names of loaded plugins |
| 309 |
// func PluginNameComplete(b *buffer.Buffer) ([]string, []string) { |
| 310 |
// c := b.GetActiveCursor() |
| 311 |
// input, argstart := buffer.GetArg(b) |
| 312 |
// |
| 313 |
// var suggestions []string |
| 314 |
// for _, pp := range config.GetAllPluginPackages(nil) { |
| 315 |
// if strings.HasPrefix(pp.Name, input) { |
| 316 |
// suggestions = append(suggestions, pp.Name) |
| 317 |
// } |
| 318 |
// } |
| 319 |
// |
| 320 |
// sort.Strings(suggestions) |
| 321 |
// completions := make([]string, len(suggestions)) |
| 322 |
// for i := range suggestions { |
| 323 |
// completions[i] = util.SliceEndStr(suggestions[i], c.X-argstart) |
| 324 |
// } |
| 325 |
// return completions, suggestions |
| 326 |
// } |
| 327 |
|
| 328 |
// // MakeCompletion registers a function from a plugin for autocomplete commands |
| 329 |
// func MakeCompletion(function string) Completion { |
| 330 |
// pluginCompletions = append(pluginCompletions, LuaFunctionComplete(function)) |
| 331 |
// return Completion(-len(pluginCompletions)) |
| 332 |
// } |