Skip to content

Commit

Permalink
feat: add options for expanding the current selection
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Aug 29, 2022
1 parent c998d83 commit af60598
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
33 changes: 32 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,44 @@
},
"blocksort.keepAppendedNewlines": {
"type": "boolean",
"description": "Keep appended newlines when sorting blocks",
"description": "Keep appended newlines in place when sorting blocks",
"default": true
},
"blocksort.alwaysExpandOverSeparators": {
"type": "boolean",
"description": "Always expand selection over separators when sorting blocks",
"default": false
},
"blocksort.expandSelection": {
"oneOf": [
{
"type": "boolean"
},
{
"type": "object",
"minProperties": 1,
"properties": {
"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 selection when sorting blocks",
"default": {
"expandOverEmptyLines": false,
"foldingComplete": true,
"indentationComplete": true
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/blockSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { commands, InputBoxOptions, Selection, Range, TextEditor, TextEditorEdit
import BlockSortProvider from "../providers/BlockSortProvider";
import ConfigurationProvider from "../providers/ConfigurationProvider";
import BlockSortFormattingProvider from "../providers/BlockSortFormattingProvider";
import { BlockSortOptions } from "../providers/BlockSortFormattingProvider";
import { BlockSortOptions } from "../types/BlockSortOptions";

export function blockSort(
editor: TextEditor | undefined,
Expand All @@ -16,7 +16,7 @@ export function blockSort(
const {
sortFunction,
sortChildren = 0,
expandSelection = ConfigurationProvider.getAlwaysExpandOverSeparators() ? "full" : "local",
expandSelection = ConfigurationProvider.getExpandSelection(),
} = options;

const { document, selection } = editor;
Expand Down
16 changes: 3 additions & 13 deletions src/providers/BlockSortFormattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,10 @@ import {
DocumentRangeFormattingEditProvider,
} from "vscode";
import { commentMarkers } from "../constants/comments";
import { BlockSortOptions } from "../types/BlockSortOptions";
import BlockSortProvider from "./BlockSortProvider";
import ConfigurationProvider from "./ConfigurationProvider";

export type BlockSortOptions = {
sortFunction: (a: string, b: string) => number;
sortChildren?: number;
expandSelection?: boolean | "local" | "full";
/**
* Mutable array of previous edits.
* Edits that are merged into the current edit are removed from this array.
*/
edits?: TextEdit[];
};

export default class BlockSortFormattingProvider
implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider
{
Expand Down Expand Up @@ -62,7 +52,7 @@ export default class BlockSortFormattingProvider
return {
sortFunction,
sortChildren: depth.includes("inf") ? Infinity : parseInt(depth, 10),
expandSelection: "full",
expandSelection: true,
};
}

Expand Down Expand Up @@ -108,7 +98,7 @@ export default class BlockSortFormattingProvider

if (options.expandSelection === false) return initialRange;

return blockSort.trimRange(blockSort.expandRange(initialRange, options.expandSelection === "full", token));
return blockSort.trimRange(blockSort.expandRange(initialRange, options.expandSelection!, token));
}

public static mapFilterBlockSortHeaders<T>(
Expand Down
22 changes: 16 additions & 6 deletions src/providers/BlockSortProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import {
TextEdit,
workspace,
} from "vscode";
import { ExpandSelectionOptions } from "../types/BlockSortOptions";
import ConfigurationProvider from "./ConfigurationProvider";
import StringProcessingProvider, { Folding, LineMeta } from "./StringProcessingProvider";

type SortingStrategy = "asc" | "desc" | "ascNatural" | "descNatural";
interface ExpandDirectionOptions extends ExpandSelectionOptions {
direction: 1 | -1;
}

export default class BlockSortProvider implements Disposable {
public static sort: Record<SortingStrategy, (a: string, b: string) => number> = {
Expand Down Expand Up @@ -204,7 +208,7 @@ export default class BlockSortProvider implements Disposable {
private expandRangeInDirection(
range: Range,
{ folding, indent }: { folding: Folding; indent: number },
{ expandOverNewlines = false, direction }: { expandOverNewlines?: boolean; direction: number },
{ expandOverEmptyLines = false, direction }: ExpandDirectionOptions,
token?: CancellationToken
): [Range, Folding] {
const { lineCount } = this.document;
Expand All @@ -225,10 +229,10 @@ export default class BlockSortProvider implements Disposable {
while (
(direction > 0 ? line < lineCount - 1 : line > 0) &&
(stringProcessor.totalOpenFolding(folding) > 0 ||
(skippedLine && !expandOverNewlines
(skippedLine && !expandOverEmptyLines
? this.documentLineMeta[line + direction].indent > indent
: this.documentLineMeta[line + direction].indent >= indent) ||
(expandOverNewlines && !this.documentLineMeta[line + direction].hasContent) ||
(expandOverEmptyLines && !this.documentLineMeta[line + direction].hasContent) ||
(lastIndent > indent && !this.documentLineMeta[line + direction].hasContent))
) {
if (token?.isCancellationRequested) return [range, folding];
Expand Down Expand Up @@ -256,14 +260,20 @@ export default class BlockSortProvider implements Disposable {
return [range, folding];
}

public expandRange(selection: Range, expandOverNewlines = false, token?: CancellationToken): Range {
public expandRange(selection: Range, expand: boolean | ExpandSelectionOptions, token?: CancellationToken): Range {
const { stringProcessor } = this;
let range: Range = this.document.validateRange(new Range(selection.start.line, 0, selection.end.line, Infinity));
if (!this.isComputed(range)) this.computeLineMeta([range], true, token);
if (!this.documentLineMeta) return range; //* TS hint, this never actually happens

const up = { expandOverNewlines, direction: -1 };
const down = { expandOverNewlines, direction: 1 };
const up: ExpandDirectionOptions = {
expandOverEmptyLines: typeof expand === "boolean" ? expand : !!expand.expandOverEmptyLines,
direction: -1,
};
const down: ExpandDirectionOptions = {
expandOverEmptyLines: typeof expand === "boolean" ? expand : !!expand.expandOverEmptyLines,
direction: 1,
};
const { min: indent } = stringProcessor.getIndentRange(
this.documentLineMeta.slice(range.start.line, range.end.line + 1),
this.document
Expand Down
5 changes: 3 additions & 2 deletions src/providers/ConfigurationProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigurationScope, DocumentSelector, TextDocument, workspace, WorkspaceConfiguration } from "vscode";
import { ExpandSelectionOptions } from "../types/BlockSortOptions";
import { FoldingMarkerDefault, FoldingMarkerList } from "./StringProcessingProvider";

const defaultFoldingMarkers: FoldingMarkerList<FoldingMarkerDefault> = {
Expand Down Expand Up @@ -124,8 +125,8 @@ export default class ConfigurationProvider {
return ConfigurationProvider.getConfiguration().keepAppendedNewlines;
}

public static getAlwaysExpandOverSeparators(): boolean {
return ConfigurationProvider.getConfiguration().alwaysExpandOverSeparators;
public static getExpandSelection(): boolean | ExpandSelectionOptions {
return ConfigurationProvider.getConfiguration().expandSelection;
}

public static getEnableNaturalSorting(): boolean {
Expand Down
19 changes: 19 additions & 0 deletions src/types/BlockSortOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { TextEdit } from "vscode";

export interface ExpandSelectionOptions {
expandOverEmptyLines?: boolean;
foldingComplete?: boolean;
indentationComplete?: boolean;
}

export type BlockSortOptions = {
sortFunction: (a: string, b: string) => number;
sortChildren?: number;
// `false` for no expansion, `true` for all options enabled
expandSelection?: boolean | ExpandSelectionOptions;
/**
* Mutable array of previous edits.
* Edits that are merged into the current edit are removed from this array.
*/
edits?: TextEdit[];
};

0 comments on commit af60598

Please sign in to comment.