Skip to content

Commit

Permalink
fix: dont auto sort markers with invalid positions
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Dec 16, 2021
1 parent 20177a7 commit 7695c3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/providers/BlockSortActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default class BlockSortActionProvider
private getBlockMarkerRange(document: TextDocument, position: Position): Range | undefined {
const blockSortProvider = this.blockSortProviders.get(document.uri);
const blockPosition = FormattingProvider.getNextBlockPosition(document, position);
return blockSortProvider?.expandRange(new Selection(blockPosition, blockPosition));
return blockPosition ? blockSortProvider?.expandRange(new Selection(blockPosition, blockPosition)) : undefined;
}

private updateBlockSortMarkers(
Expand Down
16 changes: 10 additions & 6 deletions src/providers/FormattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ export default class FormattingProvider implements DocumentFormattingEditProvide
return { sortFunction, sortChildren: depth.includes("inf") ? Infinity : parseInt(depth, 10) };
}

public static getNextBlockPosition(document: TextDocument, position: Position): Position {
public static getNextBlockPosition(document: TextDocument, position: Position): Position | null {
let line = document.lineAt(position.line);
const initialIndent = line.firstNonWhitespaceCharacterIndex;

while (line.firstNonWhitespaceCharacterIndex <= initialIndent) {
if (line.lineNumber >= document.lineCount - 1) return null;
line = document.lineAt(line.lineNumber + 1);
}

Expand All @@ -79,17 +80,20 @@ export default class FormattingProvider implements DocumentFormattingEditProvide
return TextEdit.replace(range, sorted.join("\n"));
}

public static mapBlockSortHeaders<T>(
public static mapFilterBlockSortHeaders<T>(
document: TextDocument,
headers: (TextLine | Position)[],
callback: (position: Position, options: BlockSortOptions) => T
): T[] {
return headers.map((line) => {
const result = [];
for (const line of headers) {
const position = "range" in line ? line.range.start : line;
const nextBlockPosition = FormattingProvider.getNextBlockPosition(document, position);
const options = FormattingProvider.getBlockSortMarkerOptions(document, position);
return callback(nextBlockPosition, options);
});
if (nextBlockPosition) result.push(callback(nextBlockPosition, options));
}

return result;
}

public provideDocumentFormattingEdits(
Expand All @@ -111,7 +115,7 @@ export default class FormattingProvider implements DocumentFormattingEditProvide
}

public provideBlockMarkerFormattingEdits(document: TextDocument, ...positions: (Position | TextLine)[]): TextEdit[] {
return FormattingProvider.mapBlockSortHeaders(
return FormattingProvider.mapFilterBlockSortHeaders(
document,
positions,
FormattingProvider.getBlockSortEdit.bind(this, document)
Expand Down

0 comments on commit 7695c3d

Please sign in to comment.