kahi / sublime-django-click

Open django extends/include tags simply with control-click.
16 stars 3 forks source link

Subdirectories are not searched #9

Open ascripter opened 9 years ago

ascripter commented 9 years ago

Even if a clicked template does exist, it won't open if it's not in the "templates" base-directory. I've modified the code so that it traverses all subdirectories of "templates" and opens the template if it's found. If nothing's found, still open's an empty file. It's a quick, not a clean solution and not thoroughly testet

django_click.py:

import os
import sublime
import sublime_plugin
try:
    from utils import parse_tag
except ImportError:
    from .utils import parse_tag

class DjangoClickCommand(sublime_plugin.TextCommand):
    TEMPLATE_DIR = 'templates'

    def run(self, edit):
        region = self.view.sel()[0]
        line = self.view.line(region)
        line_contents = self.view.substr(line)

        tag, targets = parse_tag(line_contents)
        if tag:
            # get the base-path of current file
            base, current_file = self.view.file_name().split(
                '%(separator)stemplates%(separator)s' % dict(
                    separator=os.path.sep), 1)
            for one in targets:
                # get the target file path
                tar = os.path.join(base, self.TEMPLATE_DIR, one)
                if os.path.isfile(tar):
                    window = sublime.active_window()
                    window.open_file(tar, sublime.ENCODED_POSITION)
                else:
                    # if file is not found in current directory, search in subdirectories
                    # of application's templates-folder. Finds not only files in subdirectories
                    # of the current file, but also in parallel directories.
                    for root, dirs, filenames in os.walk(base):
                        for f in filenames:
                            if f == one:
                                tar = os.path.join(root, one)
                                window = sublime.active_window()
                                window.open_file(tar, sublime.ENCODED_POSITION)

                # fallback: open empty file if nothing was found
                window = sublime.active_window()
                window.open_file(tar, sublime.ENCODED_POSITION)
mxdevmanuel commented 9 years ago

I'll check this and well this repo i sno longer used, so that's the reason I saw your issue till now, if you keep using django click, there's a new repo, and even a new package, thanks for contributing, I will check your code

mxdevmanuel commented 9 years ago

Your code totally works and has been merged into the project, and now it does not open an empty file if template not found :) thanks