various improvements to reflection inspector and C# console

This commit is contained in:
sinaioutlander
2020-11-12 16:15:41 +11:00
parent 2077601464
commit a7f86227fb
28 changed files with 656 additions and 420 deletions

View File

@ -44,7 +44,7 @@ namespace UnityExplorer.Console
return;
}
if (!ConsolePage.EnableAutocompletes)
if (!CodeEditor.EnableAutocompletes)
{
if (m_mainObj.activeSelf)
{
@ -187,9 +187,9 @@ namespace UnityExplorer.Console
public static void ClearAutocompletes()
{
if (ConsolePage.AutoCompletes.Any())
if (CodeEditor.AutoCompletes.Any())
{
ConsolePage.AutoCompletes.Clear();
CodeEditor.AutoCompletes.Clear();
}
}
@ -198,7 +198,7 @@ namespace UnityExplorer.Console
try
{
// Credit ManylMarco
ConsolePage.AutoCompletes.Clear();
CodeEditor.AutoCompletes.Clear();
string[] completions = ConsolePage.Instance.m_evaluator.GetCompletions(input, out string prefix);
if (completions != null)
{
@ -207,7 +207,7 @@ namespace UnityExplorer.Console
prefix = input;
}
ConsolePage.AutoCompletes.AddRange(completions
CodeEditor.AutoCompletes.AddRange(completions
.Where(x => !string.IsNullOrEmpty(x))
.Select(x => new Suggestion(x, prefix, Suggestion.Contexts.Other))
);
@ -226,7 +226,7 @@ namespace UnityExplorer.Console
x.Substring(0, trimmed.Length),
Suggestion.Contexts.Namespace));
ConsolePage.AutoCompletes.AddRange(namespaces);
CodeEditor.AutoCompletes.AddRange(namespaces);
IEnumerable<Suggestion> keywords = Suggestion.Keywords
.Where(x => x.StartsWith(trimmed) && x.Length > trimmed.Length)
@ -235,7 +235,7 @@ namespace UnityExplorer.Console
x.Substring(0, trimmed.Length),
Suggestion.Contexts.Keyword));
ConsolePage.AutoCompletes.AddRange(keywords);
CodeEditor.AutoCompletes.AddRange(keywords);
}
catch (Exception ex)
{
@ -290,7 +290,7 @@ namespace UnityExplorer.Console
var hiddenChild = UIFactory.CreateUIObject("HiddenText", buttonObj);
hiddenChild.SetActive(false);
var hiddenText = hiddenChild.AddGraphic<Text>();
var hiddenText = hiddenChild.AddComponent<Text>();
m_hiddenSuggestionTexts.Add(hiddenText);
#if CPP
@ -301,9 +301,7 @@ namespace UnityExplorer.Console
void UseAutocompleteButton()
{
ConsolePage.Instance.UseAutocomplete(hiddenText.text);
EventSystem.current.SetSelectedGameObject(ConsolePage.Instance.m_codeEditor.InputField.gameObject,
null);
ConsolePage.Instance.m_codeEditor.UseAutocomplete(hiddenText.text);
}
m_suggestionButtons.Add(buttonObj);

View File

@ -11,11 +11,6 @@ using UnityExplorer.UI.Modules;
using System.Collections.Generic;
using System.Reflection;
using UnityExplorer.UI.Shared;
#if CPP
using UnityExplorer.Unstrip;
using UnityExplorer.Helpers;
using UnhollowerRuntimeLib;
#endif
namespace UnityExplorer.Console
{
@ -27,12 +22,22 @@ namespace UnityExplorer.Console
public Text InputText { get; internal set; }
public int CurrentIndent { get; private set; }
public static bool EnableCtrlRShortcut { get; set; } = true;
public static bool EnableAutoIndent { get; set; } = true;
public static bool EnableAutocompletes { get; set; } = true;
public static List<Suggestion> AutoCompletes = new List<Suggestion>();
public string HighlightedText => inputHighlightText.text;
private Text inputHighlightText;
private readonly CSharpLexer highlightLexer;
private readonly StringBuilder sbHighlight;
internal int m_lastCaretPos;
internal int m_fixCaretPos;
internal bool m_fixwanted;
internal float m_lastSelectAlpha;
private static readonly KeyCode[] onFocusKeys =
{
KeyCode.Return, KeyCode.Backspace, KeyCode.UpArrow,
@ -76,31 +81,74 @@ The following helper methods are available:
public void Update()
{
// Check for new line
if (ConsolePage.EnableAutoIndent && InputManager.GetKeyDown(KeyCode.Return))
if (EnableCtrlRShortcut)
{
AutoIndentCaret();
}
if (EventSystem.current?.currentSelectedGameObject?.name == "InputField")
{
bool focusKeyPressed = false;
// Check for any focus key pressed
foreach (KeyCode key in onFocusKeys)
if ((InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.RightControl))
&& InputManager.GetKeyDown(KeyCode.R))
{
if (InputManager.GetKeyDown(key))
var text = InputField.text.Trim();
if (!string.IsNullOrEmpty(text))
{
focusKeyPressed = true;
break;
ConsolePage.Instance.Evaluate(text);
return;
}
}
}
if (focusKeyPressed || InputManager.GetMouseButton(0))
if (EnableAutoIndent && InputManager.GetKeyDown(KeyCode.Return))
AutoIndentCaret();
if (EnableAutocompletes && InputField.isFocused)
{
if (InputManager.GetMouseButton(0) || onFocusKeys.Any(it => InputManager.GetKeyDown(it)))
UpdateAutocompletes();
}
if (m_fixCaretPos > 0)
{
if (!m_fixwanted)
m_fixwanted = true;
else
{
ConsolePage.Instance.OnInputChanged();
InputField.caretPosition = m_fixCaretPos;
InputField.selectionFocusPosition = m_fixCaretPos;
m_fixwanted = false;
m_fixCaretPos = -1;
var color = InputField.selectionColor;
color.a = m_lastSelectAlpha;
InputField.selectionColor = color;
}
}
else if (InputField.caretPosition > 0)
{
m_lastCaretPos = InputField.caretPosition;
}
}
internal void UpdateAutocompletes()
{
AutoCompleter.CheckAutocomplete();
AutoCompleter.SetSuggestions(AutoCompletes.ToArray());
}
public void UseAutocomplete(string suggestion)
{
EventSystem.current.SetSelectedGameObject(ConsolePage.Instance.m_codeEditor.InputField.gameObject, null);
string input = InputField.text;
input = input.Insert(m_lastCaretPos, suggestion);
InputField.text = input;
m_fixCaretPos = m_lastCaretPos += suggestion.Length;
var color = InputField.selectionColor;
m_lastSelectAlpha = color.a;
color.a = 0f;
InputField.selectionColor = color;
AutoCompleter.ClearAutocompletes();
}
public void OnInputChanged(string newInput, bool forceUpdate = false)
@ -110,15 +158,11 @@ The following helper methods are available:
UpdateIndent(newInput);
if (!forceUpdate && string.IsNullOrEmpty(newText))
{
inputHighlightText.text = string.Empty;
}
else
{
inputHighlightText.text = SyntaxHighlightContent(newText);
}
ConsolePage.Instance.OnInputChanged();
UpdateAutocompletes();
}
private void UpdateIndent(string newText)
@ -298,12 +342,32 @@ The following helper methods are available:
var topBarLabel = UIFactory.CreateLabel(topBarObj, TextAnchor.MiddleLeft);
var topBarLabelLayout = topBarLabel.AddComponent<LayoutElement>();
topBarLabelLayout.preferredWidth = 800;
topBarLabelLayout.flexibleWidth = 10;
topBarLabelLayout.preferredWidth = 150;
topBarLabelLayout.flexibleWidth = 5000;
var topBarText = topBarLabel.GetComponent<Text>();
topBarText.text = "C# Console";
topBarText.fontSize = 20;
// Enable Ctrl+R toggle
var ctrlRToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle ctrlRToggle, out Text ctrlRToggleText);
#if CPP
ctrlRToggle.onValueChanged.AddListener(new Action<bool>(CtrlRToggleCallback));
#else
ctrlRToggle.onValueChanged.AddListener(CtrlRToggleCallback);
#endif
void CtrlRToggleCallback(bool val)
{
EnableCtrlRShortcut = val;
}
ctrlRToggleText.text = "Run on Ctrl+R";
ctrlRToggleText.alignment = TextAnchor.UpperLeft;
var ctrlRLayout = ctrlRToggleObj.AddComponent<LayoutElement>();
ctrlRLayout.minWidth = 140;
ctrlRLayout.flexibleWidth = 0;
ctrlRLayout.minHeight = 25;
// Enable Suggestions toggle
var suggestToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle suggestToggle, out Text suggestToggleText);
@ -314,24 +378,16 @@ The following helper methods are available:
#endif
void SuggestToggleCallback(bool val)
{
ConsolePage.EnableAutocompletes = val;
EnableAutocompletes = val;
AutoCompleter.Update();
}
suggestToggleText.text = "Suggestions";
suggestToggleText.alignment = TextAnchor.UpperLeft;
var suggestTextPos = suggestToggleText.transform.localPosition;
suggestTextPos.y = -14;
suggestToggleText.transform.localPosition = suggestTextPos;
var suggestLayout = suggestToggleObj.AddComponent<LayoutElement>();
suggestLayout.minWidth = 120;
suggestLayout.flexibleWidth = 0;
var suggestRect = suggestToggleObj.transform.Find("Background");
var suggestPos = suggestRect.localPosition;
suggestPos.y = -14;
suggestRect.localPosition = suggestPos;
suggestLayout.minHeight = 25;
// Enable Auto-indent toggle
@ -341,22 +397,15 @@ The following helper methods are available:
#else
autoIndentToggle.onValueChanged.AddListener(OnIndentChanged);
#endif
void OnIndentChanged(bool val) => ConsolePage.EnableAutoIndent = val;
void OnIndentChanged(bool val) => EnableAutoIndent = val;
autoIndentToggleText.text = "Auto-indent";
autoIndentToggleText.alignment = TextAnchor.UpperLeft;
var autoIndentTextPos = autoIndentToggleText.transform.localPosition;
autoIndentTextPos.y = -14;
autoIndentToggleText.transform.localPosition = autoIndentTextPos;
var autoIndentLayout = autoIndentToggleObj.AddComponent<LayoutElement>();
autoIndentLayout.minWidth = 120;
autoIndentLayout.flexibleWidth = 0;
var autoIndentRect = autoIndentToggleObj.transform.Find("Background");
suggestPos = autoIndentRect.localPosition;
suggestPos.y = -14;
autoIndentRect.localPosition = suggestPos;
autoIndentLayout.minHeight = 25;
#endregion
@ -385,7 +434,7 @@ The following helper methods are available:
highlightTextRect.offsetMin = new Vector2(20, 0);
highlightTextRect.offsetMax = new Vector2(14, 0);
var highlightTextInput = highlightTextObj.AddGraphic<Text>();
var highlightTextInput = highlightTextObj.AddComponent<Text>();
highlightTextInput.supportRichText = true;
highlightTextInput.fontSize = fontSize;

View File

@ -15,11 +15,11 @@ namespace UnityExplorer.Console
"mscorlib", "System.Core", "System", "System.Xml"
};
private readonly TextWriter _logger;
private readonly TextWriter tw;
public ScriptEvaluator(TextWriter logger) : base(BuildContext(logger))
public ScriptEvaluator(TextWriter tw) : base(BuildContext(tw))
{
_logger = logger;
this.tw = tw;
ImportAppdomainAssemblies(ReferenceAssembly);
AppDomain.CurrentDomain.AssemblyLoad += OnAssemblyLoad;
@ -28,7 +28,7 @@ namespace UnityExplorer.Console
public void Dispose()
{
AppDomain.CurrentDomain.AssemblyLoad -= OnAssemblyLoad;
_logger.Dispose();
tw.Dispose();
}
private void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)