workflows/lib/ide_service/vscode_service.py

195 lines
5.3 KiB
Python
Raw Normal View History

from .rpc import rpc_call
2024-05-09 21:30:09 +08:00
from .types import LocationWithText
2024-02-01 17:58:23 +08:00
@rpc_call
def run_code(code: str):
pass
2024-02-01 17:58:23 +08:00
@rpc_call
def diff_apply(filepath, content, autoedit=False):
pass
2024-02-01 17:58:23 +08:00
def find_symbol(command, abspath, line, col):
code = (
f"const position = new vscode.Position({line}, {col});"
f"const absPath = vscode.Uri.file('{abspath}');"
f"return await vscode.commands.executeCommand('{command}', absPath, position);"
)
2024-02-01 17:58:23 +08:00
result = run_code(code=code)
return result
2024-02-01 17:58:23 +08:00
def find_definition(abspath: str, line: int, col: int):
return find_symbol("vscode.executeDefinitionProvider", abspath, line, col)
2024-02-01 17:58:23 +08:00
def find_type_definition(abspath: str, line: int, col: int):
return find_symbol("vscode.executeTypeDefinitionProvider", abspath, line, col)
2024-02-01 17:58:23 +08:00
def find_declaration(abspath: str, line: int, col: int):
return find_symbol("vscode.executeDeclarationProvider", abspath, line, col)
2024-02-01 17:58:23 +08:00
def find_implementation(abspath: str, line: int, col: int):
return find_symbol("vscode.executeImplementationProvider", abspath, line, col)
2024-02-01 17:58:23 +08:00
def find_reference(abspath: str, line: int, col: int):
return find_symbol("vscode.executeReferenceProvider", abspath, line, col)
2024-02-01 17:58:23 +08:00
def document_symbols(abspath: str):
code = (
f"const fileUri = vscode.Uri.file('{abspath}');"
2024-02-01 17:58:23 +08:00
"return await vscode.commands.executeCommand("
"'vscode.executeDocumentSymbolProvider', fileUri);"
)
2024-02-01 17:58:23 +08:00
symbols = run_code(code=code)
return symbols
2024-02-01 17:58:23 +08:00
def workspace_symbols(query: str):
code = (
2024-02-01 17:58:23 +08:00
"return await vscode.commands.executeCommand('vscode.executeWorkspaceSymbolProvider',"
f" '{query}');"
)
2024-02-01 17:58:23 +08:00
return run_code(code=code)
def active_text_editor():
2024-02-01 17:58:23 +08:00
code = "return vscode.window.activeTextEditor;"
return run_code(code=code)
2024-11-28 13:15:44 +08:00
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)
2024-11-28 13:15:44 +08:00
def open_folder(folder: str):
folder = folder.replace("\\", "/")
code = (
f"const folderUri = vscode.Uri.file('{folder}');"
"vscode.commands.executeCommand(`vscode.openFolder`, folderUri);"
)
2024-02-01 17:58:23 +08:00
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)
2024-11-28 13:15:44 +08:00
def visible_lines():
active_document = active_text_editor()
fail_result = {
"filePath": "",
"visibleText": "",
"visibleRange": [-1, -1],
}
2024-02-01 17:58:23 +08:00
if not active_document:
return fail_result
2024-02-01 17:58:23 +08:00
file_path = active_document["document"]["uri"]["fsPath"]
start_line = active_document["visibleRanges"][0][0]["line"]
end_line = active_document["visibleRanges"][0][1]["line"]
2024-02-01 17:58:23 +08:00
# 获取可见文本内容
visible_text = get_visible_text()
return {
"filePath": file_path,
"visibleText": visible_text,
"visibleRange": [start_line, end_line],
}
2024-05-09 21:30:09 +08:00
def visible_range() -> LocationWithText:
visible_range_text = visible_lines()
return LocationWithText(
text=visible_range_text["visibleText"],
abspath=visible_range_text["filePath"],
range={
"start": {
"line": visible_range_text["visibleRange"][0],
"character": 0,
},
"end": {
"line": visible_range_text["visibleRange"][1],
"character": 0,
},
},
)
def selected_lines():
active_document = active_text_editor()
fail_result = {
"filePath": "",
"selectedText": "",
"selectedRange": [-1, -1, -1, -1],
}
2024-02-01 17:58:23 +08:00
if not active_document:
return fail_result
2024-02-01 17:58:23 +08:00
# 获取活动文档的文件路径
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"]
2024-02-01 17:58:23 +08:00
# 获取编辑器当前内容
selected_text = get_selected_text()
# continue with the rest of the function
return {
2024-05-09 21:30:09 +08:00
"filePath": file_path,
"selectedText": selected_text,
"selectedRange": [start_line, start_col, end_line, end_col],
}
2024-05-09 21:30:09 +08:00
def selected_range() -> LocationWithText:
selected_range_text = selected_lines()
return LocationWithText(
text=selected_range_text["selectedText"],
abspath=selected_range_text["filePath"],
range={
"start": {
"line": selected_range_text["selectedRange"][0],
"character": selected_range_text["selectedRange"][1],
},
"end": {
"line": selected_range_text["selectedRange"][2],
"character": selected_range_text["selectedRange"][3],
},
},
)