Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 8d8c9ac

Browse files
committed
Use whitespace as delimiter in completion composition, handle string/comment state with Lexer
1 parent f35beea commit 8d8c9ac

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/UI/CSConsole/CSAutoCompleter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ public void CheckAutocompletes()
5959
{
6060
startIdx--;
6161
char c = InputField.Text[startIdx];
62-
if (delimiters.Contains(c))
62+
if (delimiters.Contains(c) || char.IsWhiteSpace(c))
6363
{
6464
startIdx++;
65-
while (char.IsWhiteSpace(InputField.Text[startIdx]))
66-
startIdx++;
6765
break;
6866
}
6967
}

src/UI/CSConsole/ConsoleController.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,24 @@ private static void OnInputChanged(string value)
231231
previousInput = value;
232232

233233
if (EnableSuggestions && AutoCompleteModal.CheckEnter(Completer))
234-
{
235234
OnAutocompleteEnter();
236-
}
237-
else if (!settingCaretCoroutine)
235+
236+
var inStringOrComment = HighlightVisibleInput();
237+
238+
if (!settingCaretCoroutine)
238239
{
239240
if (EnableSuggestions)
240-
Completer.CheckAutocompletes();
241+
{
242+
if (inStringOrComment)
243+
AutoCompleteModal.Instance.ReleaseOwnership(Completer);
244+
else
245+
Completer.CheckAutocompletes();
246+
}
241247

242248
if (EnableAutoIndent)
243249
DoAutoIndent();
244250
}
245251

246-
HighlightVisibleInput();
247-
248252
UpdateCaret(out _);
249253
}
250254

@@ -372,7 +376,10 @@ private static IEnumerator SetAutocompleteCaretCoro(int caretPosition)
372376

373377
#region Lexer Highlighting
374378

375-
private static void HighlightVisibleInput()
379+
/// <summary>
380+
/// Returns true if caret is inside string or comment, false otherwise
381+
/// </summary>
382+
private static bool HighlightVisibleInput()
376383
{
377384
int startIdx = 0;
378385
int endIdx = Input.Text.Length - 1;
@@ -410,7 +417,8 @@ private static void HighlightVisibleInput()
410417
}
411418

412419
// Highlight the visible text with the LexerBuilder
413-
Panel.HighlightText.text = Lexer.BuildHighlightedString(Input.Text, startIdx, endIdx, topLine);
420+
Panel.HighlightText.text = Lexer.BuildHighlightedString(Input.Text, startIdx, endIdx, topLine, LastCaretPosition, out bool ret);
421+
return ret;
414422
}
415423

416424
#endregion

src/UI/CSConsole/LexerBuilder.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public struct MatchInfo
1414
public int startIndex;
1515
public int endIndex;
1616
public string htmlColorTag;
17+
public bool isStringOrComment;
1718
}
1819

1920
public class LexerBuilder
@@ -82,8 +83,10 @@ public LexerBuilder()
8283
/// <param name="endIdx">The last character you want to highlight</param>
8384
/// <param name="leadingLines">The amount of leading empty lines you want before the first character in the return string.</param>
8485
/// <returns>A string which contains the amount of leading lines specified, as well as the rich-text highlighted section.</returns>
85-
public string BuildHighlightedString(string input, int startIdx, int endIdx, int leadingLines)
86+
public string BuildHighlightedString(string input, int startIdx, int endIdx, int leadingLines, int caretIdx, out bool caretInStringOrComment)
8687
{
88+
caretInStringOrComment = false;
89+
8790
if (string.IsNullOrEmpty(input) || endIdx <= startIdx)
8891
return input;
8992

@@ -105,12 +108,14 @@ public string BuildHighlightedString(string input, int startIdx, int endIdx, int
105108

106109
// append the highlighted match
107110
sb.Append(match.htmlColorTag);
108-
109111
for (int i = match.startIndex; i <= match.endIndex && i <= currentEndIdx; i++)
110112
sb.Append(input[i]);
111-
112113
sb.Append(SignatureHighlighter.CLOSE_COLOR);
113114

115+
// check caretIdx to determine inStringOrComment state
116+
if (caretIdx >= match.startIndex && caretIdx <= match.endIndex)
117+
caretInStringOrComment = match.isStringOrComment;
118+
114119
// update the last unhighlighted start index
115120
lastUnhighlighted = match.endIndex + 1;
116121
}
@@ -150,6 +155,7 @@ public IEnumerable<MatchInfo> GetMatches()
150155
startIndex = startIndex,
151156
endIndex = CommittedIndex,
152157
htmlColorTag = lexer.ColorTag,
158+
isStringOrComment = lexer is StringLexer || lexer is CommentLexer,
153159
};
154160
break;
155161
}

0 commit comments

Comments
 (0)