Skip to content

Commit

Permalink
Merge pull request #46 from niksy/master
Browse files Browse the repository at this point in the history
Add support for per-project Sass lookup paths (closes #44)
Add support for per-project JS lookup paths
  • Loading branch information
anru authored May 5, 2019
2 parents 0269e44 + c691401 commit 190b1cc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
9 changes: 7 additions & 2 deletions hyper_click.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"JavaScript.sublime-syntax",
"JavaScript (Babel).sublime-syntax",
"JavaScriptNext.tmLanguage",
"JavaScript.tmLanguage",
"naomi.fjsx15.sublime-syntax",
"naomi.jsx1.sublime-syntax",
"Vue Component.sublime-syntax"
Expand Down Expand Up @@ -58,7 +59,8 @@
"js": [
"^import\\s+['\"](.+)['\"];?$",
".*from\\s+['\"](.+)['\"];?$",
".*require\\(['\"](.+?)['\"]\\).*"
".*require\\(['\"](.+?)['\"]\\).*",
".*import\\((?:\\/\\*.+?\\*\\/\\s+)?['\"](.+)['\"]\\)(?:[;\\.,])?",
],
"sass": [
"^@import\\s+['\"](.+)['\"];?$"
Expand Down Expand Up @@ -114,10 +116,13 @@
"^{%\\s+import\\s+['\"](.+)['\"].*?%}$",
"^{%\\s+from\\s+['\"](.+)['\"]\\s+import.*?%}$",
"^{%\\s+embed\\s+['\"](.+)['\"].*?%}$",
"^{%\\s+use\\s+['\"](.+)['\"].*?%}$",
"^{%\\s+.+?\\s+with\\s+['\"](.+)['\"].*?%}$",
"^{{\\s+include\\(['\"](.+)['\"].*?$"
]
},
"valid_extensions": {
"js": ["js", "jsx", "vue"],
"js": ["js", "jsx", "vue", "mjs"],
"sass": ["scss", "sass"],
"less": ["less"],
"php": ["php"],
Expand Down
6 changes: 3 additions & 3 deletions hyper_click/js_path_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def __init__(self, str_path, current_dir, roots, lang, settings, proj_settings):
self.settings = settings
self.roots = roots
self.valid_extensions = settings.get('valid_extensions', {})[lang]
self.proj_settings = proj_settings
self.vendor_dirs = settings.get('vendor_dirs', {})[lang];
self.matchingRoots = [root for root in self.roots if self.current_dir.startswith(root)]
self.currentRoot = self.matchingRoots[0] if self.matchingRoots else self.current_dir
self.lookup_paths = self.proj_settings.get('lookup_paths', {}).get(lang, False) or settings.get('lookup_paths', {}).get(lang, False) or []

def resolve(self):
if find_index(NODE_CORE_MODULES, self.str_path) != -1:
Expand Down Expand Up @@ -81,13 +83,11 @@ def resolve_node_modules(self, target, start_dir):
return result

def resolve_in_lookup_paths(self, target):
lookup_paths = self.settings.get('lookup_paths', {})[self.lang] or []
for lookup_path in lookup_paths:
for lookup_path in self.lookup_paths:
result = self.resolve_relative_to_dir(target, path.join(self.currentRoot, lookup_path))
if result:
return result


def resolve_as_file(self, path_name):
if path.isfile(path_name):
return path_name
Expand Down
33 changes: 28 additions & 5 deletions hyper_click/sass_path_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,44 @@ def __init__(self, str_path, current_dir, roots, lang, settings, proj_settings):
self.settings = settings
self.roots = roots
self.valid_extensions = settings.get('valid_extensions', {})[lang]
self.proj_settings = proj_settings
self.matchingRoot = [root for root in self.roots if self.current_dir.startswith(root)]
self.currentRoot = self.matchingRoot[0]
self.lookup_paths = self.proj_settings.get('lookup_paths', {}).get(lang, False) or settings.get('lookup_paths', {}).get(lang, False) or []

def resolve(self):
combined = path.realpath(path.join(self.current_dir, self.str_path))
result = self.resolve_relative_to_dir(self.str_path, self.current_dir)
if result:
return result

result = self.resolve_in_lookup_paths(self.str_path)
if result:
return result

return ''

def resolve_relative_to_dir(self, target, directory):
combined = path.realpath(path.join(directory, target))
return self.resolve_as_file(combined)

def resolve_in_lookup_paths(self, target):
for lookup_path in self.lookup_paths:
result = self.resolve_relative_to_dir(target, path.join(self.currentRoot, lookup_path))
if result:
return result

def resolve_as_file(self, path_name):
# matching ../variables/palette to ../variables/palette.scss
combined = path_name
for ext in self.valid_extensions:
file_path = combined + '.' + ext
if path.isfile(file_path):
return file_path

# matching ../variables/palette to ../variables/_palette.scss
pathname, filename = path.split(self.str_path)
combined = path.realpath(path.join(self.current_dir, pathname, '_' + filename))
pathname, filename = path.split(path_name)
combined = path.join(pathname, '_' + filename)
for ext in self.valid_extensions:
file_path = combined + '.' + ext
if path.isfile(file_path):
return file_path

return ''

0 comments on commit 190b1cc

Please sign in to comment.