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

Implement delete-indentation as a command #1748

Merged
merged 3 commits into from
Nov 6, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Alt key is mapped to the Meta prefix (`M`) by default and you can configure for
| `C-x C-l` (`M-l`) | | Convert to lower case (On the Emacs' original behavior, `C-x C-l` and `M-l` are assigned to the different functionalities. However, this extension assigns these keys to the same `emacs-mcx.transformToLowercase` command which calls `editor.action.transformToLowercase` command internally and works similarly to both the original Emacs' functionalities based on the context. Upper case and title case (below) are same) |
| `C-x C-u` (`M-u`) | | Convert to upper case |
| `M-c` | | Convert to title case |
| `M-S-6` (`M-^` with US keyboard) | | Merge the previous and the current line (delete-indentation) |
| `M-S-6` (`M-^` with US keyboard) | | join two lines cleanly (delete-indentation) |

## Mark Commands

Expand Down
5 changes: 2 additions & 3 deletions keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,8 @@
},
{
"key": "meta+shift+6",
"command": "emacs-mcx.executeCommands",
"when": "editorTextFocus && !editorReadOnly",
"args": ["emacs-mcx.previousLine", "editor.action.joinLines"]
"command": "emacs-mcx.deleteIndentation",
"when": "editorTextFocus && !editorReadOnly"
},
// Cancel
{
Expand Down
32 changes: 8 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3658,40 +3658,24 @@
},
{
"key": "alt+shift+6",
"command": "emacs-mcx.executeCommands",
"when": "editorTextFocus && !editorReadOnly && !config.emacs-mcx.useMetaPrefixMacCmd",
"args": [
"emacs-mcx.previousLine",
"editor.action.joinLines"
]
"command": "emacs-mcx.deleteIndentation",
"when": "editorTextFocus && !editorReadOnly && !config.emacs-mcx.useMetaPrefixMacCmd"
},
{
"key": "alt+shift+6",
"mac": "cmd+shift+6",
"command": "emacs-mcx.executeCommands",
"when": "editorTextFocus && !editorReadOnly && config.emacs-mcx.useMetaPrefixMacCmd",
"args": [
"emacs-mcx.previousLine",
"editor.action.joinLines"
]
"command": "emacs-mcx.deleteIndentation",
"when": "editorTextFocus && !editorReadOnly && config.emacs-mcx.useMetaPrefixMacCmd"
},
{
"key": "escape shift+6",
"command": "emacs-mcx.executeCommands",
"when": "editorTextFocus && !editorReadOnly && config.emacs-mcx.useMetaPrefixEscape",
"args": [
"emacs-mcx.previousLine",
"editor.action.joinLines"
]
"command": "emacs-mcx.deleteIndentation",
"when": "editorTextFocus && !editorReadOnly && config.emacs-mcx.useMetaPrefixEscape"
},
{
"key": "ctrl+[ shift+6",
"command": "emacs-mcx.executeCommands",
"when": "editorTextFocus && !editorReadOnly && config.emacs-mcx.useMetaPrefixCtrlLeftBracket",
"args": [
"emacs-mcx.previousLine",
"editor.action.joinLines"
]
"command": "emacs-mcx.deleteIndentation",
"when": "editorTextFocus && !editorReadOnly && config.emacs-mcx.useMetaPrefixCtrlLeftBracket"
},
{
"key": "escape",
Expand Down
12 changes: 12 additions & 0 deletions src/commands/indent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as vscode from "vscode";
import { TextEditor } from "vscode";
import { EmacsCommand } from ".";

export class DeleteIndentation extends EmacsCommand {
public readonly id = "deleteIndentation";

public async run(textEditor: TextEditor, isInMarkMode: boolean, prefixArgument: number | undefined): Promise<void> {
await this.emacsController.runCommand("previousLine");
await vscode.commands.executeCommand("editor.action.joinLines");
}
}
7 changes: 5 additions & 2 deletions src/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import * as CaseCommands from "./commands/case";
import { DeleteBlankLines } from "./commands/delete-blank-lines";
import * as EditCommands from "./commands/edit";
import * as IndentCommands from "./commands/tab";
import * as TabCommands from "./commands/tab";
import * as IndentCommands from "./commands/indent";
import * as FindCommands from "./commands/find";
import * as KillCommands from "./commands/kill";
import * as MoveCommands from "./commands/move";
Expand Down Expand Up @@ -165,7 +166,9 @@
this.commandRegistry.register(new DeleteBlankLines(this));
this.commandRegistry.register(new RecenterTopBottom(this));

this.commandRegistry.register(new IndentCommands.TabToTabStop(this));
this.commandRegistry.register(new TabCommands.TabToTabStop(this));

this.commandRegistry.register(new IndentCommands.DeleteIndentation(this));

const searchState: FindCommands.SearchState = {
startSelections: undefined,
Expand Down Expand Up @@ -575,7 +578,7 @@

public executeCommandWithPrefixArgument<T>(
command: string,
args: any = null,

Check warning on line 581 in src/emulator.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, native)

Unexpected any. Specify a different type

Check warning on line 581 in src/emulator.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, native)

Unexpected any. Specify a different type

Check warning on line 581 in src/emulator.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, native)

