Skip to content

Commit

Permalink
Implement Ctrl+G and :file (VSCodeVim#3723)
Browse files Browse the repository at this point in the history
These both show the file name, line count, and relative position in the status bar.
Fixes VSCodeVim#3700
  • Loading branch information
J-Fields authored and jpoon committed May 4, 2019
1 parent e3e3436 commit ffb18b7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@
"command": "extension.vim_ctrl+f",
"when": "editorTextFocus && vim.active && vim.use<C-f> && vim.mode != 'Insert' && !inDebugRepl"
},
{
"key": "ctrl+g",
"command": "extension.vim_ctrl+g",
"when": "editorTextFocus && vim.active && vim.use<C-g> && !inDebugRepl"
},
{
"key": "ctrl+h",
"command": "extension.vim_ctrl+h",
Expand Down
23 changes: 21 additions & 2 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { BaseAction } from './../base';
import { commandLine } from './../../cmd_line/commandLine';
import * as operator from './../operator';
import { Jump } from '../../jumps/jump';
import { ReportLinesChanged, ReportClear } from '../../util/statusBarTextUtils';
import { ReportLinesChanged, ReportClear, ReportFileInfo } from '../../util/statusBarTextUtils';

export class DocumentContentChangeAction extends BaseAction {
contentChanges: {
Expand Down Expand Up @@ -690,7 +690,10 @@ class CommandMoveHalfPageDown extends CommandEditorScroll {
if (newPositionLine > maxLineValue) {
newPosition = new Position(0, 0).getDocumentEnd();
} else {
const newPositionColumn = Math.min(startColumn, TextEditor.getLineMaxColumn(newPositionLine));
const newPositionColumn = Math.min(
startColumn,
TextEditor.getLineMaxColumn(newPositionLine)
);
newPosition = new Position(newPositionLine, newPositionColumn);
}
}
Expand Down Expand Up @@ -4266,3 +4269,19 @@ class ActionOverrideCmdAltUp extends BaseCommand {
return vimState;
}
}

@RegisterAction
class ActionShowFileInfo extends BaseCommand {
modes = [ModeName.Normal];
keys = [['<C-g>']];

runsOnceForEveryCursor() {
return false;
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
ReportFileInfo(position, vimState);

return vimState;
}
}
9 changes: 9 additions & 0 deletions src/cmd_line/commands/fileInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CommandBase } from "../node";
import { VimState } from "../../state/vimState";
import { ReportFileInfo } from "../../util/statusBarTextUtils";

export class FileInfoCommand extends CommandBase {
async execute(vimState: VimState): Promise<void> {
ReportFileInfo(vimState.cursors[0].start, vimState);
}
}
6 changes: 6 additions & 0 deletions src/cmd_line/subparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { parseWallCommandArgs } from './subparsers/wall';
import { parseWriteCommandArgs } from './subparsers/write';
import { parseWriteQuitCommandArgs } from './subparsers/writequit';
import { parseWriteQuitAllCommandArgs } from './subparsers/writequitall';
import { parseFileInfoCommandArgs } from './subparsers/fileInfo';

// maps command names to parsers for said commands.
export const commandParsers = {
Expand Down Expand Up @@ -108,4 +109,9 @@ export const commandParsers = {
d: parseDeleteRangeLinesCommandArgs,

sort: parseSortCommandArgs,

f: parseFileInfoCommandArgs,
fi: parseFileInfoCommandArgs,
fil: parseFileInfoCommandArgs,
file: parseFileInfoCommandArgs,
};
6 changes: 6 additions & 0 deletions src/cmd_line/subparsers/fileInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { FileInfoCommand } from "../commands/fileInfo";

export function parseFileInfoCommandArgs(args: string): FileInfoCommand {
// TODO: implement bang, file name parameters. http://vimdoc.sourceforge.net/htmldoc/editing.html#CTRL-G
return new FileInfoCommand();
}
17 changes: 17 additions & 0 deletions src/util/statusBarTextUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ModeName } from '../mode/mode';
import { StatusBar } from '../statusBar';
import { VimState } from '../state/vimState';
import { configuration } from '../configuration/configuration';
import { Position } from '../common/motion/position';

export function ReportClear(vimState: VimState) {
StatusBar.Set('', vimState.currentMode, vimState.isRecordingMacro, true);
Expand Down Expand Up @@ -48,3 +49,19 @@ export function ReportLinesYanked(numLinesYanked: number, vimState: VimState) {
ReportClear(vimState);
}
}

/**
* Shows the active file's path and line count as well as position in the file as a percentage.
* Triggered via `ctrl-g` or `:file`.
*/
export function ReportFileInfo(position: Position, vimState: VimState) {
const doc = vimState.editor.document;
const progress = Math.floor(((position.line + 1) / doc.lineCount) * 100);

StatusBar.Set(
`"${doc.fileName}" ${doc.lineCount} lines --${progress}%--`,
vimState.currentMode,
vimState.isRecordingMacro,
true
);
}

0 comments on commit ffb18b7

Please sign in to comment.