Skip to content

Commit

Permalink
misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhang-gh committed Dec 24, 2023
1 parent 09be4ab commit 54a59fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
44 changes: 18 additions & 26 deletions src/listEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function onEnterKey(modifiers?: IModifier) {
const editor = window.activeTextEditor!;
let cursorPos: Position = editor.selection.active;
let line = editor.document.lineAt(cursorPos.line);
let textBeforeCursor = line.text.substr(0, cursorPos.character);
let textAfterCursor = line.text.substr(cursorPos.character);
let textBeforeCursor = line.text.substring(0, cursorPos.character);
let textAfterCursor = line.text.substring(cursorPos.character);

let lineBreakPos = cursorPos;
if (modifiers == 'ctrl') {
Expand All @@ -52,30 +52,22 @@ function onEnterKey(modifiers?: IModifier) {
}

//// If it's an empty list item, remove it
if (/^([-+*]|[0-9]+[.)])( +\[[ x]\])?$/.test(textBeforeCursor.trim()) && textAfterCursor.trim().length == 0) {
return editor.edit(editBuilder => {
const editor = window.activeTextEditor!;
let cursor = editor.selection.active;
let document = editor.document;
let textBeforeCursor = document.lineAt(cursor.line).text.substr(0, cursor.character);

if (/^\s+([-+*]|[0-9]+[.)]) $/.test(textBeforeCursor) || /^\s+([-+*]|[0-9]+[.)]) +(\[[ x]\] )$/.test(textBeforeCursor)) {
// e.g. textBeforeCursor === ` - `, ` 1. `, ` - [ ]`, ` 1. [x]`
return outdent(editor).then(() => fixMarker(editor));
} else if (/^([-+*]|[0-9]+[.)]) $/.test(textBeforeCursor)) {
// e.g. textBeforeCursor === `- `, `1. `
return editor.edit(editBuilder => {
editBuilder.replace(new Range(cursor.with({ character: 0 }), cursor), ''.repeat(textBeforeCursor.length))
}).then(() => fixMarker(editor));
} else if (/^([-+*]|[0-9]+[.)]) +(\[[ x]\] )$/.test(textBeforeCursor)) {
// e.g. textBeforeCursor === `- [ ]`, `1. [x]`, `- [x]`, `1. [ ]`
return deleteRange(editor, new Range(cursor.with({ character: textBeforeCursor.length - 4 }), cursor)).then(() => fixMarker(editor));
} else {
return asNormal(editor, 'enter',modifiers);
}
}).then(() => {
editor.revealRange(editor.selection);
}).then(() => fixMarker(editor));
if (
/^([-+*]|[0-9]+[.)])( +\[[ x]\])?$/.test(textBeforeCursor.trim()) // It is a (task) list item
&& textAfterCursor.trim().length == 0 // It is empty
) {
if (/^\s+([-+*]|[0-9]+[.)]) +(\[[ x]\] )?$/.test(textBeforeCursor)) {
// It is not a top-level list item, outdent it
return outdent(editor).then(() => fixMarker(editor));
} else if (/^([-+*]|[0-9]+[.)]) $/.test(textBeforeCursor)) {
// It is a general list item, delete the list marker
return deleteRange(editor, new Range(cursorPos.with({ character: 0 }), cursorPos)).then(() => fixMarker(editor));
} else if (/^([-+*]|[0-9]+[.)]) +(\[[ x]\] )$/.test(textBeforeCursor)) {
// It is a task list item, delete the checkbox
return deleteRange(editor, new Range(cursorPos.with({ character: textBeforeCursor.length - 4 }), cursorPos)).then(() => fixMarker(editor));
} else {
return asNormal(editor, 'enter', modifiers);
}
}

let matches: RegExpExecArray | null;
Expand Down
1 change: 1 addition & 0 deletions src/tableFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class MarkdownDocumentRangeFormattingEditProvider extends MarkdownDocumentFormat
if (!tables || token.isCancellationRequested) {
return;
}

const selectedTables = new Array();
tables.forEach((table) => {
if (range.contains(table.range)) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/integration/listEditing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ suite("List editing.", () => {
],
new Selection(1, 0, 1, 0));
});
test("Enter key. Outdent list item when there is a space before it, until the front is empty", () => {

test("Enter key. Outdent empty list item until it is top-level", () => {
return testCommand('markdown.extension.onEnterKey',
[
'- item1',
' - '
' - '
],
new Selection(1, 6, 1, 6),
[
Expand Down

0 comments on commit 54a59fe

Please sign in to comment.