Skip to content

Commit a3144f7

Browse files
committed
Added real-time parameter information when writing out a command.
1 parent e5ac131 commit a3144f7

File tree

2 files changed

+73
-11
lines changed

2 files changed

+73
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- Added real-time parameter information when typing out a command.
89
- Added commands for executing C# expressions or statements at runtime.
910
- Added exception handling for command callbacks.
1011
- Added a reminder in the devconsole command that the console is disabled by default in release builds.

Runtime/DevConsoleMono.cs

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ internal sealed class DevConsoleMono : MonoBehaviour
176176
private bool _displayUnityErrors = true;
177177
private bool _displayUnityExceptions = true;
178178
private bool _displayUnityWarnings = true;
179-
private string[] _commandSuggestions = null;
179+
private string[] _commandStringSuggestions = null;
180+
private Command[] _commandSuggestions = null;
180181
private int _commandSuggestionIndex = 0;
181182
private readonly List<Type> _cacheEnumTypes = new List<Type>(MaxCachedEnumTypes);
182183

@@ -613,6 +614,7 @@ internal void LogCommand(string name)
613614
internal void OnInputValueChanged(string _)
614615
{
615616
RefreshCommandSuggestions();
617+
RefreshCommandParameterSuggestions();
616618
}
617619

618620
internal char OnValidateInput(string input, int charIndex, char addedChar)
@@ -879,7 +881,7 @@ private void LateUpdate()
879881
if (_inputField.isFocused)
880882
{
881883
// Allow cycling through command suggestions using the UP and DOWN arrows
882-
if (_commandSuggestions != null && _commandSuggestions.Length > 0)
884+
if (_commandStringSuggestions != null && _commandStringSuggestions.Length > 0)
883885
{
884886
if (GetKeyDown(UpArrowKey))
885887
{
@@ -2353,28 +2355,82 @@ private void RefreshCommandSuggestions()
23532355
if (InputText.Length == 0 || InputText.StartsWith(" ") || InputText.Split(' ').Length > 1 || _commandHistoryIndex != -1)
23542356
{
23552357
_suggestionText.text = string.Empty;
2358+
_commandStringSuggestions = null;
23562359
_commandSuggestions = null;
23572360
_commandSuggestionIndex = 0;
23582361
return;
23592362
}
23602363

23612364
// Get a collection of command suggestions and show the first result
2362-
_commandSuggestions = GetCommandSuggestions(InputText);
2365+
_commandStringSuggestions = GetCommandSuggestions(InputText, out _commandSuggestions);
23632366
_commandSuggestionIndex = 0;
2364-
_suggestionText.text = _commandSuggestions.FirstOrDefault() ?? string.Empty;
2367+
_suggestionText.text = _commandStringSuggestions.ElementAtOrDefault(_commandSuggestionIndex) ?? string.Empty;
23652368
}
23662369

23672370
private void RefreshCommandParameterSuggestions()
23682371
{
2372+
string suffix = "";
23692373

2374+
// If there is a current command suggestion, use it for the parameter suggestions
2375+
if (_commandStringSuggestions?.Length > 0)
2376+
{
2377+
Command command = _commandSuggestions.ElementAtOrDefault(_commandSuggestionIndex);
2378+
if (command == null)
2379+
{
2380+
return;
2381+
}
2382+
suffix = $" {string.Join(" ", command.Parameters.Select(x => x.ToString()))}";
2383+
}
2384+
2385+
// Otherwise determine the current command from the input
2386+
else
2387+
{
2388+
// Split the input
2389+
string[] splitInput = InputText.Split(' ');
2390+
if (splitInput.Length == 0)
2391+
{
2392+
return;
2393+
}
2394+
2395+
// Get the command
2396+
Command command = GetCommand(splitInput[0]);
2397+
if (command == null)
2398+
{
2399+
return;
2400+
}
2401+
2402+
// Gather the remaining parameters
2403+
List<string> parameters = new List<string>();
2404+
for (int i = splitInput.Length - 2; i < command.Parameters.Length; ++i)
2405+
{
2406+
// If the current parameter is empty, then still show the parameter suggestion
2407+
if (i + 1 > 0 && i + 1 < splitInput.Length && !string.IsNullOrEmpty(splitInput[i + 1]))
2408+
{
2409+
continue;
2410+
}
2411+
2412+
parameters.Add(command.Parameters[i].ToString());
2413+
}
2414+
2415+
if (parameters.Count == 0)
2416+
{
2417+
return;
2418+
}
2419+
2420+
suffix = $"{new string(' ', string.Join(" ", splitInput.Where(x => !string.IsNullOrEmpty(x))).Length)} {string.Join(" ", parameters)}";
2421+
}
2422+
2423+
_suggestionText.text += suffix;
23702424
}
23712425

2372-
private string[] GetCommandSuggestions(string text)
2426+
private string[] GetCommandSuggestions(string text, out Command[] commands)
23732427
{
23742428
// Get a list of command names that could fill in the missing text
23752429
// Store alias suggestions separately and add on end later (so the result contains real commands before aliases)
23762430
List<string> suggestions = new List<string>();
23772431
List<string> aliasSuggestions = new List<string>();
2432+
List<Command> cmds = new List<Command>();
2433+
List<Command> aliasCmds = new List<Command>();
23782434
string textToLower = text.ToLower();
23792435

23802436
foreach (Command command in _commands.Values)
@@ -2385,6 +2441,7 @@ private string[] GetCommandSuggestions(string text)
23852441
// Combine current input with suggestion so capitalisation remains
23862442
// Add to suggestions list
23872443
suggestions.Add(text + command.Name.Substring(text.Length));
2444+
cmds.Add(command);
23882445
}
23892446

23902447
// Iterate over the command aliases
@@ -2396,28 +2453,31 @@ private string[] GetCommandSuggestions(string text)
23962453
// Combine current input with suggestion so capitalisation remains
23972454
// Add to alias suggestions list
23982455
aliasSuggestions.Add(text + alias.Substring(text.Length));
2456+
aliasCmds.Add(command);
23992457
}
24002458
}
24012459
}
24022460
suggestions.AddRange(aliasSuggestions);
2461+
cmds.AddRange(aliasCmds);
2462+
commands = cmds.ToArray();
24032463
return suggestions.ToArray();
24042464
}
24052465

