From c3cf132faacac0ed592d1f900e23e37285ca8eb7 Mon Sep 17 00:00:00 2001 From: sean10 Date: Sat, 12 Sep 2020 23:13:45 +0800 Subject: [PATCH 1/5] add quote/unquote line/multiline support --- package.json | 11 +++++++ package.nls.zh-cn.json | 1 + src/formatting.ts | 69 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/package.json b/package.json index c35845d5..0c81e35b 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,11 @@ "title": "%command.editing.toggleCodeSpan.title%", "category": "Markdown All in One" }, + { + "command": "markdown.extension.editing.toggleQuote", + "title": "%command.editing.toggleQuote.title%", + "category": "Markdown All in One" + }, { "command": "markdown.extension.editing.toggleMath", "title": "%command.editing.toggleMath.title%", @@ -93,6 +98,12 @@ "mac": "cmd+b", "when": "editorTextFocus && !editorReadonly && editorLangId == markdown" }, + { + "command": "markdown.extension.editing.toggleQuote", + "key": "ctrl+.", + "mac": "cmd+.", + "when": "editorTextFocus && !editorReadonly && editorLangId == markdown" + }, { "command": "markdown.extension.editing.toggleItalic", "key": "ctrl+i", diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index 14d0a03b..172af41d 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -10,6 +10,7 @@ "command.editing.toggleMath.title": "触发数学环境", "command.editing.toggleMathReverse.title": "触发数学环境(反向)", "command.editing.toggleList.title": "触发列表", + "command.editing.toggleQuote.title": "触发引用", "config.title": "Markdown All in One", "config.toc.levels.description": "目录级别的范围. 例如 `2..5` 表示在目录中只包含 2 到 5 级标题", "config.toc.orderedList.description": "使用有序列表 (1. ..., 2. ...)", diff --git a/src/formatting.ts b/src/formatting.ts index fe76282a..f83fd8e2 100644 --- a/src/formatting.ts +++ b/src/formatting.ts @@ -15,6 +15,7 @@ export function activate(context: ExtensionContext) { commands.registerCommand('markdown.extension.editing.toggleHeadingDown', toggleHeadingDown), commands.registerCommand('markdown.extension.editing.toggleList', toggleList), commands.registerCommand('markdown.extension.editing.toggleCodeBlock', toggleCodeBlock), + commands.registerCommand('markdown.extension.editing.toggleQuote', toggleQuote), commands.registerCommand('markdown.extension.editing.paste', paste), commands.registerCommand('markdown.extension.editing._wrapBy', args => styleByWrapping(args['before'], args['after'])) ); @@ -45,6 +46,74 @@ function toggleCodeBlock() { return editor.insertSnippet(new SnippetString('```$0\n$TM_SELECTED_TEXT\n```')); } +enum QuoteState { + // State 1: part of lines has been quoted, need to be quoted + PARTIALQUOTED, + // State 2: all of lines has been quoted, need to be unquoted + FULLQUOTED, +} + +function getQuoteState(lineArray: Array): QuoteState { + let cntQuoted = 0; + console.log("array:", lineArray) + lineArray.forEach(function(line) { + if (line.startsWith(">")) { + cntQuoted += 1; + } + }) + + return cntQuoted != lineArray.length ? QuoteState.PARTIALQUOTED : QuoteState.FULLQUOTED; +} + +function quoteLine(line: string): string { + if(!line.startsWith("> ") && line.startsWith(">")) { + return line.replace(/>/g, "> "); + } else if(!line.startsWith("> ")){ + return "> " + line; + } else { + return line; + } +} + +function unquoteLine(line: string): string { + if (line.startsWith("> ")) { + return line.slice(2); + } else { + return line; + } +} + +function setQuote(lineArray: Array, state: QuoteState): string { + let resultArray; + switch(state) { + case QuoteState.PARTIALQUOTED: + resultArray = lineArray.map(quoteLine); + break; + case QuoteState.FULLQUOTED: + resultArray = lineArray.map(unquoteLine); + break; + default: + resultArray = lineArray; + } + return resultArray.join('\n'); +} + +async function toggleQuote() { + let editor = window.activeTextEditor; + let start = editor.selection.start; + let end = editor.selection.end; + start = editor.document.lineAt(start.line).range.start; + let linesText = editor.document.getText(new Range(start, end));; + + let line_array = linesText.split('\n'); + let state = getQuoteState(line_array); + let resultText = setQuote(line_array, state); + + return await editor.edit(editBuilder => { + editBuilder.replace(new Range(start, end), resultText); + }); +} + function toggleStrikethrough() { return styleByWrapping('~~'); } From 886d813ae014cbb11d4c5f70377bd34138028bf2 Mon Sep 17 00:00:00 2001 From: sean10 Date: Sun, 13 Sep 2020 01:29:08 +0800 Subject: [PATCH 2/5] delete default quote keybinding, use better every() replace redundant code, use eol to replace \n to fix different eol problem, fixredundant quote syntax --- package.json | 4 ++-- package.nls.json | 1 + src/formatting.ts | 37 +++++++++++++++---------------------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 0c81e35b..1391dfbb 100644 --- a/package.json +++ b/package.json @@ -100,8 +100,8 @@ }, { "command": "markdown.extension.editing.toggleQuote", - "key": "ctrl+.", - "mac": "cmd+.", + "key": "", + "mac": "", "when": "editorTextFocus && !editorReadonly && editorLangId == markdown" }, { diff --git a/package.nls.json b/package.nls.json index 4c05323e..3d6b5262 100644 --- a/package.nls.json +++ b/package.nls.json @@ -12,6 +12,7 @@ "command.editing.toggleMathReverse.title": "Toggle math environment (in reverse order)", "command.editing.toggleList.title": "Toggle list", "command.editing.toggleCodeBlock.title": "Toggle code block", + "command.editing.toggleQuote.title": "Toggle quote", "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. ...)", diff --git a/src/formatting.ts b/src/formatting.ts index f83fd8e2..7ccfc50b 100644 --- a/src/formatting.ts +++ b/src/formatting.ts @@ -1,6 +1,6 @@ 'use strict'; -import { commands, env, ExtensionContext, Position, Range, Selection, SnippetString, TextDocument, TextEditor, window, workspace, WorkspaceEdit } from 'vscode'; +import { commands, env, ExtensionContext, Position, Range, Selection, SnippetString, TextDocument, TextEditor, window, workspace, WorkspaceEdit,EndOfLine } from 'vscode'; import { fixMarker } from './listEditing'; export function activate(context: ExtensionContext) { @@ -54,36 +54,26 @@ enum QuoteState { } function getQuoteState(lineArray: Array): QuoteState { - let cntQuoted = 0; - console.log("array:", lineArray) - lineArray.forEach(function(line) { - if (line.startsWith(">")) { - cntQuoted += 1; - } - }) - - return cntQuoted != lineArray.length ? QuoteState.PARTIALQUOTED : QuoteState.FULLQUOTED; + return lineArray.every((line)=>{ return line.startsWith(">"); }) ? QuoteState.FULLQUOTED : QuoteState.PARTIALQUOTED; } function quoteLine(line: string): string { - if(!line.startsWith("> ") && line.startsWith(">")) { - return line.replace(/>/g, "> "); - } else if(!line.startsWith("> ")){ - return "> " + line; + if(!line.startsWith(">")){ + return ">" + line; } else { return line; } } function unquoteLine(line: string): string { - if (line.startsWith("> ")) { - return line.slice(2); + if (line.startsWith(">")) { + return line.slice(1); } else { return line; } } -function setQuote(lineArray: Array, state: QuoteState): string { +function setQuote(lineArray: Array, state: QuoteState, eol: string): string { let resultArray; switch(state) { case QuoteState.PARTIALQUOTED: @@ -95,21 +85,24 @@ function setQuote(lineArray: Array, state: QuoteState): string { default: resultArray = lineArray; } - return resultArray.join('\n'); + return resultArray.join(eol); } -async function toggleQuote() { +function toggleQuote() { let editor = window.activeTextEditor; let start = editor.selection.start; let end = editor.selection.end; start = editor.document.lineAt(start.line).range.start; + end = editor.document.lineAt(end.line).range.end; let linesText = editor.document.getText(new Range(start, end));; + let eol = editor.document.eol === EndOfLine.CRLF ? '\r\n' : '\n'; - let line_array = linesText.split('\n'); + let line_array = linesText.split(eol); let state = getQuoteState(line_array); - let resultText = setQuote(line_array, state); + console.log("state", state); + let resultText = setQuote(line_array, state, eol); - return await editor.edit(editBuilder => { + return editor.edit(editBuilder => { editBuilder.replace(new Range(start, end), resultText); }); } From df65a66beb6c3ce47d4239ca2885a66af929a14d Mon Sep 17 00:00:00 2001 From: sean10 Date: Sun, 13 Sep 2020 11:09:44 +0800 Subject: [PATCH 3/5] back to append space after '>' --- src/formatting.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/formatting.ts b/src/formatting.ts index 7ccfc50b..afb46fab 100644 --- a/src/formatting.ts +++ b/src/formatting.ts @@ -59,14 +59,17 @@ function getQuoteState(lineArray: Array): QuoteState { function quoteLine(line: string): string { if(!line.startsWith(">")){ - return ">" + line; + return "> " + line; } else { return line; } } function unquoteLine(line: string): string { - if (line.startsWith(">")) { + if (!line.startsWith("> ") && line.startsWith(">")) { + return line.slice(2); + } + else if (line.startsWith(">")) { return line.slice(1); } else { return line; From 4344ab6a4141030a03cec42eac6d00670a595c53 Mon Sep 17 00:00:00 2001 From: Yu Zhang <7588612+yzhang-gh@users.noreply.github.com> Date: Sun, 13 Sep 2020 01:45:21 +0800 Subject: [PATCH 4/5] remove default key binding --- package.json | 6 ------ 1 file changed, 6 deletions(-) diff --git a/package.json b/package.json index 1391dfbb..d3f18943 100644 --- a/package.json +++ b/package.json @@ -98,12 +98,6 @@ "mac": "cmd+b", "when": "editorTextFocus && !editorReadonly && editorLangId == markdown" }, - { - "command": "markdown.extension.editing.toggleQuote", - "key": "", - "mac": "", - "when": "editorTextFocus && !editorReadonly && editorLangId == markdown" - }, { "command": "markdown.extension.editing.toggleItalic", "key": "ctrl+i", From abf326a9ef3f3158ddf4dc5c3b4ce1f63adf001c Mon Sep 17 00:00:00 2001 From: Yu Zhang <7588612+yzhang-gh@users.noreply.github.com> Date: Sun, 13 Sep 2020 01:48:22 +0800 Subject: [PATCH 5/5] tweaks --- src/formatting.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/formatting.ts b/src/formatting.ts index afb46fab..bffcc7ad 100644 --- a/src/formatting.ts +++ b/src/formatting.ts @@ -1,6 +1,6 @@ 'use strict'; -import { commands, env, ExtensionContext, Position, Range, Selection, SnippetString, TextDocument, TextEditor, window, workspace, WorkspaceEdit,EndOfLine } from 'vscode'; +import { commands, env, EndOfLine, ExtensionContext, Position, Range, Selection, SnippetString, TextDocument, TextEditor, window, workspace, WorkspaceEdit } from 'vscode'; import { fixMarker } from './listEditing'; export function activate(context: ExtensionContext) { @@ -54,7 +54,7 @@ enum QuoteState { } function getQuoteState(lineArray: Array): QuoteState { - return lineArray.every((line)=>{ return line.startsWith(">"); }) ? QuoteState.FULLQUOTED : QuoteState.PARTIALQUOTED; + return lineArray.every(line => line.startsWith('>')) ? QuoteState.FULLQUOTED : QuoteState.PARTIALQUOTED; } function quoteLine(line: string): string {