Skip to content

Commit

Permalink
AnnotationBear: Return precise string/comment info
Browse files Browse the repository at this point in the history
Earlier the return used to be either a tuple
of source ranges of strings/comments now it
is a tuple of dicts with keys
'start_delimiter_range', 'end_delimiter_range'
'content_range', 'full_range' of singleline
strings/multiline strings/singleline
comments/multiline comments.
  • Loading branch information
aptrishu committed Oct 6, 2017
1 parent c4b874e commit e6468ee
Show file tree
Hide file tree
Showing 8 changed files with 608 additions and 203 deletions.
1 change: 1 addition & 0 deletions bear-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
HTTPolice~=0.5.2
aenum~=2.0.8
apertium-lint~=0.29
attrs==17.2.0
autoflake~=0.7
autopep8~=1.2
bandit~=1.2
Expand Down
278 changes: 193 additions & 85 deletions bears/general/AnnotationBear.py

Large diffs are not rendered by default.

52 changes: 29 additions & 23 deletions bears/general/IndentationBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ def run(self,
"""
lang_settings_dict = LanguageDefinition(
language, coalang_dir=coalang_dir)
annotation_dict = dependency_results[AnnotationBear.name][0].contents
dep_contents = dependency_results[AnnotationBear.name][0].contents
annotation_dict = {}
annotation_dict['strings'] = (dep_contents.singleline_strings +
dep_contents.multiline_strings)
annotation_dict['comments'] = (dep_contents.singleline_comments +
dep_contents.multiline_comments)
# sometimes can't convert strings with ':' to dict correctly
if ':' in dict(lang_settings_dict['indent_types']).keys():
indent_types = dict(lang_settings_dict['indent_types'])
Expand Down Expand Up @@ -151,8 +156,8 @@ def _get_absolute_indent_file(self,
new_line = (prev_indent + ' '*indent +
insert*(indent_levels[line] - prev_indent_level) +
indented_file[line].lstrip())
indented_file[line] = new_line if new_line.strip() != ''\
else '\n'
indented_file[line] = (new_line if new_line.strip() != ''
else '\n')
return indented_file

def get_absolute_indent_of_range(self,
Expand All @@ -168,7 +173,7 @@ def get_absolute_indent_of_range(self,
:param encaps_pos: A tuple ofSourceRanges of code regions
trapped in between a matching pair of
encapsulators.
:param annotation_dict: A dictionary containing sourceranges of all the
:param annotation_dict: A dictionary containing dicts of all the
strings and comments within a file.
:return: A tuple of tuples with first element as the
range of encapsulator and second element as the
Expand Down Expand Up @@ -196,7 +201,7 @@ def get_indent_levels(self,
:param filename: Name of the file that needs to be checked.
:param indent_types: A dictionary with keys as start of indent and
values as their corresponding closing indents.
:param annotation_dict: A dictionary containing sourceranges of all the
:param annotation_dict: A dictionary containing dicts of all the
strings and comments within a file.
:param encapsulators: A tuple of sourceranges of all encapsulators of
a language.
Expand Down Expand Up @@ -255,7 +260,7 @@ def get_specified_block_range(self,
block has begun.
:param close_specifier: A character or string indicating that the block
has ended.
:param annotation_dict: A dictionary containing sourceranges of all the
:param annotation_dict: A dictionary containing dicts of all the
strings and comments within a file.
:return: A tuple with the first source range being
the range of the outermost indentation while
Expand Down Expand Up @@ -324,7 +329,7 @@ def get_unspecified_block_range(self,
:param filename: Name of the file that needs to be checked.
:param indent_specifier: A character or string indicating that the
indentation should begin.
:param annotation_dict: A dictionary containing sourceranges of all the
:param annotation_dict: A dictionary containing dicts of all the
strings and comments within a file.
:param encapsulators: A tuple of sourceranges of all encapsulators of
a language.
Expand Down Expand Up @@ -372,7 +377,7 @@ def get_valid_sequences(file,
:param file: File that needs to be checked in the form of
a list of strings.
:param sequence: Sequence whose validity is to be checked.
:param annotation_dict: A dictionary containing sourceranges of all the
:param annotation_dict: A dictionary containing dicts of all the
strings and comments within a file.
:param encapsulators: A tuple of SourceRanges of code regions
trapped in between a matching pair of
Expand All @@ -394,22 +399,23 @@ def get_valid_sequences(file,

# ignore if within string
for string in annotation_dict['strings']:
if(gt_eq(sequence_position, string.start) and
lt_eq(sequence_position, string.end)):
if(gt_eq(sequence_position, string.full_range.start) and
lt_eq(sequence_position, string.full_range.end)):
valid = False

# ignore if within comments
for comment in annotation_dict['comments']:
if(gt_eq(sequence_position, comment.start) and
lt_eq(sequence_position, comment.end)):
if(gt_eq(sequence_position, comment.full_range.start) and
lt_eq(sequence_position, comment.full_range.end)):
valid = False

if(comment.start.line == sequence_position.line and
comment.end.line == sequence_position.line and
check_ending):
sequence_line_text = sequence_line_text[
:comment.start.column - 1] + sequence_line_text[
comment.end.column-1:]
if(comment.full_range.start.line == sequence_position.line
and comment.full_range.end.line ==
sequence_position.line and check_ending):
sequence_line_text = (sequence_line_text[
:comment.full_range.start.column - 1] +
sequence_line_text[comment.full_range.end.column-1:
])

if encapsulators:
for encapsulator in encapsulators:
Expand Down Expand Up @@ -462,7 +468,7 @@ def get_first_unindent(indent,
:param file: A tuple of strings.
:param start_line: The line from where to start searching for
unindent.
:param annotation_dict: A dictionary containing sourceranges of all the
:param annotation_dict: A dictionary containing dicts of all the
strings and comments within a file.
:param encapsulators: A tuple of SourceRanges of code regions trapped in
between a matching pair of encapsulators.
Expand All @@ -476,12 +482,12 @@ def get_first_unindent(indent,
valid = True

for comment in annotation_dict['comments']:
if(comment.start.line < line_nr + 1 and
comment.end.line >= line_nr + 1):
if(comment.full_range.start.line < line_nr + 1 and
comment.full_range.end.line >= line_nr + 1):
valid = False

first_char = file[line_nr].lstrip()[0] if file[line_nr].strip()\
else ''
first_char = (file[line_nr].lstrip()[0] if file[line_nr].strip()
else '')
if first_char in comments:
valid = False

Expand Down
8 changes: 7 additions & 1 deletion bears/general/KeywordBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ def _get_comments(dependency_results):
if isinstance(result.contents, str):
logging.error(result.contents)
else:
yield from result.contents.get('comments', [])
dep_contents = result.contents
annotation_dict = {}
annotation_dict['comments'] = \
(dep_contents.singleline_comments +
dep_contents.multiline_comments)
yield from [range.full_range
for range in annotation_dict['comments']]


def generate_diff(comments, file, filename,
Expand Down
22 changes: 14 additions & 8 deletions bears/general/QuotesBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,27 @@ def run(self, filename, file, dependency_results,
:param preferred_quotation: Your preferred quotation character, e.g.
``"`` or ``'``.
"""
if not isinstance(dependency_results[AnnotationBear.name][0],
HiddenResult):
if (not dependency_results or
not isinstance(dependency_results[AnnotationBear.name],
HiddenResult)):
return
if isinstance(dependency_results[AnnotationBear.name][0].contents,
if isinstance(dependency_results[AnnotationBear.name].contents,
str):
self.err(dependency_results[AnnotationBear.name][0].contents)
self.err(dependency_results[AnnotationBear.name].contents)
return

ranges = dependency_results[AnnotationBear.name][0].contents['strings']
dep_contents = dependency_results[AnnotationBear.name].contents
annotation_dict = {}
annotation_dict['strings'] = (dep_contents.singleline_strings +
dep_contents.multiline_strings)
ranges = annotation_dict['strings']

for string_range in ranges:
if (file[string_range.start.line-1][string_range.start.column-1] ==
temp_range = string_range.full_range
if (file[temp_range.start.line-1][temp_range.start.column-1] ==
preferred_quotation):
continue

if string_range.start.line == string_range.end.line:
if temp_range.start.line == temp_range.end.line:
yield from self.correct_single_line_str(
filename, file, string_range, preferred_quotation)
filename, file, temp_range, preferred_quotation)
Loading

0 comments on commit e6468ee

Please sign in to comment.