24062466
private void AutoComplete()
24072467
{
2408-
if (_commandSuggestions == null || _commandSuggestions.Length == 0)
2468+
if (_commandStringSuggestions == null || _commandStringSuggestions.Length == 0)
24092469
{
24102470
return;
24112471
}
24122472

24132473
// Complete the input text with the current command suggestion
2414-
InputText = _commandSuggestions[_commandSuggestionIndex];
2474+
InputText = _commandStringSuggestions[_commandSuggestionIndex];
24152475
InputCaretPosition = InputText.Length;
24162476
}
24172477

24182478
private void CycleCommandSuggestions(int direction)
24192479
{
2420-
if (_commandSuggestions == null || _commandSuggestions.Length == 0)
2480+
if (_commandStringSuggestions == null || _commandStringSuggestions.Length == 0)
24212481
{
24222482
return;
24232483
}
@@ -2426,13 +2486,14 @@ private void CycleCommandSuggestions(int direction)
24262486
_commandSuggestionIndex += direction;
24272487
if (_commandSuggestionIndex < 0)
24282488
{
2429-
_commandSuggestionIndex = _commandSuggestions.Length - 1;
2489+
_commandSuggestionIndex = _commandStringSuggestions.Length - 1;
24302490
}
2431-
else if (_commandSuggestionIndex == _commandSuggestions.Length)
2491+
else if (_commandSuggestionIndex == _commandStringSuggestions.Length)
24322492
{
24332493
_commandSuggestionIndex = 0;
24342494
}
2435-
_suggestionText.text = _commandSuggestions[_commandSuggestionIndex];
2495+
_suggestionText.text = _commandStringSuggestions[_commandSuggestionIndex];
2496+
RefreshCommandParameterSuggestions();
24362497
InputCaretPosition = InputText.Length;
24372498
}
24382499

0 commit comments

Comments
 (0)