Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnnotationBear: Use unescaped_search_for #996

Merged
merged 1 commit into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions bears/general/AnnotationBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from coalib.results.Result import Result, RESULT_SEVERITY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find -> Find

Copy link
Member

@Nosferatul Nosferatul Nov 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before Fixes.. 😉

Copy link
Member Author

@abh3po abh3po Nov 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh right, though this is a really awkward place for that review dont you think? ;) .
shouldn't it be on that main PR page?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw many times review like this

Copy link
Member Author

@abh3po abh3po Nov 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well a line note (although i dont know if they still call it that), should be for that line only right? though occasionally i guess its fine, so i'll leave it there :P

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):
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions tests/general/AnnotationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,))