From e39c32c6a0a3069ccc15ea7debae0b58b726c9c4 Mon Sep 17 00:00:00 2001 From: Nokse22 Date: Sat, 16 Dec 2023 14:24:08 +0100 Subject: [PATCH 1/6] added automatic list --- src/view.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/view.ts b/src/view.ts index c9fbae8..79d7b1f 100644 --- a/src/view.ts +++ b/src/view.ts @@ -417,6 +417,10 @@ export class WriteableStickyNote extends AbstractStickyNote { constructor(note?: Note) { super(note); + this.buffer.connect('insert-text', (buffer, loc, text, length) => { + this.on_text_inserted(buffer, loc, text, length); + }); + this.buffer.connect("changed", () => { if (this.updating) { this.updating = false; @@ -472,6 +476,25 @@ export class WriteableStickyNote extends AbstractStickyNote { if (compare_tags(tags, this.note!.tags)) return; this.change("tags", tags); } + + on_text_inserted(buffer: Gtk.TextBuffer, loc: Gtk.TextIter, text: string, length: number) { + if (text === '\n') { + const startIter = loc.copy(); + startIter.set_line_offset(0); + const endIter = startIter.copy(); + endIter.forward_char(); + const char = buffer.get_text(startIter, endIter, false); + if (char == '-') { + startIter.forward_char(); + endIter.forward_char(); + const char = buffer.get_text(startIter, endIter, false); + if (char == ' ') { + buffer.insert(loc, `- `, -1); + loc.backward_chars(2); + } + } + } + } } const compare_tags = (a: ITag[], b: ITag[]) => { From 6a14f6c3a59fa31ba35401210b77845228ca1592 Mon Sep 17 00:00:00 2001 From: Nokse22 Date: Sat, 16 Dec 2023 20:30:50 +0100 Subject: [PATCH 2/6] added support for + and *, added list exit with return --- src/view.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/view.ts b/src/view.ts index 79d7b1f..a1dac56 100644 --- a/src/view.ts +++ b/src/view.ts @@ -417,7 +417,7 @@ export class WriteableStickyNote extends AbstractStickyNote { constructor(note?: Note) { super(note); - this.buffer.connect('insert-text', (buffer, loc, text, length) => { + this.buffer.connect_after('insert-text', (buffer, loc, text, length) => { this.on_text_inserted(buffer, loc, text, length); }); @@ -480,16 +480,24 @@ export class WriteableStickyNote extends AbstractStickyNote { on_text_inserted(buffer: Gtk.TextBuffer, loc: Gtk.TextIter, text: string, length: number) { if (text === '\n') { const startIter = loc.copy(); + startIter.backward_char(); startIter.set_line_offset(0); + const endIter = startIter.copy(); - endIter.forward_char(); - const char = buffer.get_text(startIter, endIter, false); - if (char == '-') { - startIter.forward_char(); - endIter.forward_char(); - const char = buffer.get_text(startIter, endIter, false); - if (char == ' ') { - buffer.insert(loc, `- `, -1); + endIter.forward_chars(2); + + const chars = buffer.get_text(startIter, endIter, false); + + if (chars === '- ' || chars === '+ ' || chars === '* ') { + const bullet = chars[0]; + const lineEnd = loc.copy(); + lineEnd.backward_char(); + + if (lineEnd.get_line_offset() === 2) { + startIter.set_line_offset(0); + buffer.delete(startIter, loc); + } else { + buffer.insert(loc, bullet + ' ', -1); loc.backward_chars(2); } } From f41c60ed1cf8c80617c86cf702d7677aa0117229 Mon Sep 17 00:00:00 2001 From: Nokse22 Date: Sat, 16 Dec 2023 21:55:22 +0100 Subject: [PATCH 3/6] added ordered lists --- src/view.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/view.ts b/src/view.ts index a1dac56..3001951 100644 --- a/src/view.ts +++ b/src/view.ts @@ -492,13 +492,33 @@ export class WriteableStickyNote extends AbstractStickyNote { const bullet = chars[0]; const lineEnd = loc.copy(); lineEnd.backward_char(); - if (lineEnd.get_line_offset() === 2) { startIter.set_line_offset(0); buffer.delete(startIter, loc); } else { buffer.insert(loc, bullet + ' ', -1); - loc.backward_chars(2); + } + } else{ + const searchEnd = startIter.copy(); + searchEnd.forward_chars(10); // Maximum number of character to search, with 10 the max order is 99999999. (Excessive? Maybe) + const [ok, matchStart, matchEnd] = startIter.forward_search(' ', 1, searchEnd); + if (!ok) return; + const chars = buffer.get_text(startIter, matchEnd, false); + + const regexPattern = /^\d+\.\s$/; + if (regexPattern.test(chars)) { + const currentOrder = parseInt(chars.slice(0, -2)); + const newOrder = currentOrder + 1; + const newOrderBullet = `${newOrder}. `; + const lineEnd = loc.copy(); + lineEnd.backward_char(); + + if (lineEnd.get_line_offset() === currentOrder.toString().length + 2) { + startIter.set_line_offset(0); + buffer.delete(startIter, loc); + } else { + buffer.insert(loc, newOrderBullet, -1); + } } } } From ccfcee6f8da7cf00f749e70aa688a35c028de57a Mon Sep 17 00:00:00 2001 From: Nokse22 Date: Sun, 17 Dec 2023 19:32:27 +0100 Subject: [PATCH 4/6] used regex for simple lists, improved code for ordered lists, improved variable names --- src/view.ts | 64 +++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/view.ts b/src/view.ts index 3001951..67c84f6 100644 --- a/src/view.ts +++ b/src/view.ts @@ -479,45 +479,47 @@ export class WriteableStickyNote extends AbstractStickyNote { on_text_inserted(buffer: Gtk.TextBuffer, loc: Gtk.TextIter, text: string, length: number) { if (text === '\n') { - const startIter = loc.copy(); - startIter.backward_char(); - startIter.set_line_offset(0); + const start_iter = loc.copy(); + start_iter.backward_char(); + start_iter.set_line_offset(0); - const endIter = startIter.copy(); - endIter.forward_chars(2); + const end_iter = start_iter.copy(); + end_iter.forward_chars(2); + const chars = buffer.get_text(start_iter, end_iter, false); - const chars = buffer.get_text(startIter, endIter, false); - - if (chars === '- ' || chars === '+ ' || chars === '* ') { + const simple_regex_pattern = /[-+*]\s$/; + if (simple_regex_pattern.test(chars)) { const bullet = chars[0]; - const lineEnd = loc.copy(); - lineEnd.backward_char(); - if (lineEnd.get_line_offset() === 2) { - startIter.set_line_offset(0); - buffer.delete(startIter, loc); + const line_end = loc.copy(); + line_end.backward_char(); + if (line_end.get_line_offset() === 2) { + start_iter.set_line_offset(0); + buffer.delete(start_iter, loc); } else { buffer.insert(loc, bullet + ' ', -1); } } else{ - const searchEnd = startIter.copy(); - searchEnd.forward_chars(10); // Maximum number of character to search, with 10 the max order is 99999999. (Excessive? Maybe) - const [ok, matchStart, matchEnd] = startIter.forward_search(' ', 1, searchEnd); - if (!ok) return; - const chars = buffer.get_text(startIter, matchEnd, false); - - const regexPattern = /^\d+\.\s$/; - if (regexPattern.test(chars)) { - const currentOrder = parseInt(chars.slice(0, -2)); - const newOrder = currentOrder + 1; - const newOrderBullet = `${newOrder}. `; - const lineEnd = loc.copy(); - lineEnd.backward_char(); - - if (lineEnd.get_line_offset() === currentOrder.toString().length + 2) { - startIter.set_line_offset(0); - buffer.delete(startIter, loc); + const search_limit = start_iter.copy(); + const search_end = start_iter.copy(); + search_limit.forward_chars(10); + + search_end.forward_find_char((ch) => {return ch === ' '}, search_limit); + search_end.forward_char() + const chars = buffer.get_text(start_iter, search_end, false); + + const ordered_regex_pattern = /^\d+\.\s$/; + if (ordered_regex_pattern.test(chars)) { + const current_order = parseInt(chars.slice(0, -2)); + const new_order = current_order + 1; + const new_order_bullet = `${new_order}. `; + const line_end = loc.copy(); + line_end.backward_char(); + + if (line_end.get_line_offset() === current_order.toString().length + 2) { + start_iter.set_line_offset(0); + buffer.delete(start_iter, loc); } else { - buffer.insert(loc, newOrderBullet, -1); + buffer.insert(loc, new_order_bullet, -1); } } } From 4a82a41c5df17f694cd01bfb829f236c0e0f10f4 Mon Sep 17 00:00:00 2001 From: Nokse22 <44558032+Nokse22@users.noreply.github.com> Date: Sun, 17 Dec 2023 23:55:37 +0100 Subject: [PATCH 5/6] fix regex pattern Co-authored-by: Angelo Verlain Shema <37999241+vixalien@users.noreply.github.com> --- src/view.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/view.ts b/src/view.ts index 67c84f6..6811f20 100644 --- a/src/view.ts +++ b/src/view.ts @@ -487,7 +487,7 @@ export class WriteableStickyNote extends AbstractStickyNote { end_iter.forward_chars(2); const chars = buffer.get_text(start_iter, end_iter, false); - const simple_regex_pattern = /[-+*]\s$/; + const simple_regex_pattern = /^[-+*]\s$/; if (simple_regex_pattern.test(chars)) { const bullet = chars[0]; const line_end = loc.copy(); From b056bba268b1c3e68f15b02b073e73bc57d85ee9 Mon Sep 17 00:00:00 2001 From: Nokse22 Date: Mon, 18 Dec 2023 00:42:25 +0100 Subject: [PATCH 6/6] fixed regex bug \s matched space and new line too --- src/view.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/view.ts b/src/view.ts index 6811f20..4b0f43f 100644 --- a/src/view.ts +++ b/src/view.ts @@ -487,7 +487,7 @@ export class WriteableStickyNote extends AbstractStickyNote { end_iter.forward_chars(2); const chars = buffer.get_text(start_iter, end_iter, false); - const simple_regex_pattern = /^[-+*]\s$/; + const simple_regex_pattern = /^[-+*] $/; if (simple_regex_pattern.test(chars)) { const bullet = chars[0]; const line_end = loc.copy(); @@ -507,7 +507,7 @@ export class WriteableStickyNote extends AbstractStickyNote { search_end.forward_char() const chars = buffer.get_text(start_iter, search_end, false); - const ordered_regex_pattern = /^\d+\.\s$/; + const ordered_regex_pattern = /^\d+\. $/; if (ordered_regex_pattern.test(chars)) { const current_order = parseInt(chars.slice(0, -2)); const new_order = current_order + 1;