Skip to content

Commit

Permalink
fix #349 include hovered property
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 committed Jan 3, 2025
1 parent 0846fb9 commit 7d104ed
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
19 changes: 18 additions & 1 deletion dotextensions/server/handlers/basic_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion dothttp/utils/property_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<var>.*?)}}", re.DOTALL)

class PropertyProvider:
"""
Expand All @@ -126,7 +127,7 @@ class PropertyProvider:
"""

random = Random()
var_regex = re.compile(r"{{(?P<var>.*?)}}", re.DOTALL)
var_regex = property_regex
rand_map: Dict[str, FunctionType] = {
"$randomStr": get_random_str,
"$randomInt": get_random_int,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
14 changes: 14 additions & 0 deletions test/extensions/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down

0 comments on commit 7d104ed

Please sign in to comment.