diff --git a/src/UI/CSConsole/CSAutoCompleter.cs b/src/UI/CSConsole/CSAutoCompleter.cs index b81394a..c0f60c7 100644 --- a/src/UI/CSConsole/CSAutoCompleter.cs +++ b/src/UI/CSConsole/CSAutoCompleter.cs @@ -59,11 +59,9 @@ namespace UnityExplorer.UI.CSConsole { startIdx--; char c = InputField.Text[startIdx]; - if (delimiters.Contains(c)) + if (delimiters.Contains(c) || char.IsWhiteSpace(c)) { startIdx++; - while (char.IsWhiteSpace(InputField.Text[startIdx])) - startIdx++; break; } } diff --git a/src/UI/CSConsole/ConsoleController.cs b/src/UI/CSConsole/ConsoleController.cs index 5b7f2a2..a1bbce8 100644 --- a/src/UI/CSConsole/ConsoleController.cs +++ b/src/UI/CSConsole/ConsoleController.cs @@ -231,20 +231,24 @@ namespace UnityExplorer.UI.CSConsole previousInput = value; if (EnableSuggestions && AutoCompleteModal.CheckEnter(Completer)) - { OnAutocompleteEnter(); - } - else if (!settingCaretCoroutine) + + var inStringOrComment = HighlightVisibleInput(); + + if (!settingCaretCoroutine) { if (EnableSuggestions) - Completer.CheckAutocompletes(); + { + if (inStringOrComment) + AutoCompleteModal.Instance.ReleaseOwnership(Completer); + else + Completer.CheckAutocompletes(); + } if (EnableAutoIndent) DoAutoIndent(); } - HighlightVisibleInput(); - UpdateCaret(out _); } @@ -372,7 +376,10 @@ namespace UnityExplorer.UI.CSConsole #region Lexer Highlighting - private static void HighlightVisibleInput() + /// + /// Returns true if caret is inside string or comment, false otherwise + /// + private static bool HighlightVisibleInput() { int startIdx = 0; int endIdx = Input.Text.Length - 1; @@ -410,7 +417,8 @@ namespace UnityExplorer.UI.CSConsole } // Highlight the visible text with the LexerBuilder - Panel.HighlightText.text = Lexer.BuildHighlightedString(Input.Text, startIdx, endIdx, topLine); + Panel.HighlightText.text = Lexer.BuildHighlightedString(Input.Text, startIdx, endIdx, topLine, LastCaretPosition, out bool ret); + return ret; } #endregion diff --git a/src/UI/CSConsole/LexerBuilder.cs b/src/UI/CSConsole/LexerBuilder.cs index e8eb5e0..b1c9559 100644 --- a/src/UI/CSConsole/LexerBuilder.cs +++ b/src/UI/CSConsole/LexerBuilder.cs @@ -14,6 +14,7 @@ namespace UnityExplorer.UI.CSConsole public int startIndex; public int endIndex; public string htmlColorTag; + public bool isStringOrComment; } public class LexerBuilder @@ -82,8 +83,10 @@ namespace UnityExplorer.UI.CSConsole /// The last character you want to highlight /// The amount of leading empty lines you want before the first character in the return string. /// A string which contains the amount of leading lines specified, as well as the rich-text highlighted section. - public string BuildHighlightedString(string input, int startIdx, int endIdx, int leadingLines) + public string BuildHighlightedString(string input, int startIdx, int endIdx, int leadingLines, int caretIdx, out bool caretInStringOrComment) { + caretInStringOrComment = false; + if (string.IsNullOrEmpty(input) || endIdx <= startIdx) return input; @@ -105,12 +108,14 @@ namespace UnityExplorer.UI.CSConsole // append the highlighted match sb.Append(match.htmlColorTag); - for (int i = match.startIndex; i <= match.endIndex && i <= currentEndIdx; i++) sb.Append(input[i]); - sb.Append(SignatureHighlighter.CLOSE_COLOR); + // check caretIdx to determine inStringOrComment state + if (caretIdx >= match.startIndex && caretIdx <= match.endIndex) + caretInStringOrComment = match.isStringOrComment; + // update the last unhighlighted start index lastUnhighlighted = match.endIndex + 1; } @@ -150,6 +155,7 @@ namespace UnityExplorer.UI.CSConsole startIndex = startIndex, endIndex = CommittedIndex, htmlColorTag = lexer.ColorTag, + isStringOrComment = lexer is StringLexer || lexer is CommentLexer, }; break; }