Skip to content

Commit

Permalink
style fix & fix exception when no selection
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhao28 committed Aug 18, 2016
1 parent 09b828c commit 19e150e
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 109 deletions.
8 changes: 7 additions & 1 deletion bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@

package_name = 'MarkdownEditing'


def get_ingored_packages():
settings = sublime.load_settings('Preferences.sublime-settings')
return settings.get('ignored_packages', [])


def save_ingored_packages(ignored_packages):
settings.set('ignored_packages', ignored_packages)
sublime.save_settings('Preferences.sublime-settings')


def disable_native_markdown_package():
ignored_packages = get_ingored_packages()
if 'Markdown' not in ignored_packages:
ignored_packages.append('Markdown')
save_ingored_packages(ignored_packages)


def enable_native_markdown_package():
ignored_packages = get_ingored_packages()
if 'Markdown' in ignored_packages:
ignored_packages.remove('Markdown')
save_ingored_packages(ignored_packages)


def plugin_loaded():
from package_control import events

if events.install(package_name):
# Native package causes some conflicts.
disable_native_markdown_package()


def plugin_unloaded():
from package_control import events

Expand All @@ -39,4 +45,4 @@ def plugin_unloaded():
# Compat with ST2
if sys.version_info < (3,):
plugin_loaded()
unload_handler = plugin_unloaded
unload_handler = plugin_unloaded
10 changes: 6 additions & 4 deletions custom_find_under_expand.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
Re-implements `find_under_expand` command because ST refuses to use it inside macro
definitions.
Re-implements `find_under_expand` command because ST refuses to use it inside macro
definitions.
Source: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=5148
Source: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=5148
"""

import sublime, sublime_plugin
import sublime
import sublime_plugin


class CustomFindUnderExpandCommand(sublime_plugin.TextCommand):

def run(self, edit):
regions = []

Expand Down
3 changes: 1 addition & 2 deletions decide_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def on_modified_async(self, view):
title = text[title_begin: title_end]
else:
title_begin = m.end()
title_end = re.search(
'(' + m.group() + ')?(\n|$)', text[title_begin:]).start() + title_begin
title_end = re.search('(' + m.group() + ')?(\n|$)', text[title_begin:]).start() + title_begin
title = text[title_begin: title_end]
break

Expand Down
10 changes: 4 additions & 6 deletions distraction_free_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
https://github.com/maliayas/SublimeText_FullScreenStatus
"""

import sublime, sublime_plugin
import sublime
import sublime_plugin
from MarkdownEditing.mdeutils import *


def on_distraction_free():
return sublime.active_window().settings().get('fss_on_distraction_free')

def view_is_markdown(view):
if len(view.sel()) > 0:
return bool(view.score_selector(view.sel()[0].a, "text.html.markdown"))
else:
return False

class KeepCurrentLineCentered(sublime_plugin.EventListener):

def on_modified_async(self, view):
# One of the MarkdownEditing syntax files must be in use.
if not view_is_markdown(view):
Expand Down
39 changes: 12 additions & 27 deletions footnotes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sublime
import sublime_plugin
import re
from MarkdownEditing.mdeutils import *

DEFINITION_KEY = 'MarkdownEditing-footnote-definitions'
REFERENCE_KEY = 'MarkdownEditing-footnote-references'
Expand Down Expand Up @@ -64,13 +65,9 @@ def strip_trailing_whitespace(view, edit):
if tws:
view.erase(edit, tws)

def view_is_markdown(view):
if len(view.sel()) > 0:
return bool(view.score_selector(view.sel()[0].a, "text.html.markdown"))
else:
return False

class MarkFootnotes(sublime_plugin.EventListener):

def update_footnote_data(self, view):
if view_is_markdown(view):
view.add_regions(REFERENCE_KEY, view.find_all(REFERENCE_REGEX), '', 'cross', sublime.HIDDEN)
Expand All @@ -83,7 +80,8 @@ def on_load(self, view):
self.update_footnote_data(view)


class GatherMissingFootnotesCommand(sublime_plugin.TextCommand):
class GatherMissingFootnotesCommand(MDETextCommand):

