Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add "toggle block quote" #811

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. ...)",
Expand Down
1 change: 1 addition & 0 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. ...)",
Expand Down
67 changes: 66 additions & 1 deletion src/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { commands, env, ExtensionContext, Position, Range, Selection, SnippetString, TextDocument, TextEditor, window, workspace, WorkspaceEdit } 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) {
Expand All @@ -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']))
);
Expand Down Expand Up @@ -45,6 +46,70 @@ 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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is more natural to quote all the lines (not only the unquoted lines) in this case (thinking about doing multi-line comment).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In consideration of the selected text not always accurate, especially like i use three finder drag, i may select more lines which have been quoted. In this situation, what i need is just to quote the unquoted lines. There is no need to quote which have been quoted which may cause the text format disturbed. So could you share the example which is preferred to quote all selected lines?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In consideration of the selected text not always accurate, especially like i use three finder drag, i may select more lines which have been quoted. In this situation, what i need is just to quote the unquoted lines.

I agree this is probably a good design (for some people, or maybe the majority). But as a first step, I would like to make it consistent with other similar commands, e.g.

multi-line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a switch to decide wthether need to only quote unquoted lines so that default quote behavior is the same as others?

Copy link

@huyz huyz Mar 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you implement a separate unquoting command, @Sean10 can't the user just solve your case by hitting the unquoting command then hitting the quoting command? Same result.

I agree with @yzhang-gh there are many more instances when it's preferable to nest blockquotes.

// State 2: all of lines has been quoted, need to be unquoted
FULLQUOTED,
}

function getQuoteState(lineArray: Array<string>): QuoteState {
return lineArray.every(line => line.startsWith('>')) ? QuoteState.FULLQUOTED : QuoteState.PARTIALQUOTED;
}

function quoteLine(line: string): string {
if(!line.startsWith(">")){
return "> " + line;
} else {
return line;
}
}

function unquoteLine(line: string): string {
if (!line.startsWith("> ") && line.startsWith(">")) {
return line.slice(2);
}
else if (line.startsWith(">")) {
return line.slice(1);
} else {
return line;
}
}

function setQuote(lineArray: Array<string>, state: QuoteState, eol: string): 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(eol);
}

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(eol);
let state = getQuoteState(line_array);
console.log("state", state);
let resultText = setQuote(line_array, state, eol);

return editor.edit(editBuilder => {
editBuilder.replace(new Range(start, end), resultText);
});
}

function toggleStrikethrough() {
return styleByWrapping('~~');
}
Expand Down