Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to extension's settings: "Preserve indentation on empty line" #192

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
"minimum": -1,
"description": "Allowed number of empty lines.\n-1: ignore empty lines.\n0: no empty lines."
},
"ahk++.formatter.preserveIndent": {
"type": "boolean",
"default": false,
"description": "Preserve indentation on empty line."
},
"ahk++.formatter.trimExtraSpaces": {
"type": "boolean",
"default": true,
Expand Down
1 change: 1 addition & 0 deletions src/common/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export enum ConfigKey {
enableIntellisense = 'language.enableIntellisense',
executePath = 'file.executePath',
helpPath = 'file.helpPath',
preserveIndent = 'formatter.preserveIndent',
trimExtraSpaces = 'formatter.trimExtraSpaces',
}
5 changes: 5 additions & 0 deletions src/providers/formattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
let preBlockCommentAtTopLevel = true;
let preBlockCommentOneCommandCode = false;

const preserveIndentOnEmptyString = Global.getConfig<boolean>(
ConfigKey.preserveIndent,
);
const trimSpaces = Global.getConfig<boolean>(ConfigKey.trimExtraSpaces);

for (let lineIndex = 0; lineIndex < document.lineCount; lineIndex++) {
Expand Down Expand Up @@ -146,6 +149,7 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
blockCommentLine,
depth,
options,
preserveIndentOnEmptyString,
);
}
if (originalLine.match(/^\s*\*\//)) {
Expand Down Expand Up @@ -242,6 +246,7 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
formattedLine,
depth,
options,
preserveIndentOnEmptyString,
);

// Next line
Expand Down
26 changes: 21 additions & 5 deletions src/providers/formattingProvider.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ export function buildIndentationChars(
* Build indented line of code (not ready for saving)
* @param indentationChars Indentation chars
* @param formattedLine Formatted line of code
* @param preserveIndentOnEmptyString Preserve indent on empty string
*/
export function buildIndentedString(
indentationChars: string,
formattedLine: string,
preserveIndentOnEmptyString: boolean,
): string {
if (preserveIndentOnEmptyString) {
return indentationChars + formattedLine;
}
return !formattedLine?.trim()
? formattedLine
: indentationChars + formattedLine;
Expand All @@ -35,16 +40,22 @@ export function buildIndentedString(
* @param formattedLine Formatted line of code
* @param depth Depth of indent
* @param options VS Code formatting options
* @param preserveIndentOnEmptyString Preserve indent on empty string
*/
export function buildIndentedLine(
lineIndex: number,
lastLineIndex: number,
formattedLine: string,
depth: number,
options: Pick<vscode.FormattingOptions, 'insertSpaces' | 'tabSize'>,
preserveIndentOnEmptyString: boolean,
) {
const indentationChars = buildIndentationChars(depth, options);
let indentedLine = buildIndentedString(indentationChars, formattedLine);
let indentedLine = buildIndentedString(
indentationChars,
formattedLine,
preserveIndentOnEmptyString,
);
// If not last line, add newline
if (lineIndex !== lastLineIndex - 1) {
indentedLine += '\n';
Expand Down Expand Up @@ -81,14 +92,19 @@ export function removeEmptyLines(
if (allowedNumberOfEmptyLines === -1) {
return document;
}
const newLineCharacterNumber = allowedNumberOfEmptyLines + 1; // + 1 new line character from previous string with text
const newLineCharacter = new RegExp(`\\n{${newLineCharacterNumber},}`, 'g');
const emptyLines = new RegExp(
// We need not greedy quantifier for whitespaces (\s*?),
// because (\s) matches [\r\n\t\f\v ],
// it interfere with last \n in regex.
`\\n(\\s*?\\n){${allowedNumberOfEmptyLines},}`,
'g',
);
return (
document
// remove extra empty lines
.replace(newLineCharacter, '\n'.repeat(newLineCharacterNumber))
.replace(emptyLines, '\n' + '$1'.repeat(allowedNumberOfEmptyLines))
// remove empty lines at start of file
.replace(/^\n*/, '')
.replace(/^\s*\n+/, '')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,53 +69,82 @@ suite('FormattingProvider utils', () => {
// dp: , // depth of indentation
// fl: , // formatted line
// op: , // formatting options
// pi: , // preserve indent
// rs: , // expected result
// },
{
dp: 0,
fl: 'SoundBeep',
op: { insertSpaces: true, tabSize: 4 },
pi: false,
rs: 'SoundBeep',
},
{
dp: 1,
fl: 'SoundBeep',
op: { insertSpaces: true, tabSize: 4 },
pi: false,
rs: ' SoundBeep',
},
{
dp: 2,
fl: 'SoundBeep',
op: { insertSpaces: true, tabSize: 4 },
pi: false,
rs: ' SoundBeep',
},
{
dp: 1,
fl: 'SoundBeep',
op: { insertSpaces: false, tabSize: 4 },
pi: false,
rs: '\tSoundBeep',
},
{
dp: 2,
fl: 'SoundBeep',
op: { insertSpaces: false, tabSize: 4 },
pi: false,
rs: '\t\tSoundBeep',
},
{
dp: 1,
fl: '',
op: { insertSpaces: true, tabSize: 4 },
pi: true,
rs: ' ',
},
{
dp: 2,
fl: '',
op: { insertSpaces: false, tabSize: 4 },
pi: true,
rs: '\t\t',
},
];
dataList.forEach((data) => {
test(
'depth:' +
data.dp +
' spaces:' +
data.op.insertSpaces.toString() +
' preserveIndent:' +
data.pi.toString() +
" '" +
data.fl +
"' => '" +
data.rs.replace(/\t/g, '\\t') +
"'",
() => {
assert.strictEqual(
buildIndentedLine(0, 1, data.fl, data.dp, data.op),
buildIndentedLine(
0,
1,
data.fl,
data.dp,
data.op,
data.pi,
),
data.rs,
);
},
Expand Down Expand Up @@ -226,11 +255,41 @@ suite('FormattingProvider utils', () => {
ln: 3,
rs: 'text\n\n\n\ntext\n\n\n\n',
},
{
in: 'text\n \n \n \n \ntext\n \n \n \n \n',
ln: 0,
rs: 'text\ntext\n',
},
{
in: 'text\n \n \n \n \ntext\n \n \n \n \n',
ln: 1,
rs: 'text\n \ntext\n \n',
},
{
in: 'text\n \n \n \n \ntext\n \n \n \n \n',
ln: 2,
rs: 'text\n \n \ntext\n \n \n',
},
{
in: 'text\n \n \n \n \ntext\n \n \n \n \n',
ln: 3,
rs: 'text\n \n \n \ntext\n \n \n \n',
},
{
in: '\n\n\ntext',
ln: 1,
rs: 'text',
},
{
in: ' \n',
ln: 1,
rs: '',
},
{
in: '\t\n',
ln: 1,
rs: '',
},
{
in: 'text\ntext',
ln: 1,
Expand Down