def run(self, edit):
refs = get_footnote_identifiers(self.view)
defs = get_footnote_definition_markers(self.view)
Expand All @@ -93,10 +91,9 @@ def run(self, edit):
for note in missingnotes:
self.view.insert(edit, self.view.size(), '\n [^%s]: ' % note)

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class InsertFootnoteCommand(sublime_plugin.TextCommand):
class InsertFootnoteCommand(MDETextCommand):

def run(self, edit):
view = self.view
markernum = get_next_footnote_marker(view)
Expand All @@ -116,10 +113,9 @@ def run(self, edit):
if view.settings().get('command_mode'):
view.run_command('enter_insert_mode', {"insert_command": "move", "insert_args": {"by": "characters", "forward": True}})

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class GoToFootnoteDefinitionCommand(sublime_plugin.TextCommand):
class GoToFootnoteDefinitionCommand(MDETextCommand):

def run(self, edit):
defs = get_footnote_definition_markers(self.view)
regions = self.view.get_regions(REFERENCE_KEY)
Expand All @@ -142,11 +138,9 @@ def run(self, edit):
self.view.sel().add(defs[target])
self.view.show(defs[target])

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class GoToFootnoteReferenceCommand(MDETextCommand):

class GoToFootnoteReferenceCommand(sublime_plugin.TextCommand):
def run(self, edit):
refs = get_footnote_references(self.view)
match = is_footnote_definition(self.view)
Expand All @@ -156,11 +150,9 @@ def run(self, edit):
[self.view.sel().add(a) for a in refs[target]]
self.view.show(refs[target][0])

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class MagicFootnotesCommand(MDETextCommand):

class MagicFootnotesCommand(sublime_plugin.TextCommand):
def run(self, edit):
if (is_footnote_definition(self.view)):
self.view.run_command('go_to_footnote_reference')
Expand All @@ -169,22 +161,18 @@ def run(self, edit):
else:
self.view.run_command('insert_footnote')

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class SwitchToFromFootnoteCommand(MDETextCommand):

class SwitchToFromFootnoteCommand(sublime_plugin.TextCommand):
def run(self, edit):
if (is_footnote_definition(self.view)):
self.view.run_command('go_to_footnote_reference')
else:
self.view.run_command('go_to_footnote_definition')

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class SortFootnotesCommand(MDETextCommand):

class SortFootnotesCommand(sublime_plugin.TextCommand):
def run(self, edit):
strip_trailing_whitespace(self.view, edit)
defs = get_footnote_definition_markers(self.view)
Expand All @@ -205,6 +193,3 @@ def run(self, edit):

for key in keys:
self.view.insert(edit, self.view.size(), '\n\n ' + notes[key])

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))
10 changes: 5 additions & 5 deletions indent_list_item.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import sublime_plugin
import re
from MarkdownEditing.mdeutils import *

class IndentListItemCommand(sublime_plugin.TextCommand):
def run(self, edit, reverse = False):

class IndentListItemCommand(MDETextCommand):

def run(self, edit, reverse=False):
for region in self.view.sel():
line = self.view.line(region)
line_content = self.view.substr(line)
Expand Down Expand Up @@ -41,6 +44,3 @@ def run(self, edit, reverse = False):

# Insert the new item
self.view.replace(edit, line, new_line)

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))
15 changes: 5 additions & 10 deletions indent_list_multiitem.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import sublime_plugin
import re
from MarkdownEditing.mdeutils import *


class IndentListMultiitemCommand(sublime_plugin.TextCommand):
class IndentListMultiitemCommand(MDETextCommand):

def run(self, edit, reverse=False):
todo = []
Expand All @@ -29,8 +30,7 @@ def run(self, edit, reverse=False):
new_line = line_content
# Transform the bullet to the next/previous bullet type
if self.view.settings().get("mde.list_indent_auto_switch_bullet", True):
bullets = self.view.settings().get(
"mde.list_indent_bullets", ["*", "-", "+"])
bullets = self.view.settings().get("mde.list_indent_bullets", ["*", "-", "+"])

