Skip to content

Commit

Permalink
Implement sorting options for #665
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanH committed Jan 22, 2022
1 parent 63be270 commit cde9944
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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
//
// 'marker_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": "marker_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
14 changes: 13 additions & 1 deletion plugins/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,21 @@ def run(self, edit):
flatrefs.append((name, view.substr(line_reg).strip("\n")))
sel.add(line_reg)

sorting_funcs = {
"marker_order": lambda x: marker_order[x[0].lower()]
if x[0].lower() in marker_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", "marker_order")],
reverse=settings.get("mde.ref_organize_sort_reverse", False),
)

view.run_command("left_delete")
Expand Down

0 comments on commit cde9944

Please sign in to comment.