Skip to content

Commit

Permalink
fix(Studio): Few issues with new incremental auto-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Sep 7, 2024
1 parent 2b3c50a commit eb09f74
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static void Assert(CommandLine commandLine, int studioLine, string fileP
} else {
var condition = Enum.Parse<AssertCondition>(args[0], ignoreCase: true); // Must succeed, since this was checked in Parse
string expected = args[1];
string actualTemplate = commandLine.OriginalText[commandLine.Regions[3].StartIdx..];
string actualTemplate = commandLine.OriginalText[commandLine.Regions[3].StartCol..];

Running = true;
string actual = InfoCustom.ParseTemplate(actualTemplate, 0, [], false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private static void Console(CommandLine commandLine, int studioLine, string file
LoadCommand(commandName, commandArgs, slot);
} else {
// C# Debug Console uses the history to parse the arguments itself, so we have to temporarily insert it there
Engine.Commands.commandHistory.Insert(0, commandLine.OriginalText[commandLine.Regions[1].StartIdx..]);
Engine.Commands.commandHistory.Insert(0, commandLine.OriginalText[commandLine.Regions[1].StartCol..]);
Engine.Commands.ExecuteCommand(commandName, commandArgs);
Engine.Commands.commandHistory.RemoveAt(0);
}
Expand Down
2 changes: 0 additions & 2 deletions CelesteTAS-EverestInterop/TAS/Input/Commands/PressCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public async IAsyncEnumerator<CommandAutoCompleteEntry> GetAutoCompleteEntries(s
Extra = "Keys",
IsDone = true
};

await Task.Delay(TimeSpan.FromSeconds(0.1f)).ConfigureAwait(false);
}
}
}
Expand Down
121 changes: 15 additions & 106 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,22 +1301,17 @@ private void UpdateAutoComplete(bool open = true) {
}

// Use auto-complete entries for current command

// Split by the first separator
var commandLine = CommandLine.Parse(line);
var fullCommandLine = CommandLine.Parse(Document.Lines[Document.Caret.Row]);

var separatorMatch = CommandLine.SeparatorRegex.Match(line);
var args = line.Split(separatorMatch.Value);
var allArgs = Document.Lines[Document.Caret.Row].Split(separatorMatch.Value);

