Use whitespace as delimiter in completion composition, handle string/comment state with Lexer

This commit is contained in:
Sinai 2021-06-01 16:40:51 +10:00
parent f35beeaf58
commit 8d8c9ac7c9
3 changed files with 26 additions and 14 deletions

View File

@ -59,11 +59,9 @@ namespace UnityExplorer.UI.CSConsole
{ {
startIdx--; startIdx--;
char c = InputField.Text[startIdx]; char c = InputField.Text[startIdx];
if (delimiters.Contains(c)) if (delimiters.Contains(c) || char.IsWhiteSpace(c))
{ {
startIdx++; startIdx++;
while (char.IsWhiteSpace(InputField.Text[startIdx]))
startIdx++;
break; break;
} }
} }

View File

@ -231,20 +231,24 @@ namespace UnityExplorer.UI.CSConsole
previousInput = value; previousInput = value;
if (EnableSuggestions && AutoCompleteModal.CheckEnter(Completer)) if (EnableSuggestions && AutoCompleteModal.CheckEnter(Completer))
{
OnAutocompleteEnter(); OnAutocompleteEnter();
}
else if (!settingCaretCoroutine) var inStringOrComment = HighlightVisibleInput();
if (!settingCaretCoroutine)
{ {
if (EnableSuggestions) if (EnableSuggestions)
{
if (inStringOrComment)
AutoCompleteModal.Instance.ReleaseOwnership(Completer);
else
Completer.CheckAutocompletes(); Completer.CheckAutocompletes();
}
if (EnableAutoIndent) if (EnableAutoIndent)
DoAutoIndent(); DoAutoIndent();
} }
HighlightVisibleInput();
UpdateCaret(out _); UpdateCaret(out _);
} }
@ -372,7 +376,10 @@ namespace UnityExplorer.UI.CSConsole
#region Lexer Highlighting #region Lexer Highlighting
private static void HighlightVisibleInput() /// <summary>
/// Returns true if caret is inside string or comment, false otherwise
/// </summary>
private static bool HighlightVisibleInput()
{ {
int startIdx = 0; int startIdx = 0;
int endIdx = Input.Text.Length - 1; int endIdx = Input.Text.Length - 1;
@ -410,7 +417,8 @@ namespace UnityExplorer.UI.CSConsole
} }
// Highlight the visible text with the LexerBuilder // 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 #endregion

View File

@ -14,6 +14,7 @@ namespace UnityExplorer.UI.CSConsole
public int startIndex; public int startIndex;
public int endIndex; public int endIndex;
public string htmlColorTag; public string htmlColorTag;
public bool isStringOrComment;
} }
public class LexerBuilder public class LexerBuilder
@ -82,8 +83,10 @@ namespace UnityExplorer.UI.CSConsole
/// <param name="endIdx">The last character you want to highlight</param> /// <param name="endIdx">The last character you want to highlight</param>
/// <param name="leadingLines">The amount of leading empty lines you want before the first character in the return string.</param> /// <param name="leadingLines">The amount of leading empty lines you want before the first character in the return string.</param>
/// <returns>A string which contains the amount of leading lines specified, as well as the rich-text highlighted section.</returns> /// <returns>A string which contains the amount of leading lines specified, as well as the rich-text highlighted section.</returns>
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) if (string.IsNullOrEmpty(input) || endIdx <= startIdx)
return input; return input;
@ -105,12 +108,14 @@ namespace UnityExplorer.UI.CSConsole
// append the highlighted match // append the highlighted match
sb.Append(match.htmlColorTag); sb.Append(match.htmlColorTag);
for (int i = match.startIndex; i <= match.endIndex && i <= currentEndIdx; i++) for (int i = match.startIndex; i <= match.endIndex && i <= currentEndIdx; i++)
sb.Append(input[i]); sb.Append(input[i]);
sb.Append(SignatureHighlighter.CLOSE_COLOR); 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 // update the last unhighlighted start index
lastUnhighlighted = match.endIndex + 1; lastUnhighlighted = match.endIndex + 1;
} }
@ -150,6 +155,7 @@ namespace UnityExplorer.UI.CSConsole
startIndex = startIndex, startIndex = startIndex,
endIndex = CommittedIndex, endIndex = CommittedIndex,
htmlColorTag = lexer.ColorTag, htmlColorTag = lexer.ColorTag,
isStringOrComment = lexer is StringLexer || lexer is CommentLexer,
}; };
break; break;
} }