for key, bullet in enumerate(bullets):
re_bullet = re.escape(bullet)
Expand All @@ -45,13 +45,11 @@ def run(self, edit, reverse=False):
break
if not reverse:
# Do the indentation
new_line = re.sub(
bullet_pattern, tab_str + "\\1", new_line, 1)
new_line = re.sub(bullet_pattern, tab_str + "\\1", new_line, 1)

else:
# Do the unindentation
new_line = re.sub(
tab_str + bullet_pattern, "\\1", new_line, 1)
new_line = re.sub(tab_str + bullet_pattern, "\\1", new_line, 1)
else:
if not reverse:
new_line = tab_str + line_content
Expand All @@ -64,6 +62,3 @@ def run(self, edit, reverse=False):
while len(todo) > 0:
j = todo.pop()
self.view.replace(edit, j[0], j[1])

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))
14 changes: 5 additions & 9 deletions lint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import sublime
import sublime_plugin
import re
from MarkdownEditing.mdeutils import *


class mddef(object):
Expand Down Expand Up @@ -613,12 +613,10 @@ def test(self, text, s, e):

if style == 'one':
if sym != '1':
ret[mr.start(
1) + e + 1] = '%s found, \'1\' expected' % repr(sym)
ret[mr.start(1) + e + 1] = '%s found, \'1\' expected' % repr(sym)
else:
if int(sym) != int(lastSym) + 1:
ret[mr.start(1) + e + 1] = ('%s found, \'%d\' expected' %
(repr(sym), int(lastSym) + 1))
ret[mr.start(1) + e + 1] = ('%s found, \'%d\' expected' % (repr(sym), int(lastSym) + 1))
lastSym = sym
return ret

Expand Down Expand Up @@ -652,7 +650,8 @@ def test(self, text, s, e):
ret[e] = '%d spaces found, %d expected' % (nspaces, against_value)
return ret

class MarkdownLintCommand(sublime_plugin.TextCommand):

class MarkdownLintCommand(MDETextCommand):

blockdef = []
scope_block = 'markup.raw.block.markdown'
Expand Down Expand Up @@ -709,6 +708,3 @@ def test(self, tar, text):
break

return ret

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))
7 changes: 5 additions & 2 deletions mdeutils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import sublime
import sublime_plugin


def view_is_markdown(view):
if len(view.sel()) > 0:
return bool(view.score_selector(view.sel()[0].a, "text.html.markdown"))
return len(view.sel()) > 0 and bool(view.score_selector(view.sel()[0].a, "text.html.markdown"))
else:
return False


class MDETextCommand(sublime_plugin.TextCommand):

def is_enabled(self):
return view_is_markdown(self.view)
return view_is_markdown(self.view)
1 change: 1 addition & 0 deletions messages/2.1.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ feedback you can use [GitHub issues][issues].
## Bug Fixes

* Links (anchor) in headings are highlighted correctly in plain Markdown syntax too
* Fixed some errors when there is no cursor on the current opened document

## New Features

Expand Down
12 changes: 7 additions & 5 deletions numbered_list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import sublime_plugin
import re
from MarkdownEditing.mdeutils import *


class NumberListCommand(MDETextCommand):

class NumberListCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
sel = view.sel()[0]
Expand All @@ -19,10 +22,9 @@ def run(self, edit):
view.erase(edit, sel)
view.insert(edit, sel.begin(), "\n%s%d.%s" % (text[:num], int(text[num:dot]) + increment, additional_spaces))

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class NumberListReferenceCommand(sublime_plugin.TextCommand):
class NumberListReferenceCommand(MDETextCommand):

def run(self, edit):
view = self.view
sel = view.sel()[0]
Expand All @@ -34,4 +36,4 @@ def run(self, edit):
view.insert(edit, sel.begin(), "\n%d]: " % (int(text[:dot]) + 1,))
else:
view.erase(edit, sel)
view.insert(edit, sel.begin(), "\n%s%d]: " % (text[:num], int(text[num:dot]) + 1))
view.insert(edit, sel.begin(), "\n%s%d]: " % (text[:num], int(text[num:dot]) + 1))
Loading

0 comments on commit 19e150e

Please sign in to comment.