Merge pull request #133 from devchat-ai/feat/update-vscode-text-selection
feat: Enhance VSCode text selection handling
This commit is contained in:
commit
f4a4dbc529
@ -1,5 +1,3 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
from .rpc import rpc_call
|
from .rpc import rpc_call
|
||||||
from .types import LocationWithText
|
from .types import LocationWithText
|
||||||
|
|
||||||
@ -67,6 +65,18 @@ def active_text_editor():
|
|||||||
return run_code(code=code)
|
return run_code(code=code)
|
||||||
|
|
||||||
|
|
||||||
|
def get_selected_text():
|
||||||
|
code = """
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (editor) {
|
||||||
|
const selection = editor.selection;
|
||||||
|
return editor.document.getText(selection);
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
"""
|
||||||
|
return run_code(code=code)
|
||||||
|
|
||||||
|
|
||||||
def open_folder(folder: str):
|
def open_folder(folder: str):
|
||||||
folder = folder.replace("\\", "/")
|
folder = folder.replace("\\", "/")
|
||||||
code = (
|
code = (
|
||||||
@ -76,6 +86,21 @@ def open_folder(folder: str):
|
|||||||
run_code(code=code)
|
run_code(code=code)
|
||||||
|
|
||||||
|
|
||||||
|
def get_visible_text():
|
||||||
|
code = """
|
||||||
|
const editor = vscode.window.activeTextEditor;
|
||||||
|
if (editor) {
|
||||||
|
const visibleRanges = editor.visibleRanges;
|
||||||
|
if (visibleRanges.length > 0) {
|
||||||
|
const visibleRange = visibleRanges[0];
|
||||||
|
return editor.document.getText(visibleRange);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
"""
|
||||||
|
return run_code(code=code)
|
||||||
|
|
||||||
|
|
||||||
def visible_lines():
|
def visible_lines():
|
||||||
active_document = active_text_editor()
|
active_document = active_text_editor()
|
||||||
fail_result = {
|
fail_result = {
|
||||||
@ -86,22 +111,17 @@ def visible_lines():
|
|||||||
|
|
||||||
if not active_document:
|
if not active_document:
|
||||||
return fail_result
|
return fail_result
|
||||||
if not os.path.exists(active_document["document"]["uri"]["fsPath"]):
|
|
||||||
return fail_result
|
|
||||||
|
|
||||||
file_path = active_document["document"]["uri"]["fsPath"]
|
file_path = active_document["document"]["uri"]["fsPath"]
|
||||||
start_line = active_document["visibleRanges"][0][0]["line"]
|
start_line = active_document["visibleRanges"][0][0]["line"]
|
||||||
end_line = active_document["visibleRanges"][0][1]["line"]
|
end_line = active_document["visibleRanges"][0][1]["line"]
|
||||||
|
|
||||||
# read file lines from start_line to end_line
|
# 获取可见文本内容
|
||||||
with open(file_path, "r", encoding="utf-8") as file:
|
visible_text = get_visible_text()
|
||||||
_lines = file.readlines()
|
|
||||||
_visible_lines = _lines[start_line : end_line + 1]
|
|
||||||
|
|
||||||
# continue with the rest of the function
|
|
||||||
return {
|
return {
|
||||||
"filePath": file_path,
|
"filePath": file_path,
|
||||||
"visibleText": "".join(_visible_lines),
|
"visibleText": visible_text,
|
||||||
"visibleRange": [start_line, end_line],
|
"visibleRange": [start_line, end_line],
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,24 +154,24 @@ def selected_lines():
|
|||||||
|
|
||||||
if not active_document:
|
if not active_document:
|
||||||
return fail_result
|
return fail_result
|
||||||
if not os.path.exists(active_document["document"]["uri"]["fsPath"]):
|
|
||||||
return fail_result
|
|
||||||
|
|
||||||
|
# 获取活动文档的文件路径
|
||||||
file_path = active_document["document"]["uri"]["fsPath"]
|
file_path = active_document["document"]["uri"]["fsPath"]
|
||||||
|
# 获取选择区域的起始行
|
||||||
start_line = active_document["selection"]["start"]["line"]
|
start_line = active_document["selection"]["start"]["line"]
|
||||||
start_col = active_document["selection"]["start"]["character"]
|
start_col = active_document["selection"]["start"]["character"]
|
||||||
|
# 获取选择区域的结束行
|
||||||
end_line = active_document["selection"]["end"]["line"]
|
end_line = active_document["selection"]["end"]["line"]
|
||||||
|
# 获取选择区域的结束列
|
||||||
end_col = active_document["selection"]["end"]["character"]
|
end_col = active_document["selection"]["end"]["character"]
|
||||||
|
|
||||||
# read file lines from start_line to end_line
|
# 获取编辑器当前内容
|
||||||
with open(file_path, "r", encoding="utf-8") as file:
|
selected_text = get_selected_text()
|
||||||
_lines = file.readlines()
|
|
||||||
_selected_lines = _lines[start_line : end_line + 1]
|
|
||||||
|
|
||||||
# continue with the rest of the function
|
# continue with the rest of the function
|
||||||
return {
|
return {
|
||||||
"filePath": file_path,
|
"filePath": file_path,
|
||||||
"selectedText": "".join(_selected_lines),
|
"selectedText": selected_text,
|
||||||
"selectedRange": [start_line, start_col, end_line, end_col],
|
"selectedRange": [start_line, start_col, end_line, end_col],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user