diff --git a/resources/functionalTests/using/6optional-statements/input.p b/resources/functionalTests/using/6optional-statements/input.p new file mode 100644 index 0000000..ddd113c --- /dev/null +++ b/resources/functionalTests/using/6optional-statements/input.p @@ -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. \ No newline at end of file diff --git a/resources/functionalTests/using/6optional-statements/target.p b/resources/functionalTests/using/6optional-statements/target.p new file mode 100644 index 0000000..1296bfe --- /dev/null +++ b/resources/functionalTests/using/6optional-statements/target.p @@ -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. \ No newline at end of file diff --git a/resources/functionalTests/using/6optional-statements2/input.p b/resources/functionalTests/using/6optional-statements2/input.p new file mode 100644 index 0000000..d1b8ecf --- /dev/null +++ b/resources/functionalTests/using/6optional-statements2/input.p @@ -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. diff --git a/resources/functionalTests/using/6optional-statements2/target.p b/resources/functionalTests/using/6optional-statements2/target.p new file mode 100644 index 0000000..b805b16 --- /dev/null +++ b/resources/functionalTests/using/6optional-statements2/target.p @@ -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. diff --git a/resources/functionalTests/using/6optional-statements3/input.p b/resources/functionalTests/using/6optional-statements3/input.p new file mode 100644 index 0000000..5aac9f8 --- /dev/null +++ b/resources/functionalTests/using/6optional-statements3/input.p @@ -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. diff --git a/resources/functionalTests/using/6optional-statements3/target.p b/resources/functionalTests/using/6optional-statements3/target.p new file mode 100644 index 0000000..743de56 --- /dev/null +++ b/resources/functionalTests/using/6optional-statements3/target.p @@ -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. diff --git a/src/v2/formatters/using/UsingFormatter.ts b/src/v2/formatters/using/UsingFormatter.ts index 256de6d..095ddc2 100644 --- a/src/v2/formatters/using/UsingFormatter.ts +++ b/src/v2/formatters/using/UsingFormatter.ts @@ -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): boolean { if (node.type === SyntaxNodeType.UsingStatement) { @@ -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); } @@ -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; +};