Skip to content

Commit

Permalink
Add test cases and fix the tab behavior when the cursor in the first …
Browse files Browse the repository at this point in the history
…line
  • Loading branch information
whitphx committed May 16, 2024
1 parent 800fca5 commit 6c32e8f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/commands/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class TabToTabStop extends EmacsCommand {
prevNonEmptyLine = line;
break;
}
const prevIndentHeadChar = prevNonEmptyLine?.firstNonWhitespaceCharacterIndex ?? 0;
const newIndentUnits = Math.floor(prevIndentHeadChar / tabSize) + 1;
const prevIndentChars = prevNonEmptyLine?.firstNonWhitespaceCharacterIndex;
const newIndentUnits = prevIndentChars != null ? Math.floor(prevIndentChars / tabSize) + 1 : 0;
const newIndentChars = newIndentUnits * indentChars;

const curLine = textEditor.document.lineAt(selection.active.line);
Expand Down
59 changes: 50 additions & 9 deletions src/test/suite/commands/tab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import * as vscode from "vscode";
import { EmacsEmulator } from "../../../emulator";
import { assertTextEqual, cleanUpWorkspace, setEmptyCursors, assertCursorsEqual, setupWorkspace } from "../utils";

suite("TabToTabStop", () => {
suite("TabToTabStop with a free-form language", () => {
const language = "javascript";
const tabSize = 2;

let activeTextEditor: vscode.TextEditor;
let emulator: EmacsEmulator;

Expand All @@ -11,8 +14,8 @@ suite("TabToTabStop", () => {
console.log("hello");
}`;
setup(async () => {
activeTextEditor = await setupWorkspace(initialText, { language: "javascript" });
activeTextEditor.options.tabSize = 2;
activeTextEditor = await setupWorkspace(initialText, { language });
activeTextEditor.options.tabSize = tabSize;
emulator = new EmacsEmulator(activeTextEditor);
});

Expand Down Expand Up @@ -44,8 +47,8 @@ console.log("hello");
}`;

setup(async () => {
activeTextEditor = await setupWorkspace(initialText, { language: "javascript" });
activeTextEditor.options.tabSize = 2;
activeTextEditor = await setupWorkspace(initialText, { language });
activeTextEditor.options.tabSize = tabSize;
emulator = new EmacsEmulator(activeTextEditor);
});

Expand Down Expand Up @@ -82,8 +85,8 @@ console.log("hello");
}`;

setup(async () => {
activeTextEditor = await setupWorkspace(initialText, { language: "javascript" });
activeTextEditor.options.tabSize = 2;
activeTextEditor = await setupWorkspace(initialText, { language });
activeTextEditor.options.tabSize = tabSize;
emulator = new EmacsEmulator(activeTextEditor);
});

Expand Down Expand Up @@ -122,8 +125,8 @@ console.log("hello");
}`;

setup(async () => {
activeTextEditor = await setupWorkspace(initialText, { language: "javascript" });
activeTextEditor.options.tabSize = 2;
activeTextEditor = await setupWorkspace(initialText, { language });
activeTextEditor.options.tabSize = tabSize;
emulator = new EmacsEmulator(activeTextEditor);
});

Expand Down Expand Up @@ -158,3 +161,41 @@ console.log("hello");
});
});
});

suite("TabToTabStop with a offside-rule language", () => {
const language = "python";
const tabSize = 4;

let activeTextEditor: vscode.TextEditor;
let emulator: EmacsEmulator;

suite("new indent", () => {
const initialText = `def f():
print("hello")`;
setup(async () => {
activeTextEditor = await setupWorkspace(initialText, { language });
activeTextEditor.options.tabSize = tabSize;
emulator = new EmacsEmulator(activeTextEditor);
});

teardown(cleanUpWorkspace);

test("reindent works when the cursor is at the line where it should", async () => {
setEmptyCursors(activeTextEditor, [1, 0]);
await emulator.runCommand("tabToTabStop");
assertTextEqual(
activeTextEditor,
`def f():
print("hello")`,
);
assertCursorsEqual(activeTextEditor, [1, 4]);
});

test("reindent doesn't work when the cursor is at the line where it shouldn't", async () => {
setEmptyCursors(activeTextEditor, [0, 0]);
await emulator.runCommand("tabToTabStop");
assertTextEqual(activeTextEditor, initialText);
assertCursorsEqual(activeTextEditor, [0, 0]);
});
});
});

0 comments on commit 6c32e8f

Please sign in to comment.