From ffb18b785b73b6743fb290f3081556ff105da296 Mon Sep 17 00:00:00 2001 From: Jason Fields Date: Sat, 4 May 2019 01:03:47 -0400 Subject: [PATCH] Implement Ctrl+G and :file (#3723) These both show the file name, line count, and relative position in the status bar. Fixes #3700 --- package.json | 5 +++++ src/actions/commands/actions.ts | 23 +++++++++++++++++++++-- src/cmd_line/commands/fileInfo.ts | 9 +++++++++ src/cmd_line/subparser.ts | 6 ++++++ src/cmd_line/subparsers/fileInfo.ts | 6 ++++++ src/util/statusBarTextUtils.ts | 17 +++++++++++++++++ 6 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/cmd_line/commands/fileInfo.ts create mode 100644 src/cmd_line/subparsers/fileInfo.ts diff --git a/package.json b/package.json index 84c85071e54..446132caec4 100644 --- a/package.json +++ b/package.json @@ -178,6 +178,11 @@ "command": "extension.vim_ctrl+f", "when": "editorTextFocus && vim.active && vim.use && vim.mode != 'Insert' && !inDebugRepl" }, + { + "key": "ctrl+g", + "command": "extension.vim_ctrl+g", + "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl" + }, { "key": "ctrl+h", "command": "extension.vim_ctrl+h", diff --git a/src/actions/commands/actions.ts b/src/actions/commands/actions.ts index 9bf9b038210..3ff2b72a2a8 100644 --- a/src/actions/commands/actions.ts +++ b/src/actions/commands/actions.ts @@ -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: { @@ -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); } } @@ -4266,3 +4269,19 @@ class ActionOverrideCmdAltUp extends BaseCommand { return vimState; } } + +@RegisterAction +class ActionShowFileInfo extends BaseCommand { + modes = [ModeName.Normal]; + keys = [['']]; + + runsOnceForEveryCursor() { + return false; + } + + public async exec(position: Position, vimState: VimState): Promise { + ReportFileInfo(position, vimState); + + return vimState; + } +} diff --git a/src/cmd_line/commands/fileInfo.ts b/src/cmd_line/commands/fileInfo.ts new file mode 100644 index 00000000000..e55115782e3 --- /dev/null +++ b/src/cmd_line/commands/fileInfo.ts @@ -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 { + ReportFileInfo(vimState.cursors[0].start, vimState); + } +} diff --git a/src/cmd_line/subparser.ts b/src/cmd_line/subparser.ts index 36f9cccbb52..4e711911a1b 100644 --- a/src/cmd_line/subparser.ts +++ b/src/cmd_line/subparser.ts @@ -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 = { @@ -108,4 +109,9 @@ export const commandParsers = { d: parseDeleteRangeLinesCommandArgs, sort: parseSortCommandArgs, + + f: parseFileInfoCommandArgs, + fi: parseFileInfoCommandArgs, + fil: parseFileInfoCommandArgs, + file: parseFileInfoCommandArgs, }; diff --git a/src/cmd_line/subparsers/fileInfo.ts b/src/cmd_line/subparsers/fileInfo.ts new file mode 100644 index 00000000000..9869c87ef63 --- /dev/null +++ b/src/cmd_line/subparsers/fileInfo.ts @@ -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(); +} diff --git a/src/util/statusBarTextUtils.ts b/src/util/statusBarTextUtils.ts index 8c5ef984c66..186859ae317 100644 --- a/src/util/statusBarTextUtils.ts +++ b/src/util/statusBarTextUtils.ts @@ -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); @@ -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 + ); +}