diff --git a/djlsp/parser.py b/djlsp/parser.py index 4e40b2f..a04055d 100644 --- a/djlsp/parser.py +++ b/djlsp/parser.py @@ -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): @@ -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:]