Skip to content

Commit

Permalink
Add basic workspace index dataclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
krukas committed Jun 12, 2024
1 parent 3fe15e4 commit e2174b2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
83 changes: 83 additions & 0 deletions djlsp/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from dataclasses import dataclass, field


@dataclass
class Template:
file_path: str = ""
workspace_file_path: str = ""
context: dict = field(default_factory=dict)
# Attributes filled by parser
extends: str | None = None
loaded_libraries: list[str] | None = None
blocks: list[str] | None = None

def clear(self):
self.extends = None
self.blocks = None


@dataclass
class Tag:
name: str = ""
in_between_tags: list[str] = ""
closing_tag: str = ""


@dataclass
class Library:
name: str = ""
tags: dict[str, Tag] = field(default_factory=dict)
filters: list[str] = field(default_factory=list)


@dataclass
class WorkspaceIndex:
file_watcher_globs: [str] = field(default_factory=list)
static_files: [str] = field(default_factory=list)
urls: [str] = field(default_factory=list)
libraries: dict[str, Library] = field(default_factory=dict)
templates: dict[str, Template] = field(default_factory=dict)

def update(self, django_data: dict):
self.file_watcher_globs = django_data.get(
"file_watcher_globs", self.file_watcher_globs
)
self.static_files = django_data.get("static_files", self.static_files)
self.urls = django_data.get("urls", self.urls)

# TODO: add support in django collector
# if libraries := django_data.get("libraries"):
# self.libraries = {
# library.get("name"): Library(
# name=library.get("name"),
# filters=library.get("filters", []),
# tags={
# tag.get("name"): Tag(
# name=tag.get("name"),
# in_between_tags=tag.get("in_between_tags", []),
# closing_tag=tag.get("closing_tag"),
# )
# for tag in library.get("tags", [])
# },
# )
# for library in libraries
# }

# TODO: Add Support in django collector
# Update templates
# if templates := django_data.get("templates"):
# found_templates = []
# for template in templates:
# # TODO: how to handle template override
# file_path = template.get("file_path")
# found_templates.append(file_path)
# if file_path in self.templates:
# self.templates[file_path].context = template.get("context", dict)
# else:
# self.templates[file_path] = Template(
# file_path=file_path,
# workspace_file_path=template.get("workspace_file_path", ""),
# context=template.get("context", dict),
# )

# self.templates = [t for t in self.templates if t in found_templates]
8 changes: 6 additions & 2 deletions djlsp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pygls.workspace import TextDocument

from djlsp.constants import BUILTIN
from djlsp.index import WorkspaceIndex

logger = logging.getLogger(__name__)

Expand All @@ -19,7 +20,11 @@ class TemplateParser:
re_filter = re.compile(r"^.*({%|{{) ?[\w \.\|]*\|(\w*)$")
re_template = re.compile(r""".*{% ?(extends|include) ('|")([\w\-:]*)$""")

def __init__(self, django_data: dict, document: TextDocument):
def __init__(
self, workspace_index: WorkspaceIndex, django_data: dict, document: TextDocument
):
# TODO: remove use of django_data and only use workspace_index
self.workspace_index: WorkspaceIndex = workspace_index
self.django_data = django_data
self.document: TextDocument = document

Expand Down Expand Up @@ -88,7 +93,6 @@ def get_url_completions(self, match: Match):
def get_template_completions(self, match: Match):
prefix = match.group(3)
logger.debug(f"Find {match.group(1)} matches for: {prefix}")
logger.debug(self.django_data["templates"])
return sorted(
[
template
Expand Down
6 changes: 5 additions & 1 deletion djlsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from djlsp import __version__
from djlsp.constants import FALLBACK_DJANGO_DATA
from djlsp.index import WorkspaceIndex
from djlsp.parser import TemplateParser

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -52,6 +53,8 @@ def __init__(self, *args):
self.docker_compose_service = "django"
self.django_settings_module = ""
self.django_data = FALLBACK_DJANGO_DATA
self.workspace_index = WorkspaceIndex()
self.workspace_index.update(FALLBACK_DJANGO_DATA)

def set_initialization_options(self, options: dict):
self.docker_compose_file = options.get(
Expand Down Expand Up @@ -96,6 +99,7 @@ def get_django_data(self):
if django_data:
# TODO: Maybe validate data
self.django_data = django_data
self.workspace_index.update(django_data)
logger.info("Collected project Django data:")
logger.info(f" - Libraries: {len(django_data['libraries'])}")
logger.info(f" - Templates: {len(django_data['templates'])}")
Expand Down Expand Up @@ -230,7 +234,7 @@ def completions(ls: DjangoTemplateLanguageServer, params: CompletionParams):
logger.debug(f"PARAMS: {params}")
items = []
document = server.workspace.get_document(params.text_document.uri)
template = TemplateParser(ls.django_data, document)
template = TemplateParser(ls.workspace_index, ls.django_data, document)
for completion in template.completions(
params.position.line, params.position.character
):
Expand Down

0 comments on commit e2174b2

Please sign in to comment.