2024-02-01 17:46:49 +08:00
|
|
|
import os
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
from .rpc import rpc_call
|
2024-05-09 21:30:09 +08:00
|
|
|
from .types import LocationWithText
|
2024-02-01 17:46:49 +08:00
|
|
|
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
@rpc_call
|
|
|
|
def run_code(code: str):
|
|
|
|
pass
|
|
|
|
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
@rpc_call
|
2024-07-04 17:07:59 +08:00
|
|
|
def diff_apply(filepath, content, autoedit=False):
|
2024-02-01 17:46:49 +08:00
|
|
|
pass
|
|
|
|
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +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)
|
2024-02-01 17:46:49 +08:00
|
|
|
return result
|
|
|
|
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +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
|
|
|
|
2024-02-01 17:46:49 +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
|
|
|
|
2024-02-01 17:46:49 +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
|
|
|
|
2024-02-01 17:46:49 +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
|
|
|
|
2024-02-01 17:46:49 +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
|
|
|
|
2024-02-01 17:46:49 +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:46:49 +08:00
|
|
|
)
|
2024-02-01 17:58:23 +08:00
|
|
|
symbols = run_code(code=code)
|
2024-02-01 17:46:49 +08:00
|
|
|
return symbols
|
|
|
|
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +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:46:49 +08:00
|
|
|
)
|
2024-02-01 17:58:23 +08:00
|
|
|
return run_code(code=code)
|
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
|
|
|
|
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 12:48:01 +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-02-01 17:46:49 +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)
|
2024-02-01 17:46:49 +08:00
|
|
|
|
|
|
|
|
2024-11-28 12:48:01 +08:00
|
|
|
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-02-01 17:46:49 +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
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
if not active_document:
|
|
|
|
return fail_result
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-02-01 17:46:49 +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
|
|
|
|
2024-11-28 12:48:01 +08:00
|
|
|
# 获取可见文本内容
|
|
|
|
visible_text = get_visible_text()
|
2024-02-01 17:46:49 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
"filePath": file_path,
|
2024-11-28 12:48:01 +08:00
|
|
|
"visibleText": visible_text,
|
2024-02-01 17:46:49 +08:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
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
|
|
|
|
2024-02-01 17:46:49 +08:00
|
|
|
if not active_document:
|
|
|
|
return fail_result
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-11-28 12:48:01 +08:00
|
|
|
# 获取活动文档的文件路径
|
2024-02-01 17:46:49 +08:00
|
|
|
file_path = active_document["document"]["uri"]["fsPath"]
|
2024-11-28 12:48:01 +08:00
|
|
|
# 获取选择区域的起始行
|
2024-02-01 17:46:49 +08:00
|
|
|
start_line = active_document["selection"]["start"]["line"]
|
|
|
|
start_col = active_document["selection"]["start"]["character"]
|
2024-11-28 12:48:01 +08:00
|
|
|
# 获取选择区域的结束行
|
2024-02-01 17:46:49 +08:00
|
|
|
end_line = active_document["selection"]["end"]["line"]
|
2024-11-28 12:48:01 +08:00
|
|
|
# 获取选择区域的结束列
|
2024-02-01 17:46:49 +08:00
|
|
|
end_col = active_document["selection"]["end"]["character"]
|
2024-02-01 17:58:23 +08:00
|
|
|
|
2024-11-28 12:48:01 +08:00
|
|
|
# 获取编辑器当前内容
|
|
|
|
selected_text = get_selected_text()
|
2024-02-01 17:46:49 +08:00
|
|
|
|
|
|
|
# continue with the rest of the function
|
|
|
|
return {
|
2024-05-09 21:30:09 +08:00
|
|
|
"filePath": file_path,
|
2024-11-28 12:48:01 +08:00
|
|
|
"selectedText": selected_text,
|
2024-02-01 17:46:49 +08:00
|
|
|
"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],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|