Unexpected any. Specify a different type

Check warning on line 581 in src/emulator.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, web)

Unexpected any. Specify a different type
prefixArgumentKey = "prefixArgument",
): Thenable<T | undefined> {
const prefixArgument = this.prefixArgumentHandler.getPrefixArgument();
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

function registerEmulatorCommand(
commandName: string,
callback: (emulator: EmacsEmulator, ...args: Unreliable<any>[]) => unknown,

Check warning on line 86 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, native)

Unexpected any. Specify a different type

Check warning on line 86 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, native)

Unexpected any. Specify a different type

Check warning on line 86 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, native)

Unexpected any. Specify a different type

Check warning on line 86 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, web)

Unexpected any. Specify a different type
onNoEmulator?: (...args: unknown[]) => unknown,
) {
const disposable = vscode.commands.registerCommand(commandName, (...args) => {
Expand Down Expand Up @@ -335,6 +335,10 @@
emulator.runCommand("tabToTabStop");
});

registerEmulatorCommand("emacs-mcx.deleteIndentation", (emulator) => {
emulator.runCommand("deleteIndentation");
});

registerEmulatorCommand("emacs-mcx.paredit.forwardSexp", (emulator) => {
emulator.runCommand("paredit.forwardSexp");
});
Expand Down Expand Up @@ -397,7 +401,7 @@
return emulator.insertRegister(arg);
});

vscode.commands.registerCommand("emacs-mcx.executeCommands", async (...args: any[]) => {

Check warning on line 404 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, native)

Unexpected any. Specify a different type

Check warning on line 404 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, native)

Unexpected any. Specify a different type

Check warning on line 404 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, native)

Unexpected any. Specify a different type

Check warning on line 404 in src/extension.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, web)

Unexpected any. Specify a different type
if (1 <= args.length) {
executeCommands(args[0]);
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/suite/commands/indent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as vscode from "vscode";
import { EmacsEmulator } from "../../../emulator";
import { assertTextEqual, cleanUpWorkspace, setEmptyCursors, assertCursorsEqual, setupWorkspace } from "../utils";

suite("DeleteIndentation", () => {
let activeTextEditor: vscode.TextEditor;
let emulator: EmacsEmulator;

const initialText = "0123456789\nabcdefghij\nABCDEFGHIJ";

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

teardown(cleanUpWorkspace);

test("merge the current line with the previous line", async () => {
setEmptyCursors(activeTextEditor, [1, 2]);
await emulator.runCommand("deleteIndentation");
assertTextEqual(activeTextEditor, "0123456789 abcdefghij\nABCDEFGHIJ");
assertCursorsEqual(activeTextEditor, [0, 10]);
});
});
Loading