Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement sorting options for #665 #672

Open
wants to merge 2 commits into
base: st3176
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Preferences.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
// Adds <Tab> after list items instead of a single <space>.
"mde.list_align_text": false,

// MarkdownEditing (References):
// The sorting method used by the Organize References command.
// Should be one of
//
// 'reference_order': List in order of appearance in document
// 'alphabetical': Alphabetical based on reference name, sorting numerals lexagraphically
// 'numeric': Alphabetical based on reference name, sorting numeral chunks numerically
"mde.ref_organize_sort": "reference_order",
"mde.ref_organize_sort_reverse": false,

// MarkdownEditing:
// Automatically switches list bullet when indenting blank list item with <Tab>.
"mde.list_indent_auto_switch_bullet": true,
Expand Down
20 changes: 16 additions & 4 deletions plugins/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ def run(self, edit):

# reorder
markers = getMarkers(view)
marker_order = sorted(
reference_order = sorted(
markers.keys(), key=lambda marker: min(markers[marker].regions, key=lambda reg: reg.a).a
)
marker_order = dict(zip(marker_order, range(0, len(marker_order))))
reference_order = dict(zip(reference_order, range(0, len(reference_order))))

refs = getReferences(view)
flatrefs = []
Expand All @@ -539,9 +539,21 @@ def run(self, edit):
flatrefs.append((name, view.substr(line_reg).strip("\n")))
sel.add(line_reg)

sorting_funcs = {
"reference_order": lambda x: reference_order[x[0].lower()]
if x[0].lower() in reference_order
else 9999,
"alphabetical": lambda x: x[0].lower(),
"numeric": lambda x: [
int(p) if p.isnumeric() else p for p in re.split(r"[ _.-]", x[0].lower())
],
}
settings = view.settings()

flatfns.sort(key=operator.itemgetter(0))
flatrefs.sort(
key=lambda x: marker_order[x[0].lower()] if x[0].lower() in marker_order else 9999
key=sorting_funcs[settings.get("mde.ref_organize_sort", "reference_order")],
reverse=settings.get("mde.ref_organize_sort_reverse", False),
)

view.run_command("left_delete")
Expand Down Expand Up @@ -596,7 +608,7 @@ def run(self, edit):
lower_refs = [ref.lower() for ref in refs]
missings = []
for ref in refs:
if ref not in marker_order:
if ref not in reference_order:
missings.append(refs[ref].label)
if len(missings) > 0:
output += "Error: Definition [%s] %s no reference\n" % (
Expand Down