From f6ff15144f88328ff1699d44cd727eb095ffd45e Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 14 Oct 2024 17:24:33 +0100 Subject: [PATCH 1/3] Ensure only list lines are renumbered --- .../markdown-actions/markdown-actions.vala | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/plugins/markdown-actions/markdown-actions.vala b/plugins/markdown-actions/markdown-actions.vala index 024e97c3b6..48006675bf 100644 --- a/plugins/markdown-actions/markdown-actions.vala +++ b/plugins/markdown-actions/markdown-actions.vala @@ -115,20 +115,19 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable private void fix_ordered_list_numbering () { Gtk.TextIter next; + var count = 1; + var item_text = ""; var current_buffer = current_source.buffer; + current_buffer.get_iter_at_offset (out next, current_buffer.cursor_position); var line = get_current_line (next).strip (); - int count = 1; - string item_text; + // Get list item number from current line parse_ordered_list_item (line, ref count, out item_text); - - while (next.forward_line ()) { + // Start checking following lines + next.forward_line (); + line = get_current_line (next).strip (); + while (parse_ordered_list_item (line, ref count, out item_text)) { count++; - line = get_current_line (next).strip (); - if (line.length == 0) { - break; - } - var next_mark = current_buffer.create_mark (null, next, true); var point_offset = line.index_of_char ('.'); var start = next; @@ -140,6 +139,8 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable var to_insert = "%d".printf (count); current_buffer.insert (ref next, to_insert, to_insert.length); + next.forward_line (); + line = get_current_line (next).strip (); } } From 901d71585e5aa3498f5132b3506cf973d1fbafdd Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 14 Oct 2024 17:24:45 +0100 Subject: [PATCH 2/3] Fix code style --- plugins/markdown-actions/markdown-actions.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/markdown-actions/markdown-actions.vala b/plugins/markdown-actions/markdown-actions.vala index 48006675bf..29e5fb4d40 100644 --- a/plugins/markdown-actions/markdown-actions.vala +++ b/plugins/markdown-actions/markdown-actions.vala @@ -144,7 +144,7 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable } } - private string get_current_line (Gtk.TextIter? start=null) { + private string get_current_line (Gtk.TextIter? start = null) { var current_buffer = current_source.buffer; Gtk.TextIter end; From 3bf1719363162dc23d3ef5883248b1255fa1ef55 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Tue, 15 Oct 2024 14:31:04 +0100 Subject: [PATCH 3/3] Handle indents when appending or inserting ordered lines --- .../markdown-actions/markdown-actions.vala | 74 ++++++++++++------- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/plugins/markdown-actions/markdown-actions.vala b/plugins/markdown-actions/markdown-actions.vala index 29e5fb4d40..7094405f43 100644 --- a/plugins/markdown-actions/markdown-actions.vala +++ b/plugins/markdown-actions/markdown-actions.vala @@ -77,7 +77,7 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable if (evt.keyval == Gdk.Key.Return) { char ul_marker; - int ol_number = 1; + int indent_spaces, ol_number; string item_text; var line = get_current_line (); if (parse_unordered_list_item (line, out ul_marker)) { @@ -88,13 +88,13 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable current_source.buffer.insert_at_cursor (to_insert, to_insert.length); } return true; - } else if (parse_ordered_list_item (line, ref ol_number, out item_text)) { + } else if (parse_ordered_list_item (line, out ol_number, out item_text, out indent_spaces)) { if (item_text.length == 0) { delete_empty_item (); } else { - string to_insert = "\n%d. ".printf (ol_number + 1); + string to_insert = "\n%s%d. ".printf (string.nfill (indent_spaces, ' '), ol_number + 1); current_source.buffer.insert_at_cursor (to_insert, to_insert.length); - fix_ordered_list_numbering (); + fix_ordered_list_numbering (indent_spaces); } return true; } @@ -111,36 +111,44 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable end.forward_to_line_end (); current_buffer.delete (ref start, ref end); current_buffer.insert_at_cursor ("\n", 1); + current_buffer.get_iter_at_offset (out start, current_buffer.cursor_position); } - private void fix_ordered_list_numbering () { + private void fix_ordered_list_numbering (int indent_spaces) { Gtk.TextIter next; - var count = 1; - var item_text = ""; var current_buffer = current_source.buffer; - current_buffer.get_iter_at_offset (out next, current_buffer.cursor_position); - var line = get_current_line (next).strip (); + var line = get_current_line (next); // Get list item number from current line - parse_ordered_list_item (line, ref count, out item_text); + int next_indent_spaces, count, next_count; + string item_text; + parse_ordered_list_item (line, out count, out item_text, out next_indent_spaces); // Start checking following lines next.forward_line (); - line = get_current_line (next).strip (); - while (parse_ordered_list_item (line, ref count, out item_text)) { - count++; - var next_mark = current_buffer.create_mark (null, next, true); - var point_offset = line.index_of_char ('.'); - var start = next; - var end = start; - end.forward_chars (point_offset); - - current_buffer.delete (ref start, ref end); - current_buffer.get_iter_at_mark (out next, next_mark); - - var to_insert = "%d".printf (count); - current_buffer.insert (ref next, to_insert, to_insert.length); + line = get_current_line (next); // Next now at line start + // Search for ordered list lines at the same level until level falls below + while (parse_ordered_list_item (line, out next_count, out item_text, out next_indent_spaces) && + next_indent_spaces >= indent_spaces) { + + // Only update lines at same indent within same block + if (next_indent_spaces == indent_spaces) { + count++; + next.forward_chars (indent_spaces); + var next_mark = current_buffer.create_mark (null, next, true); + var point_offset = line.strip ().index_of_char ('.'); + var start = next; + var end = start; + end.forward_chars (point_offset); + + current_buffer.delete (ref start, ref end); + current_buffer.get_iter_at_mark (out next, next_mark); + + var to_insert = "%d".printf (count); + current_buffer.insert (ref next, to_insert, to_insert.length); + } + next.forward_line (); - line = get_current_line (next).strip (); + line = get_current_line (next); } } @@ -159,8 +167,16 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable return current_buffer.get_text (start, end, false); } - private bool parse_ordered_list_item (string line, ref int current_number, out string item_text) { + private bool parse_ordered_list_item ( + string line, + out int current_number, + out string item_text, + out int indent_spaces) { + item_text = ""; + indent_spaces = -1; + current_number = -1; + int first_point_character = line.index_of_char ('.'); if (first_point_character < 0) { return false; @@ -169,17 +185,21 @@ public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable item_text = line.substring (first_point_character + 1).strip (); var line_start = line.substring (0, first_point_character); + indent_spaces = line_start.last_index_of_char (' ') + 1; if (!int.try_parse (line_start, out current_number)) { return false; } - return true; + + return indent_spaces >= 0 && current_number >= 1; } private bool parse_unordered_list_item (string line, out char ul_marker) { + line.chug (); // Remove leading spaces if ((line[0] == '*' || line[0] == '-') && line[1] == ' ') { ul_marker = line[0]; return true; } + ul_marker = '\0'; return false; }