Skip to content

Commit

Permalink
Add folding
Browse files Browse the repository at this point in the history
Related to #171
  • Loading branch information
felixhao28 committed Dec 9, 2016
1 parent 9164b2f commit e7cd2a8
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Default (Linux).sublime-keymap
Expand Up @@ -593,5 +593,11 @@
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(> )+", "match_all": true }
]
},
{ "keys": ["shift+tab"], "command": "fold_section", "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
]
6 changes: 6 additions & 0 deletions Default (OSX).sublime-keymap
Expand Up @@ -598,5 +598,11 @@
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(> )+", "match_all": true }
]
},
{ "keys": ["shift+tab"], "command": "fold_section", "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
]
6 changes: 6 additions & 0 deletions Default (Windows).sublime-keymap
Expand Up @@ -593,5 +593,11 @@
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(> )+", "match_all": true }
]
},
{ "keys": ["shift+tab"], "command": "fold_section", "context":
[
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
]
}
]
4 changes: 4 additions & 0 deletions Default.sublime-commands
Expand Up @@ -72,5 +72,9 @@
{
"caption": "MarkdownEditing: Organize References",
"command": "reference_organize"
},
{
"caption": "MarkdownEditing: Toggle Folding Current Section",
"command": "fold_section"
}
]
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -84,6 +84,7 @@ The preferred method of installation is via [Sublime Package Control][wbond].
| <kbd>⌘</kbd><kbd>⌥</kbd><kbd>B</kbd> <kbd>⌘</kbd><kbd>⌥</kbd><kbd>I</kbd> | <kbd>Alt</kbd><kbd>B</kbd> <kbd>Alt</kbd><kbd>I</kbd> | These are bound to bold and italic. They work both with and without selections. If there is no selection, they will just transform the word under the cursor. These keybindings will unbold/unitalicize selection if it is already bold/italic.
| <kbd>⌘</kbd><kbd>^</kbd><kbd>1...6</kbd> | <kbd>Ctrl</kbd><kbd>1...6</kbd> | These will add the corresponding number of hashmarks for headlines. Works on blank lines and selected text in tandem with the above headline tools. If you select an entire existing headline, the current hashmarks will be removed and replaced with the header level you requested. This command respects the `mde.match_header_hashes` preference setting.
| <kbd>⌥</kbd><kbd>⇧</kbd><kbd>6</kbd> | <kbd>Alt</kbd><kbd>Shift</kbd><kbd>6</kbd> | Inserts a footnote.
| <kbd>⌘</kbd><kbd>Tab</kbd> | <kbd>Shift</kbd><kbd>Tab</kbd> | Fold/Unfold current section.

## GFM Specific Features

Expand Down
2 changes: 1 addition & 1 deletion decide_title.py
Expand Up @@ -9,7 +9,7 @@ def on_modified_async(self, view):
syntax = view.settings().get('syntax')
if syntax and 'Markdown' in syntax:
text = view.substr(sublime.Region(0, view.size()))
it = re.finditer(r'^(#{1,6}(?!#))|(-{3,}|={3,})', text, re.M)
it = re.finditer(r'^(#{1,6}(?!#))|^(-{3,}|={3,})', text, re.M)
title = ''
for m in it:
if re.match(r'^(-{3,}|={3,})$', m.group()):
Expand Down
58 changes: 58 additions & 0 deletions folding.py
@@ -0,0 +1,58 @@
import sublime
import sublime_plugin
import re
try:
from MarkdownEditing.mdeutils import *
except ImportError:
from mdeutils import *

def getFoldedRegion(view, reg):
for i in view.folded_regions():
if i.contains(reg):
return i
return None

class FoldSectionCommand(MDETextCommand):

def run(self, edit):
view = self.view
sections = []
shouldUnfold = False
for sel in view.sel():
text = view.substr(sublime.Region(0, view.size()))
it = re.finditer(r'^(#{1,6}(?!#))|^(-{3,}|={3,})', text, re.M)
section_start = -1
section_end = view.size() - 1
section_level = 0
for m in it:
if re.match(r'^(-{3,}|={3,})$', m.group()):
title_end = m.start() - 1
title_begin = text.rfind('\n', 0, title_end) + 1
title_end = m.end() + 1
level = 2 if text[m.start()] == '-' else 1
else:
title_begin = m.end()
title_end = re.search('(' + m.group() + ')?(\n|$)', text[title_begin:]).start() + title_begin
title_begin = m.start()
level = m.end() - m.start()
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:
sections.append(folded)
shouldUnfold = True
else:
sections.append(reg)

for reg in sections:
if shouldUnfold:
view.unfold(reg)
else:
view.fold(reg)

8 changes: 8 additions & 0 deletions messages/2.2.1.md
Expand Up @@ -3,10 +3,18 @@
Your _MarkdownEditing_ plugin is updated. Enjoy new version. For any type of
feedback you can use [GitHub issues][issues].

Just FYI, you can change the color theme of MarkdownEditing. If you are willing to do so, there are [Dark][github 2] and [yellow][github 3] theme available, plus [thirdparty themes](additional-color-themes). See [configuration](configuration) section to learn **how to change the theme**.

## Bug Fixes

## New Features

* `Shift+Tab` is now default key binding to toggle folding in current sections.

## Changes

[issues]: https://github.com/SublimeText-Markdown/MarkdownEditing/issues
[github 2]: https://raw.github.com/SublimeText-Markdown/MarkdownEditing/master/screenshots/dark.png
[github 3]: https://raw.github.com/SublimeText-Markdown/MarkdownEditing/master/screenshots/yellow.png
[additional-color-themes]: https://github.com/SublimeText-Markdown/MarkdownEditing/tree/master#additional-color-themes
[configuration]: https://github.com/SublimeText-Markdown/MarkdownEditing/tree/master#configuration

0 comments on commit e7cd2a8

Please sign in to comment.