Skip to content

Commit

Permalink
Merge pull request #213 from kindermannhubert/auto-format
Browse files Browse the repository at this point in the history
Support for automatic formatting
  • Loading branch information
kindermannhubert committed Aug 3, 2022
2 parents 98bc412 + b795980 commit 4f4dc87
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/PrettyPrompt/Documents/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public void Clear(CodePane codePane)
}
}

public void SetContents(CodePane codePane, string contents)
public void SetContents(CodePane codePane, string contents, int caret)
{
using (BeginChanges(codePane))
{
stringBuilder.SetContents(contents);
stringBuilder.SetContents(contents, caret);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/PrettyPrompt/Documents/StringBuilderWithCaret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public void Clear()
}
}

public void SetContents(string contents)
public void SetContents(string contents, int? caret = null)
{
sb.SetContents(contents);
Caret = sb.Length;
Caret = caret ?? sb.Length;
InvokeChangedEvent();
}

Expand Down
2 changes: 1 addition & 1 deletion src/PrettyPrompt/History/HistoryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private static void SetContents(CodePane codepane, string contents)
{
if (codepane.Document.Equals(contents)) return;

codepane.Document.SetContents(codepane, contents);
codepane.Document.SetContents(codepane, contents, caret: contents.Length);
}

internal void Track(CodePane codePane)
Expand Down
2 changes: 1 addition & 1 deletion src/PrettyPrompt/Panes/OverloadPane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ private void UpdateSelectedItem()
signatureLines = WordWrapping.WrapWords(selectedOverload.Signature, maxLength: availableWidth, maxLines: dedicatedLines[0]);
summaryLines = WordWrapping.WrapWords(selectedOverload.Summary, maxLength: availableWidth, maxLines: dedicatedLines[1]);

var paramIndex = selectedArgumentIndex.Clamp(0, selectedOverload.Parameters.Count - 1);
if (selectedOverload.Parameters.Count > 0)
{
var paramIndex = selectedArgumentIndex.Clamp(0, selectedOverload.Parameters.Count - 1);
var param = selectedOverload.Parameters[paramIndex];
var name = new FormattedString(param.Name, new ConsoleFormat(Bold: true));
paramDescriptionLines = WordWrapping.WrapWords(spacesUnderCounter + name + ": " + param.Description, maxLength: availableWidth, maxLines: dedicatedLines[2]);
Expand Down
2 changes: 1 addition & 1 deletion src/PrettyPrompt/PrettyPrompt.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>4.0.0-beta5</Version>
<Version>4.0.0-beta6</Version>

<PackageId>PrettyPrompt</PackageId>
<PackageTags>repl readline console cli</PackageTags>
Expand Down
23 changes: 20 additions & 3 deletions src/PrettyPrompt/Prompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public Prompt(
public async Task<PromptResult> ReadLineAsync()
{
using var renderer = new Renderer(console, configuration);

// code pane contains the code the user is typing. It does not include the prompt (i.e. "> ")
var codePane = new CodePane(console, configuration, clipboard);

renderer.RenderPrompt(codePane);

var overloadPane = new OverloadPane(
Expand Down Expand Up @@ -114,7 +114,7 @@ public async Task<PromptResult> ReadLineAsync()

Debug.Assert(false, "Should never reach here due to infinite " + nameof(KeyPress.ReadForever));
return null;

async Task InterpretKeyPress(KeyPress key, CancellationToken cancellationToken)
{
if (!completionPane.WouldKeyPressCommitCompletionItem(key))
Expand All @@ -128,6 +128,23 @@ async Task InterpretKeyPress(KeyPress key, CancellationToken cancellationToken)
foreach (var panes in new IKeyPressHandler[] { completionPane, overloadPane, history, codePane })
await panes.OnKeyUp(key, cancellationToken).ConfigureAwait(false);

//Possible automatic formatting of the document;
var text = codePane.Document.GetText();
var (formattedText, newCaret) = await promptCallbacks.FormatInput(text, codePane.Document.Caret, key, cancellationToken).ConfigureAwait(false);
if (text != formattedText)
{
int removedChars = 0;
for (int i = 0; i < newCaret; i++)
{
if (formattedText[i] == '\r') ++removedChars;
}
codePane.Document.SetContents(codePane, formattedText.Replace("\r\n", "\n"), newCaret - removedChars);
}
else
{
Debug.Assert(codePane.Document.Caret == newCaret);
}

//we don't support text selection while completion list is open
//text selection can put completion list into broken state, where filtering does not work
//so we want this assert to be true
Expand Down
21 changes: 21 additions & 0 deletions src/PrettyPrompt/PromptCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public interface IPromptCallbacks
/// <param name="cancellationToken">Cancellation token</param>
/// <returns><see langword="true"/> if the completion item should be submitted or <see langword="false"/> otherwise.</returns>
Task<bool> ConfirmCompletionCommit(string text, int caret, KeyPress keyPress, CancellationToken cancellationToken);

/// <summary>
/// Provides way to automatically format input text.
/// </summary>
/// <param name="text">The user's input text</param>
/// <param name="caret">The index of the text caret in the input text</param>
/// <param name="keyPress">Key press pattern in question</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Formatted input and new caret position.</returns>
Task<(string Text, int Caret)> FormatInput(string text, int caret, KeyPress keyPress, CancellationToken cancellationToken);
}

public class PromptCallbacks : IPromptCallbacks
Expand Down Expand Up @@ -188,6 +198,13 @@ Task<bool> IPromptCallbacks.ConfirmCompletionCommit(string text, int caret, KeyP
return ConfirmCompletionCommit(text, caret, keyPress, cancellationToken);
}

Task<(string Text, int Caret)> IPromptCallbacks.FormatInput(string text, int caret, KeyPress keyPress, CancellationToken cancellationToken)
{
Debug.Assert(caret >= 0 && caret <= text.Length);

return FormatInput(text, caret, keyPress, cancellationToken);
}

Task<(IReadOnlyList<OverloadItem>, int ArgumentIndex)> IPromptCallbacks.GetOverloadsAsync(string text, int caret, CancellationToken cancellationToken)
{
Debug.Assert(caret >= 0 && caret <= text.Length);
Expand Down Expand Up @@ -275,6 +292,10 @@ protected virtual Task<KeyPress> TransformKeyPressAsync(string text, int caret,
protected virtual Task<bool> ConfirmCompletionCommit(string text, int caret, KeyPress keyPress, CancellationToken cancellationToken)
=> Task.FromResult(true);

/// <inheritdoc cref="IPromptCallbacks.FormatInput(string, int, KeyPress, CancellationToken)"/>
protected virtual Task<(string Text, int Caret)> FormatInput(string text, int caret, KeyPress keyPress, CancellationToken cancellationToken)
=> Task.FromResult((text, caret));

/// <inheritdoc cref="GetOverloadsAsync(string, int, CancellationToken)"/>
protected virtual Task<(IReadOnlyList<OverloadItem>, int ArgumentIndex)> GetOverloadsAsync(string text, int caret, CancellationToken cancellationToken)
=> Task.FromResult<(IReadOnlyList<OverloadItem>, int ArgumentIndex)>((Array.Empty<OverloadItem>(), 0));
Expand Down

0 comments on commit 4f4dc87

Please sign in to comment.