Skip to content

Commit c6b4cec

Browse files
committed
Add vimState.document getter
Just removing a bit of boilerplate which will make ripping out unnecessary TextEditor methods slightly less painful. Refs #5663
1 parent e191e13 commit c6b4cec

File tree

21 files changed

+62
-71
lines changed

21 files changed

+62
-71
lines changed

extensionBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export async function activate(
203203
if (modeHandler == null || modeHandler.vimState.editor === undefined) {
204204
shouldDelete = true;
205205
} else {
206-
const document = modeHandler.vimState.editor.document;
206+
const document = modeHandler.vimState.document;
207207
if (!documents.includes(document)) {
208208
shouldDelete = true;
209209
if (closedDocument === document) {

src/actions/commands/actions.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ abstract class CommandScrollAndMoveCursor extends BaseCommand {
577577
const newPositionLine = clamp(
578578
position.line + (this.to === 'down' ? moveLines : -moveLines),
579579
0,
580-
vimState.editor.document.lineCount - 1
580+
vimState.document.lineCount - 1
581581
);
582582
vimState.cursorStopPosition = new Position(
583583
newPositionLine,
@@ -747,13 +747,13 @@ class CommandOverrideCopy extends BaseCommand {
747747
text = vimState.cursors
748748
.map((range) => {
749749
const [start, stop] = sorted(range.start, range.stop);
750-
return vimState.editor.document.getText(new vscode.Range(start, stop.getRight()));
750+
return vimState.document.getText(new vscode.Range(start, stop.getRight()));
751751
})
752752
.join('\n');
753753
} else if (vimState.currentMode === Mode.VisualLine) {
754754
text = vimState.cursors
755755
.map((range) => {
756-
return vimState.editor.document.getText(
756+
return vimState.document.getText(
757757
new vscode.Range(
758758
earlierOf(range.start.getLineBegin(), range.stop.getLineBegin()),
759759
laterOf(range.start.getLineEnd(), range.stop.getLineEnd())
@@ -768,7 +768,7 @@ class CommandOverrideCopy extends BaseCommand {
768768
} else if (vimState.currentMode === Mode.Insert || vimState.currentMode === Mode.Normal) {
769769
text = vimState.editor.selections
770770
.map((selection) => {
771-
return vimState.editor.document.getText(new vscode.Range(selection.start, selection.end));
771+
return vimState.document.getText(new vscode.Range(selection.start, selection.end));
772772
})
773773
.join('\n');
774774
}
@@ -903,7 +903,7 @@ async function createSearchStateAndMoveToMatch(args: {
903903

904904
reportSearch(
905905
nextMatch.index,
906-
globalState.searchState.getMatchRanges(vimState.editor.document).length,
906+
globalState.searchState.getMatchRanges(vimState.document).length,
907907
vimState
908908
);
909909
} else {
@@ -1666,7 +1666,7 @@ async function selectLastSearchWord(vimState: VimState, direction: SearchDirecti
16661666
vimState.cursorStopPosition
16671667
);
16681668

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

16711671
await vimState.setCurrentMode(Mode.Visual);
16721672
}
@@ -2084,7 +2084,7 @@ class CommandNavigateLastBOL extends BaseCommand {
20842084
}
20852085
const jump = new Jump({
20862086
editor: vimState.editor,
2087-
fileName: vimState.editor.document.fileName,
2087+
fileName: vimState.document.fileName,
20882088
position: lastJump.position.getLineBegin(),
20892089
});
20902090
globalState.jumpTracker.recordJump(Jump.fromStateNow(vimState), jump);
@@ -3358,7 +3358,7 @@ class CommandUnicodeName extends BaseCommand {
33583358
}
33593359

33603360
public async exec(position: Position, vimState: VimState): Promise<void> {
3361-
const char = vimState.editor.document.getText(new vscode.Range(position, position.getRight()));
3361+
const char = vimState.document.getText(new vscode.Range(position, position.getRight()));
33623362
const charCode = char.charCodeAt(0);
33633363
// TODO: Handle charCode > 127 by also including <M-x>
33643364
StatusBar.setText(

src/actions/commands/commandLine.ts

Lines changed: 3 additions & 7 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 = vimState.editor.document.uri;
94+
const currentUri = vimState.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(vimState.editor.document).length === 0) {
377+
if (searchState.getMatchRanges(vimState.document).length === 0) {
378378
StatusBar.displayError(
379379
vimState,
380380
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString)
@@ -408,11 +408,7 @@ class CommandInsertInSearchMode extends BaseCommand {
408408

409409
vimState.cursorStopPosition = nextMatch.pos;
410410

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

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

src/actions/commands/insert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CommandEscInsertMode extends BaseCommand {
4949
const lastActionBeforeEsc = vimState.keyHistory[vimState.keyHistory.length - 2];
5050
if (
5151
['o', 'O', '\n'].includes(lastActionBeforeEsc) &&
52-
vimState.editor.document.languageId !== 'plaintext' &&
52+
vimState.document.languageId !== 'plaintext' &&
5353
/^\s+$/.test(TextEditor.getLineAt(vimState.cursors[i].stop).text)
5454
) {
5555
vimState.recordedState.transformer.addTransformation({

src/actions/motion.ts

Lines changed: 4 additions & 12 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(vimState.editor.document).length === 0) {
424+
if (searchState.getMatchRanges(vimState.document).length === 0) {
425425
StatusBar.displayError(
426426
vimState,
427427
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString)
@@ -454,11 +454,7 @@ class CommandNextSearchMatch extends BaseMovement {
454454
return position;
455455
}
456456

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

463459
return nextMatch.pos;
464460
}
@@ -479,7 +475,7 @@ class CommandPreviousSearchMatch extends BaseMovement {
479475
// Turn one of the highlighting flags back on (turned off with :nohl)
480476
globalState.hl = true;
481477

482-
if (searchState.getMatchRanges(vimState.editor.document).length === 0) {
478+
if (searchState.getMatchRanges(vimState.document).length === 0) {
483479
StatusBar.displayError(
484480
vimState,
485481
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString)
@@ -502,11 +498,7 @@ class CommandPreviousSearchMatch extends BaseMovement {
502498
return position;
503499
}
504500

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

511503
return prevMatch.pos;
512504
}

src/actions/operator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class DeleteOperator extends BaseOperator {
144144
}
145145
}
146146

147-
let text = vimState.editor.document.getText(new vscode.Range(start, end));
147+
let text = vimState.document.getText(new vscode.Range(start, end));
148148

149149
// If we delete linewise to the final line of the document, we expect the line
150150
// to be removed. This is actually a special case because the newline
@@ -415,7 +415,7 @@ export class UpperCaseOperator extends BaseOperator {
415415

416416
public async run(vimState: VimState, start: Position, end: Position): Promise<void> {
417417
const range = new vscode.Range(start, new Position(end.line, end.character + 1));
418-
let text = vimState.editor.document.getText(range);
418+
let text = vimState.document.getText(range);
419419

420420
await TextEditor.replace(range, text.toUpperCase());
421421

@@ -438,7 +438,7 @@ class UpperCaseVisualBlockOperator extends BaseOperator {
438438
public async run(vimState: VimState, startPos: Position, endPos: Position): Promise<void> {
439439
for (const { start, end } of TextEditor.iterateLinesInBlock(vimState)) {
440440
const range = new vscode.Range(start, end);
441-
let text = vimState.editor.document.getText(range);
441+
let text = vimState.document.getText(range);
442442
await TextEditor.replace(range, text.toUpperCase());
443443
}
444444

@@ -456,7 +456,7 @@ export class LowerCaseOperator extends BaseOperator {
456456

457457
public async run(vimState: VimState, start: Position, end: Position): Promise<void> {
458458
const range = new vscode.Range(start, new Position(end.line, end.character + 1));
459-
let text = vimState.editor.document.getText(range);
459+
let text = vimState.document.getText(range);
460460

461461
await TextEditor.replace(range, text.toLowerCase());
462462

@@ -479,7 +479,7 @@ class LowerCaseVisualBlockOperator extends BaseOperator {
479479
public async run(vimState: VimState, startPos: Position, endPos: Position): Promise<void> {
480480
for (const { start, end } of TextEditor.iterateLinesInBlock(vimState)) {
481481
const range = new vscode.Range(start, end);
482-
let text = vimState.editor.document.getText(range);
482+
let text = vimState.document.getText(range);
483483
await TextEditor.replace(range, text.toLowerCase());
484484
}
485485

@@ -633,7 +633,7 @@ export class ChangeOperator extends BaseOperator {
633633
}
634634

635635
public async runRepeat(vimState: VimState, position: Position, count: number): Promise<void> {
636-
const thisLineIndent = vimState.editor.document.getText(
636+
const thisLineIndent = vimState.document.getText(
637637
new vscode.Range(position.getLineBegin(), position.getLineBeginRespectingIndent())
638638
);
639639

@@ -646,7 +646,7 @@ export class ChangeOperator extends BaseOperator {
646646
);
647647

648648
if (configuration.autoindent) {
649-
if (vimState.editor.document.languageId === 'plaintext') {
649+
if (vimState.document.languageId === 'plaintext') {
650650
vimState.recordedState.transformer.addTransformation({
651651
type: 'insertText',
652652
text: thisLineIndent,

src/actions/plugins/sneak.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class SneakForward extends BaseMovement {
3535

3636
const searchString = this.keysPressed[1] + this.keysPressed[2];
3737

38-
const document = vimState.editor.document;
38+
const document = vimState.document;
3939
const lineCount = document.lineCount;
4040
for (let i = position.line; i < lineCount; ++i) {
4141
const lineText = document.lineAt(i).text;
@@ -99,7 +99,7 @@ export class SneakBackward extends BaseMovement {
9999

100100
const searchString = this.keysPressed[1] + this.keysPressed[2];
101101

102-
const document = vimState.editor.document;
102+
const document = vimState.document;
103103
for (let i = position.line; i >= 0; --i) {
104104
const lineText = document.lineAt(i).text;
105105

src/cmd_line/commands/close.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CloseCommand extends node.CommandBase {
2727
}
2828

2929
async execute(vimState: VimState): Promise<void> {
30-
if (vimState.editor.document.isDirty && !this.arguments.bang) {
30+
if (vimState.document.isDirty && !this.arguments.bang) {
3131
throw error.VimError.fromCode(error.ErrorCode.NoWriteSinceLastChange);
3232
}
3333

src/cmd_line/commands/deleteRange.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ export class DeleteRangeCommand extends node.CommandBase {
3737
let start = new Position(startLine, 0);
3838
let end = new Position(endLine, 0).getLineEndIncludingEOL();
3939

40-
if (endLine < vimState.editor.document.lineCount - 1) {
40+
if (endLine < vimState.document.lineCount - 1) {
4141
end = end.getRightThroughLineBreaks();
4242
} else if (startLine > 0) {
4343
start = start.getLeftThroughLineBreaks();
4444
}
4545

4646
const range = new vscode.Range(start, end);
47-
const text = vimState.editor.document
47+
const text = vimState.document
4848
.getText(range)
4949
// Remove leading or trailing newline
5050
.replace(/^\r?\n/, '')

src/cmd_line/commands/quit.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ export class QuitCommand extends node.CommandBase {
2929
async execute(vimState: VimState): Promise<void> {
3030
// NOTE: We can't currently get all open text editors, so this isn't perfect. See #3809
3131
const duplicatedInSplit =
32-
vscode.window.visibleTextEditors.filter(
33-
(editor) => editor.document === vimState.editor.document
34-
).length > 1;
32+
vscode.window.visibleTextEditors.filter((editor) => editor.document === vimState.document)
33+
.length > 1;
3534
if (
36-
vimState.editor.document.isDirty &&
35+
vimState.document.isDirty &&
3736
!this.arguments.bang &&
3837
(!duplicatedInSplit || this._arguments.quitAll)
3938
) {

0 commit comments

Comments
 (0)