Skip to content

Commit eab12e3

Browse files
committed
Modify the new "Sort predicates/non-terminals" refactoring operation to reformat after sorting in the case of multi-line directives
1 parent 28fde48 commit eab12e3

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ To add a declaration for a local predicate (or non-terminal), right-click on the
384384

385385
The "Split in individual directives" refactoring operation is available when the user right-clicks on the directive name in a predicate directive with a list argument. The directive is split into individual directives, one for each element in the list.
386386

387-
The "Sort predicates/non-terminals" refactoring operation is available when the user right-clicks on the directive name in a predicate directive with a list argument. The list is sorted alphabetically while trying to preserve the formatting (single-line or multi-line). For directives defining aliases (using the `as` operator), the sorting is based on the original predicate/non-terminal name (the left operand of the `as` operator).
387+
The "Sort predicates/non-terminals" refactoring operation is available when the user right-clicks on the directive name in a predicate directive with a list argument. The list is sorted alphabetically. Single line directives are kept as single line. Multi-line directives are formatted after sorting using the same code as the "Format Selection" command. For directives defining aliases (using the `as` operator), the sorting is based on the original predicate/non-terminal name (the left operand of the `as` operator).
388388

389389
#### Converting between object, protocol, and category entity types
390390

src/features/refactorProvider.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { LogtalkDeclarationProvider } from "./declarationProvider";
2828
import { LogtalkDefinitionProvider } from "./definitionProvider";
2929
import { LogtalkImplementationProvider } from "./implementationProvider";
3030
import { LogtalkReferenceProvider } from "./referenceProvider";
31+
import { LogtalkDocumentRangeFormattingEditProvider } from "./documentRangeFormattingEditProvider";
3132
import { SymbolUtils, PatternSets, SymbolRegexes } from "../utils/symbols";
3233
import LogtalkTerminal from "./terminal";
3334
import * as path from "path";
@@ -45,6 +46,7 @@ export class LogtalkRefactorProvider implements CodeActionProvider {
4546
private definitionProvider = new LogtalkDefinitionProvider();
4647
private implementationProvider = new LogtalkImplementationProvider();
4748
private referenceProvider = new LogtalkReferenceProvider();
49+
private rangeFormatter = new LogtalkDocumentRangeFormattingEditProvider();
4850

4951
public async provideCodeActions(
5052
document: TextDocument,
@@ -11272,6 +11274,11 @@ export class LogtalkRefactorProvider implements CodeActionProvider {
1127211274

1127311275
const success = await workspace.applyEdit(edit);
1127411276
if (success) {
11277+
// If multi-line, apply formatting using the formatter
11278+
if (directiveInfo.isMultiLine) {
11279+
await this.formatSortedDirective(document, directiveInfo);
11280+
}
11281+
1127511282
this.logger.info(`Successfully sorted ${directiveInfo.directiveName} directive`);
1127611283
window.showInformationMessage(`Sorted ${directiveInfo.directiveName} directive`);
1127711284
} else {
@@ -11283,4 +11290,56 @@ export class LogtalkRefactorProvider implements CodeActionProvider {
1128311290
}
1128411291
}
1128511292

11293+
/**
11294+
* Format a sorted directive using the document range formatter
11295+
*/
11296+
private async formatSortedDirective(
11297+
document: TextDocument,
11298+
directiveInfo: {
11299+
directiveName: string;
11300+
directiveRange: { start: number; end: number };
11301+
isSingleArgumentDirective: boolean;
11302+
}
11303+
): Promise<void> {
11304+
try {
11305+
// Get the updated document (after sorting was applied)
11306+
const updatedDocument = await workspace.openTextDocument(document.uri);
11307+
11308+
// Create a range for the directive
11309+
const range = new Range(
11310+
new Position(directiveInfo.directiveRange.start, 0),
11311+
new Position(
11312+
directiveInfo.directiveRange.end,
11313+
updatedDocument.lineAt(directiveInfo.directiveRange.end).text.length
11314+
)
11315+
);
11316+
11317+
// Use the document range formatter to format the directive
11318+
const formattingOptions = {
11319+
tabSize: 4,
11320+
insertSpaces: false
11321+
};
11322+
11323+
const edits = this.rangeFormatter.provideDocumentRangeFormattingEdits(
11324+
updatedDocument,
11325+
range,
11326+
formattingOptions,
11327+
null as any // CancellationToken - not needed for our use case
11328+
);
11329+
11330+
if (edits && edits.length > 0) {
11331+
const formatEdit = new WorkspaceEdit();
11332+
edits.forEach(edit => {
11333+
formatEdit.replace(updatedDocument.uri, edit.range, edit.newText);
11334+
});
11335+
11336+
await workspace.applyEdit(formatEdit);
11337+
this.logger.info(`Applied formatting to sorted ${directiveInfo.directiveName} directive`);
11338+
}
11339+
} catch (error) {
11340+
this.logger.error(`Error formatting sorted directive: ${error}`);
11341+
// Don't show error to user - formatting is optional
11342+
}
11343+
}
11344+
1128611345
}

0 commit comments

Comments
 (0)