Skip to content

Commit e191e13

Browse files
committed
Reduce use of activeTextEditor in favor of vimState.editor
Refs #5663
1 parent 8a15cdb commit e191e13

File tree

15 files changed

+61
-93
lines changed

15 files changed

+61
-93
lines changed

src/actions/commands/actions.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ class CommandEsc extends BaseCommand {
417417
}
418418

419419
if (vimState.currentMode === Mode.EasyMotionMode) {
420-
vimState.easyMotion.clearDecorations();
420+
vimState.easyMotion.clearDecorations(vimState.editor);
421421
}
422422

423423
// Abort surround operation
@@ -839,7 +839,7 @@ async function searchCurrentWord(
839839
* Search for the word under the cursor; used by [g]* and [g]# in visual mode when `visualstar` is enabled
840840
*/
841841
async function searchCurrentSelection(vimState: VimState, direction: SearchDirection) {
842-
const selection = TextEditor.getSelection();
842+
const selection = vimState.editor.selection;
843843
const end = new Position(selection.end.line, selection.end.character);
844844
const currentSelection = TextEditor.getText(selection.with(selection.start, end));
845845

@@ -901,7 +901,11 @@ async function createSearchStateAndMoveToMatch(args: {
901901
if (nextMatch) {
902902
vimState.cursorStopPosition = nextMatch.pos;
903903

904-
reportSearch(nextMatch.index, globalState.searchState.getMatchRanges().length, vimState);
904+
reportSearch(
905+
nextMatch.index,
906+
globalState.searchState.getMatchRanges(vimState.editor.document).length,
907+
vimState
908+
);
905909
} else {
906910
StatusBar.displayError(
907911
vimState,
@@ -1657,12 +1661,12 @@ async function selectLastSearchWord(vimState: VimState, direction: SearchDirecti
16571661
vimState.cursorStopPosition = result.end.getLeftThroughLineBreaks(); // end is exclusive
16581662

16591663
// Move the cursor, this is a bit hacky...
1660-
vscode.window.activeTextEditor!.selection = new vscode.Selection(
1664+
vimState.editor.selection = new vscode.Selection(
16611665
vimState.cursorStartPosition,
16621666
vimState.cursorStopPosition
16631667
);
16641668

1665-
reportSearch(result.index, searchState.getMatchRanges().length, vimState);
1669+
reportSearch(result.index, searchState.getMatchRanges(vimState.editor.document).length, vimState);
16661670

16671671
await vimState.setCurrentMode(Mode.Visual);
16681672
}
@@ -1742,7 +1746,7 @@ class CommandOpenFile extends BaseCommand {
17421746
public async exec(position: Position, vimState: VimState): Promise<void> {
17431747
let fullFilePath: string;
17441748
if (vimState.currentMode === Mode.Visual) {
1745-
fullFilePath = TextEditor.getText(TextEditor.getSelection());
1749+
fullFilePath = TextEditor.getText(vimState.editor.selection);
17461750
} else {
17471751
const range = new vscode.Range(
17481752
getWordLeft(position, WordType.FileName, true),

src/actions/commands/commandLine.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CommandTabInCommandline extends BaseCommand {
9191
// File completion by searching if there is a space after the first word/command
9292
// ideally it should be a process of white-listing to selected commands like :e and :vsp
9393
let filePathInCmd = evalCmd.substring(fileRegex.lastIndex);
94-
const currentUri = vscode.window.activeTextEditor!.document.uri;
94+
const currentUri = vimState.editor.document.uri;
9595
const isRemote = !!vscode.env.remoteName;
9696

9797
const { fullDirPath, baseName, partialPath, path: p } = getPathDetails(
@@ -374,7 +374,7 @@ class CommandInsertInSearchMode extends BaseCommand {
374374
globalState.addSearchStateToHistory(searchState);
375375
globalState.hl = true;
376376

377-
if (searchState.getMatchRanges().length === 0) {
377+
if (searchState.getMatchRanges(vimState.editor.document).length === 0) {
378378
StatusBar.displayError(
379379
vimState,
380380
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString)
@@ -408,7 +408,11 @@ class CommandInsertInSearchMode extends BaseCommand {
408408

409409
vimState.cursorStopPosition = nextMatch.pos;
410410

411-
reportSearch(nextMatch.index, searchState.getMatchRanges().length, vimState);
411+
reportSearch(
412+
nextMatch.index,
413+
searchState.getMatchRanges(vimState.editor.document).length,
414+
vimState
415+
);
412416

413417
return;
414418
} else if (key === '<up>' || key === '<C-p>') {

src/actions/commands/insert.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export class CommandDeleteInInsertMode extends BaseCommand {
279279
keys = ['<Del>'];
280280

281281
public async exec(position: Position, vimState: VimState): Promise<void> {
282-
const selection = TextEditor.getSelection();
282+
const selection = vimState.editor.selection;
283283

284284
if (!selection.isEmpty) {
285285
// If a selection is active, delete it
@@ -559,8 +559,8 @@ class CommandNavigateAutocompleteDown extends BaseCommand {
559559
* without this we execute it once per multi cursor, meaning it skips over the autocomplete
560560
* list suggestions
561561
*/
562-
if (vimState.isMultiCursor && vscode.window.activeTextEditor) {
563-
const selection = vscode.window.activeTextEditor.selections[0];
562+
if (vimState.isMultiCursor) {
563+
const selection = vimState.editor.selection;
564564
if (
565565
selection.active.line === position.line &&
566566
selection.active.character === position.character
@@ -585,8 +585,8 @@ class CommandNavigateAutocompleteUp extends BaseCommand {
585585
* without this we execute it once per multi cursor, meaning it skips over the autocomplete
586586
* list suggestions
587587
*/
588-
if (vimState.isMultiCursor && vscode.window.activeTextEditor) {
589-
const selection = vscode.window.activeTextEditor.selections[0];
588+
if (vimState.isMultiCursor) {
589+
const selection = vimState.editor.selection;
590590
if (
591591
selection.active.line === position.line &&
592592
selection.active.character === position.character

src/actions/motion.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class CommandNextSearchMatch extends BaseMovement {
421421
// Turn one of the highlighting flags back on (turned off with :nohl)
422422
globalState.hl = true;
423423

424-
if (searchState.getMatchRanges().length === 0) {
424+
if (searchState.getMatchRanges(vimState.editor.document).length === 0) {
425425
StatusBar.displayError(
426426
vimState,
427427
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString)
@@ -454,7 +454,11 @@ class CommandNextSearchMatch extends BaseMovement {
454454
return position;
455455
}
456456

457-
reportSearch(nextMatch.index, searchState.getMatchRanges().length, vimState);
457+
reportSearch(
458+
nextMatch.index,
459+
searchState.getMatchRanges(vimState.editor.document).length,
460+
vimState
461+
);
458462

459463
return nextMatch.pos;
460464
}
@@ -475,7 +479,7 @@ class CommandPreviousSearchMatch extends BaseMovement {
475479
// Turn one of the highlighting flags back on (turned off with :nohl)
476480
globalState.hl = true;
477481

478-
if (searchState.getMatchRanges().length === 0) {
482+
if (searchState.getMatchRanges(vimState.editor.document).length === 0) {
479483
StatusBar.displayError(
480484
vimState,
481485
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString)
@@ -498,7 +502,11 @@ class CommandPreviousSearchMatch extends BaseMovement {
498502
return position;
499503
}
500504

501-
reportSearch(prevMatch.index, searchState.getMatchRanges().length, vimState);
505+
reportSearch(
506+
prevMatch.index,
507+
searchState.getMatchRanges(vimState.editor.document).length,
508+
vimState
509+
);
502510

503511
return prevMatch.pos;
504512
}

src/actions/plugins/easymotion/easymotion.cmd.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ abstract class BaseEasyMotionCommand extends BaseCommand {
9393
const marker = vimState.easyMotion.markers[0];
9494
// Set cursor position based on marker entered
9595
vimState.cursorStopPosition = marker.position;
96-
vimState.easyMotion.clearDecorations();
96+
vimState.easyMotion.clearDecorations(vimState.editor);
9797
} else {
9898
// Store mode to return to after performing easy motion
9999
vimState.easyMotion.previousMode = vimState.currentMode;
@@ -394,15 +394,15 @@ class MoveEasyMotion extends BaseCommand {
394394
// Only one found, navigate to it
395395
const marker = markers[0];
396396

397-
vimState.easyMotion.clearDecorations();
397+
vimState.easyMotion.clearDecorations(vimState.editor);
398398
// Restore the mode from before easy motion
399399
await vimState.setCurrentMode(vimState.easyMotion.previousMode);
400400

401401
// Set cursor position based on marker entered
402402
vimState.cursorStopPosition = marker.position;
403403
} else if (markers.length === 0) {
404404
// None found, exit mode
405-
vimState.easyMotion.clearDecorations();
405+
vimState.easyMotion.clearDecorations(vimState.editor);
406406
await vimState.setCurrentMode(vimState.easyMotion.previousMode);
407407
}
408408
}

src/actions/plugins/easymotion/easymotion.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ export class EasyMotion {
7575
/**
7676
* Clear all decorations
7777
*/
78-
public clearDecorations() {
79-
const editor = vscode.window.activeTextEditor!;
80-
78+
public clearDecorations(editor: vscode.TextEditor) {
8179
for (let i = 1; i <= this.decorations.length; i++) {
8280
editor.setDecorations(EasyMotion.getDecorationType(i), []);
8381
}
@@ -225,14 +223,13 @@ export class EasyMotion {
225223
return this.getMarkerColor(configuration.easymotionDimColor, '#777777');
226224
}
227225

228-
public updateDecorations() {
229-
this.clearDecorations();
226+
public updateDecorations(editor: vscode.TextEditor) {
227+
this.clearDecorations(editor);
230228

231229
this.visibleMarkers = [];
232230
this.decorations = [];
233231

234232
// Set the decorations for all the different marker lengths
235-
const editor = vscode.window.activeTextEditor!;
236233
const dimmingZones: vscode.DecorationOptions[] = [];
237234
const dimmingRenderOptions: vscode.ThemableDecorationRenderOptions = {
238235
// we update the color here again in case the configuration has changed

src/actions/plugins/sneak.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as vscode from 'vscode';
21
import { VimState } from '../../state/vimState';
32
import { configuration } from './../../configuration/configuration';
43
import { RegisterAction } from './../base';
@@ -29,17 +28,15 @@ export class SneakForward extends BaseMovement {
2928
vimState.lastCommaRepeatableMovement = new SneakBackward(this.keysPressed, true);
3029
}
3130

32-
const editor = vscode.window.activeTextEditor!;
33-
const document = editor.document;
34-
const lineCount = document.lineCount;
35-
3631
if (this.keysPressed[2] === '\n') {
3732
// Single key sneak
3833
this.keysPressed[2] = '';
3934
}
4035

4136
const searchString = this.keysPressed[1] + this.keysPressed[2];
4237

38+
const document = vimState.editor.document;
39+
const lineCount = document.lineCount;
4340
for (let i = position.line; i < lineCount; ++i) {
4441
const lineText = document.lineAt(i).text;
4542

@@ -95,16 +92,14 @@ export class SneakBackward extends BaseMovement {
9592
vimState.lastCommaRepeatableMovement = new SneakForward(this.keysPressed, true);
9693
}
9794

98-
const editor = vscode.window.activeTextEditor!;
99-
const document = editor.document;
100-
10195
if (this.keysPressed[2] === '\n') {
10296
// Single key sneak
10397
this.keysPressed[2] = '';
10498
}
10599

106100
const searchString = this.keysPressed[1] + this.keysPressed[2];
107101

102+
const document = vimState.editor.document;
108103
for (let i = position.line; i >= 0; --i) {
109104
const lineText = document.lineAt(i).text;
110105

src/cmd_line/commandLine.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ class CommandLine {
9191
* Prompts the user for a command using an InputBox, and runs the provided command
9292
*/
9393
public async PromptAndRun(initialText: string, vimState: VimState): Promise<void> {
94-
if (!vscode.window.activeTextEditor) {
95-
this._logger.debug('No active document');
96-
return;
97-
}
98-
let cmd = await vscode.window.showInputBox(this.getInputBoxOptions(initialText));
94+
const cmd = await vscode.window.showInputBox(this.getInputBoxOptions(initialText));
9995
await this.Run(cmd!, vimState);
10096
}
10197

@@ -109,19 +105,12 @@ class CommandLine {
109105
}
110106

111107
public async showHistory(initialText: string): Promise<string | undefined> {
112-
if (!vscode.window.activeTextEditor) {
113-
this._logger.debug('No active document.');
114-
return '';
115-
}
116-
117108
this._history.add(initialText);
118109

119-
let cmd = await vscode.window.showQuickPick(this._history.get().slice().reverse(), {
110+
return vscode.window.showQuickPick(this._history.get().slice().reverse(), {
120111
placeHolder: 'Vim command history',
121112
ignoreFocusOut: false,
122113
});
123-
124-
return cmd;
125114
}
126115
}
127116

src/mode/modeHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ export class ModeHandler implements vscode.Disposable {
15431543

15441544
if (this.currentMode === Mode.EasyMotionMode) {
15451545
// Update all EasyMotion decorations
1546-
this.vimState.easyMotion.updateDecorations();
1546+
this.vimState.easyMotion.updateDecorations(this.vimState.editor);
15471547
}
15481548

15491549
StatusBar.clear(this.vimState, false);

src/state/globalState.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ class GlobalState {
103103
* @returns The SearchState that was selected by the user, if there was one.
104104
*/
105105
public async showSearchHistory(): Promise<SearchState | undefined> {
106-
if (!vscode.window.activeTextEditor) {
107-
return undefined;
108-
}
109-
110106
const items = this._searchStatePrevious
111107
.slice()
112108
.reverse()

0 commit comments

Comments
 (0)