forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bears: Remove leading blank line option
Implemented a feature for spaceconsistencyBear that control the removal of blank lines. Closes coala#2207
- Loading branch information
1 parent
6d4ae18
commit eaa2de4
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ def run(self, | |
file, | ||
use_spaces: bool, | ||
allow_trailing_whitespace: bool = False, | ||
allow_leading_blanklines: bool = False, | ||
indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH, | ||
enforce_newline_at_EOF: bool = True, | ||
): | ||
|
@@ -34,13 +35,58 @@ def run(self, | |
Number of spaces per indentation level. | ||
:param enforce_newline_at_EOF: | ||
Whether to enforce a newline at the End Of File. | ||
:param allow_leading_blanklines: | ||
Whether to allow leading blank lines at the start | ||
of file or not. | ||
''' | ||
spacing_helper = SpacingHelper(indent_size) | ||
result_texts = [] | ||
additional_info_texts = [] | ||
|
||
for line_number, line in enumerate(file, start=1): | ||
replacement = line | ||
def end_blanklines(): | ||
This comment has been minimized.
Sorry, something went wrong. |
||
end_line = False | ||
This comment has been minimized.
Sorry, something went wrong.
Naveenaidu
|
||
enumerated_zip_obj = zip(range(1, len(file) + 1), | ||
file) | ||
enumerated_tuple = tuple(enumerated_zip_obj) | ||
|
||
for line_number, line in enumerated_tuple: | ||
if replacement.strip() == '': | ||
This comment has been minimized.
Sorry, something went wrong. |
||
end_line = line_number | ||
else: | ||
break | ||
|
||
return end_line | ||
|
||
if allow_leading_blanklines: | ||
start_line_of_file = 1 | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
else: | ||
end_blanklines = end_blanklines() | ||
start_line_of_file = 1 | ||
if end_blanklines: | ||
start_line_of_file = end_blanklines + 1 | ||
result_texts.append('Leading blank lines.') | ||
additional_info_texts.append( | ||
'Your source code contains leading blank lines.' | ||
'Those usually have no meaning. Please consider ' | ||
'removing them.') | ||
diff = Diff(file) | ||
diff.delete_lines(1, end_blanklines) | ||
inconsistencies = ''.join('\n- ' + string | ||
for string in result_texts) | ||
yield Result.from_values( | ||
self, | ||
'Line contains following spacing inconsistencies:' | ||
+ inconsistencies, | ||
diffs={filename: diff}, | ||
file=filename, | ||
additional_info='\n\n'.join(additional_info_texts)) | ||
result_texts = [] | ||
additional_info_texts = [] | ||
|
||
for line_number, line in enumerate(file[start_line_of_file - 1:], | ||
start=start_line_of_file): | ||
replacement = line | ||
|
||
if enforce_newline_at_EOF: | ||
# Since every line contains at the end at least one \n, only | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Please add a comment under the function which states the role about the function. It becomes easier to review