Skip to content

Commit

Permalink
fix: check for folding when sorting inner blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Sep 2, 2024
1 parent ec3a77e commit 0281d86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/providers/BlockSortProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ExpandSelectionOptions } from "../types/BlockSortOptions";
import ConfigurationProvider from "./ConfigurationProvider";
import StringProcessingProvider, { Folding, LineMeta } from "./StringProcessingProvider";
import { StringSortProvider } from "./StringSortProvider";
import { join, sep } from "path";

interface ExpandDirectionOptions extends ExpandSelectionOptions {
direction: 1 | -1;
Expand Down Expand Up @@ -194,20 +195,24 @@ export default class BlockSortProvider implements Disposable {
if (!this.isComputed(block)) this.computeLineMeta([block], true, token);
if (!this.documentLineMeta) return []; //* TS hint, this never actually happens

const indent = this.stringProcessor.getIndentRange(
const indentRange = this.stringProcessor.getIndentRange(
this.documentLineMeta.slice(block.start.line, block.end.line),
this.document
);

let checkFolding: Folding | null = null;
let start = 0;
let end = -1;
for (let i = block.start.line; i <= block.end.line; i++) {
if (token?.isCancellationRequested) return [];

const lineMeta = this.documentLineMeta[i];
const { indent, folding } = this.documentLineMeta[i];

if (checkFolding) checkFolding = this.stringProcessor.mergeFolding(checkFolding, folding);

if (lineMeta.indent === indent.min) {
if (indent === indentRange.min || (checkFolding && !this.stringProcessor.hasFolding(checkFolding))) {
if (end >= start) break;
if (this.stringProcessor.hasFolding(folding)) checkFolding = folding;
start++;
}
end++;
Expand Down Expand Up @@ -485,7 +490,7 @@ export default class BlockSortProvider implements Disposable {
const tail: Range = new Range(blocks[blocks.length - 1]?.end || block.end, block.end);

if (token?.isCancellationRequested) return "";
if (head.isEmpty && tail.isEmpty) return this.document.getText(block);
if (blocks.length <= 1) return this.document.getText(block);

return (
this.document.getText(head) +
Expand Down Expand Up @@ -546,7 +551,7 @@ export default class BlockSortProvider implements Disposable {
return lines.join("\n");
}

private applySort(blocks: string[], sortProvider: StringSortProvider) {
private applySort(blocks: string[], sortProvider: StringSortProvider, ...affixes: string[][]) {
const compare = sortProvider.compare.bind(sortProvider);
const precomputed = blocks
.map((original, index) => ({
Expand All @@ -561,6 +566,9 @@ export default class BlockSortProvider implements Disposable {
a.forceFirst || b.forceLast ? -1 : a.forceLast || b.forceFirst ? 1 : compare(a.sanitized, b.sanitized)
);

precomputed.forEach(({ original }, index) => (blocks[index] = original));
precomputed.forEach(({ original, index: originalIndex }, index) => {
blocks[index] = original;
affixes.forEach((affix) => (affix[index] = affix[originalIndex]));
});
}
}
2 changes: 1 addition & 1 deletion src/providers/StringProcessingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class StringProcessingProvider {
}

public hasFolding(folding: Folding): boolean {
for (const key of Object.keys(folding)) if (folding[key].level) return true;
for (const { level } of Object.values(folding)) if (level) return true;

return false;
}
Expand Down

0 comments on commit 0281d86

Please sign in to comment.