Skip to content

Commit

Permalink
cli: Add space after file completions
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Nov 12, 2023
1 parent fbb8cf2 commit b4a510c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
26 changes: 12 additions & 14 deletions cli/HintHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> GetCompletions(
private IEnumerable<Completion> GetCompletions(
string completionTarget,
ShellStyleInvocationInfo? invocationInfo,
bool isTextArgument)
Expand All @@ -85,22 +89,16 @@ private IEnumerable<string> 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
? FileType.All
: FileType.Executable,
CompletionKind.Hint
);

return fileCompletions.Select(x => x.CompletionText);
}

public void Reset()
Expand Down
2 changes: 2 additions & 0 deletions readline/Completion.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
6 changes: 5 additions & 1 deletion readline/Render/CompletionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
10 changes: 8 additions & 2 deletions src/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ public static IList<Completion> 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())
Expand All @@ -146,7 +149,10 @@ public static IList<Completion> 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,
});
}
}

Expand Down

0 comments on commit b4a510c

Please sign in to comment.