Skip to content

Commit

Permalink
feat: Enhance VSCode text selection handling
Browse files Browse the repository at this point in the history
- 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()
  • Loading branch information
bobo yang authored and bobo yang committed Nov 28, 2024
1 parent ed9898d commit d1321f4
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions lib/ide_service/vscode_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("\\", "/")
Expand All @@ -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 = {
Expand All @@ -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],
}

Expand Down Expand Up @@ -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],
}

Expand Down

0 comments on commit d1321f4

Please sign in to comment.