Skip to content

Commit

Permalink
Merge pull request #278 from BalticAmadeus/276-conditional-using-stat…
Browse files Browse the repository at this point in the history
…ements-should-not-be-sorted

276 change sorting of using statements
  • Loading branch information
PauliusKu authored Dec 10, 2024
2 parents 9a5b4a6 + 492309f commit b8dd75c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion resources/functionalTests/using/3lowercase/input.p
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"abl.completion.upperCase": false}*/

using as.
define variable a as integer no-undo.
//define variable a as integer no-undo.
USING b.
using c.
USING a.
2 changes: 1 addition & 1 deletion resources/functionalTests/using/3lowercase/target.p
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"abl.completion.upperCase": false}*/

using a.
define variable a as integer no-undo.
//define variable a as integer no-undo.
using as.
using b.
using c.
4 changes: 2 additions & 2 deletions resources/functionalTests/using/4optional-statements/input.p
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"abl.completion.upperCase": false}*/

using A from propath.
define variable c as integer no-undo.
//define variable c as integer no-undo.
USING b.
using c FROM aSsemblY.
message ".".
//message ".".
USING a.
4 changes: 2 additions & 2 deletions resources/functionalTests/using/4optional-statements/target.p
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"abl.completion.upperCase": false}*/

using A from propath.
define variable c as integer no-undo.
//define variable c as integer no-undo.
using a.
using b.
message ".".
//message ".".
using c from assembly.
22 changes: 17 additions & 5 deletions src/v2/formatters/using/UsingFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ export class UsingFormatter extends AFormatter implements IFormatter {

private usingStatementsFound: number = 0;
private alignOptionalStatements: number = 0;
private usingStatements: UsingStatement[] = [];
private textUsingStatements: string[] = [];

public constructor(configurationManager: IConfigurationManager) {
super(configurationManager);
this.settings = new UsingSettings(configurationManager);
}

private usingStatements: UsingStatement[] = [];
private textUsingStatements: string[] = [];

match(node: Readonly<SyntaxNode>): boolean {
if (node.type === SyntaxNodeType.UsingStatement) {
return true;
Expand All @@ -44,10 +43,13 @@ export class UsingFormatter extends AFormatter implements IFormatter {
this.textUsingStatements.sort();
}
const text = FormatterHelper.getCurrentText(node, fullText);
if (this.usingStatementsFound > this.usingStatements.length) {
if (this.usingStatementsFound > this.textUsingStatements.length) {
return undefined;
}
const newText = this.textUsingStatements[this.usingStatementsFound - 1];
if (this.usingStatementsFound === this.textUsingStatements.length) {
this.reset();
}
return this.getCodeEdit(node, text, newText, fullText);
}

Expand All @@ -56,9 +58,12 @@ export class UsingFormatter extends AFormatter implements IFormatter {
fullText: FullText
): void {
for (node; node !== null; node = node.nextSibling) {
if (!this.match(node)) {
if (node.type === SyntaxNodeType.Comment) {
continue;
}
if (!this.match(node)) {
break;
}

const keywordChild = node.child(0);
const identifierChild = node.child(1);
Expand Down Expand Up @@ -126,6 +131,13 @@ export class UsingFormatter extends AFormatter implements IFormatter {
);
}
}

private reset(): void {
this.usingStatementsFound = 0;
this.alignOptionalStatements = 0;
this.usingStatements = [];
this.textUsingStatements = [];
}
}

type UsingStatement = {
Expand Down

0 comments on commit b8dd75c

Please sign in to comment.