Skip to content

Commit

Permalink
feat: add quick sort picker command
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Feb 19, 2025
1 parent e5b1ee3 commit 523a57d
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 77 deletions.
146 changes: 111 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"onCommand:blocksort.sortInnerBlocksDesc",
"onCommand:blocksort.sortInnerBlocksShuffle",
"onCommand:blocksort.expandSelectionLocally",
"onCommand:blocksort.expandSelectionFull"
"onCommand:blocksort.expandSelectionFull",
"onCommand:blocksort.quickSort"
],
"main": "./dist/extension.js",
"contributes": {
Expand Down Expand Up @@ -140,6 +141,11 @@
"dark": "resources/icon_expand_dark.svg",
"light": "resources/icon_expand.svg"
}
},
{
"command": "blocksort.quickSort",
"title": "Quick Sort",
"category": "Sort"
}
],
"configuration": [
Expand Down Expand Up @@ -572,42 +578,112 @@
"foldingComplete": true,
"indentationComplete": true
}
}
},
"blocksort.expandCursor": {
"oneOf": [
{
"type": "boolean"
},
{
"type": "object",
"minProperties": 1,
"properties": {
"expandLocally": {
"type": "boolean",
"description": "Expand selection locally while in the same blocks (stopping at empty lines)"
},
"expandOverEmptyLines": {
"type": "boolean",
"description": "Expand selection over empty lines"
},
"foldingComplete": {
"type": "boolean",
"description": "Expand selection, so that all folding markers are closed"
},
"indentationComplete": {
"type": "boolean",
"description": "Expand selection, so that all indentation is complete"
},
"blocksort.expandCursor": {
"oneOf": [
{
"type": "boolean"
},
{
"type": "object",
"minProperties": 1,
"properties": {
"expandLocally": {
"type": "boolean",
"description": "Expand selection locally while in the same blocks (stopping at empty lines)"
},
"expandOverEmptyLines": {
"type": "boolean",
"description": "Expand selection over empty lines"
},
"foldingComplete": {
"type": "boolean",
"description": "Expand selection, so that all folding markers are closed"
},
"indentationComplete": {
"type": "boolean",
"description": "Expand selection, so that all indentation is complete"
}
}
}
],
"description": "Expand cursor to selection when sorting blocks",
"default": {
"expandLocally": true,
"expandOverEmptyLines": false,
"foldingComplete": true,
"indentationComplete": true
}
],
"description": "Expand cursor to selection when sorting blocks",
"default": {
"expandLocally": true,
"expandOverEmptyLines": false,
"foldingComplete": true,
"indentationComplete": true
},
"blocksort.quickSortCommands": {
"type": "object",
"default": [
{
"command": "blocksort.sortBlocksAsc",
"label": "$(sort-asc) Sort Blocks Ascending",
"description": "Expand Selection and sort blocks in ascending order",
"picked": true
},
{
"command": "blocksort.sortBlocksDesc",
"label": "$(sort-desc) Sort Blocks Descending",
"description": "Expand Selection and sort blocks in descending order"
},
{
"command": "blocksort.sortBlocksShuffle",
"label": "$(arrow-switch) Shuffle Blocks",
"description": "Expand Selection and shuffle blocks"
},
{
"command": "editor.action.sortLinesAscending",
"label": "$(sort-asc) Sort Lines Ascending",
"description": "Sort selected lines in ascending order"
},
{
"command": "editor.action.sortLinesDescending",
"label": "$(sort-desc) Sort Lines Descending",
"description": "Sort selected lines in descending order"
},
{
"command": "blocksort.expandSelectionLocally",
"label": "$(unfold) Expand Selection",
"description": "Expand current Selection to surrounding Block"
}
],
"properties": {
"command": {
"type": "string",
"description": "The command to execute"
},
"args": {
"type": "array",
"description": "The arguments to pass to the command"
},
"label": {
"type": "string",
"description": "A human-readable string which is rendered prominent"
},
"description": {
"type": "string",
"description": "A human-readable string which is rendered less prominent in the same line"
},
"detail": {
"type": "string",
"description": "A human-readable string which is rendered less prominent in a separate line"
},
"picked": {
"type": "boolean",
"description": "Optional flag indicating if this item is picked initially"
},
"alwaysShow": {
"type": "boolean",
"description": "Always show this item"
}
},
"required": [
"command",
"label"
]
}
}
}
Expand Down Expand Up @@ -715,4 +791,4 @@
]
]
}
}
}
4 changes: 2 additions & 2 deletions src/commands/blockSort.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { commands, InputBoxOptions, Selection, Range, TextEditor, TextEditorEdit, window } from "vscode";
import ConfigurationProvider, { BlockSortCollatorOptions } from "../providers/ConfigurationProvider";
import { Selection, Range, TextEditor, TextEditorEdit, window } from "vscode";
import ConfigurationProvider from "../providers/ConfigurationProvider";
import BlockSortFormattingProvider from "../providers/BlockSortFormattingProvider";
import { BlockSortOptions } from "../types/BlockSortOptions";
import { showNumberQuickPick } from "../helpers/showNumberQuickPick";
Expand Down
9 changes: 9 additions & 0 deletions src/commands/quickSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { commands, window } from "vscode";
import ConfigurationProvider from "../providers/ConfigurationProvider";

