Skip to content

Commit

Permalink
add context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhao28 committed Jul 11, 2017
1 parent 35ef993 commit 267a4b6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
@@ -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"
}
]
56 changes: 56 additions & 0 deletions folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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):

Expand Down
3 changes: 3 additions & 0 deletions mdeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions messages/2.2.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 267a4b6

Please sign in to comment.