Skip to content

Commit

Permalink
Added tabSize and useSpaces in TemplateConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sante Barbuto committed Nov 17, 2023
1 parent 09c4ced commit 87cfdd1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ export class Extension {
const eol = configuration.get('file.eol', EOL);
const usingsInclude = configuration.get(`${EXTENSION_NAME}.usings.include`, true);
const usingsImplicit = configuration.get(`${EXTENSION_NAME}.usings.implicit`, true);
const tabSize = configuration.get('editor.tabSize', 4);
const useSpaces = configuration.get('editor.useSpaces', true);
const useFileScopedNamespace = configuration.get<boolean>(`${EXTENSION_NAME}.useFileScopedNamespace`, false);
const csprojReader = await CsprojReader.createFromPath(`${pathWithoutExtension}.cs`);
const isTargetFrameworkAboveEqualNet6 = await csprojReader?.isTargetFrameworkHigherThanOrEqualToDotNet6() === true;
Expand Down Expand Up @@ -156,6 +158,8 @@ export class Extension {
useImplicitUsings,
globalUsings,
customTemplate,
tabSize,
useSpaces,
)
.AndThen(config => CSharpFileCreator.create(config)
.AndThen(async creator => await creator.create(templatesPath, pathWithoutExtension, newFilename)));
Expand Down
8 changes: 6 additions & 2 deletions src/template/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ export default class Template {
}

const eol = this._configuration.getEolSettings();
const tabSize = this._configuration.getTabSize();
const fillingString = this._configuration.getUseSapces() ? SPACE : '\t';

const indentationLevel = this._configuration.getUseFileScopedNamespace() ? 1 : 2;
const indentation = getIndentation(4, indentationLevel);
const indentation = getIndentation(tabSize, indentationLevel, fillingString);

return `${eol}${customTemplate.genericsWhereClauses
.map((gwc) => `${indentation}${gwc}`)
Expand All @@ -106,8 +108,10 @@ export default class Template {
}

const eol = this._configuration.getEolSettings();
const tabSize = this._configuration.getTabSize();
const fillingString = this._configuration.getUseSapces() ? SPACE : '\t';
const indentationLevel = this._configuration.getUseFileScopedNamespace() ? 0 : 1;
const indentation = indentationLevel > 0 ? getIndentation(4, indentationLevel) : EMPTY;
const indentation = indentationLevel > 0 ? getIndentation(tabSize, indentationLevel, fillingString) : EMPTY;

return `${customTemplate.attributes
.map(a => `[${indentation}${a.replace('\n', EMPTY).replace('${classname}', filename)}]`)
Expand Down
23 changes: 21 additions & 2 deletions src/template/templateConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default class TemplateConfiguration {
private _includeNamespaces: boolean;
private _useFileScopedNamespace: boolean;
private _eolSettings: string;
private _tabSize: number;
private _useSpaces: boolean;
private _requiredUsings: Array<string>;
private _optionalUsings: Array<string>;
private _useImplicitUsings: boolean;
Expand All @@ -25,7 +27,9 @@ export default class TemplateConfiguration {
optionalUsings: Array<string>,
useImplicitUsings: boolean,
implicitUsings: Array<string>,
customTemplate?: CustomTemplate
tabSize: number,
useSpaces: boolean,
customTemplate?: CustomTemplate,
) {
this._templateType = templateType;
this._includeNamespaces = includeNamespaces;
Expand All @@ -36,6 +40,8 @@ export default class TemplateConfiguration {
this._useImplicitUsings = useImplicitUsings;
this._implicitUsings = implicitUsings;
this._customTemplate = customTemplate;
this._tabSize = tabSize;
this._useSpaces = useSpaces;
}

public getTemplateType(): TemplateType { return this._templateType; }
Expand All @@ -47,6 +53,8 @@ export default class TemplateConfiguration {
public getUseImplicitUsings(): boolean { return this._useImplicitUsings; }
public getImplicitUsings(): Array<string> { return this._implicitUsings; }
public getCustomTemplate(): CustomTemplate | undefined { return this._customTemplate; }
public getTabSize(): number { return this._tabSize; }
public getUseSapces(): boolean{ return this._useSpaces; }

public static create(
type: TemplateType,
Expand All @@ -56,8 +64,17 @@ export default class TemplateConfiguration {
isTargetFrameworkAboveNet6: boolean,
useImplicitUsings: boolean,
implictUsings: Array<string>,
customTemplate?: CustomTemplate
customTemplate?: CustomTemplate,
tabSize = 4,
useSpaces = true,
): Result<TemplateConfiguration> {
if (tabSize < 1) {
return Result.error<TemplateConfiguration>(
templateConfigurationStatuses.templateConfigurationCreationError,
'Tab Size must be a positive number',
);
}

if (type === TemplateType.Record && !isTargetFrameworkAboveNet6) {
return Result.error<TemplateConfiguration>(
templateConfigurationStatuses.templateConfigurationCreationError,
Expand Down Expand Up @@ -92,6 +109,8 @@ export default class TemplateConfiguration {
optionalUsings,
useImplicitUsings,
implictUsings,
tabSize,
useSpaces,
customTemplate,
)
);
Expand Down

0 comments on commit 87cfdd1

Please sign in to comment.