From b4a510c75a895b8c0c8ca070c22dcfb542c28b77 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Sun, 12 Nov 2023 22:56:04 +0100 Subject: [PATCH] cli: Add space after file completions --- cli/HintHandler.cs | 26 ++++++++++++-------------- readline/Completion.cs | 2 ++ readline/Render/CompletionState.cs | 6 +++++- src/FileUtils.cs | 10 ++++++++-- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/cli/HintHandler.cs b/cli/HintHandler.cs index 25dc12cc..8ef5a276 100644 --- a/cli/HintHandler.cs +++ b/cli/HintHandler.cs @@ -60,23 +60,27 @@ private string GetFileHint() invocationInfo, isTextArgument: activeTextArgument != null ); - var fullPathCompletion = completions.FirstOrDefault(); - if (fullPathCompletion == null) + var completion = completions.FirstOrDefault(); + if (completion == null) return ""; var completionStart = completionTarget.LastIndexOf('/') + 1; var fileNameLength = completionTarget.Length - completionStart; - if (fileNameLength >= fullPathCompletion.Length) + if (fileNameLength >= completion.CompletionText.Length) { - Debug.Assert(fileNameLength == fullPathCompletion.Length); + Debug.Assert(fileNameLength == completion.CompletionText.Length); return ""; } - return Utils.Escape(fullPathCompletion[fileNameLength..]); + var trailingSpace = completion.HasTrailingSpace + ? " " + : ""; + + return Utils.Escape(completion.CompletionText[fileNameLength..]) + trailingSpace; } - private IEnumerable GetCompletions( + private IEnumerable GetCompletions( string completionTarget, ShellStyleInvocationInfo? invocationInfo, bool isTextArgument) @@ -85,13 +89,9 @@ private IEnumerable GetCompletions( ? null : _customCompletionProvider.Get(invocationInfo.Name); if (completionParser != null) - { - return completionParser - .GetCompletions(completionTarget, null, CompletionKind.Hint) - .Select(x => x.CompletionText); - } + return completionParser.GetCompletions(completionTarget, null, CompletionKind.Hint); - var fileCompletions = FileUtils.GetPathCompletions( + return FileUtils.GetPathCompletions( Utils.Unescape(completionTarget), _shell.WorkingDirectory, isTextArgument @@ -99,8 +99,6 @@ private IEnumerable GetCompletions( : FileType.Executable, CompletionKind.Hint ); - - return fileCompletions.Select(x => x.CompletionText); } public void Reset() diff --git a/readline/Completion.cs b/readline/Completion.cs index 926ca8a0..e24b27a3 100644 --- a/readline/Completion.cs +++ b/readline/Completion.cs @@ -1,6 +1,8 @@ namespace Elk.ReadLine; public record Completion(string CompletionText, string DisplayText, string? Description = null) { + public bool HasTrailingSpace { get; init; } + public Completion(string completionText) : this(completionText, completionText) { diff --git a/readline/Render/CompletionState.cs b/readline/Render/CompletionState.cs index 321f4a75..f885674a 100644 --- a/readline/Render/CompletionState.cs +++ b/readline/Render/CompletionState.cs @@ -91,8 +91,12 @@ public void Previous() private void InsertCompletion() { _renderer.RemoveLeft(_renderer.Caret - _completionStart, render: false); + var completion = _completions[_listing.SelectedIndex]; + var trailingSpace = completion.HasTrailingSpace + ? " " + : ""; _renderer.Insert( - _completions[_listing.SelectedIndex].CompletionText, + completion.CompletionText + trailingSpace, includeHint: false ); } diff --git a/src/FileUtils.cs b/src/FileUtils.cs index a9375015..6e5ef1d0 100644 --- a/src/FileUtils.cs +++ b/src/FileUtils.cs @@ -126,7 +126,10 @@ public static IList GetPathCompletions( .Where(x => x.name.StartsWith(completionTarget)) .Where(x => fileType != FileType.Executable || FileIsExecutable(x.path)) .Order() - .Select(x => new Completion(x.name)); + .Select(x => new Completion(x.name) + { + HasTrailingSpace = true, + }); } if (completionKind != CompletionKind.Hint && !directories.Any() && !files.Any()) @@ -146,7 +149,10 @@ public static IList GetPathCompletions( .Where(x => x.name.Contains(completionTarget, comparison)) .Where(x => fileType != FileType.Executable || FileIsExecutable(x.path)) .Order() - .Select(x => new Completion(x.name)); + .Select(x => new Completion(x.name) + { + HasTrailingSpace = true, + }); } }