Skip to content

Commit 8c37355

Browse files
committed
✨ Respect editorconfig and file indendation
resolves #193, resolves #338
1 parent e144336 commit 8c37355

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

client/src/language/beautify.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FormattingOptions } from 'vscode';
12
import { FormattingLiterals, FormattingTags } from "../interfaces";
23
import { CONFIG } from "../configuration";
34

@@ -19,7 +20,7 @@ export class BeautifySmarty {
1920
end: new Set(["block", "capture", "for", "foreach", "function", "if", "literal", "section", "setfilter", "strip", "while"])
2021
};
2122

22-
public beautify(docText: String): string {
23+
public beautify(docText: String, options: FormattingOptions): string {
2324
const embeddedRegExp: RegExp = /(<(?:script|style)[\s\S]*?>)([\s\S]*?)(<\/(?:script|style)>)/g;
2425
const smartyRegExp: RegExp = /^(?:\t| )*(.*{{?[^}\n\s]}?.*)$/gm;
2526

@@ -34,8 +35,8 @@ export class BeautifySmarty {
3435
});
3536

3637
// format using js-beautify
37-
const beautifyOptions = this.getBeautifyOptions();
38-
let formatted = beautify(docText, beautifyOptions);
38+
const beautifyConfig = this.beautifyConfig(options);
39+
let formatted = beautify(docText, beautifyConfig);
3940

4041
// split into lines
4142
const literalPattern: string = Object.values(this.literals).map(r => r.source).join("|");
@@ -54,7 +55,7 @@ export class BeautifySmarty {
5455
}
5556
}
5657

57-
const indent_char = beautifyOptions.indent_with_tabs ? "\t" : " ".repeat(beautifyOptions.indent_size);
58+
const indent_char = beautifyConfig.indent_with_tabs ? "\t" : " ".repeat(beautifyConfig.indent_size);
5859
const region = /{{?(\/?)(\w+).*?}}?/g;
5960

6061
const startedRegions = [];
@@ -117,10 +118,10 @@ export class BeautifySmarty {
117118
return formatted;
118119
}
119120

120-
private getBeautifyOptions() {
121-
const options = {
122-
indent_size: CONFIG.tabSize,
123-
indent_with_tabs: !CONFIG.insertSpaces,
121+
private beautifyConfig(options: FormattingOptions) {
122+
const config = {
123+
indent_size: options.tabSize,
124+
indent_with_tabs: !options.insertSpaces,
124125
indent_handlebars: false,
125126
indent_inner_html: CONFIG.indentInnerHtml,
126127
max_preserve_newlines: CONFIG.maxPreserveNewLines,
@@ -138,7 +139,7 @@ export class BeautifySmarty {
138139
templating: ["smarty"]
139140
};
140141

141-
return options;
142+
return config;
142143
}
143144

144145
}

client/src/language/formatter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ export class FormattingProvider implements DocumentFormattingEditProvider, Docum
1010

1111
private SmartyFormatter: BeautifySmarty = new BeautifySmarty();
1212

13-
provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, _options: FormattingOptions, _token: CancellationToken): ProviderResult<TextEdit[]> {
13+
provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, options: FormattingOptions, _token: CancellationToken): ProviderResult<TextEdit[]> {
1414
const newrange = new Range(range.start.line, 0, range.end.line, range.end.character);
1515
const text = document.getText(newrange);
16-
const formatted = this.SmartyFormatter.beautify(text);
16+
const formatted = this.SmartyFormatter.beautify(text, options);
1717

1818
return [TextEdit.replace(newrange, formatted)];
1919
}
2020

21-
provideDocumentFormattingEdits(document: TextDocument, _options: FormattingOptions, _token: CancellationToken): ProviderResult<TextEdit[]> {
21+
provideDocumentFormattingEdits(document: TextDocument, options: FormattingOptions, _token: CancellationToken): ProviderResult<TextEdit[]> {
2222
const text = document.getText();
23-
const formatted = this.SmartyFormatter.beautify(text);
23+
const formatted = this.SmartyFormatter.beautify(text, options);
2424
const range = fullDocumentRange(document);
2525

2626
return [TextEdit.replace(range, formatted)];

0 commit comments

Comments
 (0)