Skip to content

Commit ed79fea

Browse files
committed
resolve variables for documentation view
1 parent 794cb4b commit ed79fea

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

robotcode/language_server/robotframework/parts/code_action.py

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from concurrent.futures import ProcessPoolExecutor
88
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
99
from threading import Thread
10-
from typing import TYPE_CHECKING, Any, List, Optional, Union, cast
10+
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union, cast
1111
from urllib.parse import parse_qs, urlparse
1212

1313
from ....utils.logging import LoggingDescriptor
@@ -21,8 +21,12 @@
2121
Range,
2222
)
2323
from ...common.text_document import TextDocument
24-
from ..diagnostics.library_doc import get_library_doc, get_robot_library_html_doc_str
25-
from ..diagnostics.namespace import LibraryEntry
24+
from ..diagnostics.library_doc import (
25+
get_library_doc,
26+
get_robot_library_html_doc_str,
27+
resolve_robot_variables,
28+
)
29+
from ..diagnostics.namespace import LibraryEntry, Namespace
2630
from ..utils.ast_utils import Token, get_node_at_position
2731
from .model_helper import ModelHelperMixin
2832

@@ -219,19 +223,18 @@ async def collect(
219223

220224
if isinstance(node, (LibraryImport, ResourceImport)):
221225

222-
args = f"&args={'::'.join(node.args)}" if isinstance(node, LibraryImport) and node.args else ""
226+
url = await self.build_url(
227+
node.name, node.args if isinstance(node, LibraryImport) else (), document, namespace
228+
)
229+
223230
return [
224231
CodeAction(
225232
"Open Documentation",
226233
kind=CodeActionKinds.SOURCE + ".openDocumentation",
227234
command=Command(
228235
"Open Documentation",
229236
"robotcode.showDocumentation",
230-
[
231-
f"http://localhost:{self._documentation_server_port}/?name={node.name}"
232-
f"{args}"
233-
f"&basedir={document.uri.to_path().parent}"
234-
],
237+
[url],
235238
),
236239
)
237240
]
@@ -271,23 +274,56 @@ async def collect(
271274
if entry is None:
272275
return None
273276

274-
args = f"&args={'::'.join(entry.args)}" if entry.args else ""
277+
url = await self.build_url(entry.import_name, entry.args, document, namespace, kw_doc.name)
278+
275279
return [
276280
CodeAction(
277281
"Open Documentation",
278282
kind=CodeActionKinds.SOURCE + ".openDocumentation",
279283
command=Command(
280284
"Open Documentation",
281285
"robotcode.showDocumentation",
282-
[
283-
f"http://localhost:{self._documentation_server_port}"
284-
f"/?name={entry.import_name}"
285-
f"{args}"
286-
f"&basedir={document.uri.to_path().parent}"
287-
f"#{kw_doc.name}"
288-
],
286+
[url],
289287
),
290288
)
291289
]
292290

293291
return None
292+
293+
async def build_url(
294+
self,
295+
name: str,
296+
args: Tuple[Any, ...],
297+
document: TextDocument,
298+
namespace: Namespace,
299+
target: Optional[str] = None,
300+
) -> str:
301+
302+
base_dir = str(document.uri.to_path().parent)
303+
304+
robot_variables = resolve_robot_variables(
305+
str(namespace.imports_manager.folder.to_path()),
306+
base_dir,
307+
variables=await namespace.get_resolvable_variables(),
308+
)
309+
try:
310+
name = robot_variables.replace_string(name.replace("\\", "\\\\"), ignore_errors=False)
311+
312+
args = tuple(robot_variables.replace_string(v.replace("\\", "\\\\"), ignore_errors=False) for v in args)
313+
314+
except (SystemExit, KeyboardInterrupt):
315+
raise
316+
except BaseException:
317+
pass
318+
319+
url_args = f"&args={'::'.join(args)}" if args else ""
320+
321+
url = (
322+
f"http://localhost:{self._documentation_server_port}"
323+
f"/?name={name}"
324+
f"{url_args}"
325+
f"&basedir={base_dir}"
326+
f"{'#{target}' if target else ''}"
327+
)
328+
329+
return url

0 commit comments

Comments
 (0)