Skip to content

Commit

Permalink
#237, #307 new command toggleList
Browse files Browse the repository at this point in the history
cycle through ``, `- `, `* `, `+ `, `1. `, `1) `
  • Loading branch information
yzhang-gh committed Jun 16, 2019
1 parent 4d190e6 commit 62deb65
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"title": "%command.editing.toggleMathReverse.title%"
},
{
"command": "markdown.extension.editing.toggleUnorderedList",
"title": "%command.editing.toggleUnorderedList.title%"
"command": "markdown.extension.editing.toggleList",
"title": "%command.editing.toggleList.title%"
}
],
"keybindings": [
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"command.editing.toggleCodeSpan.title": "Markdown: Toggle code span",
"command.editing.toggleMath.title": "Markdown: Toggle math environment",
"command.editing.toggleMathReverse.title": "Markdown: Toggle math environment (in reverse order)",
"command.editing.toggleUnorderedList.title": "Markdown: Toggle unordered list",
"command.editing.toggleList.title": "Markdown: Toggle list",
"config.title": "Markdown All in One",
"config.toc.levels.description": "Range of levels for table of contents. Use `x..y` for level x to y",
"config.toc.orderedList.description": "Use ordered list (1. ..., 2. ...)",
Expand Down
2 changes: 1 addition & 1 deletion package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"command.editing.toggleCodeSpan.title": "Markdown: 触发代码块",
"command.editing.toggleMath.title": "Markdown: 触发数学环境",
"command.editing.toggleMathReverse.title": "Markdown: 触发数学环境(反向)",
"command.editing.toggleUnorderedList.title": "Markdown: 触发无序列表",
"command.editing.toggleList.title": "Markdown: 触发列表",
"config.title": "Markdown All in One",
"config.toc.levels.description": "目录级别的范围. 例如 `2..5` 表示在目录中只包含 2 到 5 级标题",
"config.toc.orderedList.description": "使用有序列表 (1. ..., 2. ...)",
Expand Down
67 changes: 38 additions & 29 deletions src/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import { commands, env, ExtensionContext, Position, Range, Selection, TextEditor, window, workspace, WorkspaceEdit } from 'vscode';
import { commands, env, ExtensionContext, Position, Range, Selection, TextDocument, TextEditor, window, workspace, WorkspaceEdit } from 'vscode';
import { fixMarker } from './listEditing';

export function activate(context: ExtensionContext) {
context.subscriptions.push(
Expand All @@ -12,7 +13,7 @@ export function activate(context: ExtensionContext) {
commands.registerCommand('markdown.extension.editing.toggleMathReverse', () => toggleMath(reverseTransTable)),
commands.registerCommand('markdown.extension.editing.toggleHeadingUp', toggleHeadingUp),
commands.registerCommand('markdown.extension.editing.toggleHeadingDown', toggleHeadingDown),
commands.registerCommand('markdown.extension.editing.toggleUnorderedList', toggleUnorderedList),
commands.registerCommand('markdown.extension.editing.toggleList', toggleList),
commands.registerCommand('markdown.extension.editing.paste', paste)
);
}
Expand Down Expand Up @@ -190,33 +191,41 @@ function toggleMath(transTable) {
setMathState(editor, cursor, oldMathBlockState, transTable[(currentStateIndex + 1) % transTable.length])
}

function toggleUnorderedList() {
let editor = window.activeTextEditor;
if (!editor.selection.isEmpty) return;
let cursor = editor.selection.active;
let textBeforeCursor = editor.document.lineAt(cursor.line).text.substr(0, cursor.character);

let indentation = 0;
switch (textBeforeCursor.trim()) {
case '':
return editor.edit(editBuilder => {
editBuilder.insert(cursor, '- ');
});
case '-':
indentation = textBeforeCursor.indexOf('-');
return editor.edit(editBuilder => {
editBuilder.replace(new Range(cursor.line, indentation, cursor.line, cursor.character), '*' + ' '.repeat(textBeforeCursor.length - indentation - 1));
});
case '*':
indentation = textBeforeCursor.indexOf('*');
return editor.edit(editBuilder => {
editBuilder.replace(new Range(cursor.line, indentation, cursor.line, cursor.character), '+' + ' '.repeat(textBeforeCursor.length - indentation - 1));
});
case '+':
indentation = textBeforeCursor.indexOf('+');
return editor.edit(editBuilder => {
editBuilder.delete(new Range(cursor.line, indentation, cursor.line, cursor.character));
});
function toggleList() {
const editor = window.activeTextEditor;
const doc = editor.document;
let batchEdit = new WorkspaceEdit();

editor.selections.forEach(selection => {
if (selection.isEmpty) {
toggleListSingleLine(doc, selection.active.line, batchEdit);
} else {
for (let i = selection.start.line; i <= selection.end.line; i++) {
toggleListSingleLine(doc, i, batchEdit);
}
}
});

return workspace.applyEdit(batchEdit).then(() => fixMarker());
}

function toggleListSingleLine(doc: TextDocument, line: number, wsEdit: WorkspaceEdit) {
const lineText = doc.lineAt(line).text;
const indentation = lineText.trim().length === 0 ? lineText.length : lineText.indexOf(lineText.trim());
const lineTextContent = lineText.substr(indentation);

if (lineTextContent.startsWith("- ")) {
wsEdit.replace(doc.uri, new Range(line, indentation, line, indentation + 2), "* ");
} else if (lineTextContent.startsWith("* ")) {
wsEdit.replace(doc.uri, new Range(line, indentation, line, indentation + 2), "+ ");
} else if (lineTextContent.startsWith("+ ")) {
wsEdit.replace(doc.uri, new Range(line, indentation, line, indentation + 2), "1. ");
} else if (/^\d\. /.test(lineTextContent)) {
wsEdit.replace(doc.uri, new Range(line, indentation + 1, line, indentation + 2), ")");
} else if (/^\d\) /.test(lineTextContent)) {
wsEdit.delete(doc.uri, new Range(line, indentation, line, indentation + 3));
} else {
wsEdit.insert(doc.uri, new Position(line, indentation), "- ");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/listEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function lookUpwardForMarker(editor: TextEditor, line: number, currentIndentatio
/**
* Fix ordered list marker *iteratively* starting from current line
*/
function fixMarker(line?: number) {
export function fixMarker(line?: number) {
if (!workspace.getConfiguration('markdown.extension.orderedList').get<boolean>('autoRenumber')) return;
if (workspace.getConfiguration('markdown.extension.orderedList').get<string>('marker') == 'one') return;

Expand Down

0 comments on commit 62deb65

Please sign in to comment.