Implement auto-indenting for CSConsole, some cleanups

This commit is contained in:
Sinai
2021-05-12 20:48:56 +10:00
parent fedecb80a0
commit ada239c828
21 changed files with 681 additions and 3702 deletions
+38 -8
View File
@@ -37,7 +37,23 @@ namespace UnityExplorer
/// <summary>Key: Type.FullName</summary>
public static readonly SortedDictionary<string, Type> AllTypes = new SortedDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
private static readonly SortedSet<string> allTypeNames = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
//private static readonly SortedSet<string> allTypeNames = new SortedSet<string>(StringComparer.OrdinalIgnoreCase);
private static string[] allTypesArray;
private static string[] GetTypeNameArray()
{
if (allTypesArray == null || allTypesArray.Length != AllTypes.Count)
{
allTypesArray = new string[AllTypes.Count];
int i = 0;
foreach (var name in AllTypes.Keys)
{
allTypesArray[i] = name;
i++;
}
}
return allTypesArray;
}
private static void SetupTypeCache()
{
@@ -64,7 +80,7 @@ namespace UnityExplorer
else
{
AllTypes.Add(type.FullName, type);
allTypeNames.Add(type.FullName);
//allTypeNames.Add(type.FullName);
}
OnTypeLoaded?.Invoke(type);
@@ -211,14 +227,24 @@ namespace UnityExplorer
/// </summary>
/// <param name="baseType">The base type, which can optionally be abstract / interface.</param>
/// <returns>All implementations of the type in the current AppDomain.</returns>
public static HashSet<Type> GetImplementationsOf(Type baseType, bool allowAbstract, bool allowGeneric)
public static HashSet<Type> GetImplementationsOf(Type baseType, bool allowAbstract, bool allowGeneric, bool allowRecursive = true)
{
var key = GetImplementationKey(baseType); //baseType.FullName;
var key = GetImplementationKey(baseType);
int count = AllTypes.Count;
HashSet<Type> ret;
if (!baseType.IsGenericParameter)
return GetImplementations(key, baseType, allowAbstract, allowGeneric);
ret = GetImplementations(key, baseType, allowAbstract, allowGeneric);
else
return GetGenericParameterImplementations(key, baseType, allowAbstract, allowGeneric);
ret = GetGenericParameterImplementations(key, baseType, allowAbstract, allowGeneric);
// types were resolved during the parse, do it again if we're not already rebuilding.
if (allowRecursive && AllTypes.Count != count)
{
ret = GetImplementationsOf(baseType, allowAbstract, allowGeneric, false);
}
return ret;
}
private static HashSet<Type> GetImplementations(string key, Type baseType, bool allowAbstract, bool allowGeneric)
@@ -226,8 +252,10 @@ namespace UnityExplorer
if (!typeInheritance.ContainsKey(key))
{
var set = new HashSet<Type>();
foreach (var name in allTypeNames)
var names = GetTypeNameArray();
for (int i = 0; i < names.Length; i++)
{
var name = names[i];
try
{
var type = AllTypes[name];
@@ -263,8 +291,10 @@ namespace UnityExplorer
{
var set = new HashSet<Type>();
foreach (var name in allTypeNames)
var names = GetTypeNameArray();
for (int i = 0; i < names.Length; i++)
{
var name = names[i];
try
{
var type = AllTypes[name];
-3
View File
@@ -10,9 +10,6 @@ using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityExplorer.Core;
using UnityExplorer.Core.CSharp;
using UnityExplorer.Core.Input;
namespace UnityExplorer.Core.Runtime.Mono
{
File diff suppressed because it is too large Load Diff
+1
View File
@@ -44,6 +44,7 @@ namespace UnityExplorer
public static readonly Color StringOrange = new Color(0.83f, 0.61f, 0.52f);
public static readonly Color EnumGreen = new Color(0.57f, 0.76f, 0.43f);
public static readonly Color KeywordBlue = new Color(0.3f, 0.61f, 0.83f);
public static readonly string keywordBlueHex = KeywordBlue.ToHex();
public static readonly Color NumberGreen = new Color(0.71f, 0.8f, 0.65f);
internal static string GetClassColor(Type type)
+51 -26
View File
@@ -3,70 +3,95 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityExplorer.UI.CSharpConsole.Lexers;
using UnityExplorer.UI.CSConsole.Lexers;
using UnityExplorer.UI.Widgets.AutoComplete;
namespace UnityExplorer.UI.CSharpConsole
namespace UnityExplorer.UI.CSConsole
{
public class CSAutoCompleter : ISuggestionProvider
{
public InputFieldRef InputField => CSConsole.Input;
public InputFieldRef InputField => ConsoleController.Input;
public bool AnchorToCaretPosition => true;
public void OnSuggestionClicked(Suggestion suggestion)
{
CSConsole.InsertSuggestionAtCaret(suggestion.UnderlyingValue);
ConsoleController.InsertSuggestionAtCaret(suggestion.UnderlyingValue);
AutoCompleteModal.Instance.ReleaseOwnership(this);
}
// Delimiters for completions, notably does not include '.'
private readonly HashSet<char> delimiters = new HashSet<char>
{
'{', '}', ',', ';', '<', '>', '(', ')', '[', ']', '=', '|', '&', '?'
};
//private readonly HashSet<string> expressions = new HashSet<string>
//{
// "new",
// "is", "as",
// "return", "yield", "throw", "in",
// "do", "for", "foreach",
// "else",
//};
private readonly List<Suggestion> suggestions = new List<Suggestion>();
public void CheckAutocompletes()
{
if (string.IsNullOrEmpty(InputField.Text))
{
AutoCompleteModal.Instance.ReleaseOwnership(this);
return;
}
int caret = Math.Max(0, Math.Min(InputField.Text.Length - 1, InputField.Component.caretPosition - 1));
int i = caret;
suggestions.Clear();
while (i > 0)
int caret = Math.Max(0, Math.Min(InputField.Text.Length - 1, InputField.Component.caretPosition - 1));
int start = caret;
// If the character at the caret index is whitespace or delimiter,
// or if the next character (if it exists) is not whitespace,
// then we don't want to provide suggestions.
if (char.IsWhiteSpace(InputField.Text[caret])
|| delimiters.Contains(InputField.Text[caret])
|| (InputField.Text.Length > caret + 1 && !char.IsWhiteSpace(InputField.Text[caret + 1])))
{
i--;
char c = InputField.Text[i];
AutoCompleteModal.Instance.ReleaseOwnership(this);
return;
}
// get the current composition string (from caret back to last delimiter or whitespace)
while (start > 0)
{
start--;
char c = InputField.Text[start];
if (char.IsWhiteSpace(c) || delimiters.Contains(c))
{
i++;
start++;
break;
}
}
string input = InputField.Text.Substring(start, caret - start + 1);
i = Math.Max(0, i);
// Get MCS completions
string input = InputField.Text.Substring(i, (caret - i + 1));
string[] evaluatorCompletions = ConsoleController.Evaluator.GetCompletions(input, out string prefix);
string[] evaluatorCompletions = CSConsole.Evaluator.GetCompletions(input, out string prefix);
if (evaluatorCompletions != null && evaluatorCompletions.Any())
if (!string.IsNullOrEmpty(prefix) && evaluatorCompletions != null && evaluatorCompletions.Any())
{
var suggestions = from completion in evaluatorCompletions
select new Suggestion($"<color=cyan>{prefix}</color>{completion}", completion);
suggestions.AddRange(from completion in evaluatorCompletions
select new Suggestion($"<color=cyan>{prefix}</color>{completion}", completion));
}
// Get manual keyword completions
foreach (var kw in KeywordLexer.keywords)
{
if (kw.StartsWith(input))
{
string completion = kw.Substring(input.Length, kw.Length - input.Length);
suggestions.Add(new Suggestion(
$"<color=cyan>{input}</color>" +
$"<color=#{SignatureHighlighter.keywordBlueHex}>{completion}</color>",
completion));
}
}
if (suggestions.Any())
{
AutoCompleteModal.Instance.TakeOwnership(this);
AutoCompleteModal.Instance.SetSuggestions(suggestions);
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -2,7 +2,7 @@
using System.Linq;
using UnityEngine;
namespace UnityExplorer.UI.CSharpConsole.Lexers
namespace UnityExplorer.UI.CSConsole.Lexers
{
public class CommentLexer : Lexer
{
+2 -2
View File
@@ -2,14 +2,14 @@
using System.Text;
using UnityEngine;
namespace UnityExplorer.UI.CSharpConsole.Lexers
namespace UnityExplorer.UI.CSConsole.Lexers
{
public class KeywordLexer : Lexer
{
// system blue
protected override Color HighlightColor => new Color(0.33f, 0.61f, 0.83f, 1.0f);
private readonly HashSet<string> keywords = new HashSet<string>
public static readonly HashSet<string> keywords = new HashSet<string>
{
// reserved keywords
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue",
+1 -1
View File
@@ -2,7 +2,7 @@
using UnityEngine;
using System.Linq;
namespace UnityExplorer.UI.CSharpConsole.Lexers
namespace UnityExplorer.UI.CSConsole.Lexers
{
public abstract class Lexer
{
+1 -1
View File
@@ -1,6 +1,6 @@
using UnityEngine;
namespace UnityExplorer.UI.CSharpConsole.Lexers
namespace UnityExplorer.UI.CSConsole.Lexers
{
public class NumberLexer : Lexer
{
+1 -1
View File
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using UnityEngine;
namespace UnityExplorer.UI.CSharpConsole.Lexers
namespace UnityExplorer.UI.CSConsole.Lexers
{
public class StringLexer : Lexer
{
+1 -1
View File
@@ -3,7 +3,7 @@ using System.Linq;
using System.Text;
using UnityEngine;
namespace UnityExplorer.UI.CSharpConsole.Lexers
namespace UnityExplorer.UI.CSConsole.Lexers
{
public class SymbolLexer : Lexer
{
+2 -1
View File
@@ -2,11 +2,12 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Mono.CSharp;
// Thanks to ManlyMarco for this
namespace UnityExplorer.Core.CSharp
namespace UnityExplorer.UI.CSConsole
{
public class ScriptEvaluator : Evaluator, IDisposable
{
+4 -4
View File
@@ -6,7 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using UnityExplorer.Core.Runtime;
namespace UnityExplorer.UI.CSharpConsole
namespace UnityExplorer.UI.CSConsole
{
public class ScriptInteraction : InteractiveBase
{
@@ -22,17 +22,17 @@ namespace UnityExplorer.UI.CSharpConsole
public static void AddUsing(string directive)
{
CSConsole.AddUsing(directive);
ConsoleController.AddUsing(directive);
}
public static void GetUsing()
{
ExplorerCore.Log(CSConsole.Evaluator.GetUsing());
ExplorerCore.Log(ConsoleController.Evaluator.GetUsing());
}
public static void Reset()
{
CSConsole.ResetConsole();
ConsoleController.ResetConsole();
}
public static object CurrentTarget()
+3 -2
View File
@@ -338,9 +338,9 @@ namespace UnityExplorer.UI.CacheObject
// Blacklists
private static readonly HashSet<string> bl_typeAndMember = new HashSet<string>
{
// these cause a crash in IL2CPP
// these can cause a crash in IL2CPP
#if CPP
//"Type.DeclaringMethod",
"Type.DeclaringMethod",
"Rigidbody2D.Cast",
"Collider2D.Cast",
"Collider2D.Raycast",
@@ -363,6 +363,7 @@ namespace UnityExplorer.UI.CacheObject
"Component.renderer",
"Component.rigidbody",
"Component.rigidbody2D",
"Light.flare",
};
private static readonly HashSet<string> bl_methodNameStartsWith = new HashSet<string>
{
+4 -4
View File
@@ -6,7 +6,7 @@ using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityExplorer.Core.Config;
using UnityExplorer.UI.CSharpConsole;
using UnityExplorer.UI.CSConsole;
using UnityExplorer.UI.Utility;
namespace UnityExplorer.UI.Panels
@@ -44,7 +44,7 @@ namespace UnityExplorer.UI.Panels
{
base.Update();
CSConsole.Update();
ConsoleController.Update();
}
// Saving
@@ -111,9 +111,9 @@ namespace UnityExplorer.UI.Panels
int fontSize = 16;
var inputObj = UIFactory.CreateSrollInputField(this.content, "ConsoleInput", CSConsole.STARTUP_TEXT, out var inputScroller, fontSize);
var inputObj = UIFactory.CreateSrollInputField(this.content, "ConsoleInput", ConsoleController.STARTUP_TEXT, out var inputScroller, fontSize);
InputScroll = inputScroller;
CSConsole.defaultInputFieldAlpha = Input.Component.selectionColor.a;
ConsoleController.defaultInputFieldAlpha = Input.Component.selectionColor.a;
Input.OnValueChanged += InvokeOnValueChanged;
InputText = Input.Component.textComponent;
+2 -2
View File
@@ -8,7 +8,7 @@ using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityExplorer.Core.Config;
using UnityExplorer.Core.Input;
using UnityExplorer.UI.CSharpConsole;
using UnityExplorer.UI.CSConsole;
using UnityExplorer.UI.Models;
using UnityExplorer.UI.Panels;
using UnityExplorer.UI.Utility;
@@ -178,7 +178,7 @@ namespace UnityExplorer.UI
CSharpConsole = new CSConsolePanel();
CSharpConsole.ConstructUI();
CSConsole.Init();
ConsoleController.Init();
ShowMenu = !ConfigManager.Hide_On_Startup.Value;
@@ -12,14 +12,14 @@ using UnityExplorer.UI.Panels;
namespace UnityExplorer.UI.Widgets.AutoComplete
{
// Shared modal panel for "AutoComplete" suggestions.
// A data source implements ISuggestionProvider and uses TakeOwnership and ReleaseOwnership
// for control, and SetSuggestions to set the actual suggestion data.
public class AutoCompleteModal : UIPanel
{
// Static
public static AutoCompleteModal Instance => UIManager.AutoCompleter;
// Instance
public override string Name => "AutoCompleter";
public override UIManager.Panels PanelType => UIManager.Panels.AutoCompleter;
public override int MinWidth => -1;
@@ -154,14 +154,14 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
if (CurrentHandler.AnchorToCaretPosition)
{
var textGen = input.Component.textComponent.cachedTextGeneratorForLayout;
var textGen = input.Component.cachedInputTextGenerator;
int caretIdx = Math.Max(0, Math.Min(textGen.characterCount - 1, input.Component.caretPosition));
// normalize the caret horizontal position
Vector3 caretPos = textGen.characters[caretIdx].cursorPos;
caretPos += new Vector3(input.Rect.rect.width * 0.5f, 0, 0);
// transform to world point
caretPos = input.UIRoot.transform.TransformPoint(caretPos);
caretPos += new Vector3(input.Rect.rect.width * 0.5f, -(input.Rect.rect.height * 0.5f), 0);
uiRoot.transform.position = new Vector3(caretPos.x + 10, caretPos.y - 30, 0);
}

Some files were not shown because too many files have changed in this diff Show More