From d1321f486384c3a983d0c0e80efb3328407485b6 Mon Sep 17 00:00:00 2001 From: bobo yang Date: Thu, 28 Nov 2024 12:48:01 +0800 Subject: [PATCH 1/2] feat: Enhance VSCode text selection handling - Add functions to get selected and visible text - Update visible_lines() to use new get_visible_text() - Refactor selected_lines() to use get_selected_text() --- lib/ide_service/vscode_service.py | 49 +++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/ide_service/vscode_service.py b/lib/ide_service/vscode_service.py index 7de4536..1cac140 100644 --- a/lib/ide_service/vscode_service.py +++ b/lib/ide_service/vscode_service.py @@ -66,6 +66,16 @@ def active_text_editor(): code = "return vscode.window.activeTextEditor;" 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): folder = folder.replace("\\", "/") @@ -76,6 +86,20 @@ def open_folder(folder: str): 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(): active_document = active_text_editor() fail_result = { @@ -86,22 +110,17 @@ def visible_lines(): if not active_document: return fail_result - if not os.path.exists(active_document["document"]["uri"]["fsPath"]): - return fail_result file_path = active_document["document"]["uri"]["fsPath"] start_line = active_document["visibleRanges"][0][0]["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: - _lines = file.readlines() - _visible_lines = _lines[start_line : end_line + 1] + # 获取可见文本内容 + visible_text = get_visible_text() - # continue with the rest of the function return { "filePath": file_path, - "visibleText": "".join(_visible_lines), + "visibleText": visible_text, "visibleRange": [start_line, end_line], } @@ -134,24 +153,24 @@ def selected_lines(): if not active_document: return fail_result - if not os.path.exists(active_document["document"]["uri"]["fsPath"]): - return fail_result + # 获取活动文档的文件路径 file_path = active_document["document"]["uri"]["fsPath"] + # 获取选择区域的起始行 start_line = active_document["selection"]["start"]["line"] start_col = active_document["selection"]["start"]["character"] + # 获取选择区域的结束行 end_line = active_document["selection"]["end"]["line"] + # 获取选择区域的结束列 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: - _lines = file.readlines() - _selected_lines = _lines[start_line : end_line + 1] + # 获取编辑器当前内容 + selected_text = get_selected_text() # continue with the rest of the function return { "filePath": file_path, - "selectedText": "".join(_selected_lines), + "selectedText": selected_text, "selectedRange": [start_line, start_col, end_line, end_col], } From 656c2287e42163e040d4a352007cbe5481b5297f Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Thu, 28 Nov 2024 13:15:44 +0800 Subject: [PATCH 2/2] fix lint error --- lib/ide_service/vscode_service.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ide_service/vscode_service.py b/lib/ide_service/vscode_service.py index 1cac140..356a76e 100644 --- a/lib/ide_service/vscode_service.py +++ b/lib/ide_service/vscode_service.py @@ -1,5 +1,3 @@ -import os - from .rpc import rpc_call from .types import LocationWithText @@ -66,6 +64,7 @@ def active_text_editor(): code = "return vscode.window.activeTextEditor;" return run_code(code=code) + def get_selected_text(): code = """ const editor = vscode.window.activeTextEditor; @@ -77,6 +76,7 @@ def get_selected_text(): """ return run_code(code=code) + def open_folder(folder: str): folder = folder.replace("\\", "/") code = ( @@ -100,6 +100,7 @@ def get_visible_text(): """ return run_code(code=code) + def visible_lines(): active_document = active_text_editor() fail_result = {