-
Notifications
You must be signed in to change notification settings - Fork 633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement of Code Feedback Formatting in utils.py #515
Conversation
PR Analysis(review updated until commit 654938f)
PR Feedback
|
Persistent review updated to latest commit 654938f |
PR Code Suggestions💡 Suggestion: Instead of using string concatenation for building the markdown text, consider using a list of strings and join them at the end. This can improve performance as string concatenation in Python creates a new string and can be costly for large strings. File: pr_agent/algo/utils.py (94-140) Example code:Existing code: markdown_text = ""
if gfm_supported and 'relevant line' in code_suggestions:
markdown_text += '<table>'
relevant_file = ''
for sub_key, sub_value in code_suggestions.items():
try:
if sub_key.lower() == 'relevant file':
relevant_file = sub_value.strip('`').strip('"').strip("'")
markdown_text += f"<tr><td>{sub_key}</td><td>{relevant_file}</td></tr>"
elif sub_key.lower() == 'suggestion':
markdown_text += f"<tr><td>{sub_key} </td><td><strong>{sub_value}</strong></td></tr>"
elif sub_key.lower() == 'relevant line':
markdown_text += f"<tr><td>relevant line</td>"
sub_value_list = sub_value.split('](')
relevant_line = sub_value_list[0].lstrip('`').lstrip('[')
if len(sub_value_list) > 1:
link = sub_value_list[1].rstrip(')').strip('`')
markdown_text += f"<td><a href={link}>{relevant_line}</a></td>"
else:
markdown_text += f"<td>{relevant_line}</td>"
markdown_text += "</tr>"
except Exception as e:
get_logger().exception(f"Failed to parse code suggestion: {e}")
pass
markdown_text += '</table>'
markdown_text += "<hr>"
else:
for sub_key, sub_value in code_suggestions.items():
if isinstance(sub_value, dict): # "code example"
markdown_text += f" - **{sub_key}:**\n"
for code_key, code_value in sub_value.items(): # 'before' and 'after' code
code_str = f"```\n{code_value}\n```"
code_str_indented = textwrap.indent(code_str, ' ')
markdown_text += f" - **{code_key}:**\n{code_str_indented}\n"
else:
if "relevant file" in sub_key.lower():
markdown_text += f"\n - **{sub_key}:** {sub_value} \n"
else:
markdown_text += f" **{sub_key}:** {sub_value} \n"
if not gfm_supported:
if "relevant line" not in sub_key.lower(): # nicer presentation
markdown_text = markdown_text.rstrip('\n') + " \n" # works for gitlab and bitbucker
markdown_text += "\n"
return markdown_text Improved code: markdown_text = []
if gfm_supported and 'relevant line' in code_suggestions:
markdown_text.append('<table>')
relevant_file = ''
for sub_key, sub_value in code_suggestions.items():
try:
if sub_key.lower() == 'relevant file':
relevant_file = sub_value.strip('`').strip('"').strip("'")
markdown_text.append(f"<tr><td>{sub_key}</td><td>{relevant_file}</td></tr>")
elif sub_key.lower() == 'suggestion':
markdown_text.append(f"<tr><td>{sub_key} </td><td><strong>{sub_value}</strong></td></tr>")
elif sub_key.lower() == 'relevant line':
markdown_text.append(f"<tr><td>relevant line</td>")
sub_value_list = sub_value.split('](')
relevant_line = sub_value_list[0].lstrip('`').lstrip('[')
if len(sub_value_list) > 1:
link = sub_value_list[1].rstrip(')').strip('`')
markdown_text.append(f"<td><a href={link}>{relevant_line}</a></td>")
else:
markdown_text.append(f"<td>{relevant_line}</td>")
markdown_text.append("</tr>")
except Exception as e:
get_logger().exception(f"Failed to parse code suggestion: {e}")
pass
markdown_text.append('</table>')
markdown_text.append("<hr>")
else:
for sub_key, sub_value in code_suggestions.items():
if isinstance(sub_value, dict): # "code example"
markdown_text.append(f" - **{sub_key}:**\n")
for code_key, code_value in sub_value.items(): # 'before' and 'after' code
code_str = f"```\n{code_value}\n```"
code_str_indented = textwrap.indent(code_str, ' ')
markdown_text.append(f" - **{code_key}:**\n{code_str_indented}\n")
else:
if "relevant file" in sub_key.lower():
markdown_text.append(f"\n - **{sub_key}:** {sub_value} \n")
else:
markdown_text.append(f" **{sub_key}:** {sub_value} \n")
if not gfm_supported:
if "relevant line" not in sub_key.lower(): # nicer presentation
markdown_text.append(markdown_text.pop().rstrip('\n') + " \n") # works for gitlab and bitbucker
markdown_text.append("\n")
return ''.join(markdown_text) |
Persistent review updated to latest commit 654938f |
1 similar comment
Persistent review updated to latest commit 654938f |
/describe |
PR Description updated to latest commit (a247fc3) |
Enhancement of Code Feedback Formatting in utils.py
Type
Enhancement
Description
This PR introduces changes to the
convert_to_markdown
andparse_code_suggestion
functions in theutils.py
file. The main enhancements include:convert_to_markdown
function.parse_code_suggestion
function.parse_code_suggestion
function.PR changes walkthrough
1 files
utils.py
pr_agent/algo/utils.py
The `convert_to_markdown` function has been updated to
modify the markdown formatting for code feedback and add
enumeration to list items. The `parse_code_suggestion`
function has been updated to include an index parameter, add
a horizontal rule when the index is 0, and format the code
suggestions in a table format when GFM is supported.