Skip to content

Commit

Permalink
IgnoreResultAction: Add Multiline comment support
Browse files Browse the repository at this point in the history
Adds support for using multiline comment delimiters, if the
language does not have singleline delimiters.
If no delimiters are found, emits a warning.

Closes #coala#3441
  • Loading branch information
nemani authored and CosminIcatoiu committed Jan 9, 2017
1 parent 73c8393 commit 6764abb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
48 changes: 44 additions & 4 deletions coalib/results/result_actions/IgnoreResultAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from os.path import exists
from os.path import isfile
import shutil
import logging

from coala_utils.decorators import enforce_signature

Expand Down Expand Up @@ -36,10 +37,11 @@ def apply(self, result, original_file_dict, file_diff_dict, language: str,
"""
Add ignore comment
"""
comment_delimiter = Language[
language].get_default_version().comment_delimiter
ignore_comment = (str(comment_delimiter) + ' Ignore ' + result.origin +
'\n')

ignore_comment = self.get_ignore_comment(result.origin, language)

if not ignore_comment:
return file_diff_dict

source_range = next(filter(lambda sr: exists(sr.file),
result.affected_code))
Expand All @@ -65,3 +67,41 @@ def apply(self, result, original_file_dict, file_diff_dict, language: str,
file.writelines(ignore_diff.modified)

return file_diff_dict

def get_ignore_comment(self, origin, language):
r"""
Returns a string of Ignore Comment, depending on the language
Supports Single Line Comments
>>> IgnoreResultAction().get_ignore_comment("Bear", "css")
'/* Ignore Bear */\n'
And Multiline Comments
>>> IgnoreResultAction().get_ignore_comment("Bear", "c")
'// Ignore Bear\n'
"""
try:
comment_delimiter = Language[
language].get_default_version().comment_delimiter
ignore_comment = (str(comment_delimiter) + ' Ignore ' +
origin + '\n')
except AttributeError:
# singleline comments not supported by language
try:
multiline_comment_delimiter = Language[
language].get_default_version().multiline_comment_delimiters
start_comment, end_comment = next(iter(
multiline_comment_delimiter.items()))
ignore_comment = (str(start_comment) + ' Ignore ' +
origin + ' ' +
str(end_comment) + '\n')
except AttributeError:
# multiline comments also not supported by language
logging.warning(
'coala does not support Ignore in "{language}". Consider'
' opening an issue at https://github.com/coala/coala/issues'
' so we can add support for this language.'.format(
language=language))
ignore_comment = None
return ignore_comment
9 changes: 9 additions & 0 deletions docs/Developers/Newcomers_Guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ Now you can start working on it.

.. note::

As stated before, you should never work on an issue without any
assignment. Fortunately, cobot is here to help you! So, if you are
interested in picking up an issue just write in the gitter chat the
following command::

cobot assign <issue_link>

Take care to write the full link to the issue

Before starting to write your first commit, check out this
link: `Writing good commits <http://coala.io/commit>`_.

Expand Down
18 changes: 16 additions & 2 deletions tests/results/result_actions/IgnoreResultActionTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,23 @@ def test_ignore(self):

# Apply a second patch, old patch has to stay!
uut.apply(Result.from_values('else', 'msg', f_a, 1),
file_dict, file_diff_dict, 'c')
file_dict, file_diff_dict, 'css')
self.assertEqual(
file_diff_dict[f_a].modified,
['1 // Ignore else\n', '2 // Ignore origin\n', '3\n'])
['1 /* Ignore else */\n', '2 // Ignore origin\n', '3\n'])
with open(f_a, 'r') as f:
self.assertEqual(file_diff_dict[f_a].modified, f.readlines())

import logging
logger = logging.getLogger()

with unittest.mock.patch('subprocess.call'):
with self.assertLogs(logger, 'WARNING') as log:
uut.apply(Result.from_values('else', 'msg', f_a, 1),
file_dict, file_diff_dict, 'dothraki')

self.assertEqual(1, len(log.output))
self.assertIn(
'coala does not support Ignore in "dothraki".',
log.output[0]
)

0 comments on commit 6764abb

Please sign in to comment.