Skip to content

Commit e86d9bc

Browse files
committed
open report.html and log.html with internal documentation server
1 parent eb6c93d commit e86d9bc

File tree

4 files changed

+68
-52
lines changed

4 files changed

+68
-52
lines changed

robotcode/language_server/robotframework/parts/code_action.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
import traceback
77
import urllib.parse
88
from concurrent.futures import ProcessPoolExecutor
9+
from dataclasses import dataclass
910
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
1011
from threading import Thread
1112
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, cast
1213
from urllib.parse import parse_qs, urlparse
1314

15+
from ....jsonrpc2.protocol import rpc_method
1416
from ....utils.logging import LoggingDescriptor
1517
from ....utils.net import find_free_port
18+
from ....utils.uri import Uri
1619
from ...common.decorators import code_action_kinds, language_id
1720
from ...common.lsp_types import (
1821
CodeAction,
1922
CodeActionContext,
2023
CodeActionKinds,
2124
Command,
25+
Model,
2226
Range,
2327
)
2428
from ...common.text_document import TextDocument
@@ -39,6 +43,12 @@
3943

4044
from .protocol_part import RobotLanguageServerProtocolPart
4145

46+
47+
@dataclass(repr=False)
48+
class ConvertUriParams(Model):
49+
uri: str
50+
51+
4252
HTML_ERROR_TEMPLATE = Template(
4353
"""\n
4454
<!doctype html>
@@ -337,3 +347,17 @@ async def build_url(
337347
url = f"{base_url}" f"/?&{params}" f"{f'#{target}' if target else ''}"
338348

339349
return url
350+
351+
@rpc_method(name="robot/documentationServer/convertUri", param_type=ConvertUriParams)
352+
@_logger.call
353+
async def _convert_uri(self, uri: str, *args: Any, **kwargs: Any) -> Optional[str]:
354+
real_uri = Uri(uri)
355+
356+
folder = self.parent.workspace.get_workspace_folder(real_uri)
357+
358+
if folder:
359+
path = real_uri.to_path().relative_to(folder.uri.to_path())
360+
361+
return f"http://localhost:{self._documentation_server_port}/{path}"
362+
363+
return None

vscode-client/debugmanager.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,6 @@ import { CONFIG_SECTION } from "./config";
88
import { LanguageClientsManager, toVsCodeRange } from "./languageclientsmanger";
99
import { WeakValueSet } from "./utils";
1010

11-
async function openPreviewOrExternal(uri: vscode.Uri) {
12-
let livePreviewResult = false;
13-
try {
14-
const ex = vscode.extensions.getExtension("ms-vscode.live-server");
15-
if (ex) {
16-
await ex.activate();
17-
if ((await vscode.commands.getCommands()).includes("livePreview.start.preview.atFile")) {
18-
await vscode.commands.executeCommand("livePreview.start.preview.atFile", uri);
19-
livePreviewResult = true;
20-
}
21-
}
22-
} catch {
23-
livePreviewResult = false;
24-
}
25-
26-
if (!livePreviewResult) {
27-
vscode.env.openExternal(uri).then(
28-
() => undefined,
29-
() => undefined
30-
);
31-
}
32-
}
33-
3411
const DEBUG_ADAPTER_DEFAULT_TCP_PORT = 6611;
3512
const DEBUG_ADAPTER_DEFAULT_HOST = "127.0.0.1";
3613

@@ -285,12 +262,7 @@ export class DebugManager {
285262
break;
286263
}
287264
case "robotExited": {
288-
await DebugManager.OnRobotExited(
289-
event.session,
290-
event.body.outputFile,
291-
event.body.logFile,
292-
event.body.reportFile
293-
);
265+
await this.OnRobotExited(event.session, event.body.outputFile, event.body.logFile, event.body.reportFile);
294266
break;
295267
}
296268
}
@@ -446,16 +418,16 @@ export class DebugManager {
446418
}
447419
}
448420

449-
private static async OnRobotExited(
421+
private async OnRobotExited(
450422
session: vscode.DebugSession,
451423
_outputFile?: string,
452424
logFile?: string,
453425
reportFile?: string
454426
): Promise<void> {
455427
if (session.configuration?.openOutputAfterRun === "report" && reportFile) {
456-
await openPreviewOrExternal(vscode.Uri.file(reportFile));
428+
await this.languageClientsManager.openUriInDocumentationView(vscode.Uri.file(reportFile));
457429
} else if (session.configuration?.openOutputAfterRun === "log" && logFile) {
458-
await openPreviewOrExternal(vscode.Uri.file(logFile));
430+
await this.languageClientsManager.openUriInDocumentationView(vscode.Uri.file(logFile));
459431
}
460432
}
461433
}

vscode-client/extension.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,7 @@ export async function activateAsync(context: vscode.ExtensionContext): Promise<v
6060
return [];
6161
},
6262
async handleTerminalLink(link: TerminalLink) {
63-
let livePreviewResult = false;
64-
try {
65-
const ex = vscode.extensions.getExtension("ms-vscode.live-server");
66-
if (ex) {
67-
await ex.activate();
68-
if ((await vscode.commands.getCommands()).includes("livePreview.start.preview.atFile")) {
69-
await vscode.commands.executeCommand("livePreview.start.preview.atFile", vscode.Uri.file(link.path));
70-
livePreviewResult = true;
71-
}
72-
}
73-
} catch {
74-
livePreviewResult = false;
75-
}
76-
77-
if (!livePreviewResult) {
78-
vscode.env.openExternal(vscode.Uri.file(link.path)).then(
79-
() => undefined,
80-
() => undefined
81-
);
82-
}
63+
await languageClientManger.openUriInDocumentationView(vscode.Uri.file(link.path));
8364
},
8465
})
8566
);

vscode-client/languageclientsmanger.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,45 @@ export class LanguageClientsManager {
618618
);
619619
}
620620

621+
public async openUriInDocumentationView(uri: vscode.Uri): Promise<void> {
622+
const doc_uri = await this.convertToDocumententationUri(uri);
623+
if (doc_uri) {
624+
await vscode.commands.executeCommand("robotcode.showDocumentation", doc_uri.toString(true));
625+
} else {
626+
vscode.env.openExternal(uri).then(
627+
() => undefined,
628+
() => undefined
629+
);
630+
}
631+
}
632+
633+
public async convertToDocumententationUri(
634+
uri: vscode.Uri,
635+
token?: vscode.CancellationToken
636+
): Promise<vscode.Uri | undefined> {
637+
const client = await this.getLanguageClientForResource(uri);
638+
639+
if (!client) return;
640+
641+
return (
642+
(token
643+
? vscode.Uri.parse(
644+
await client.sendRequest<string>(
645+
"robot/documentationServer/convertUri",
646+
{
647+
uri: uri.toString(),
648+
},
649+
token
650+
)
651+
)
652+
: vscode.Uri.parse(
653+
await client.sendRequest<string>("robot/documentationServer/convertUri", {
654+
uri: uri.toString(),
655+
})
656+
)) ?? undefined
657+
);
658+
}
659+
621660
public async getEvaluatableExpression(
622661
document: vscode.TextDocument,
623662
position: Position,

0 commit comments

Comments
 (0)