Skip to content

Commit

Permalink
add support for changing indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Sep 6, 2016
1 parent 07f8e75 commit 009adab
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ Status | Command | Description
:white_check_mark: | Del | delete the character under the cursor
:white_check_mark: | CTRL-W | delete word before the cursor
:white_check_mark: | CTRL-U | delete all entered characters in the current line
| CTRL-T | insert one shiftwidth of indent in front of the current line
| CTRL-D | delete one shiftwidth of indent in front of the current line
:white_check_mark: | CTRL-T | insert one shiftwidth of indent in front of the current line
:white_check_mark: | CTRL-D | delete one shiftwidth of indent in front of the current line
| 0 CTRL-D | delete all indent in the current line
| ^ CTRL-D | delete all indent in the current line, restore indent in next line

Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
{
"key": "ctrl+d",
"command": "extension.vim_ctrl+d",
"when": "editorTextFocus"
"when": "editorTextFocus && vim.useCtrlKeys"
},
{
"key": "ctrl+[",
Expand All @@ -132,6 +132,11 @@
"command": "extension.vim_ctrl+x",
"when": "editorTextFocus && vim.useCtrlKeys"
},
{
"key": "ctrl+t",
"command": "extension.vim_ctrl+t",
"when": "editorTextFocus && vim.useCtrlKeys"
},
{
"key": "left",
"command": "extension.vim_left",
Expand Down
46 changes: 46 additions & 0 deletions src/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,52 @@ class CommandInsertBelowChar extends BaseCommand {
}
}

@RegisterAction
class CommandInsertIndentInCurrentLine extends BaseCommand {
modes = [ModeName.Insert];
keys = ["<C-t>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const originalText = TextEditor.getLineAt(position).text;
const indentationWidth = TextEditor.getIndentationLevel(originalText);
const tabSize = Configuration.getInstance().tabstop;
const newIndentationWidth = (indentationWidth / tabSize + 1) * tabSize;
const result = TextEditor.setIndentationLevel(originalText, newIndentationWidth);
await TextEditor.replace(new vscode.Range(position.getLineBegin(), position.getLineEnd()), TextEditor.setIndentationLevel(originalText, newIndentationWidth));

const cursorPosition = Position.FromVSCodePosition(position.with(position.line, position.character + (newIndentationWidth - indentationWidth) / tabSize));
vimState.cursorPosition = cursorPosition;
vimState.cursorStartPosition = cursorPosition;
vimState.currentMode = ModeName.Insert;
return vimState;
}
}

@RegisterAction
class CommandDeleteIndentInCurrentLine extends BaseCommand {
modes = [ModeName.Insert];
keys = ["<C-d>"];

public async exec(position: Position, vimState: VimState): Promise<VimState> {
const originalText = TextEditor.getLineAt(position).text;
const indentationWidth = TextEditor.getIndentationLevel(originalText);

if (indentationWidth === 0) {
return vimState;
}

const tabSize = Configuration.getInstance().tabstop;
const newIndentationWidth = (indentationWidth / tabSize - 1) * tabSize;
await TextEditor.replace(new vscode.Range(position.getLineBegin(), position.getLineEnd()), TextEditor.setIndentationLevel(originalText, newIndentationWidth < 0 ? 0: newIndentationWidth));

const cursorPosition = Position.FromVSCodePosition(position.with(position.line, position.character + (newIndentationWidth - indentationWidth) / tabSize ));
vimState.cursorPosition = cursorPosition;
vimState.cursorStartPosition = cursorPosition;
vimState.currentMode = ModeName.Insert;
return vimState;
}
}

@RegisterAction
class CommandCtrlY extends BaseCommand {
modes = [ModeName.Normal];
Expand Down

0 comments on commit 009adab

Please sign in to comment.