Skip to content

Commit c986d7f

Browse files
committed
Big refactor of errors
1 parent e3940b3 commit c986d7f

35 files changed

+546
-405
lines changed

src/actions/commands/actions.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { doesFileExist } from 'platform/fs';
55
import { Position } from 'vscode';
66
import { WriteQuitCommand } from '../../cmd_line/commands/writequit';
77
import { Cursor } from '../../common/motion/cursor';
8-
import { ErrorCode, VimError } from '../../error';
8+
import { VimError } from '../../error';
99
import { globalState } from '../../state/globalState';
1010
import { RecordedState } from '../../state/recordedState';
1111
import { VimState } from '../../state/vimState';
@@ -223,7 +223,7 @@ class CommandExecuteLastMacro extends BaseCommand {
223223
replay: 'contentChange',
224224
});
225225
} else {
226-
StatusBar.displayError(vimState, VimError.fromCode(ErrorCode.NoPreviouslyUsedRegister));
226+
StatusBar.displayError(vimState, VimError.NoPreviouslyUsedRegister());
227227
}
228228
}
229229
}
@@ -240,10 +240,7 @@ class CommandExecuteMacro extends BaseCommand {
240240

241241
const isFilenameRegister = register === '%' || register === '#';
242242
if (!Register.isValidRegister(register) || isFilenameRegister) {
243-
StatusBar.displayError(
244-
vimState,
245-
VimError.fromCode(ErrorCode.InvalidRegisterName, `'${register}'`),
246-
);
243+
StatusBar.displayError(vimState, VimError.InvalidRegisterName(register));
247244
}
248245

249246
if (Register.has(register)) {
@@ -526,12 +523,9 @@ export class CommandShowSearchHistory extends BaseCommand {
526523
);
527524

528525
if (!nextMatch) {
529-
throw VimError.fromCode(
530-
this.direction === SearchDirection.Forward
531-
? ErrorCode.SearchHitBottom
532-
: ErrorCode.SearchHitTop,
533-
searchState.searchString,
534-
);
526+
throw this.direction === SearchDirection.Forward
527+
? VimError.SearchHitBottom(searchState.searchString)
528+
: VimError.SearchHitTop(searchState.searchString);
535529
}
536530

537531
vimState.cursorStopPosition = nextMatch.pos;
@@ -884,7 +878,7 @@ class CommandOpenFile extends BaseCommand {
884878
if (workspaceRoot) {
885879
uri = Uri.file(path.join(workspaceRoot.fsPath, pathStr));
886880
if (!(await doesFileExist(uri))) {
887-
throw VimError.fromCode(ErrorCode.CantFindFileInPath, pathStr);
881+
throw VimError.CantFindFileInPath(pathStr);
888882
}
889883
}
890884
}
@@ -2275,7 +2269,7 @@ class ActionGoToAlternateFile extends BaseCommand {
22752269
if (altFile?.text instanceof RecordedState) {
22762270
throw new Error(`# register unexpectedly contained a RecordedState`);
22772271
} else if (altFile === undefined || altFile.text === '') {
2278-
StatusBar.displayError(vimState, VimError.fromCode(ErrorCode.NoAlternateFile));
2272+
StatusBar.displayError(vimState, VimError.NoAlternateFile());
22792273
} else {
22802274
let files: vscode.Uri[];
22812275
if (await doesFileExist(vscode.Uri.file(altFile.text))) {

src/actions/commands/commandLine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22

33
import { CommandLine, ExCommandLine, SearchCommandLine } from '../../cmd_line/commandLine';
44
import { ChangeCommand } from '../../cmd_line/commands/change';
5-
import { ErrorCode, VimError } from '../../error';
5+
import { VimError } from '../../error';
66
import { Mode } from '../../mode/mode';
77
import { Register, RegisterMode } from '../../register/register';
88
import { RecordedState } from '../../state/recordedState';
@@ -305,7 +305,7 @@ class CommandInsertRegisterContentInCommandLine extends CommandLineAction {
305305
if (register === undefined) {
306306
StatusBar.displayError(
307307
vimState,
308-
VimError.fromCode(ErrorCode.NothingInRegister, vimState.recordedState.registerName),
308+
VimError.NothingInRegister(vimState.recordedState.registerName),
309309
);
310310
return;
311311
}

src/actions/commands/insert.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22

33
import { Position } from 'vscode';
44
import { lineCompletionProvider } from '../../completion/lineCompletionProvider';
5-
import { ErrorCode, VimError } from '../../error';
5+
import { VimError } from '../../error';
66
import { RecordedState } from '../../state/recordedState';
77
import { VimState } from '../../state/vimState';
88
import { StatusBar } from '../../statusBar';
@@ -131,7 +131,7 @@ export class CommandInsertPreviousText extends BaseCommand {
131131
!(register.text instanceof RecordedState) ||
132132
!register.text.actionsRun
133133
) {
134-
throw VimError.fromCode(ErrorCode.NoInsertedTextYet);
134+
throw VimError.NoInsertedTextYet();
135135
}
136136

137137
const recordedState = register.text.clone();
@@ -343,7 +343,7 @@ class CommandInsertRegisterContent extends BaseCommand {
343343

344344
const register = await Register.get(registerKey, this.multicursorIndex);
345345
if (register === undefined) {
346-
StatusBar.displayError(vimState, VimError.fromCode(ErrorCode.NothingInRegister, registerKey));
346+
StatusBar.displayError(vimState, VimError.NothingInRegister(registerKey));
347347
return;
348348
}
349349

src/actions/commands/put.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { TextEditor } from '../../textEditor';
1010
import { reportLinesChanged } from '../../util/statusBarTextUtils';
1111
import { BaseCommand, RegisterAction } from '../base';
1212
import { StatusBar } from '../../statusBar';
13-
import { VimError, ErrorCode } from '../../error';
13+
import { VimError } from '../../error';
1414
import { Cursor } from '../../common/motion/cursor';
1515
import { Transformation } from '../../transformations/transformations';
1616

@@ -39,7 +39,7 @@ abstract class BasePutCommand extends BaseCommand {
3939
if (register === undefined) {
4040
StatusBar.displayError(
4141
vimState,
42-
VimError.fromCode(ErrorCode.NothingInRegister, vimState.recordedState.registerName),
42+
VimError.NothingInRegister(vimState.recordedState.registerName),
4343
);
4444
return;
4545
}

src/actions/commands/search.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Position, Selection } from 'vscode';
44
import { SearchCommandLine } from '../../cmd_line/commandLine';
55
import { sorted } from '../../common/motion/position';
66
import { configuration } from '../../configuration/configuration';
7-
import { ErrorCode, VimError } from '../../error';
7+
import { VimError } from '../../error';
88
import { Mode, isVisualMode } from '../../mode/mode';
99
import { Register } from '../../register/register';
1010
import { globalState } from '../../state/globalState';
@@ -55,7 +55,7 @@ async function searchCurrentWord(
5555
searchStartCursorPosition,
5656
});
5757
} else {
58-
StatusBar.displayError(vimState, VimError.fromCode(ErrorCode.NoStringUnderCursor));
58+
StatusBar.displayError(vimState, VimError.NoStringUnderCursor());
5959
}
6060
}
6161

@@ -130,12 +130,9 @@ async function createSearchStateAndMoveToMatch(args: {
130130
} else {
131131
StatusBar.displayError(
132132
vimState,
133-
VimError.fromCode(
134-
args.direction === SearchDirection.Forward
135-
? ErrorCode.SearchHitBottom
136-
: ErrorCode.SearchHitTop,
137-
globalState.searchState.searchString,
138-
),
133+
args.direction === SearchDirection.Forward
134+
? VimError.SearchHitBottom(globalState.searchState.searchString)
135+
: VimError.SearchHitTop(globalState.searchState.searchString),
139136
);
140137
}
141138
}

src/actions/motion.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { TagMatcher } from './../common/matching/tagMatcher';
1111
import { VimState } from './../state/vimState';
1212
import { configuration } from './../configuration/configuration';
1313
import { shouldWrapKey } from './wrapping';
14-
import { VimError, ErrorCode } from '../error';
14+
import { VimError } from '../error';
1515
import { BaseMovement, SelectionType, IMovement, isIMovement, failedMovement } from './baseMotion';
1616
import { globalState } from '../state/globalState';
1717
import { reportSearch } from '../util/statusBarTextUtils';
@@ -468,10 +468,7 @@ class CommandNextSearchMatch extends BaseMovement {
468468
globalState.hl = true;
469469

470470
if (searchState.getMatchRanges(vimState).length === 0) {
471-
StatusBar.displayError(
472-
vimState,
473-
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString),
474-
);
471+
StatusBar.displayError(vimState, VimError.PatternNotFound(searchState.searchString));
475472
return failedMovement(vimState);
476473
}
477474

@@ -488,12 +485,9 @@ class CommandNextSearchMatch extends BaseMovement {
488485
if (!nextMatch) {
489486
StatusBar.displayError(
490487
vimState,
491-
VimError.fromCode(
492-
searchState.direction === SearchDirection.Forward
493-
? ErrorCode.SearchHitBottom
494-
: ErrorCode.SearchHitTop,
495-
searchState.searchString,
496-
),
488+
searchState.direction === SearchDirection.Forward
489+
? VimError.SearchHitBottom(searchState.searchString)
490+
: VimError.SearchHitTop(searchState.searchString),
497491
);
498492
return failedMovement(vimState);
499493
}
@@ -523,10 +517,7 @@ class CommandPreviousSearchMatch extends BaseMovement {
523517
globalState.hl = true;
524518

525519
if (searchState.getMatchRanges(vimState).length === 0) {
526-
StatusBar.displayError(
527-
vimState,
528-
VimError.fromCode(ErrorCode.PatternNotFound, searchState.searchString),
529-
);
520+
StatusBar.displayError(vimState, VimError.PatternNotFound(searchState.searchString));
530521
return failedMovement(vimState);
531522
}
532523

@@ -546,12 +537,9 @@ class CommandPreviousSearchMatch extends BaseMovement {
546537
if (!prevMatch) {
547538
StatusBar.displayError(
548539
vimState,
549-
VimError.fromCode(
550-
searchState.direction === SearchDirection.Forward
551-
? ErrorCode.SearchHitTop
552-
: ErrorCode.SearchHitBottom,
553-
searchState.searchString,
554-
),
540+
searchState.direction === SearchDirection.Forward
541+
? VimError.SearchHitTop(searchState.searchString)
542+
: VimError.SearchHitBottom(searchState.searchString),
555543
);
556544
return failedMovement(vimState);
557545
}
@@ -576,15 +564,15 @@ export abstract class BaseMarkMovement extends BaseMovement {
576564
const mark = vimState.historyTracker.getMark(markName);
577565

578566
if (mark === undefined) {
579-
throw VimError.fromCode(ErrorCode.MarkNotSet);
567+
throw VimError.MarkNotSet();
580568
}
581569

582570
if (
583571
mark.isUppercaseMark &&
584572
vimState.recordedState.operator &&
585573
mark.document !== vimState.document
586574
) {
587-
throw VimError.fromCode(ErrorCode.MarkNotSet);
575+
throw VimError.MarkNotSet();
588576
}
589577

590578
if (this.registerMode) {

src/actions/plugins/replaceWithRegister.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { VimState } from '../../state/vimState';
55
import { BaseOperator } from '../operator';
66
import { RegisterAction } from './../base';
77
import { StatusBar } from '../../statusBar';
8-
import { VimError, ErrorCode } from '../../error';
8+
import { VimError } from '../../error';
99
import { Position, Range } from 'vscode';
1010
import { PositionDiff } from '../../common/motion/position';
1111

@@ -32,7 +32,7 @@ class ReplaceOperator extends BaseOperator {
3232
if (register === undefined) {
3333
StatusBar.displayError(
3434
vimState,
35-
VimError.fromCode(ErrorCode.NothingInRegister, vimState.recordedState.registerName),
35+
VimError.NothingInRegister(vimState.recordedState.registerName),
3636
);
3737
return;
3838
}

src/cmd_line/commandLine.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ export class SearchCommandLine extends CommandLine {
480480
globalState.hl = true;
481481

482482
if (this.searchState.getMatchRanges(vimState).length === 0) {
483-
StatusBar.displayError(vimState, VimError.fromCode(ErrorCode.PatternNotFound, this.text));
483+
StatusBar.displayError(vimState, VimError.PatternNotFound(this.text));
484484
return;
485485
}
486486

@@ -489,12 +489,9 @@ export class SearchCommandLine extends CommandLine {
489489
if (currentMatch === undefined) {
490490
StatusBar.displayError(
491491
vimState,
492-
VimError.fromCode(
493-
this.searchState.direction === SearchDirection.Backward
494-
? ErrorCode.SearchHitTop
495-
: ErrorCode.SearchHitBottom,
496-
this.text,
497-
),
492+
this.searchState.direction === SearchDirection.Backward
493+
? VimError.SearchHitTop(this.text)
494+
: VimError.SearchHitBottom(this.text),
498495
);
499496
return;
500497
}

src/cmd_line/commands/bufferDelete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { alt, optWhitespace, Parser, seq, whitespace } from 'parsimmon';
22
import * as vscode from 'vscode';
33

4-
import * as error from '../../error';
4+
import { VimError } from '../../error';
55
import { VimState } from '../../state/vimState';
66
import { StatusBar } from '../../statusBar';
77
import { ExCommand } from '../../vimscript/exCommand';
@@ -30,7 +30,7 @@ export class BufferDeleteCommand extends ExCommand {
3030

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

3636
if (this.arguments.buffers.length === 0) {
@@ -50,7 +50,7 @@ export class BufferDeleteCommand extends ExCommand {
5050
try {
5151
await vscode.commands.executeCommand(`workbench.action.openEditorAtIndex${buffer}`);
5252
} catch (e) {
53-
throw error.VimError.fromCode(error.ErrorCode.NoBuffersDeleted);
53+
throw VimError.NoBuffersDeleted();
5454
}
5555

5656
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');

src/cmd_line/commands/close.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Parser } from 'parsimmon';
22
import * as vscode from 'vscode';
33

4-
import * as error from '../../error';
4+
import { VimError } from '../../error';
55
import { VimState } from '../../state/vimState';
66
import { ExCommand } from '../../vimscript/exCommand';
77
import { bangParser } from '../../vimscript/parserUtils';
@@ -23,11 +23,11 @@ export class CloseCommand extends ExCommand {
2323

2424
async execute(vimState: VimState): Promise<void> {
2525
if (vimState.document.isDirty && !this.bang) {
26-
throw error.VimError.fromCode(error.ErrorCode.NoWriteSinceLastChange);
26+
throw VimError.NoWriteSinceLastChange();
2727
}
2828

2929
if (vscode.window.visibleTextEditors.length === 1) {
30-
throw error.VimError.fromCode(error.ErrorCode.CannotCloseLastWindow);
30+
throw VimError.CannotCloseLastWindow();
3131
}
3232

3333
const oldViewColumn = vimState.editor.viewColumn;

0 commit comments

Comments
 (0)