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

Dedent on else: and similar statements #27

Merged
merged 6 commits into from
Jun 26, 2019
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
30 changes: 24 additions & 6 deletions src/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ export function newlineAndIndent(

try {
if (textEditor.document.languageId === 'python') {
const indent = nextIndentationLevel(
textEditor.document.getText(
new vscode.Range(0, 0, position.line, position.character)).split("\n"),
tabSize
);
const lines = textEditor.document.getText(
new vscode.Range(0, 0, position.line, position.character)).split("\n");
let indent = nextIndentationLevel(lines, tabSize);
const spacesToRemove = dedentCurrentLine(currentLine, tabSize);
if (spacesToRemove > 0) {
// don't dedent the current line if we already dedented it, e.g. after a "return"
if (!parseLines(lines.slice(0, -1)).dedentNext) {
edit.delete(new vscode.Range(position.line, 0, position.line, spacesToRemove));
indent = Math.max(indent - spacesToRemove, 0);
}
}
hanging = shouldHang(currentLine, position.character);
if (hanging === Hanging.Partial) {
toInsert = '\n' + ' '.repeat(indentationLevel(currentLine) + tabSize);
Expand Down Expand Up @@ -63,7 +69,7 @@ export function nextIndentationLevel(
openBracketStack, lastClosedRow, lastColonRow, dedentNext
} = parseOutput;

if (dedentNext && !openBracketStack.length) {
if (dedentNext && !openBracketStack.length) {
return indentationLevel(lines[row]) - tabSize;
}

Expand Down Expand Up @@ -326,3 +332,15 @@ export function shouldHang(line: string, char: number): Hanging {
}
return Hanging.None;
}

// Returns the number of spaces that should be removed from the current line
export function dedentCurrentLine(line: string, tabSize: number): number {
const dedentKeywords = ["elif", "else", "except", "finally"];
const trimmed = line.trim();
if (trimmed.endsWith(":")) {
if (dedentKeywords.some((keyword) => trimmed.startsWith(keyword))) {
return Math.min(tabSize, indentationLevel(line));
}
}
return 0;
}
25 changes: 24 additions & 1 deletion src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ x = ['here(\\'(', 'is', 'a',\n\
));
});
});
suite("dedent", function () {
suite("dedent next line", function () {
test("return", function () {
assert.equal(0, indent.nextIndentationLevel(
[
Expand Down Expand Up @@ -527,4 +527,27 @@ x = ['here(\\'(', 'is', 'a',\n\
));
});
});
suite("dedent current line", function () {
test("normal else", function () {
assert.equal(4, indent.dedentCurrentLine(" else:", 4));
});
test("else with small tabsize", function () {
assert.equal(2, indent.dedentCurrentLine(" else:", 2));
});
test("else resulting in over dedentation", function () {
assert.equal(0, indent.dedentCurrentLine("else:", 4));
});
test("else resulting in over dedentation, 2", function () {
assert.equal(2, indent.dedentCurrentLine(" else:", 4));
});
test("elif", function () {
assert.equal(2, indent.dedentCurrentLine(" elif x == 5:", 2));
});
test("except", function () {
assert.equal(2, indent.dedentCurrentLine(" except ValueError:", 2));
});
test("finally", function () {
assert.equal(2, indent.dedentCurrentLine(" finally:", 2));
});
});
});