if (commandLine == null || commandLine.Value.Arguments.Length == 0 ||
fullCommandLine == null || fullCommandLine.Value.Arguments.Length == 0)
{
autoCompleteMenu.Entries = baseAutoCompleteEntries;
autoCompleteMenu.Entries.Clear();
autoCompleteMenu.Entries.AddRange(baseAutoCompleteEntries);
autoCompleteMenu.Filter = line;
} else {
var command = CommunicationWrapper.Commands.FirstOrDefault(cmd => string.Equals(cmd.Name, args[0], StringComparison.OrdinalIgnoreCase));
var command = CommunicationWrapper.Commands.FirstOrDefault(cmd => string.Equals(cmd.Name, commandLine.Value.Command, StringComparison.OrdinalIgnoreCase));
var commandArgs = commandLine.Value.Arguments[..^1];
Console.WriteLine($"Args {string.Join('+', commandArgs)}");

Expand All @@ -1331,7 +1326,7 @@ private void UpdateAutoComplete(bool open = true) {
autoCompleteMenu.Entries.Add(loadingEntry);

if (!string.IsNullOrEmpty(command.Name)) {
int lastArgStart = commandLine.Value.Regions[^1].StartIdx;
var lastArgRegion = commandLine.Value.Regions[^1];

commandAutoCompleteTokenSource?.Cancel();
commandAutoCompleteTokenSource?.Dispose();
Expand Down Expand Up @@ -1363,12 +1358,7 @@ await Application.Instance.InvokeAsync(() => {
DisplayText = entry.Name,
ExtraText = entry.Extra,
OnUse = () => {
var insert = entry.FullName;

string commandText = Document.Lines[Document.Caret.Row][..commandLine.Value.Regions[^1].EndIdx];
if (commandLine.Value.Arguments.Length != fullCommandLine.Value.Arguments.Length) {
commandText += separatorMatch.Value;
}
string insert = entry.FullName;

var selectedQuickEdit = GetQuickEdits()
.FirstOrDefault(anchor => Document.Caret.Row == anchor.Row &&
Expand Down Expand Up @@ -1411,21 +1401,21 @@ await Application.Instance.InvokeAsync(() => {
}
} else {
if (!entry.IsDone) {
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgStart, commandText.Length, insert);
Document.Caret.Col = desiredVisualCol = lastArgStart + insert.Length;
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgRegion.StartCol, lastArgRegion.EndCol, insert);
Document.Caret.Col = desiredVisualCol = lastArgRegion.StartCol + insert.Length;
Document.Selection.Clear();

UpdateAutoComplete();
} else if (entry.HasNext ?? false/*command.Value.AutoCompleteEntries.Length != allArgs.Length - 1*/) {
// Include separator for next argument
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgStart, commandText.Length, insert + separatorMatch.Value);
Document.Caret.Col = desiredVisualCol = lastArgStart + insert.Length + separatorMatch.Value.Length;
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgRegion.StartCol, lastArgRegion.EndCol, insert + commandLine.Value.ArgumentSeparator);
Document.Caret.Col = desiredVisualCol = lastArgRegion.StartCol + insert.Length + commandLine.Value.ArgumentSeparator.Length;
Document.Selection.Clear();

UpdateAutoComplete();
} else {
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgStart, commandText.Length, insert);
Document.Caret.Col = desiredVisualCol = lastArgStart + insert.Length;
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgRegion.StartCol, lastArgRegion.EndCol, insert);
Document.Caret.Col = desiredVisualCol = lastArgRegion.StartCol + insert.Length;
Document.Selection.Clear();

CloseAutoCompletePopup();
Expand Down Expand Up @@ -1453,97 +1443,16 @@ await Application.Instance.InvokeAsync(() => {
await Task.Delay(TimeSpan.FromSeconds(0.25f), token).ConfigureAwait(false);
}
}, token);
}

/*
if (!string.IsNullOrEmpty(command.Name) && command.Value.AutoCompleteEntries.Length >= commandArgs.Length) {
int lastArgStart = line.LastIndexOf(args[^1], StringComparison.Ordinal);
var entries = command.Value.AutoCompleteEntries[commandArgs.Length - 1](commandArgs);
autoCompleteMenu.Entries = entries.Select(entry => new PopupMenu.Entry {
SearchText = entry.FullName,
DisplayText = entry.Name,
ExtraText = entry.Extra,
OnUse = () => {
var insert = entry.FullName;
var commandLine = Document.Lines[Document.Caret.Row][..(lastArgStart + args[^1].Length)];
if (allArgs.Length != args.Length) {
commandLine += separatorMatch.Value;
}
var selectedQuickEdit = GetQuickEdits()
.FirstOrDefault(anchor => Document.Caret.Row == anchor.Row &&
Document.Caret.Col >= anchor.MinCol &&
Document.Caret.Col <= anchor.MaxCol);
// Jump to the next parameter and open the auto-complete menu if applicable
if (selectedQuickEdit != null) {
// Replace the current quick-edit instead
Document.ReplaceRangeInLine(selectedQuickEdit.Row, selectedQuickEdit.MinCol, selectedQuickEdit.MaxCol, insert);
if (entry.IsDone) {
var quickEdits = GetQuickEdits().ToArray();
bool lastQuickEditSelected = quickEdits.Length != 0 &&
quickEdits[^1].Row == Document.Caret.Row &&
quickEdits[^1].MinCol <= Document.Caret.Col &&
quickEdits[^1].MaxCol >= Document.Caret.Col;
if (lastQuickEditSelected) {
ClearQuickEdits();
Document.Selection.Clear();
Document.Caret.Col = Document.Lines[Document.Caret.Row].Length;
CloseAutoCompletePopup();
} else {
SelectNextQuickEdit();
// Don't start a new base auto-complete. Only arguments
if (!string.IsNullOrWhiteSpace(Document.Lines[Document.Caret.Row])) {
UpdateAutoComplete();
} else {
CloseAutoCompletePopup();
}
}
} else {
Document.Selection.Clear();
Document.Caret.Col = selectedQuickEdit.MinCol + insert.Length;
UpdateAutoComplete();
}
} else {
if (!entry.IsDone) {
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgStart, commandLine.Length, insert);
Document.Caret.Col = desiredVisualCol = lastArgStart + insert.Length;
Document.Selection.Clear();
UpdateAutoComplete();
} else if (entry.HasNext ?? command.Value.AutoCompleteEntries.Length != allArgs.Length - 1) {
// Include separator for next argument
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgStart, commandLine.Length, insert + separatorMatch.Value);
Document.Caret.Col = desiredVisualCol = lastArgStart + insert.Length + separatorMatch.Value.Length;
Document.Selection.Clear();
UpdateAutoComplete();
} else {
Document.ReplaceRangeInLine(Document.Caret.Row, lastArgStart, commandLine.Length, insert);
Document.Caret.Col = desiredVisualCol = lastArgStart + insert.Length;
Document.Selection.Clear();
CloseAutoCompletePopup();
}
}
},
}).ToList();
} else {
autoCompleteMenu.Entries = [];
autoCompleteMenu.Entries.Clear();
autoCompleteMenu.Recalc();
}
*/

if (GetSelectedQuickEdit() is { } quickEdit && args[^1] == quickEdit.DefaultText) {
if (GetSelectedQuickEdit() is { } quickEdit && commandLine.Value.Arguments[^1] == quickEdit.DefaultText) {
// Display all entries when quick-edit still contains the default
autoCompleteMenu.Filter = string.Empty;
} else {
autoCompleteMenu.Filter = args[^1];
autoCompleteMenu.Filter = commandLine.Value.Arguments[^1];
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions StudioCommunication/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public readonly record struct CommandLine(
string OriginalText,
string ArgumentSeparator
) {
public readonly record struct ArgumentRegion(int StartIdx, int EndIdx);
public readonly record struct ArgumentRegion(int StartCol, int EndCol);

// Matches against command or space or both as a separator
public static readonly Regex SeparatorRegex = new(@"(?:\s+)|(?:\s*,\s*)", RegexOptions.Compiled);
Expand All @@ -34,8 +34,8 @@ public static bool TryParse(string line, out CommandLine commandLine) {

var regions = new ArgumentRegion[split.Length];
for (int i = 0, idx = 0; i < split.Length; i++) {
regions[i] = new ArgumentRegion(idx, idx + split[i].Length - 1);
idx += split[i].Length + separatorMatch.Length - 1;
regions[i] = new ArgumentRegion(idx, idx + split[i].Length);
idx += split[i].Length + separatorMatch.Length;
}

commandLine = new CommandLine {
Expand Down

0 comments on commit eb09f74

Please sign in to comment.