From e635c33d0d336e7be54336ed2d6e043470f00398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 19 Sep 2023 11:40:54 +0200 Subject: Fix "string mode". Previously parenthesis were (incorrectly) highlighted even in strings. Now they are correctly ignored inside. --- rainbow_parenthesis/__init__.py | 73 ++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 33 deletions(-) (limited to 'rainbow_parenthesis') diff --git a/rainbow_parenthesis/__init__.py b/rainbow_parenthesis/__init__.py index 1f5e2f7..e665bdb 100644 --- a/rainbow_parenthesis/__init__.py +++ b/rainbow_parenthesis/__init__.py @@ -80,39 +80,46 @@ def colorize(strm: io.TextIOBase) -> Generator[str | Colored, None, None]: depth = Stackpointer() while c := strm.read(1): - match c: - case '(': - yield color(depth(1), c) - paren += 1 - case ')': - yield color(depth(-1), c) - paren -= 1 - case '[': - yield color(depth(1), c) - brack += 1 - case ']': - yield color(depth(-1), c) - brack -= 1 - case '{': - yield color(depth(1), c) - brace += 1 - case '}': - yield color(depth(-1), c) - brace -= 1 - case "'": - if in_string == "'": - in_string = False - else: - in_string = "'" + match [in_string, c]: + case ['"', '"']: + # The string is ending + in_string = False yield c - case '"': - if in_string == '"': - in_string = False - else: - in_string = '"' + case ["'", "'"]: + in_string = False + # The string is ending yield c - case '\\': - yield c - yield strm.read(1) - case c: + case [False, _]: + # We are not in a string, emit the next character + # colored if it's a parenthesis (or similar), and + # plainly otherwise. + # Can also ether strings + match c: + case '(': + yield color(depth(1), c) + paren += 1 + case ')': + yield color(depth(-1), c) + paren -= 1 + case '[': + yield color(depth(1), c) + brack += 1 + case ']': + yield color(depth(-1), c) + brack -= 1 + case '{': + yield color(depth(1), c) + brace += 1 + case '}': + yield color(depth(-1), c) + brace -= 1 + case "'" | '"': + in_string = c + yield c + case '\\': + yield c + yield strm.read(1) + case c: + yield c + case [_, _]: yield c -- cgit v1.2.3