From 267a4b6e99bcbe7b81f17d4408192babdf111244 Mon Sep 17 00:00:00 2001 From: Felix Hao Date: Tue, 11 Jul 2017 12:43:39 +0800 Subject: [PATCH] add context menu --- Context.sublime-menu | 14 +++++++++++ folding.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ mdeutils.py | 3 +++ messages/2.2.3.md | 1 + references.py | 9 +++++++ 5 files changed, 83 insertions(+) create mode 100644 Context.sublime-menu diff --git a/Context.sublime-menu b/Context.sublime-menu new file mode 100644 index 00000000..c9417f12 --- /dev/null +++ b/Context.sublime-menu @@ -0,0 +1,14 @@ +[ +{ + "caption": "MDE: Fold Section", + "command": "fold_section_context" +}, +{ + "caption": "MDE: Unfold Section", + "command": "unfold_section_context" +}, +{ + "caption": "MDE: Jump Reference", + "command": "reference_jump_context" +} +] \ No newline at end of file diff --git a/folding.py b/folding.py index 42c67797..8c6270c1 100644 --- a/folding.py +++ b/folding.py @@ -45,6 +45,9 @@ def get_current_level(view, p): class FoldSectionCommand(MDETextCommand): + def description(self): + return 'Toggle fold/unfold on current section' + def run(self, edit): view = self.view sections = [] @@ -76,6 +79,59 @@ def run(self, edit): view.fold(reg) sublime.status_message('%d region%s %sfolded' % (len(sections), 's' if len(sections) > 1 else '', 'un' if shouldUnfold else '')) +class FoldSectionContextCommand(FoldSectionCommand): + + def is_visible(self): + if not FoldSectionCommand.is_visible(self): + return False + view = self.view + hasSection = False + for sel in view.sel(): + section_start = -1 + section_end = view.size() + section_level = 0 + for (title_begin, title_end, level) in all_headings(view): + if title_begin <= sel.a: + section_start = title_end + section_level = level + elif section_level >= level: + section_end = title_begin - 1 + break + if section_start >= 0 and section_end >= section_start: + reg = sublime.Region(section_start, section_end) + folded = getFoldedRegion(view, reg) + if folded != None: + return False + else: + hasSection = True + return hasSection + +class UnfoldSectionContextCommand(FoldSectionCommand): + + def is_visible(self): + if not FoldSectionCommand.is_visible(self): + return False + view = self.view + hasSection = False + for sel in view.sel(): + section_start = -1 + section_end = view.size() + section_level = 0 + for (title_begin, title_end, level) in all_headings(view): + if title_begin <= sel.a: + section_start = title_end + section_level = level + elif section_level >= level: + section_end = title_begin - 1 + break + if section_start >= 0 and section_end >= section_start: + reg = sublime.Region(section_start, section_end) + folded = getFoldedRegion(view, reg) + if folded != None: + hasSection = True + else: + return False + return hasSection class ShowFoldAllSectionsCommand(MDETextCommand): diff --git a/mdeutils.py b/mdeutils.py index 9d6d6809..8bbf27be 100644 --- a/mdeutils.py +++ b/mdeutils.py @@ -13,3 +13,6 @@ class MDETextCommand(sublime_plugin.TextCommand): def is_enabled(self): return view_is_markdown(self.view) + + def is_visible(self): + return view_is_markdown(self.view) diff --git a/messages/2.2.3.md b/messages/2.2.3.md index 842ae595..e24dc2a9 100644 --- a/messages/2.2.3.md +++ b/messages/2.2.3.md @@ -15,6 +15,7 @@ feedback you can use [GitHub issues][issues]. * Organize References command now sorts references by referencing order rather than alphebetic order. * Code block highlight for clojure. * You can use `MarkdownEditing: Change color scheme...` command in command palette to preview and change the color scheme you are using for markdown. +* Added `MDE: (Un)Fold Section` and `MDE: Jump Reference` to context menu (only appear if applicable based on the cursor's location). ### Configurable Key Bindings diff --git a/references.py b/references.py index 44e644ce..11e2ec85 100644 --- a/references.py +++ b/references.py @@ -156,6 +156,9 @@ def get_reference(view, pos): class ReferenceJumpCommand(MDETextCommand): # reference_jump command + def description(self): + return 'Jump between definition and reference' + def run(self, edit): view = self.view edit_regions = [] @@ -192,6 +195,12 @@ def run(self, edit): sublime.status_message("The definition%s of %s and the marker%s of %s cannot be found." % ("" if len(missingRefs) == 1 else "s", ", ".join(missingRefs), "" if len(missingMarkers) == 1 else "s", ", ".join(missingMarkers))) +class ReferenceJumpContextCommand(ReferenceJumpCommand): + + def is_visible(self): + return ReferenceJumpCommand.is_visible(self) and any(get_reference(self.view, sel.begin())[0] for sel in self.view.sel()) + + def is_url(contents): # Returns if contents contains an URL re_match_urls = re.compile(r"""((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.‌​][a-z]{2,4}/)(?:[^\s()<>]+|(([^\s()<>]+|(([^\s()<>]+)))*))+(?:(([^\s()<>]+|(‌​([^\s()<>]+)))*)|[^\s`!()[]{};:'".,<>?«»“”‘’]))""", re.DOTALL)