diff --git a/README.md b/README.md index e6f47126..511be9fe 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,12 @@ Set to `false` to disable `Ctrl+` keybindings. Set to `true` to start in Insert mode when opening files. +#### `amVim.smartRelativeLineNumbers` + +`Boolean`, Default: `false` + +Set to `true` to will make line numbers relative when not in Insert mode. + #### `amVim.vimStyleNavigationInListView` `Boolean`, Default: `true` diff --git a/package.json b/package.json index 49dbd443..1e89ee0b 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,11 @@ "default": false, "description": "Set to `true` to start in Insert mode when opening files." }, + "amVim.smartRelativeLineNumbers": { + "type": "boolean", + "default": false, + "description": "Set to `true` to will make line numbers relative when not in Insert mode." + }, "amVim.vimStyleNavigationInListView": { "type": "boolean", "default": true, diff --git a/src/Actions/RelativeLineNumbers.ts b/src/Actions/RelativeLineNumbers.ts new file mode 100644 index 00000000..931fa99d --- /dev/null +++ b/src/Actions/RelativeLineNumbers.ts @@ -0,0 +1,37 @@ +import { TextEditorLineNumbersStyle, window } from 'vscode'; + +export class ActionRelativeLineNumbers { + + static on(): Thenable { + return new Promise((resolve) => { + setTimeout(() => { + const activeTextEditor = window.activeTextEditor; + + if (! activeTextEditor) { + return Promise.resolve(false); + } + + activeTextEditor.options.lineNumbers = TextEditorLineNumbersStyle.Relative; + + resolve(true); + }, 0); + }); + } + + static off(): Thenable { + return new Promise((resolve) => { + setTimeout(() => { + const activeTextEditor = window.activeTextEditor; + + if (! activeTextEditor) { + return Promise.resolve(false); + } + + activeTextEditor.options.lineNumbers = TextEditorLineNumbersStyle.On; + + resolve(true); + }, 0); + }); + } + +} diff --git a/src/Configuration.ts b/src/Configuration.ts index e7ffab02..5836e849 100644 --- a/src/Configuration.ts +++ b/src/Configuration.ts @@ -12,6 +12,9 @@ export class Configuration { private static _defaultModeID: ModeID; static get defaultModeID(): ModeID { return this._defaultModeID; } + private static _smartRelativeLineNumbers: boolean; + static get smartRelativeLineNumbers(): boolean { return this._smartRelativeLineNumbers; } + static init(): void { if (this.isReady) { return; @@ -29,7 +32,10 @@ export class Configuration { private static onDidChangeConfiguration(): void { this.updateCache(); this.updateKeybindingContexts(); + this._defaultModeID = this.getExtensionSetting('startInInsertMode', false) ? ModeID.INSERT : ModeID.NORMAL; + this._smartRelativeLineNumbers = this.getExtensionSetting('smartRelativeLineNumbers', false); + UtilWord.updateCharacterKindCache(this.getEditorSetting('wordSeparators', '`~!@#$%^&*()-=+[{]}\\|;:\'",.<>/?')); } diff --git a/src/Modes/Insert.ts b/src/Modes/Insert.ts index 94adcb19..24c84a06 100644 --- a/src/Modes/Insert.ts +++ b/src/Modes/Insert.ts @@ -1,7 +1,9 @@ import {window, TextEditor, Position} from 'vscode'; import {Mode, ModeID} from './Mode'; +import {Configuration} from '../Configuration'; import {MatchResultKind} from '../Mappers/Generic'; import {CommandMap} from '../Mappers/Command'; +import {ActionRelativeLineNumbers} from '../Actions/RelativeLineNumbers'; import {ActionInsert} from '../Actions/Insert'; import {ActionDelete} from '../Actions/Delete'; import {ActionSelection} from '../Actions/Selection'; @@ -54,6 +56,10 @@ export class ModeInsert extends Mode { this.textEditor = window.activeTextEditor; this.startRecord(); + + if (Configuration.smartRelativeLineNumbers) { + ActionRelativeLineNumbers.off(); + } } exit(): void { diff --git a/src/Modes/Normal.ts b/src/Modes/Normal.ts index 39790ee7..3052e6f3 100644 --- a/src/Modes/Normal.ts +++ b/src/Modes/Normal.ts @@ -2,8 +2,10 @@ import {TextEditorRevealType} from 'vscode'; import {StaticReflect} from '../LanguageExtensions/StaticReflect'; import {SymbolMetadata} from '../Symbols/Metadata'; import {Mode, ModeID} from './Mode'; +import {Configuration} from '../Configuration'; import {CommandMap} from '../Mappers/Command'; import {ActionBlockCursor} from '../Actions/BlockCursor'; +import {ActionRelativeLineNumbers} from '../Actions/RelativeLineNumbers'; import {ActionMoveCursor} from '../Actions/MoveCursor'; import {ActionPage} from '../Actions/Page'; import {ActionInsert} from '../Actions/Insert'; @@ -246,6 +248,10 @@ export class ModeNormal extends Mode { super.enter(); ActionBlockCursor.on(); + + if (Configuration.smartRelativeLineNumbers) { + ActionRelativeLineNumbers.on(); + } } exit(): void { diff --git a/src/Modes/Visual.ts b/src/Modes/Visual.ts index e9df90b7..a8ff362f 100644 --- a/src/Modes/Visual.ts +++ b/src/Modes/Visual.ts @@ -3,7 +3,9 @@ import {StaticReflect} from '../LanguageExtensions/StaticReflect'; import {SymbolMetadata} from '../Symbols/Metadata'; import {RangeOffset} from '../Types/RangeOffset'; import {Mode, ModeID} from './Mode'; +import {Configuration} from '../Configuration'; import {CommandMap} from '../Mappers/Command'; +import {ActionRelativeLineNumbers} from '../Actions/RelativeLineNumbers'; import {ActionMoveCursor} from '../Actions/MoveCursor'; import {ActionPage, PageMoveType} from '../Actions/Page'; import {ActionSelection} from '../Actions/Selection'; @@ -143,6 +145,10 @@ export class ModeVisual extends Mode { super.enter(); ActionSelection.expandToOne(); + + if (Configuration.smartRelativeLineNumbers) { + ActionRelativeLineNumbers.on(); + } } private _recordedCommandMaps: CommandMap[]; diff --git a/src/Modes/VisualLine.ts b/src/Modes/VisualLine.ts index 47dad5f5..f0e15d5b 100644 --- a/src/Modes/VisualLine.ts +++ b/src/Modes/VisualLine.ts @@ -3,7 +3,9 @@ import {StaticReflect} from '../LanguageExtensions/StaticReflect'; import {SymbolMetadata} from '../Symbols/Metadata'; import {RangeOffset} from '../Types/RangeOffset'; import {Mode, ModeID} from './Mode'; +import {Configuration} from '../Configuration'; import {CommandMap} from '../Mappers/Command'; +import {ActionRelativeLineNumbers} from '../Actions/RelativeLineNumbers'; import {ActionMoveCursor} from '../Actions/MoveCursor'; import {ActionPage, PageMoveType} from '../Actions/Page'; import {ActionSelection} from '../Actions/Selection'; @@ -148,6 +150,10 @@ export class ModeVisualLine extends Mode { super.enter(); ActionSelection.expandToLine(); + + if (Configuration.smartRelativeLineNumbers) { + ActionRelativeLineNumbers.on(); + } } private _recordedCommandMaps: CommandMap[];