From 0156096bb8c31d1350363b3ea79b72367eef36d0 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 | 35 +++++++++++++++++++++-------- tests/general/AnnotationBearTest.py | 10 +++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/bears/general/AnnotationBear.py b/bears/general/AnnotationBear.py index f84007a04f..05ecf7ad75 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,13 @@ 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:])) + end_end = position + end_match.span()[1] + except StopIteration: + end_end = -1 + if end_end == -1: _range = SourceRange.from_absolute_position( filename, AbsolutePosition(file, position)) @@ -238,9 +242,18 @@ 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:])) + end_position = (position + end_match.span()[1]) + except StopIteration: + end_position = -1 + try: + newline_match = next( + unescaped_search_for("\n", text[position + 1:])) + newline = position + newline_match.span()[1] + except StopIteration: + newline = -1 if newline == -1: newline = len(text) if end_position == -1: @@ -273,7 +286,11 @@ 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:])) + end_position = position + end_match.span()[1] + except StopIteration: + end_position = -1 if end_position == -1: end_position = len(text) - 1 return (SourceRange.from_absolute_position( @@ -287,4 +304,4 @@ class NoCloseError(Exception): def __init__(self, annotation, code): Exception.__init__(self, annotation + " has no closure") - self.code = code + self.code=code diff --git a/tests/general/AnnotationBearTest.py b/tests/general/AnnotationBearTest.py index fbb967629d..bffb8ef807 100644 --- a/tests/general/AnnotationBearTest.py +++ b/tests/general/AnnotationBearTest.py @@ -140,3 +140,13 @@ 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: + self.assertEqual(result[0].contents["strings"], (test_range,))