diff --git a/src/listEditing.ts b/src/listEditing.ts index fe83c534..76b97ad0 100644 --- a/src/listEditing.ts +++ b/src/listEditing.ts @@ -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') { @@ -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 + 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; diff --git a/src/tableFormatter.ts b/src/tableFormatter.ts index 6f08da95..c6de7ee6 100644 --- a/src/tableFormatter.ts +++ b/src/tableFormatter.ts @@ -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)) { diff --git a/src/test/suite/integration/listEditing.test.ts b/src/test/suite/integration/listEditing.test.ts index 2f86a113..408453c5 100644 --- a/src/test/suite/integration/listEditing.test.ts +++ b/src/test/suite/integration/listEditing.test.ts @@ -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), [