From 9761cc710f5f0f805772c5b65ade1bed32f069e1 Mon Sep 17 00:00:00 2001 From: Jason Kozak Date: Fri, 10 Aug 2012 16:03:36 -0300 Subject: [PATCH 1/3] Reload modelines on save --- sublime_modelines.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sublime_modelines.py b/sublime_modelines.py index 08f9d46..0ba199a 100644 --- a/sublime_modelines.py +++ b/sublime_modelines.py @@ -91,11 +91,17 @@ class ExecuteSublimeTextModeLinesCommand(sublime_plugin.EventListener): MAX_LINES_TO_CHECK * LINE_LENGTH defines the size of the regions to be scanned. """ - def on_load(self, view): + def do_modelines(self, view): for setter, name, value in gen_modeline_options(view): try: setter(name, to_json_type(value)) except ValueError, e: sublime.status_message("[SublimeModelines] Bad modeline detected.") print "[SublimeModelines] Bad option detected: %s, %s" % (name, value) - print "[SublimeModelines] Tip: Keys cannot be empty strings." \ No newline at end of file + print "[SublimeModelines] Tip: Keys cannot be empty strings." + + def on_load(self, view): + self.do_modelines(view) + + def on_post_save(self, view): + self.do_modelines(view) \ No newline at end of file From bb8a4376561067b2bbc7169f3608b248b68ecce3 Mon Sep 17 00:00:00 2001 From: Jason Kozak Date: Fri, 10 Aug 2012 16:06:04 -0300 Subject: [PATCH 2/3] Allow trailing semi-colons and post-name colons --- sublime_modelines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sublime_modelines.py b/sublime_modelines.py index 0ba199a..b04ab3b 100644 --- a/sublime_modelines.py +++ b/sublime_modelines.py @@ -44,7 +44,7 @@ def gen_modeline_options(view): modelines = gen_modelines(view) for opt in gen_raw_options(modelines): name, sep, value = opt.partition(' ') - yield view.settings().set, name, value + yield view.settings().set, name.rstrip(':'), value.rstrip(';') def get_line_comment_char(view): From 41efef412fd3486919cb381b68be5bb61aa0073d Mon Sep 17 00:00:00 2001 From: Jason Kozak Date: Fri, 10 Aug 2012 16:09:50 -0300 Subject: [PATCH 3/3] Support for C-style /* one-line */ comments Comment start is now also properly escaped, and only determined once per scan, instead of per-line. --- sublime_modelines.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/sublime_modelines.py b/sublime_modelines.py index b04ab3b..34bb465 100644 --- a/sublime_modelines.py +++ b/sublime_modelines.py @@ -11,8 +11,8 @@ MODELINES_REG_SIZE = MAX_LINES_TO_CHECK * LINE_LENGTH -def is_modeline(view, line): - return bool(re.match(build_modeline_prefix(view), view.substr(line))) +def is_modeline(prefix, line): + return bool(re.match(prefix, line)) def gen_modelines(view): @@ -26,7 +26,10 @@ def gen_modelines(view): ((view.size() - MODELINES_REG_SIZE), 0))[0] candidates += view.lines(sublime.Region(bottomRegStart, view.size())) - for modeline in (view.substr(c) for c in candidates if is_modeline(view, c)): + prefix = build_modeline_prefix(view) + modelines = (view.substr(c) for c in candidates if is_modeline(prefix, view.substr(c))) + + for modeline in modelines: yield modeline @@ -49,16 +52,22 @@ def gen_modeline_options(view): def get_line_comment_char(view): commentChar = "" + commentChar2 = "" try: for pair in view.meta_info("shellVariables", 0): if pair["name"] == "TM_COMMENT_START": commentChar = pair["value"] + if pair["name"] == "TM_COMMENT_START_2": + commentChar2 = pair["value"] + if commentChar and commentChar2: break except TypeError: pass - return commentChar.strip() - + if not commentChar2: + return re.escape(commentChar.strip()) + else: + return "("+re.escape(commentChar.strip())+"|"+re.escape(commentChar2.strip())+")" def build_modeline_prefix(view): lineComment = get_line_comment_char(view).lstrip() or DEFAULT_LINE_COMMENT