Skip to content

Commit

Permalink
IndentationBear: Ignore doc comments
Browse files Browse the repository at this point in the history
This adds a new implementation to ignore all
the doc comments from IndentationBear since
doc comments are not supossed to follow indentation.

Closes coala#644
  • Loading branch information
bkhanale committed Aug 22, 2019
1 parent 7c38b2d commit f9aa8c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 16 additions & 1 deletion bears/general/IndentationBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from coalib.bears.LocalBear import LocalBear
from coalib.bearlib import deprecate_settings
from coalib.bearlib.languages.LanguageDefinition import LanguageDefinition
from coalib.bearlib.languages.documentation.DocBaseClass import (
DocBaseClass)
from coalib.bearlib.spacing.SpacingHelper import SpacingHelper
from coalib.results.SourceRange import SourceRange
from coalib.results.Result import Result, RESULT_SEVERITY
Expand All @@ -11,7 +13,7 @@
from bears.general.AnnotationBear import AnnotationBear


class IndentationBear(LocalBear):
class IndentationBear(DocBaseClass, LocalBear):

AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
Expand All @@ -25,6 +27,7 @@ def run(self,
file,
dependency_results: dict,
language: str,
docstyle: str = None,
use_spaces: bool = True,
indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH,
coalang_dir: str = None,
Expand Down Expand Up @@ -58,6 +61,10 @@ def run(self,
Results given by the AnnotationBear.
:param language:
Language to be used for indentation.
:param docstyle:
The docstyle to use. For example ``default`` or
``doxygen``. Docstyles are language dependent, meaning
not every language is supported by a certain docstyle.
:param use_spaces:
Insert spaces instead of tabs for indentation.
:param indent_size:
Expand All @@ -66,6 +73,10 @@ def run(self,
Full path of external directory containing the coalang
file for language.
"""
doc_comments = []
if docstyle:
doc_comments = self.extract(file, language, docstyle)

lang_settings_dict = LanguageDefinition(
language, coalang_dir=coalang_dir)
annotation_dict = dependency_results[AnnotationBear.name][0].contents
Expand Down Expand Up @@ -118,6 +129,10 @@ def run(self,
if new_file != list(file):
wholediff = Diff.from_string_arrays(file, new_file)
for diff in wholediff.split_diff():
if any(gt_eq(diff.range(filename).start, string.range.start)
and lt_eq(diff.range(filename).end, string.range.end)
for string in doc_comments):
continue
yield Result(
self,
'The indentation could be changed to improve readability.',
Expand Down
10 changes: 10 additions & 0 deletions tests/general/IndentationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,13 @@ def test_invalid_specifiers(self):
valid_file = ('This is a valid specifier: # A comment\n',
'\tand so it indents\n')
self.verify_bear(valid_file)

def test_ignore_doc_comments(self):
test_file = ('"""\n',
' This is not properly indented\n',
' but still should be valid.\n',
'"""')
self.verify_bear(invalid_file=test_file)
self.section.append(Setting('language', 'python'))
self.section.append(Setting('docstyle', 'doxygen'))
self.verify_bear(valid_file=test_file)

0 comments on commit f9aa8c3

Please sign in to comment.