From e4fb6789a9f43cb4719805a5f66ac7c4e6f1e74e Mon Sep 17 00:00:00 2001 From: Abhay Raizada Date: Tue, 15 Nov 2016 03:18:40 +0530 Subject: [PATCH] AnnotationBear: Use unescaped_search_for find function doesn't ignore escape sequences hence we use unescaped_search_for to ignore the sequences which are escaped. Fixes https://github.com/coala/coala-bears/issues/993 --- bears/general/AnnotationBear.py | 36 ++++++++++++++++++++++------- tests/general/AnnotationBearTest.py | 11 +++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/bears/general/AnnotationBear.py b/bears/general/AnnotationBear.py index f84007a04f..fcf54a43f7 100644 --- a/bears/general/AnnotationBear.py +++ b/bears/general/AnnotationBear.py @@ -4,6 +4,7 @@ from coalib.results.Result import Result, RESULT_SEVERITY from coalib.results.SourceRange import SourceRange from coalib.results.AbsolutePosition import AbsolutePosition +from coala_utils.string_processing.Core import unescaped_search_for class AnnotationBear(LocalBear): @@ -199,10 +200,14 @@ def get_multiline(file, A SourceRange object holding the range of the multi-line annotation and the end_position of the annotation as an integer. """ - end_start = text.find(annotation_end, - position + 1) - end_end = end_start + len(annotation_end) - 1 - if end_start == -1: + try: + end_match = next(unescaped_search_for(annotation_end, + text[position + 1:])) + except StopIteration: + end_match = None + print(end_match) + end_end = position + end_match.span()[1] if end_match else -1 + if end_end == -1: _range = SourceRange.from_absolute_position( filename, AbsolutePosition(file, position)) @@ -238,9 +243,19 @@ def get_singleline_strings(file, A SourceRange object identifying the range of the single-line string and the end_position of the string as an integer. """ - end_position = (text.find(string_end, position + 1) - + len(string_end) - 1) - newline = text.find("\n", position + 1) + try: + end_match = next(unescaped_search_for( + string_end, text[position + 1:])) + except StopIteration: + end_match = None + end_position = (position + end_match.span()[1] if end_match + else -1) + try: + newline_match = next( + unescaped_search_for("\n", text[position + 1:])) + except StopIteration: + newline_match = None + newline = position + newline_match.span()[1] if newline_match else -1 if newline == -1: newline = len(text) if end_position == -1: @@ -273,7 +288,12 @@ def get_singleline_comment(file, filename, text, comment, position): A SourceRange object identifying the range of the single-line comment and the end_position of the comment as an integer. """ - end_position = text.find("\n", position + 1) + try: + end_match = next(unescaped_search_for("\n", text[position + 1:])) + except StopIteration: + end_match = None + end_position = (position + end_match.span()[1] if end_match + else -1) if end_position == -1: end_position = len(text) - 1 return (SourceRange.from_absolute_position( diff --git a/tests/general/AnnotationBearTest.py b/tests/general/AnnotationBearTest.py index fbb967629d..653a996c8b 100644 --- a/tests/general/AnnotationBearTest.py +++ b/tests/general/AnnotationBearTest.py @@ -140,3 +140,14 @@ def test_no_coalang(self): with execute_bear(uut, "F", text) as result: self.assertEqual(result[0].contents, "coalang specification for Valyrian not found.") + + def test_escape_strings(self): + text = [r"'I\'ll be back' -T1000"] + uut = AnnotationBear(self.section1, Queue()) + test_range = SourceRange.from_absolute_position( + "F", + AbsolutePosition(text, 0), + AbsolutePosition(text, text[0].find("'", 4))) + with execute_bear(uut, "F", text) as result: + print(result[0].contents['strings'], test_range) + self.assertEqual(result[0].contents["strings"], (test_range,))