diff options
Diffstat (limited to 'apps/Sublime Text 4/Packages/User/HSMusic Editing.py')
-rw-r--r-- | apps/Sublime Text 4/Packages/User/HSMusic Editing.py | 62 |
1 files changed, 57 insertions, 5 deletions
diff --git a/apps/Sublime Text 4/Packages/User/HSMusic Editing.py b/apps/Sublime Text 4/Packages/User/HSMusic Editing.py index 41c6408..9b8c369 100644 --- a/apps/Sublime Text 4/Packages/User/HSMusic Editing.py +++ b/apps/Sublime Text 4/Packages/User/HSMusic Editing.py @@ -22,6 +22,25 @@ class ProjectSpecificKeyBinding(sublime_plugin.EventListener): elif operator == sublime.QueryOperator.NOT_REGEX_CONTAINS: return not re.search(operand, current_project_name) +class FileSpecificKeyBinding(sublime_plugin.EventListener): + def on_query_context(self, view, key, operator, operand, match_all): + current_file_name = view.file_name() + + if not current_file_name: + return None + + if key != "file_name" and os.path.isfile(current_file_name): + return None + + if operator == sublime.OP_EQUAL: + return current_file_name == operand + elif operator == sublime.OP_NOT_EQUAL: + return current_file_name != operand + elif operator == sublime.QueryOperator.REGEX_CONTAINS: + return re.search(operand, current_file_name) + elif operator == sublime.QueryOperator.NOT_REGEX_CONTAINS: + return not re.search(operand, current_file_name) + class _CursorAdaptiveCommand(sublime_plugin.TextCommand): handle_region_fallback = ('keep',) @@ -170,7 +189,7 @@ def _normalize_region(region): def _match_tag_in_progress(view, region): region = _normalize_region(region) to_start = sublime.Region(view.line(region).a, region.a) - match = re.search('(\[\[(?:(?!]]|\|).)*)$', view.substr(to_start)) + match = re.search(r'(\[\[(?:(?!]]|\|).)*)$', view.substr(to_start)) if match: return match.group(1) @@ -180,13 +199,13 @@ def _match_regex_ahead_same_line(view, region, regex): return re.search(regex, view.substr(to_end)) def _match_rest_of_tag_in_progress(view, region): - regex = '^((?:(?!\[\[).)*]])' + regex = r'^((?:(?!\[\[).)*]])' match = _match_regex_ahead_same_line(view, region, regex) if match: return match.group(1) def _match_through_closing_html_tag(view, region, tag): - regex = f'^(.*?</{tag}>)' + regex = rf'^(.*?</{tag}>)' match = _match_regex_ahead_same_line(view, region, regex) if match: return match.group(1) @@ -199,7 +218,7 @@ def _consider_reference_cycle(view, region, cycle): rest_of_tag = _match_rest_of_tag_in_progress(view, region) - rest_of_ref = re.search('^((?:(?!\|).)*:)', rest_of_tag) + rest_of_ref = re.search(r'^((?:(?!\|).)*:)', rest_of_tag) if rest_of_ref: return ('move', len(rest_of_ref.group(1))) @@ -225,6 +244,35 @@ def _consider_reference_cycle(view, region, cycle): else: return ('insert', 'right', next_ref + ':', 0) +def _consider_transform_url(view, region): + region = _normalize_region(region) + selected_text = view.substr(region) + + start_through_selection = sublime.Region(view.line(region).a, region.b) + match = re.search(r'https?://\S+$', view.substr(start_through_selection)) + + if not match: + return None + + # We don't have a convenient way to just replace a selection in one move, + # so just move to the end of the selection and try again next time... + if selected_text: + return ('collapse_move', len(selected_text)) + + url = match.group(0) + link = None + + hsm = r'(?:https://(?:preview\.|staging\.)?hsmusic\.wiki/|http://localhost:\d+/)' + + match = re.search(hsm + r'(album|track|artist|group|tag|flash)/(.+?)/', url) + if match: + link = f'[[{match.group(1)}:{match.group(2)}]]' + + if not link: + return None + + return ('replace_left', len(url), link, 0, 'right') + def _guess_directory(name): directory = name directory = "-".join(directory.split(" ")) @@ -257,11 +305,15 @@ class ExitWikiTagCommand(_CursorAdaptiveCommand): if rest_of_tag: return ('collapse_move', len(rest_of_tag)) + transform_url_action = _consider_transform_url(self.view, region) + if transform_url_action: + return transform_url_action + region = _normalize_region(region) to_start = sublime.Region(self.view.line(region).a, region.a) to_end = sublime.Region(region.a, self.view.line(region).b) if to_start.a != to_start.b and to_end.a != to_end.b: - match = re.search('.*?([).,:;!?][).,:;!?"\']*|\\s(?=[(\'"])|[\'"](?=\\s|$)|$)', self.view.substr(to_end)) + match = re.search(r'.*?([).,:;!?][).,:;!?"\']*|\\s(?=[(\'"])|[\'"](?=\\s|$)|$)', self.view.substr(to_end)) if match: return ('collapse_move', len(match.group(0))) |