Skip to content

Commit

Permalink
Merge pull request #30 from eparisio/master
Browse files Browse the repository at this point in the history
add jstl language support + mini refactoring to resolver
  • Loading branch information
anru authored Nov 25, 2017
2 parents 681eacf + d6c12bb commit a45e125
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extended to support more languages.
| Stylus | `Stylus.tmLanguage` |
| PHP | `PHP.sublime-syntax` <br> `PHP Source.sublime-syntax` |
| HTML | `HTML.sublime-syntax` |
| JSTL | `JSTL.sublime-syntax` |

*You can contribute and add more languages by adding a path resolver like [SassPathResolver](https://github.com/aziz/SublimeHyperClick/blob/master/hyper_click/sass_path_resolver.py)*

Expand Down Expand Up @@ -115,6 +116,9 @@ see [hyper_click.sublime-settings](https://github.com/aziz/SublimeHyperClick/blo
],
"html": [
"HTML.sublime-syntax"
],
"jstl": [
"jstl.tmLanguage"
]
},
"import_line_regex": {
Expand All @@ -140,6 +144,9 @@ see [hyper_click.sublime-settings](https://github.com/aziz/SublimeHyperClick/blo
],
"html": [
".*?<link\\s+rel=\"import\"\\s+href=['\"](.+)['\"]/?>.*?"
],
"jstl": [
"(<([\\w-]*)\\:([\\w-]*))",
]
},
"valid_extensions": {
Expand All @@ -148,17 +155,23 @@ see [hyper_click.sublime-settings](https://github.com/aziz/SublimeHyperClick/blo
"less": ["less"],
"php": ["php"],
"stylus": ["styl", "stylus"],
"html": ["html"]
"html": ["html"],
"jstl": ["jsp", "tag"]
},
"default_filenames": {
"js": ["index"]
},
"vendor_dirs": {
"js": ["node_modules"],
"html": ["node_modules", "bower_components"]
"html": ["node_modules", "bower_components"],
"jstl": ["WEB-INF"]
},
"lookup_paths": {
"js": []
"js": [],
"jstl": [
"taglib *prefix ?= ?[\"'](.*)[\"'] *tagdir ?= ?[\"'](.*)[\"']",
"xmlns:(\\w*)=[\"']urn:jsptagdir:(.*?)[\"']"
]
},
"annotation_found_text": "",
"annotation_not_found_text": "",
Expand Down
18 changes: 15 additions & 3 deletions hyper_click.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
],
"html": [
"HTML.sublime-syntax"
],
"jstl": [
"jstl.tmLanguage"
]
},
"import_line_regex": {
Expand All @@ -47,6 +50,9 @@
],
"html": [
".*?<link\\s+rel=\"import\"\\s+href=['\"](.+)['\"]/?>.*?"
],
"jstl": [
"(<([\\w-]*)\\:([\\w-]*))",
]
},
"valid_extensions": {
Expand All @@ -55,17 +61,23 @@
"less": ["less"],
"php": ["php"],
"stylus": ["styl", "stylus"],
"html": ["html"]
"html": ["html"],
"jstl": ["jsp", "tag"]
},
"default_filenames": {
"js": ["index"]
},
"vendor_dirs": {
"js": ["node_modules"],
"html": ["node_modules", "bower_components"]
"html": ["node_modules", "bower_components"],
"jstl": ["WEB-INF"]
},
"lookup_paths": {
"js": []
"js": [],
"jstl": [
"taglib *prefix ?= ?[\"'](.*)[\"'] *tagdir ?= ?[\"'](.*)[\"']",
"xmlns:(\\w*)=[\"']urn:jsptagdir:(.*?)[\"']"
]
},
"annotation_found_text": "➜",
"annotation_not_found_text": "✘",
Expand Down
51 changes: 51 additions & 0 deletions hyper_click/jstl_path_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
import re
from os import path


class JstlPathResolver:
def __init__(self, view, str_lookup, current_dir, roots, lang, settings):
self.view = view
self.import_line_regex = settings.get('import_line_regex', {})[lang]
prog = re.compile(self.import_line_regex[0])
regMatch = prog.match(str_lookup)
self.str_path = regMatch.group(2)
self.str_file = regMatch.group(3)
self.current_dir = current_dir
self.lang = lang
self.settings = settings
self.roots = roots
self.valid_extensions = settings.get('valid_extensions', {})[lang]
self.dirs_regex = settings.get('lookup_paths', {})[lang]
self.vendors = settings.get('vendor_dirs', {})[lang]

def resolve(self):
for regex_str in self.dirs_regex:
regions = self.view.find_all(regex_str)
for region in regions:
file_path = self.find_folder(region)
if file_path:
return file_path
return ''

def find_folder(self, region):
text = self.view.substr(region)
matched = self.is_valid_line(text)
if matched:
if matched.group(1) == self.str_path:
complete_path = matched.group(2) + "/" + self.str_file + ".tag"
file_path = path.realpath(path.join(self.current_dir, self.str_path))
for vendor in self.vendors:
base_dir = re.split(vendor,file_path)
dest_file_path = base_dir[0] + complete_path
if path.isfile(dest_file_path):
return dest_file_path
return ''

def is_valid_line(self, line_content):
for regex_str in self.dirs_regex:
pattern = re.compile(regex_str)
matched = pattern.match(line_content)
if matched:
return matched
return False
6 changes: 5 additions & 1 deletion hyper_click/path_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from .js_path_resolver import JsPathResolver
from .sass_path_resolver import SassPathResolver
from .less_path_resolver import LessPathResolver
from .jstl_path_resolver import JstlPathResolver
from .php_path_resolver import PhpPathResolver
from .html_path_resolver import HTMLPathResolver
from .path_generic_subfolder_resolver import GenericSubfolderResolver


class HyperClickPathResolver:
def __init__(self, str_path, current_file, roots, lang, settings):
def __init__(self, view, str_path, roots, lang, settings):
current_file = view.file_name()
current_dir = path.dirname(path.realpath(current_file))
if lang == 'js':
self.resolver = JsPathResolver(str_path, current_dir, roots, lang, settings)
Expand All @@ -21,6 +23,8 @@ def __init__(self, str_path, current_file, roots, lang, settings):
self.resolver = PhpPathResolver(str_path, current_dir, roots, lang, settings)
elif lang == 'html':
self.resolver = HTMLPathResolver(str_path, current_dir, roots, lang, settings)
elif lang == 'jstl':
self.resolver = JstlPathResolver(view, str_path, current_dir, roots, lang, settings)
else:
self.resolver = GenericSubfolderResolver(str_path, current_dir, roots, lang, settings)

Expand Down
4 changes: 2 additions & 2 deletions hyper_click_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def annotate(self, point):

if matched:
destination_str = matched.group(1)
file_path = HyperClickPathResolver(
destination_str, v.file_name(),
file_path = HyperClickPathResolver(v,
destination_str,
self.roots, self.lang, self.settings
)
region = sublime.Region(line_range.b, line_range.b)
Expand Down
4 changes: 2 additions & 2 deletions hyper_click_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def run(self, edit):
matched = self.is_valid_line(line_content)
if matched:
destination_str = matched.group(1)
file_path = HyperClickPathResolver(
destination_str, v.file_name(),
file_path = HyperClickPathResolver(v,
destination_str,
self.roots, self.lang, self.settings
)
resolved_path = file_path.resolve()
Expand Down

0 comments on commit a45e125

Please sign in to comment.