From e68e893aa4693c5ebe56082fe81281fbcfd0a8d0 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 | 24 ++++++++++++++++-------- tests/general/AnnotationBearTest.py | 10 ++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bears/general/AnnotationBear.py b/bears/general/AnnotationBear.py index f84007a04f..684898a5a5 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,8 @@ 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: + end_end = get_end_position(annotation_end, text, position) + if end_end == -1: _range = SourceRange.from_absolute_position( filename, AbsolutePosition(file, position)) @@ -238,9 +237,8 @@ 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) + end_position = get_end_position(string_end, text, position) + newline = get_end_position("\n", text, position) if newline == -1: newline = len(text) if end_position == -1: @@ -273,7 +271,7 @@ 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) + end_position = get_end_position("\n", text, position) if end_position == -1: end_position = len(text) - 1 return (SourceRange.from_absolute_position( @@ -283,6 +281,16 @@ def get_singleline_comment(file, filename, text, comment, position): end_position) +def get_end_position(end_marker, text, position): + try: + end_match = next(unescaped_search_for(end_marker, text[position + 1:])) + end_position = position + end_match.span()[1] + except StopIteration: + end_position = -1 + + return end_position + + class NoCloseError(Exception): def __init__(self, annotation, 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,))