Skip to content

Commit

Permalink
Add partial support for goto context variables
Browse files Browse the repository at this point in the history
This will use context as is with Jedi, Django templates does some magic
with list/dicts/functions and wont work in that case.
  • Loading branch information
krukas committed Aug 27, 2024
1 parent eb378ee commit e5bbb57
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 e5bbb57

Please sign in to comment.