Skip to content

Commit

Permalink
cli: Hints for custom completions (if enabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Nov 12, 2023
1 parent 2e5e762 commit 6db9516
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cli/HighlightHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private string NextPath()

return (
Color(string.Concat(highlightedTextArguments), 36),
new(textArguments, caretAtArgumentIndex)
new TextArgumentsInfo(textArguments, caretAtArgumentIndex)
);
}

Expand Down
6 changes: 5 additions & 1 deletion cli/HintHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ private string GetFileHint()
return "";

var completionTarget = activeTextArgument ?? invocationInfo.Name;
// For some reason TextArgumentsInfo.Arguments contains elements for the spaces
// that separate the arguments. This should probably be fixed at some point.
var completions = GetCompletions(
string.Concat(invocationInfo.TextArgumentsInfo.Arguments).TrimStart(),
completionTarget,
invocationInfo,
isTextArgument: activeTextArgument != null
Expand All @@ -81,6 +84,7 @@ private string GetFileHint()
}

private IEnumerable<Completion> GetCompletions(
string allTextArguments,
string completionTarget,
ShellStyleInvocationInfo? invocationInfo,
bool isTextArgument)
Expand All @@ -89,7 +93,7 @@ private IEnumerable<Completion> GetCompletions(
? null
: _customCompletionProvider.Get(invocationInfo.Name);
if (completionParser != null)
return completionParser.GetCompletions(completionTarget, null, CompletionKind.Hint);
return completionParser.GetCompletions(allTextArguments, null, CompletionKind.Hint);

return FileUtils.GetPathCompletions(
Utils.Unescape(completionTarget),
Expand Down
12 changes: 10 additions & 2 deletions src/Resources/completions/git.elk
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,20 @@ fn handleHelp(parser) {

fn handleAdd(parser) {
parser
| cli::addArgument({ "identifier": "file", "completionHandler": &unstagedFilesHandler, "variadic": true })
| cli::addArgument({
"identifier": "file",
"completionHandler": &unstagedFilesHandler,
"allowCustomCompletionHints": true,
"variadic": true
})
}

fn handleBranch(parser) {
parser
| cli::addArgument({ "completionHandler": &branchHandler })
| cli::addArgument({
"completionHandler": &branchHandler,
"allowCustomCompletionHints": true,
})
| cli::addFlag({ "short": "u", "valueKind": "text" })
| cli::addFlag({ "long": "unset-upstream" })
| cli::addFlag({ "short": "d" })
Expand Down
2 changes: 2 additions & 0 deletions src/Std/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static RuntimeObject AddFlag(RuntimeCliParser parser, RuntimeDictionary f
_ => CliValueKind.None,
},
IsRequired = flag.GetValue<RuntimeBoolean>("required")?.IsTrue ?? false,
AllowCustomCompletionHints = flag.GetValue<RuntimeBoolean>("allowCustomCompletionHints")?.IsTrue ?? false,
CompletionHandler = completionHandler,
});

Expand Down Expand Up @@ -160,6 +161,7 @@ public static RuntimeObject AddArgument(RuntimeCliParser parser, RuntimeDictiona
_ => CliValueKind.None,
},
IsVariadic = argument.GetValue<RuntimeBoolean>("variadic")?.IsTrue ?? false,
AllowCustomCompletionHints = argument.GetValue<RuntimeBoolean>("allowCustomCompletionHints")?.IsTrue ?? false,
CompletionHandler = completionHandler,
};
parser.AddArgument(typedArgument);
Expand Down
2 changes: 2 additions & 0 deletions src/Std/DataTypes/Serialization/CommandLine/CliArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class CliArgument

public bool IsVariadic { get; init; }

public bool AllowCustomCompletionHints { get; init; }

public Func<string, CliResult, IEnumerable<Completion>>? CompletionHandler { get; init; }
}
2 changes: 2 additions & 0 deletions src/Std/DataTypes/Serialization/CommandLine/CliFlag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public class CliFlag

public bool IsRequired { get; init; }

public bool AllowCustomCompletionHints { get; init; }

public Func<string, CliResult, IEnumerable<Completion>>? CompletionHandler { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private IEnumerable<Completion> GetCompletions(
: x.completion
);
var collectedTokens = tokens.ToList();
if (collectedTokens.Count == 1 && _verbs.Any() && !last.StartsWith("-"))
if (collectedTokens.Count == 1 && _verbs.Any() && !last.StartsWith('-'))
{
// Also include matchedFlags, in case the token is empty,
// which would mean it could either be a verb or a flag.
Expand All @@ -226,7 +226,8 @@ private IEnumerable<Completion> GetCompletions(
flag = _flags.FirstOrDefault(x => x.ShortName == secondLast[1..]);
}

if (completionKind != CompletionKind.Hint && flag?.CompletionHandler != null && cliResult != null)
var allowCustom = completionKind != CompletionKind.Hint || flag?.AllowCustomCompletionHints is true;
if (allowCustom && flag?.CompletionHandler != null && cliResult != null)
return flag.CompletionHandler(last, cliResult);

if (flag is { ValueKind: CliValueKind.Path or CliValueKind.Directory })
Expand Down Expand Up @@ -254,7 +255,8 @@ private IEnumerable<Completion> GetCompletions(
var currentArgument = lastIsArgument
? _arguments.ElementAtOrDefault(argumentCount - 1) ?? _arguments.Last()
: null;
if (completionKind != CompletionKind.Hint && currentArgument?.CompletionHandler != null && cliResult != null)
var allowCustomCompletion = completionKind != CompletionKind.Hint || currentArgument?.AllowCustomCompletionHints is true;
if (allowCustomCompletion && currentArgument?.CompletionHandler != null && cliResult != null)
return currentArgument.CompletionHandler(last, cliResult);

if (currentArgument is { ValueKind: CliValueKind.Path or CliValueKind.Directory })
Expand Down

0 comments on commit 6db9516

Please sign in to comment.