Skip to content

Commit

Permalink
Handle cases of pure text addition (except for adding a line to the e…
Browse files Browse the repository at this point in the history
…nd of the file)
  • Loading branch information
oleg-derevenetz authored and platisd committed Nov 24, 2023
1 parent 2393cb9 commit 121c8f8
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions run_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,28 +243,6 @@ def generate_single_comment(
diag_message_replacements = diag_message["Replacements"]

for file_path in {item["FilePath"] for item in diag_message_replacements}:
# The following logic is designed to work with changes of the type:
#
# + changed line
# - original line
# [...] (in different combinations)
#
# as well as completely removed lines:
#
# - original line
# - original line
# [...]
#
# but it does not support cases like this:
#
# + new line
# [...]
#
# In my defense, I can say that I have never seen fixes.yml from Clang-Tidy,
# which would simply add new lines to the file.
#
# In case such a change does occur, assertion statements have been added.

line_num = 1
start_line_num = None
end_line_num = None
Expand Down Expand Up @@ -311,9 +289,18 @@ def generate_single_comment(
# end of the section to replace
elif line.startswith(" "):
if replacement_text is not None:
assert (
start_line_num is not None and end_line_num is not None
)
# If there is a replacement text, but there is no information about
# the section to replace, then this is not a replacement, but a pure
# addition of text. Add the current line to the end of the replacement
# text and "replace" it with the replacement text.
if start_line_num is None:
assert end_line_num is None

start_line_num = line_num
end_line_num = line_num
replacement_text += line[2:]
else:
assert end_line_num is not None

print(
# pylint: disable=line-too-long
Expand Down Expand Up @@ -348,7 +335,12 @@ def generate_single_comment(

# The end of the file is reached, but there is a section to replace
if replacement_text is not None:
assert start_line_num is not None and end_line_num is not None
# Pure addition of text to the end of the file is not currently supported. If
# you have an example of a Clang-Tidy replacement of this kind, please contact
# the repository maintainer.
assert (
start_line_num is not None and end_line_num is not None
), "Please report this to the repository maintainer"

print(
# pylint: disable=line-too-long
Expand Down

0 comments on commit 121c8f8

Please sign in to comment.