export async function quickSort() {
const options = ConfigurationProvider.getQuickSortCommands();
const { command, args = [] } = (await window.showQuickPick(options)) ?? {};

if (command) commands.executeCommand(command, ...args);
}
2 changes: 2 additions & 0 deletions src/contribute/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../commands/blockSort";
import { expandSelectionFull, expandSelectionLocally } from "../commands/expandSelection";
import BlockSortFormattingProvider from "../providers/BlockSortFormattingProvider";
import { quickSort } from "../commands/quickSort";

export default function contributeCommands(b: BlockSortFormattingProvider) {
return [
Expand All @@ -26,5 +27,6 @@ export default function contributeCommands(b: BlockSortFormattingProvider) {
commands.registerTextEditorCommand("blocksort.sortInnerBlocksShuffle", blockSortShuffle.bind(null, b)),
commands.registerTextEditorCommand("blocksort.expandSelectionLocally", expandSelectionLocally.bind(null, b)),
commands.registerTextEditorCommand("blocksort.expandSelectionFull", expandSelectionFull.bind(null, b)),
commands.registerTextEditorCommand("blocksort.quickSort", quickSort),
];
}
48 changes: 11 additions & 37 deletions src/providers/ConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {
} from "vscode";
import { ExpandSelectionOptions } from "../types/BlockSortOptions";
import { FoldingMarkerDefault, FoldingMarkerList } from "./StringProcessingProvider";
import {
BlockSortCollatorOptions,
BlockSortConfiguration,
NaturalSortOptions,
SortCommandOptions,
} from "../types/BlockSortConfiguration";

const defaultFoldingMarkers: FoldingMarkerList<FoldingMarkerDefault> = {
"()": { start: "\\(", end: "\\)" },
Expand All @@ -28,43 +34,6 @@ const defaultIndentIgnoreMarkers = [
"esac|fi",
];

/** @deprecated Will no longer be applied. Use collatorOptions instead */
export interface NaturalSortOptions {
padding: number;
omitUuids: boolean;
sortNegativeValues: boolean;
}

export interface BlockSortCollatorOptions extends Omit<Intl.CollatorOptions, "usage"> {
locales?: string;
customSortOrder?: string;
customIgnoreCharacters?: string;
}

export interface BlockSortConfiguration {
defaultMultilevelDepth: number;
defaultSkipParents: number;
askForMultilevelDepth: boolean;
askForSkipParents: boolean;
indentIgnoreMarkers: string[];
completeBlockMarkers: string[];
foldingMarkers: FoldingMarkerList;
/** @deprecated Use collatorOptions.numeric instead */
enableNaturalSorting: boolean;
/** @deprecated Will no longer be applied. Use collatorOptions instead */
naturalSorting: NaturalSortOptions;
collatorOptions: BlockSortCollatorOptions;
sortConsecutiveBlockHeaders: boolean;
enableCodeLens: DocumentSelector | boolean;
enableCodeActions: DocumentSelector | boolean;
enableDocumentFormatting: DocumentSelector | boolean;
enableRangeFormatting: DocumentSelector | boolean;
forceBlockHeaderFirstRegex: string;
forceBlockHeaderLastRegex: string;
multiBlockHeaderRegex: string;
incompleteBlockRegex: string;
}

export default class ConfigurationProvider {
public static readonly invalidatingConfigurationKeys: string[] = [
"enableNaturalSorting",
Expand Down Expand Up @@ -209,6 +178,11 @@ export default class ConfigurationProvider {
return typeof tabSize === "string" ? 4 : tabSize ?? 4;
}

public static getQuickSortCommands(): SortCommandOptions[] {
const quickSortCommands: SortCommandOptions[] = ConfigurationProvider.getConfiguration().quickSortCommands || [];
return quickSortCommands;
}

public static onConfigurationChanged(): void {
ConfigurationProvider.configuration.clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/providers/StringSortProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BlockSortCollatorOptions } from "./ConfigurationProvider";
import { BlockSortCollatorOptions } from "../types/BlockSortConfiguration";

export class StringSortProvider extends Intl.Collator {
public readonly customSortOrder?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CodeActionKind, Position, Range } from "vscode";
import { ExpandSelectionOptions } from "../../types/BlockSortOptions";
import { BlockSortCollatorOptions } from "../../providers/ConfigurationProvider";
import { BlockSortCollatorOptions } from "../../types/BlockSortConfiguration";

export interface BaseTest {
file: string;
Expand Down
45 changes: 45 additions & 0 deletions src/types/BlockSortConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { DocumentSelector, QuickPickItem } from "vscode";
import { FoldingMarkerList } from "../providers/StringProcessingProvider";

/** @deprecated Will no longer be applied. Use collatorOptions instead */
export interface NaturalSortOptions {
padding: number;
omitUuids: boolean;
sortNegativeValues: boolean;
}

export interface BlockSortCollatorOptions extends Omit<Intl.CollatorOptions, "usage"> {
locales?: string;
customSortOrder?: string;
customIgnoreCharacters?: string;
}

export interface SortCommandOptions extends QuickPickItem {
command: string;
args: any[];
}

export interface BlockSortConfiguration {
defaultMultilevelDepth: number;
defaultSkipParents: number;
askForMultilevelDepth: boolean;
askForSkipParents: boolean;
indentIgnoreMarkers: string[];
completeBlockMarkers: string[];
foldingMarkers: FoldingMarkerList;
/** @deprecated Use collatorOptions.numeric instead */
enableNaturalSorting: boolean;
/** @deprecated Will no longer be applied. Use collatorOptions instead */
naturalSorting: NaturalSortOptions;
collatorOptions: BlockSortCollatorOptions;
sortConsecutiveBlockHeaders: boolean;
enableCodeLens: DocumentSelector | boolean;
enableCodeActions: DocumentSelector | boolean;
enableDocumentFormatting: DocumentSelector | boolean;
enableRangeFormatting: DocumentSelector | boolean;
forceBlockHeaderFirstRegex: string;
forceBlockHeaderLastRegex: string;
multiBlockHeaderRegex: string;
incompleteBlockRegex: string;
quickSortCommands: SortCommandOptions[];
}
2 changes: 1 addition & 1 deletion src/types/BlockSortOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TextEdit } from "vscode";
import { BlockSortCollatorOptions } from "../providers/ConfigurationProvider";
import { BlockSortCollatorOptions } from "./BlockSortConfiguration";

export interface ExpandSelectionOptions {
expandLocally?: boolean;
Expand Down

0 comments on commit 523a57d

Please sign in to comment.