Skip to content

Commit

Permalink
Merge pull request #47 from fourdigits/goto-context
Browse files Browse the repository at this point in the history
Add partial support for goto context variables
  • Loading branch information
rubenhesselink authored Aug 27, 2024
2 parents 71b660d + e5bbb57 commit 80ac161
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions djlsp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ def goto_definition(self, line, character):
re.compile(r""".*{% ?(extends|include) ('|")([\w\-\./]*)$"""),
self.get_template_definition,
),
(
re.compile(r".*({{|{% \w+ ).*?([\w\d_\.]*)$"),
self.get_context_definition,
),
]
for regex, definition in matchers:
if match := regex.match(line_fragment):
Expand All @@ -385,6 +389,26 @@ def get_template_definition(self, line, character, match: Match):
),
)

def get_context_definition(self, line, character, match: Match):
first_match = match.group(2)
full_match = self._get_full_definition_name(line, character, first_match)
logger.debug(f"Find context goto definition for: {full_match}")
if gotos := self.create_jedi_script(full_match).goto(column=len(first_match)):
goto = gotos[0]
if goto.module_name == "__main__":
# Location is in fake script get type location
if infers := goto.infer():
goto = infers[0]
else:
return None
return Location(
uri=f"file://{goto.module_path}",
range=Range(
start=Position(line=goto.line, character=goto.column),
end=Position(line=goto.line, character=goto.column),
),
)

def _get_full_definition_name(self, line, character, first_part):
if match_after := re.match(
r"^([\w\d]+).*", self.document.lines[line][character:]
Expand Down

0 comments on commit 80ac161

Please sign in to comment.