Skip to content

Commit

Permalink
Merge pull request #260 from BalticAmadeus/259-have-using-formatter-a…
Browse files Browse the repository at this point in the history
…lign-from-keyword

259 have using formatter align from keyword
  • Loading branch information
PauliusKu authored Nov 7, 2024
2 parents 36dffe8 + 3bfa91a commit a5e0f03
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 17 deletions.
12 changes: 12 additions & 0 deletions resources/functionalTests/using/6optional-statements/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* formatterSettingsOverride */
/* { "AblFormatter.usingFormatting": true,
"abl.completion.upperCase": false}*/

using Common.* from propath.
using Exceptions.* from propath.
using Framework.* from propath.
using Framework.Ccs.* from propath.
using Framework.Enum.* from propath.
using Framework.Exceptions.* from propath.
using Util.* from propath.
using Lang.* from propath.
12 changes: 12 additions & 0 deletions resources/functionalTests/using/6optional-statements/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* formatterSettingsOverride */
/* { "AblFormatter.usingFormatting": true,
"abl.completion.upperCase": false}*/

using Common.* from propath.
using Exceptions.* from propath.
using Framework.* from propath.
using Framework.Ccs.* from propath.
using Framework.Enum.* from propath.
using Framework.Exceptions.* from propath.
using Lang.* from propath.
using Util.* from propath.
11 changes: 11 additions & 0 deletions resources/functionalTests/using/6optional-statements2/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* formatterSettingsOverride */
/* { "AblFormatter.usingFormatting": true,
"abl.completion.upperCase": false}*/

using Alpha.Beta.* from propath.
using Gamma.Delta.* from propath.
using Epsilon.Zeta.* from propath.
using Theta.Iota.* from assembly.
using Kappa.Lambda.* from propath.
using Mu.Nu.* from propath.
using Xi.Omicron.* from assembly.
11 changes: 11 additions & 0 deletions resources/functionalTests/using/6optional-statements2/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* formatterSettingsOverride */
/* { "AblFormatter.usingFormatting": true,
"abl.completion.upperCase": false}*/

using Alpha.Beta.* from propath.
using Epsilon.Zeta.* from propath.
using Gamma.Delta.* from propath.
using Kappa.Lambda.* from propath.
using Mu.Nu.* from propath.
using Theta.Iota.* from assembly.
using Xi.Omicron.* from assembly.
16 changes: 16 additions & 0 deletions resources/functionalTests/using/6optional-statements3/input.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* formatterSettingsOverride */
/* { "AblFormatter.usingFormatting": true,
"abl.completion.upperCase": false}*/

using Alpha.*.
using Alpha.Beta.* from propath.
using Beta.Gamma.*.
using Chi.Psi.Omega.* from propath.
using Delta.Epsilon.Zeta.Eta.* from propath.
using Epsilon.Zeta.* from assembly.
using Eta.Theta.* from propath.
using Gamma.Delta.* from propath.
using Kappa.Lambda.Mu.Nu.* from propath.
using Sigma.Tau.Upsilon.Phi.* from propath.
using Theta.Iota.* from propath.
using Xi.Omicron.Pi.Rho.* from propath.
16 changes: 16 additions & 0 deletions resources/functionalTests/using/6optional-statements3/target.p
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* formatterSettingsOverride */
/* { "AblFormatter.usingFormatting": true,
"abl.completion.upperCase": false}*/

using Alpha.*.
using Alpha.Beta.* from propath.
using Beta.Gamma.*.
using Chi.Psi.Omega.* from propath.
using Delta.Epsilon.Zeta.Eta.* from propath.
using Epsilon.Zeta.* from assembly.
using Eta.Theta.* from propath.
using Gamma.Delta.* from propath.
using Kappa.Lambda.Mu.Nu.* from propath.
using Sigma.Tau.Upsilon.Phi.* from propath.
using Theta.Iota.* from propath.
using Xi.Omicron.Pi.Rho.* from propath.
66 changes: 49 additions & 17 deletions src/v2/formatters/using/UsingFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export class UsingFormatter extends AFormatter implements IFormatter {
private readonly settings: UsingSettings;

private usingStatementsFound: number = 0;
private alignOptionalStatements: number = 0;

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

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

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

Expand All @@ -67,36 +72,63 @@ export class UsingFormatter extends AFormatter implements IFormatter {
fullText
);
keyword = this.settings.casing()
? keyword.toUpperCase()
: keyword.toLowerCase();
? keyword.trim().toUpperCase()
: keyword.trim().toLowerCase();
const identifier = FormatterHelper.getCurrentText(
identifierChild,
fullText
);
).trim();

let optionalDefinitions = "";
this.alignOptionalStatements = Math.max(
this.alignOptionalStatements,
/* The format is this:
USING IDENTIFIER OPTIONAL_DEFINITIONS.
therefore we add +1 for the spaces between different parts.
*/
keyword.length + 1 + identifier.length + 1
);
if (node.childCount > 2) {
for (let i = 2; i < node.childCount; ++i) {
const currentChild = node.child(i);
if (currentChild === null) {
continue;
}
optionalDefinitions += FormatterHelper.getCurrentText(
currentChild,
fullText
);
optionalDefinitions +=
FormatterHelper.getCurrentText(
currentChild,
fullText
).trim() + " ";
}
optionalDefinitions = this.settings.casing()
? optionalDefinitions.toUpperCase()
: optionalDefinitions.toLowerCase();
? optionalDefinitions.trim().toUpperCase()
: optionalDefinitions.trim().toLowerCase();
}

this.usingStatements.push(
keyword
.concat(identifier)
.concat(optionalDefinitions)
.concat(".")
this.usingStatements.push({
identifier: keyword + " " + identifier,
optionalDefinitions,
});
}
}

private usingStatementToString(statement: UsingStatement): string {
if (statement.optionalDefinitions === "") {
return statement.identifier + ".";
} else {
return (
statement.identifier +
" ".repeat(
this.alignOptionalStatements - statement.identifier.length
) +
statement.optionalDefinitions +
"."
);
}
}
}

type UsingStatement = {
identifier: string;
optionalDefinitions: string;
};

0 comments on commit a5e0f03

Please sign in to comment.