Skip to content

Commit

Permalink
Merge pull request #651 from Codium-ai/tr/br_inside_code
Browse files Browse the repository at this point in the history
insert_br_after_x_chars can already handle code
  • Loading branch information
mrT23 authored Feb 11, 2024
2 parents 8800bad + c75413f commit 79dac3f
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions pr_agent/tools/pr_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ def process_pr_files_prediction(self, pr_body, value):
for filename, file_changes_title, file_change_description in list_tuples:
filename = filename.replace("'", "`").rstrip()
filename_publish = filename.split("/")[-1]
file_changes_title_br = insert_br_after_x_chars(file_changes_title, x=(delta - 5))
file_changes_title_extended = file_changes_title_br.strip() + "</code>"
if len(file_changes_title_extended) < (delta - 5):
file_changes_title_extended += "&nbsp; " * ((delta - 5) - len(file_changes_title_extended))
filename_publish = f"<strong>{filename_publish}</strong><dd><code>{file_changes_title_extended}</dd>"
file_changes_title_code = f"<code>{file_changes_title}</code>"
file_changes_title_code_br = insert_br_after_x_chars(file_changes_title_code, x=(delta - 5)).strip()
if len(file_changes_title_code_br) < (delta - 5):
file_changes_title_code_br += "&nbsp; " * ((delta - 5) - len(file_changes_title_code_br))
filename_publish = f"<strong>{filename_publish}</strong><dd>{file_changes_title_code_br}</dd>"
diff_plus_minus = ""
delta_nbsp = ""
diff_files = self.git_provider.diff_files
Expand Down Expand Up @@ -431,12 +431,20 @@ def process_pr_files_prediction(self, pr_body, value):
pass
return pr_body


def count_chars_without_html(string):
if '<' not in string:
return len(string)
no_html_string = re.sub('<[^>]+>', '', string)
return len(no_html_string)


def insert_br_after_x_chars(text, x=70):
"""
Insert <br> into a string after a word that increases its length above x characters.
Use proper HTML tags for code and new lines.
"""
if len(text) < x:
if count_chars_without_html(text) < x:
return text

# replace odd instances of ` with <code> and even instances of ` with </code>
Expand All @@ -458,12 +466,6 @@ def insert_br_after_x_chars(text, x=70):
if i < len(lines) - 1:
words[-1] += "<br>"

def count_chars_without_html(string):
if '<' not in string:
return len(string)
no_html_string = re.sub('<[^>]+>', '', string)
return len(no_html_string)

new_text = []
is_inside_code = False
current_length = 0
Expand Down

0 comments on commit 79dac3f

Please sign in to comment.