From 6764abb11cbfc33ea71e3107216c9e60b5270882 Mon Sep 17 00:00:00 2001 From: Arjun Nemani Date: Thu, 5 Jan 2017 23:56:50 +0530 Subject: [PATCH] IgnoreResultAction: Add Multiline comment support Adds support for using multiline comment delimiters, if the language does not have singleline delimiters. If no delimiters are found, emits a warning. Closes #https://github.com/coala/coala/issues/3441 --- .../result_actions/IgnoreResultAction.py | 48 +++++++++++++++++-- docs/Developers/Newcomers_Guide.rst | 9 ++++ .../result_actions/IgnoreResultActionTest.py | 18 ++++++- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/coalib/results/result_actions/IgnoreResultAction.py b/coalib/results/result_actions/IgnoreResultAction.py index 124dab8a5f..69286f0733 100644 --- a/coalib/results/result_actions/IgnoreResultAction.py +++ b/coalib/results/result_actions/IgnoreResultAction.py @@ -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 @@ -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)) @@ -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 diff --git a/docs/Developers/Newcomers_Guide.rst b/docs/Developers/Newcomers_Guide.rst index 1754999bd9..3d090abe98 100644 --- a/docs/Developers/Newcomers_Guide.rst +++ b/docs/Developers/Newcomers_Guide.rst @@ -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 + + Take care to write the full link to the issue + Before starting to write your first commit, check out this link: `Writing good commits `_. diff --git a/tests/results/result_actions/IgnoreResultActionTest.py b/tests/results/result_actions/IgnoreResultActionTest.py index 8a4fd27570..9e52f2d299 100644 --- a/tests/results/result_actions/IgnoreResultActionTest.py +++ b/tests/results/result_actions/IgnoreResultActionTest.py @@ -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] + )