From 7d104ed3ac0993f88793e60c606757443614e2a9 Mon Sep 17 00:00:00 2001 From: cedric05 Date: Fri, 3 Jan 2025 02:51:47 +0000 Subject: [PATCH] fix #349 include hovered property --- .../server/handlers/basic_handlers.py | 19 ++++++++++++++++++- dothttp/utils/property_util.py | 3 ++- pyproject.toml | 2 +- test/extensions/test_commands.py | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/dotextensions/server/handlers/basic_handlers.py b/dotextensions/server/handlers/basic_handlers.py index 9c20d78..bea2df3 100644 --- a/dotextensions/server/handlers/basic_handlers.py +++ b/dotextensions/server/handlers/basic_handlers.py @@ -5,6 +5,7 @@ from dothttp.__version__ import __version__ from dothttp.exceptions import DotHttpException +from dothttp.utils.property_util import property_regex from dothttp.models.parse_models import ScriptType from dothttp.parse import ( BaseModelProcessor, @@ -309,7 +310,17 @@ def get_resolved(self, command: Command) -> Result: if content: model: MultidefHttp = dothttp_model.model_from_str(content) else: - model: MultidefHttp = dothttp_model.model_from_file(filename) + with open(filename) as f: + content = f.read() + model: MultidefHttp = dothttp_model.model_from_str(content) + command.params["content"] = content + property_hovered = None + matches = property_regex.finditer(content) + for match in matches: + if match.start() <= pos <= match.end(): + property_hovered = match.group()[2:-2].split("=")[0].strip() + break + type_dict = TypeFromPos.figure_n_get(model, pos) if "target" not in type_dict: command.params["target"] = 1 @@ -319,6 +330,12 @@ def get_resolved(self, command: Command) -> Result: comp: RequestCompiler = self.get_request_comp(config) comp.load_def() type_type = type_dict['type'] + + if property_hovered: + type_dict["property_at_pos"] = { + "name": property_hovered, + "value": comp.property_util.resolve_property_string(property_hovered), + } if type_type == DothttpTypes.URL.value: type_dict["resolved"] = comp.httpdef.url elif type_type == DothttpTypes.NAME.value: diff --git a/dothttp/utils/property_util.py b/dothttp/utils/property_util.py index e3d31e4..30dc1c1 100644 --- a/dothttp/utils/property_util.py +++ b/dothttp/utils/property_util.py @@ -110,6 +110,7 @@ def evaluate_expression(expression: str) -> str: base_logger.error(f"Error evaluating expression `{expression}`: {e}") return expression +property_regex = re.compile(r"{{(?P.*?)}}", re.DOTALL) class PropertyProvider: """ @@ -126,7 +127,7 @@ class PropertyProvider: """ random = Random() - var_regex = re.compile(r"{{(?P.*?)}}", re.DOTALL) + var_regex = property_regex rand_map: Dict[str, FunctionType] = { "$randomStr": get_random_str, "$randomInt": get_random_int, diff --git a/pyproject.toml b/pyproject.toml index ae0aea0..3b576fd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dothttp-req" -version = "0.0.44a9" +version = "0.0.44a10" description = "Dothttp is Simple http client for testing and development" authors = ["Prasanth "] license = "MIT" diff --git a/test/extensions/test_commands.py b/test/extensions/test_commands.py index b255aea..e8c9b3f 100644 --- a/test/extensions/test_commands.py +++ b/test/extensions/test_commands.py @@ -258,6 +258,19 @@ def test_hover_context_with_no_target(self): ) expected = {"totalSeconds": 10800, "rama": "ranga"} self.assertEquals(expected, result.result["resolved"]) + + # look for property name and resolve it + + index = content.find("{{numOfSeconds}}") + result = resolve_handler.run( + Command( + method=resolve_handler.name, + params={"content": content, + "position": index, "contexts": contexts}, + id=1, + ) + ) + self.assertEquals({'name': 'numOfSeconds', 'value': 10800}, result.result["property_at_pos"]) def test_hover_import_relative_content(self): resolve_handler = GetHoveredResolvedParamContentHandler() @@ -280,6 +293,7 @@ def test_hover_import_relative_content(self): ) ) expected = "https://req.dothttp.dev/ram" + self.assertEquals(expected, result.result["resolved"]) class FileExecute(TestBase):