Skip to content

Commit 9521c24

Browse files
authored
Merge pull request #3 from BroWar-Collective/feature/auto-complete
Feature/auto-complete
2 parents bc46026 + 6ca146c commit 9521c24

19 files changed

Lines changed: 366 additions & 36 deletions

Assets/Examples/Scriptables/Commands Repository.asset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ MonoBehaviour:
1515
commands:
1616
- {fileID: 11400000, guid: 5fb50d2c52cb39146aae5361ef608c1e, type: 2}
1717
- {fileID: 11400000, guid: f3548dc1b603c1a40907cf987711c149, type: 2}
18+
- {fileID: 11400000, guid: 91eaf8f74e69a8b4bbd6c675154a8e00, type: 2}
1819
- {fileID: 11400000, guid: eb4bb9d9179980a4e8f3514df0548380, type: 2}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 4ddb93007c21aa749860a8429d300b4d, type: 3}
13+
m_Name: Multiple Arguments Command
14+
m_EditorClassIdentifier:

Assets/Examples/Scriptables/Commands/Multiple Arguments Command.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Examples/Scripts.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Examples/Scripts/Commands.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace BroWar.Debugging.Examples.Commands
5+
{
6+
using BroWar.Debugging.Console;
7+
8+
[CreateAssetMenu(fileName = "Multiple Arguments Command", menuName = "Examples/Commands/Multiple Arguments")]
9+
public class MultipleArgumentsCommand : ConsoleCommand, IAutocompleteOptionProvider
10+
{
11+
public void ModifyArray(string arg1, string arg2)
12+
{ }
13+
14+
void IAutocompleteOptionProvider.GetParameterAutocompleteOptions(string[] words, int wordIndex, IList<string> options)
15+
{
16+
switch (wordIndex)
17+
{
18+
case 1:
19+
options.Add("insert");
20+
options.Add("remove");
21+
break;
22+
case 2:
23+
options.Add("item");
24+
options.Add("range");
25+
break;
26+
}
27+
}
28+
}
29+
}

Assets/Package/Runtime/Console/ArgumentExtractionException.cs.meta renamed to Assets/Examples/Scripts/Commands/MultipleArgumentsCommand.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Package/Runtime/Console/ArgumentExtractionException.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace BroWar.Debugging.Console
5+
{
6+
public class AutocompleteHandler
7+
{
8+
private readonly IList<string> options = new List<string>();
9+
10+
private bool TryGetShortestValidMatchStarting(string word, out string bestMatch)
11+
{
12+
bestMatch = string.Empty;
13+
var wordFormatted = word.ToLower();
14+
foreach (var option in options)
15+
{
16+
var optionFormatted = option.ToLower();
17+
if (optionFormatted.StartsWith(wordFormatted) && !optionFormatted.Equals(wordFormatted)
18+
&& (string.IsNullOrEmpty(bestMatch) || optionFormatted.Length < bestMatch.Length))
19+
{
20+
bestMatch = option;
21+
}
22+
}
23+
24+
return !string.IsNullOrEmpty(bestMatch);
25+
}
26+
27+
private bool TryGetShortestValidMatchContaining(string word, out string bestMatch)
28+
{
29+
bestMatch = string.Empty;
30+
var wordFormatted = word.ToLower();
31+
foreach (var option in options)
32+
{
33+
var optionFormatted = option.ToLower();
34+
if (optionFormatted.Contains(wordFormatted) && !optionFormatted.Equals(wordFormatted)
35+
&& (string.IsNullOrEmpty(bestMatch) || optionFormatted.Length < bestMatch.Length))
36+
{
37+
bestMatch = option;
38+
}
39+
}
40+
41+
return !string.IsNullOrEmpty(bestMatch);
42+
}
43+
44+
private void RefreshOptions(IAutocompleteOptionProvider optionProvider, string[] splitInput)
45+
{
46+
options.Clear();
47+
optionProvider.GetParameterAutocompleteOptions(splitInput, splitInput.Length - 1, options);
48+
}
49+
50+
public string GetBestMatch(IAutocompleteOptionProvider optionProvider, string input, string encapsulationCharacter)
51+
{
52+
if (string.IsNullOrEmpty(input))
53+
{
54+
return string.Empty;
55+
}
56+
57+
var splitInput = ConsoleUtility.SplitInput(input, encapsulationCharacter);
58+
RefreshOptions(optionProvider, splitInput);
59+
if (TryGetShortestValidMatchStarting(splitInput.Last(), out var bestMatch))
60+
{
61+
return bestMatch;
62+
}
63+
64+
if (TryGetShortestValidMatchContaining(splitInput.Last(), out bestMatch))
65+
{
66+
return bestMatch;
67+
}
68+
69+
return string.Empty;
70+
}
71+
}
72+
}

Assets/Package/Runtime/Console/AutocompleteHandler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)