Sort by Length

This commit is contained in:
Mike Phares 2020-07-11 20:53:22 -07:00
parent ff449a9c5f
commit 91cb50b187
4 changed files with 28 additions and 12 deletions

Binary file not shown.

View File

@ -4,7 +4,7 @@
"description": "Helper for VS Code in TypeScript",
"publisher": "IFX",
"repository": "https://github.com/mikepharesjr/YO-VSCode/tree/master/type-script-helper",
"version": "1.2.1",
"version": "1.2.2",
"engines": {
"vscode": "^1.40.0"
},
@ -24,7 +24,8 @@
"onCommand:helper.sortNormal",
"onCommand:helper.unwrapSql",
"onCommand:helper.wrapSqlCSharp",
"onCommand:helper.wrapSqlVB"
"onCommand:helper.wrapSqlVB",
"onCommand:helper.sortLength"
],
"contributes": {
"commands": [
@ -39,7 +40,8 @@
{ "command": "helper.sortNormal", "title": "My Sort lines (ascending, case sensitive)" },
{ "command": "helper.unwrapSql", "title": "Un-wrap Sql" },
{ "command": "helper.wrapSqlCSharp", "title": "Wrap Sql for C#" },
{ "command": "helper.wrapSqlVB", "title": "Wrap Sql for VB" }
{ "command": "helper.wrapSqlVB", "title": "Wrap Sql for VB" },
{ "command": "helper.sortLength", "title": "Sort by Length" }
],
"keybindings": [
{

View File

@ -36,7 +36,8 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('helper.sortNormal', helper.sortNormal),
vscode.commands.registerCommand('helper.unwrapSql', helper.unwrapSql),
vscode.commands.registerCommand('helper.wrapSqlCSharp', helper.wrapSqlCSharp),
vscode.commands.registerCommand('helper.wrapSqlVB', helper.wrapSqlVB)
vscode.commands.registerCommand('helper.wrapSqlVB', helper.wrapSqlVB),
vscode.commands.registerCommand('helper.sortLength', helper.sortLength)
];
commands.forEach(command => context.subscriptions.push(command));

View File

@ -16,7 +16,8 @@ enum LinesAction {
sortNormal,
unwrapSql,
wrapSqlCSharp,
wrapSqlVB
wrapSqlVB,
sortLength
}
function removeBlanks(lines: string[]): void {
@ -140,12 +141,6 @@ function prettySqlLogic(lines: string[]): void {
removeBlanks(lines);
}
function makeSorter(algorithm?: SortingAlgorithm): ArrayTransformer {
return function (lines: string[]): string[] {
return lines.sort(algorithm);
};
}
function removeCommentLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
lines[i] = lines[i].trim();
@ -158,6 +153,12 @@ function removeCommentLogic(lines: string[]): void {
}
}
function makeSorter(algorithm?: SortingAlgorithm): ArrayTransformer {
return function (lines: string[]): string[] {
return lines.sort(algorithm);
};
}
function sortNormalLogic(lines: string[]): void {
var transformers: ArrayTransformer[] = [];
transformers.push(makeSorter());
@ -212,6 +213,16 @@ function wrapSqlVBLogic(lines: string[]): void {
}
}
function sortLengthLogic(lines: string[]): void {
lines.sort(function(a, b) {
// ASC -> a.length - b.length
// DESC -> b.length - a.length
return a.length - b.length || // sort by length, if equal then
a.localeCompare(b); // sort by dictionary order
});
removeBlanks(lines);
}
function returnLines(textEditor: vscode.TextEditor, startLine: number, endLine: number, lines: string[]) {
return textEditor.edit(editBuilder => {
const range = new vscode.Range(startLine, 0, endLine, textEditor.document.lineAt(endLine).text.length);
@ -245,6 +256,7 @@ function linesFunction(linesAction: LinesAction): Thenable<boolean> | undefined
case LinesAction.unwrapSql: { unwrapSqlLogic(lines); break; }
case LinesAction.wrapSqlCSharp: { wrapSqlCSharpLogic(lines); break; }
case LinesAction.wrapSqlVB: { wrapSqlVBLogic(lines); break; }
case LinesAction.sortLength: { sortLengthLogic(lines); break; }
default: { throw new Error(); }
}
return returnLines(textEditor, startLine, endLine, lines);
@ -261,4 +273,5 @@ export const removeComment = () => linesFunction(LinesAction.removeComment);
export const sortNormal = () => linesFunction(LinesAction.sortNormal);
export const unwrapSql = () => linesFunction(LinesAction.unwrapSql);
export const wrapSqlCSharp = () => linesFunction(LinesAction.wrapSqlCSharp);
export const wrapSqlVB = () => linesFunction(LinesAction.wrapSqlVB);
export const wrapSqlVB = () => linesFunction(LinesAction.wrapSqlVB);
export const sortLength = () => linesFunction(LinesAction.sortLength);