Refactor code and improve comments in main.py
This commit is contained in:
parent
ddd14faffa
commit
dd716e4475
@ -62,7 +62,6 @@ here is an error example:
|
|||||||
// const posOffset = document.offsetAt(position);
|
// const posOffset = document.offsetAt(position);
|
||||||
// await outputAst(filepath, fileContent, posOffset);
|
// await outputAst(filepath, fileContent, posOffset);
|
||||||
// // await testTreesitterQuery(filepath, fileContent);
|
// // await testTreesitterQuery(filepath, fileContent);
|
||||||
// const result2 = await findSimilarCodeBlock(filepath, fileContent, position.line, position.character);
|
|
||||||
// logger.channel()?.info("Result:", result2);
|
// logger.channel()?.info("Result:", result2);
|
||||||
// if (1) {
|
// if (1) {
|
||||||
// return [];
|
// return [];
|
||||||
@ -89,7 +88,7 @@ output is :
|
|||||||
In this example, string in code is changed, so this output is bad.
|
In this example, string in code is changed, so this output is bad.
|
||||||
|
|
||||||
Output should format as code block.
|
Output should format as code block.
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -98,7 +97,7 @@ file: a1.py
|
|||||||
```
|
```
|
||||||
print("hello")
|
print("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -107,7 +106,7 @@ file: a1.py
|
|||||||
# print hello
|
# print hello
|
||||||
print("hello")
|
print("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -116,7 +115,7 @@ file: a1.py
|
|||||||
```
|
```
|
||||||
print("hell\\nworld")
|
print("hell\\nworld")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -125,7 +124,7 @@ file: a1.py
|
|||||||
# print hello world
|
# print hello world
|
||||||
print("hell\\nworld")
|
print("hell\\nworld")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -140,7 +139,7 @@ file: t2.ts
|
|||||||
length: response!.code.length
|
length: response!.code.length
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -159,7 +158,7 @@ file: t2.ts
|
|||||||
length: response!.code.length
|
length: response!.code.length
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -174,7 +173,7 @@ file: t2.ts
|
|||||||
console.log("a is 2");
|
console.log("a is 2");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -190,8 +189,8 @@ file: t2.ts
|
|||||||
console.log("a is 2");
|
console.log("a is 2");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -209,18 +208,22 @@ def get_selected_code():
|
|||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
return selected_data
|
return selected_data
|
||||||
|
|
||||||
|
|
||||||
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_FEW_SHOT)
|
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_FEW_SHOT)
|
||||||
|
|
||||||
|
|
||||||
def get_prompt():
|
def get_prompt():
|
||||||
ide_language = IDEService().ide_language()
|
ide_language = IDEService().ide_language()
|
||||||
return PROMPT_ZH if ide_language == "zh" else PROMPT
|
return PROMPT_ZH if ide_language == "zh" else PROMPT
|
||||||
|
|
||||||
|
|
||||||
@chat(prompt=get_prompt(), stream_out=True, memory=memory)
|
@chat(prompt=get_prompt(), stream_out=True, memory=memory)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def add_comments(selected_text, file_path):
|
def add_comments(selected_text, file_path):
|
||||||
"""Call AI to rewrite selected code"""
|
"""Call AI to rewrite selected code"""
|
||||||
pass # pylint: disable=unnecessary-pass
|
pass # pylint: disable=unnecessary-pass
|
||||||
|
|
||||||
|
|
||||||
def extract_markdown_block(text):
|
def extract_markdown_block(text):
|
||||||
"""Extracts the first Markdown code block from the given text without the language specifier."""
|
"""Extracts the first Markdown code block from the given text without the language specifier."""
|
||||||
pattern = r"```(?:\w+)?\s*\n(.*?)\n```"
|
pattern = r"```(?:\w+)?\s*\n(.*?)\n```"
|
||||||
@ -233,21 +236,23 @@ def extract_markdown_block(text):
|
|||||||
return None
|
return None
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def remove_unnecessary_escapes(code_a, code_b):
|
def remove_unnecessary_escapes(code_a, code_b):
|
||||||
code_copy = code_b # Create a copy of the original code
|
code_copy = code_b # Create a copy of the original code
|
||||||
escape_chars = re.finditer(r"\\(.)", code_b)
|
escape_chars = re.finditer(r"\\(.)", code_b)
|
||||||
remove_char_index = []
|
remove_char_index = []
|
||||||
for match in escape_chars:
|
for match in escape_chars:
|
||||||
before = code_b[max(0, match.start()-4):match.start()]
|
before = code_b[max(0, match.start() - 4) : match.start()]
|
||||||
after = code_b[match.start()+1:match.start()+5]
|
after = code_b[match.start() + 1 : match.start() + 5]
|
||||||
substr = before + after
|
substr = before + after
|
||||||
if substr in code_a:
|
if substr in code_a:
|
||||||
remove_char_index.append(match.start())
|
remove_char_index.append(match.start())
|
||||||
remove_char_index.reverse()
|
remove_char_index.reverse()
|
||||||
for index in remove_char_index:
|
for index in remove_char_index:
|
||||||
code_copy = code_copy[:index] + code_copy[index+1:]
|
code_copy = code_copy[:index] + code_copy[index + 1 :]
|
||||||
return code_copy
|
return code_copy
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
selected_text = get_selected_code()
|
selected_text = get_selected_code()
|
||||||
file_path = selected_text.get("abspath", "")
|
file_path = selected_text.get("abspath", "")
|
||||||
@ -260,7 +265,9 @@ def main():
|
|||||||
ide_lang = IDEService().ide_language()
|
ide_lang = IDEService().ide_language()
|
||||||
error_msg = (
|
error_msg = (
|
||||||
"\n\nThe output of the LLM is incomplete and cannot perform code operations.\n\n"
|
"\n\nThe output of the LLM is incomplete and cannot perform code operations.\n\n"
|
||||||
if ide_lang != "zh" else "\n\n大模型输出不完整,不能进行代码操作。\n\n")
|
if ide_lang != "zh"
|
||||||
|
else "\n\n大模型输出不完整,不能进行代码操作。\n\n"
|
||||||
|
)
|
||||||
print(error_msg)
|
print(error_msg)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
@ -268,5 +275,6 @@ def main():
|
|||||||
IDEService().diff_apply("", new_code)
|
IDEService().diff_apply("", new_code)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
@ -7,7 +7,6 @@ from devchat.ide.vscode_services import selected_lines, visible_lines
|
|||||||
from devchat.llm import chat
|
from devchat.llm import chat
|
||||||
from devchat.memory import FixSizeChatMemory
|
from devchat.memory import FixSizeChatMemory
|
||||||
|
|
||||||
|
|
||||||
PROMPT = prompt = """
|
PROMPT = prompt = """
|
||||||
file: {file_path}
|
file: {file_path}
|
||||||
```
|
```
|
||||||
@ -24,6 +23,7 @@ PROMPT_ZH = prompt = """
|
|||||||
输出内容使用中文,我的母语为中文。
|
输出内容使用中文,我的母语为中文。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def get_prompt():
|
def get_prompt():
|
||||||
ide_language = IDEService().ide_language()
|
ide_language = IDEService().ide_language()
|
||||||
return PROMPT_ZH if ide_language == "zh" else PROMPT
|
return PROMPT_ZH if ide_language == "zh" else PROMPT
|
||||||
@ -40,10 +40,12 @@ And just add the documents for the selected portion of the code.
|
|||||||
Output documentation comment is format as code block.\
|
Output documentation comment is format as code block.\
|
||||||
|
|
||||||
You must follow the following rules:
|
You must follow the following rules:
|
||||||
1. Output documentation comment in ```comment <documentation comments without code lines> ``` format.
|
1. Output documentation comment in ```comment <documentation comments without code lines> ``` \
|
||||||
2. Different languages have different comment symbols, please choose the correct comment symbol according to the file name.
|
format.
|
||||||
|
2. Different languages have different comment symbols, please choose the correct comment symbol \
|
||||||
|
according to the file name.
|
||||||
3. You must output ... to indicate the remaining code, output all code block can make more errors.
|
3. You must output ... to indicate the remaining code, output all code block can make more errors.
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -54,7 +56,7 @@ file: a1.py
|
|||||||
print("hello")
|
print("hello")
|
||||||
print("world")
|
print("world")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -73,7 +75,7 @@ file: a1.py
|
|||||||
print("hello")
|
print("hello")
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -84,7 +86,7 @@ file: t1.java
|
|||||||
System.out.println("Hello, World!");
|
System.out.println("Hello, World!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -99,7 +101,7 @@ file: t1.java
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -116,7 +118,7 @@ def content_to_json(content):
|
|||||||
except Exception as err:
|
except Exception as err:
|
||||||
raise err
|
raise err
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -138,8 +140,8 @@ def content_to_json(content):
|
|||||||
try:
|
try:
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -174,6 +176,7 @@ def get_selected_code():
|
|||||||
|
|
||||||
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_A)
|
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_A)
|
||||||
|
|
||||||
|
|
||||||
@chat(prompt=get_prompt(), stream_out=True, memory=memory)
|
@chat(prompt=get_prompt(), stream_out=True, memory=memory)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def add_docstring(selected_text, file_path):
|
def add_docstring(selected_text, file_path):
|
||||||
@ -202,6 +205,7 @@ def extract_markdown_block(text):
|
|||||||
return None
|
return None
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def get_indent_level(text):
|
def get_indent_level(text):
|
||||||
"""
|
"""
|
||||||
Returns the indentation level of the given text.
|
Returns the indentation level of the given text.
|
||||||
@ -219,6 +223,7 @@ def get_indent_level(text):
|
|||||||
break
|
break
|
||||||
return indent_level
|
return indent_level
|
||||||
|
|
||||||
|
|
||||||
def offset_indent_level(text, indent_level):
|
def offset_indent_level(text, indent_level):
|
||||||
"""
|
"""
|
||||||
Offsets the indentation level of the given text by the specified amount.
|
Offsets the indentation level of the given text by the specified amount.
|
||||||
@ -237,6 +242,7 @@ def offset_indent_level(text, indent_level):
|
|||||||
text = "\n".join(new_lines)
|
text = "\n".join(new_lines)
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def merge_code(selected_text, docstring):
|
def merge_code(selected_text, docstring):
|
||||||
user_selected_lines = selected_text.split("\n")
|
user_selected_lines = selected_text.split("\n")
|
||||||
docstring_lines = docstring.split("\n")
|
docstring_lines = docstring.split("\n")
|
||||||
@ -264,11 +270,14 @@ def merge_code(selected_text, docstring):
|
|||||||
for index_doc, line_doc in enumerate(docstring_trim_lines):
|
for index_doc, line_doc in enumerate(docstring_trim_lines):
|
||||||
if line_doc == "...":
|
if line_doc == "...":
|
||||||
break
|
break
|
||||||
if line.strip().find(line_doc.strip())!= -1 or line_doc.strip().find(line.strip())!= -1:
|
if (
|
||||||
|
line.strip().find(line_doc.strip()) != -1
|
||||||
|
or line_doc.strip().find(line.strip()) != -1
|
||||||
|
):
|
||||||
break
|
break
|
||||||
if ((line.strip().find(line_doc.strip()) == -1 and
|
if (
|
||||||
line_doc.strip().find(line.strip()) == -1) or
|
line.strip().find(line_doc.strip()) == -1 and line_doc.strip().find(line.strip()) == -1
|
||||||
index == index_doc):
|
) or index == index_doc:
|
||||||
continue
|
continue
|
||||||
return "\n".join(docstring_lines[:index_doc] + user_selected_lines[index:])
|
return "\n".join(docstring_lines[:index_doc] + user_selected_lines[index:])
|
||||||
return docstring + "\n" + selected_text
|
return docstring + "\n" + selected_text
|
||||||
@ -280,12 +289,11 @@ def main():
|
|||||||
|
|
||||||
# Rewrite
|
# Rewrite
|
||||||
response = add_docstring(
|
response = add_docstring(
|
||||||
selected_text=selected_text.get('text', ''),
|
selected_text=selected_text.get("text", ""), file_path=selected_text.get("abspath", "")
|
||||||
file_path=selected_text.get('abspath', '')
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get indent level
|
# Get indent level
|
||||||
indent = get_indent_level(selected_text.get('text', ''))
|
indent = get_indent_level(selected_text.get("text", ""))
|
||||||
|
|
||||||
# Apply new code to editor
|
# Apply new code to editor
|
||||||
new_code = extract_markdown_block(response)
|
new_code = extract_markdown_block(response)
|
||||||
@ -298,12 +306,13 @@ def main():
|
|||||||
new_code = offset_indent_level(new_code, indent)
|
new_code = offset_indent_level(new_code, indent)
|
||||||
|
|
||||||
# Merge code
|
# Merge code
|
||||||
docstring_code = merge_code(selected_text.get('text', ''), new_code)
|
docstring_code = merge_code(selected_text.get("text", ""), new_code)
|
||||||
# Apply diff
|
# Apply diff
|
||||||
IDEService().diff_apply("", docstring_code)
|
IDEService().diff_apply("", docstring_code)
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def print_message(language):
|
def print_message(language):
|
||||||
if language == "zh":
|
if language == "zh":
|
||||||
print("\n\n大模型输出不完整,不能进行代码操作。\n\n")
|
print("\n\n大模型输出不完整,不能进行代码操作。\n\n")
|
||||||
|
@ -7,7 +7,6 @@ from devchat.ide.vscode_services import selected_lines, visible_lines
|
|||||||
from devchat.llm import chat
|
from devchat.llm import chat
|
||||||
from devchat.memory import FixSizeChatMemory
|
from devchat.memory import FixSizeChatMemory
|
||||||
|
|
||||||
|
|
||||||
PROMPT = prompt = """
|
PROMPT = prompt = """
|
||||||
file: {file_path}
|
file: {file_path}
|
||||||
```
|
```
|
||||||
@ -36,14 +35,16 @@ MESSAGES_A = [
|
|||||||
"role": "system",
|
"role": "system",
|
||||||
"content": """
|
"content": """
|
||||||
Your task is:
|
Your task is:
|
||||||
Refine internal variable and function names within the code to achieve concise and meaningful identifiers that comply with English naming conventions.
|
Refine internal variable and function names within the code to achieve concise and \
|
||||||
|
meaningful identifiers that comply with English naming conventions.
|
||||||
|
|
||||||
Rules:
|
Rules:
|
||||||
1. Don't rename a call or global variable. for example, xx() is function call, xx is a bad name, but you MUST not rename it .
|
1. Don't rename a call or global variable. for example, xx() is function call, xx \
|
||||||
|
is a bad name, but you MUST not rename it .
|
||||||
2. You can rename a local variable or parameter variable name.
|
2. You can rename a local variable or parameter variable name.
|
||||||
3. Current function's name can be renamed. Always this is a new function.
|
3. Current function's name can be renamed. Always this is a new function.
|
||||||
|
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -54,7 +55,7 @@ file: a1.py
|
|||||||
a = "hello world"
|
a = "hello world"
|
||||||
print(a)
|
print(a)
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -64,7 +65,7 @@ file: a1.py
|
|||||||
msg = "hello world"
|
msg = "hello world"
|
||||||
print(msg)
|
print(msg)
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -74,7 +75,7 @@ file: t1.py
|
|||||||
def print_hello(a: str):
|
def print_hello(a: str):
|
||||||
print(a)
|
print(a)
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -83,7 +84,7 @@ file: t1.py
|
|||||||
def print_hello(msg: str):
|
def print_hello(msg: str):
|
||||||
print(msg)
|
print(msg)
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -93,7 +94,7 @@ file: t1.py
|
|||||||
def some():
|
def some():
|
||||||
print("hello")
|
print("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -102,7 +103,7 @@ file: t1.py
|
|||||||
def output_hello():
|
def output_hello():
|
||||||
print("hello")
|
print("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@ -112,7 +113,7 @@ file: t1.py
|
|||||||
def print_hello():
|
def print_hello():
|
||||||
print("hello")
|
print("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -121,14 +122,14 @@ file: t1.py
|
|||||||
def print_hello():
|
def print_hello():
|
||||||
output("hello")
|
output("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
"content": """
|
"content": """
|
||||||
Your response is error, you changed call name.
|
Your response is error, you changed call name.
|
||||||
print is a function call, if you rename it, this will make a compile error.
|
print is a function call, if you rename it, this will make a compile error.
|
||||||
"""
|
""",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"role": "assistant",
|
||||||
@ -137,10 +138,11 @@ print is a function call, if you rename it, this will make a compile error.
|
|||||||
def print_hello():
|
def print_hello():
|
||||||
print("hello")
|
print("hello")
|
||||||
```
|
```
|
||||||
"""
|
""",
|
||||||
}
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_selected_code():
|
def get_selected_code():
|
||||||
"""
|
"""
|
||||||
Retrieves the selected lines of code from the user's selection.
|
Retrieves the selected lines of code from the user's selection.
|
||||||
@ -172,6 +174,7 @@ def get_selected_code():
|
|||||||
|
|
||||||
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_A)
|
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_A)
|
||||||
|
|
||||||
|
|
||||||
@chat(prompt=get_prompt(), stream_out=True, memory=memory)
|
@chat(prompt=get_prompt(), stream_out=True, memory=memory)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def reanme_variable(selected_text, file_path):
|
def reanme_variable(selected_text, file_path):
|
||||||
@ -203,12 +206,12 @@ def extract_markdown_block(text):
|
|||||||
|
|
||||||
def remove_unnecessary_escapes(code_a, code_b):
|
def remove_unnecessary_escapes(code_a, code_b):
|
||||||
code_copy = code_b # Create a copy of the original code
|
code_copy = code_b # Create a copy of the original code
|
||||||
escape_chars = re.finditer(r'\\(.)', code_b)
|
escape_chars = re.finditer(r"\\(.)", code_b)
|
||||||
|
|
||||||
remove_char_index = []
|
remove_char_index = []
|
||||||
for match in escape_chars:
|
for match in escape_chars:
|
||||||
before = code_b[max(0, match.start()-4):match.start()]
|
before = code_b[max(0, match.start() - 4) : match.start()]
|
||||||
after = code_b[match.start()+1:match.start()+5]
|
after = code_b[match.start() + 1 : match.start() + 5]
|
||||||
substr = before + after
|
substr = before + after
|
||||||
if substr in code_a:
|
if substr in code_a:
|
||||||
remove_char_index.append(match.start())
|
remove_char_index.append(match.start())
|
||||||
@ -216,14 +219,15 @@ def remove_unnecessary_escapes(code_a, code_b):
|
|||||||
# visit remove_char_index in reverse order
|
# visit remove_char_index in reverse order
|
||||||
remove_char_index.reverse()
|
remove_char_index.reverse()
|
||||||
for index in remove_char_index:
|
for index in remove_char_index:
|
||||||
code_copy = code_copy[:index] + code_copy[index+1:]
|
code_copy = code_copy[:index] + code_copy[index + 1 :]
|
||||||
return code_copy
|
return code_copy
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# prepare code
|
# prepare code
|
||||||
selected_text = get_selected_code()
|
selected_text = get_selected_code()
|
||||||
selected_code = selected_text.get('text', '')
|
selected_code = selected_text.get("text", "")
|
||||||
selected_file = selected_text.get('abspath', '')
|
selected_file = selected_text.get("abspath", "")
|
||||||
|
|
||||||
# rewrite
|
# rewrite
|
||||||
response = reanme_variable(selected_text=selected_code, file_path=selected_file)
|
response = reanme_variable(selected_text=selected_code, file_path=selected_file)
|
||||||
@ -237,7 +241,7 @@ def main():
|
|||||||
print("\n\nThe output of the LLM is incomplete and cannot perform code operations.\n\n")
|
print("\n\nThe output of the LLM is incomplete and cannot perform code operations.\n\n")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
new_code = remove_unnecessary_escapes(selected_text.get('text', ''), new_code)
|
new_code = remove_unnecessary_escapes(selected_text.get("text", ""), new_code)
|
||||||
IDEService().diff_apply("", new_code)
|
IDEService().diff_apply("", new_code)
|
||||||
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user