| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | |
|
| | 7 | | namespace GDX.Developer |
| | 8 | | { |
| | 9 | | #if UNITY_2022_2_OR_NEWER |
| | 10 | | public static class ConsoleAutoCompleteProvider |
| | 11 | | { |
| | 12 | | static int s_AutoCompleteOffset; |
| | 13 | | static string[] s_AutoCompleteSuggestions; |
| | 14 | | static string s_CurrentSuggestion; |
| | 15 | |
|
| | 16 | | public static string GetCurrentSuggestion() |
| 0 | 17 | | { |
| 0 | 18 | | return s_CurrentSuggestion; |
| 0 | 19 | | } |
| | 20 | |
|
| | 21 | | public static bool UpdateSuggestion(string inputString) |
| 0 | 22 | | { |
| 0 | 23 | | string[] split = inputString.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries); |
| | 24 | |
|
| 0 | 25 | | if (split == null || split.Length == 0) |
| 0 | 26 | | { |
| 0 | 27 | | Reset(); |
| 0 | 28 | | return false; |
| | 29 | | } |
| | 30 | |
|
| | 31 | | // Check if we have a command |
| 0 | 32 | | ConsoleCommandBase command = Console.GetCommand(split[0]); |
| 0 | 33 | | if (command != null) |
| 0 | 34 | | { |
| 0 | 35 | | s_AutoCompleteSuggestions = |
| | 36 | | command.GetArgumentAutoCompleteSuggestions(split.Length == 2 ? split[1] : null, |
| | 37 | | s_AutoCompleteSuggestions); |
| | 38 | |
|
| 0 | 39 | | if (s_AutoCompleteSuggestions == null || s_AutoCompleteSuggestions.Length == 0) |
| 0 | 40 | | { |
| 0 | 41 | | Reset(); |
| 0 | 42 | | } |
| | 43 | | else |
| 0 | 44 | | { |
| 0 | 45 | | s_CurrentSuggestion = $" {s_AutoCompleteSuggestions[s_AutoCompleteOffset]}"; |
| 0 | 46 | | s_AutoCompleteOffset++; |
| 0 | 47 | | if (s_AutoCompleteOffset >= s_AutoCompleteSuggestions.Length) |
| 0 | 48 | | { |
| 0 | 49 | | s_AutoCompleteOffset = 0; |
| 0 | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| 0 | 53 | | return true; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | // Variable Search |
| 0 | 57 | | ConsoleVariableBase variable = Console.GetVariable(split[0]); |
| 0 | 58 | | if (variable != null) |
| 0 | 59 | | { |
| 0 | 60 | | s_AutoCompleteSuggestions = |
| | 61 | | variable.GetArgumentAutoCompleteSuggestions(split.Length == 2 ? split[1] : null, |
| | 62 | | s_AutoCompleteSuggestions); |
| | 63 | |
|
| 0 | 64 | | if (s_AutoCompleteSuggestions == null || s_AutoCompleteSuggestions.Length == 0) |
| 0 | 65 | | { |
| 0 | 66 | | Reset(); |
| 0 | 67 | | } |
| | 68 | | else |
| 0 | 69 | | { |
| 0 | 70 | | s_CurrentSuggestion = $" {s_AutoCompleteSuggestions[s_AutoCompleteOffset]}"; |
| 0 | 71 | | s_AutoCompleteOffset++; |
| 0 | 72 | | if (s_AutoCompleteOffset >= s_AutoCompleteSuggestions.Length) |
| 0 | 73 | | { |
| 0 | 74 | | s_AutoCompleteOffset = 0; |
| 0 | 75 | | } |
| 0 | 76 | | } |
| | 77 | |
|
| 0 | 78 | | return true; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | // Wide Search |
| 0 | 82 | | s_AutoCompleteSuggestions = Console.GetCommandAutoCompleteSuggestions(split[0], s_AutoCompleteSuggestions); |
| 0 | 83 | | if (s_AutoCompleteSuggestions == null || s_AutoCompleteSuggestions.Length == 0) |
| 0 | 84 | | { |
| 0 | 85 | | s_CurrentSuggestion = string.Empty; |
| 0 | 86 | | s_AutoCompleteOffset = 0; |
| 0 | 87 | | return false; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | // TODO : if you have a bad command and a space and a var it will get here and implode on auto |
| 0 | 91 | | s_CurrentSuggestion = s_AutoCompleteSuggestions[s_AutoCompleteOffset].Substring(inputString.Length); |
| 0 | 92 | | s_AutoCompleteOffset++; |
| 0 | 93 | | if (s_AutoCompleteOffset >= s_AutoCompleteSuggestions.Length) |
| 0 | 94 | | { |
| 0 | 95 | | s_AutoCompleteOffset = 0; |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | return true; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public static void Reset() |
| 0 | 102 | | { |
| 0 | 103 | | s_AutoCompleteOffset = 0; |
| 0 | 104 | | s_AutoCompleteSuggestions = null; |
| 0 | 105 | | } |
| | 106 | | } |
| | 107 | | #endif // UNITY_2022_2_OR_NEWER |
| | 108 | | } |