Skip to content

Commit e819f6f

Browse files
committed
Unsuccessful gf throws error
1 parent 410ba01 commit e819f6f

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/actions/commands/actions.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,36 +1029,35 @@ class CommandOpenFile extends BaseCommand {
10291029

10301030
const fileInfo = fullFilePath.match(/(.*?(?=:[0-9]+)|.*):?([0-9]*)$/);
10311031
if (fileInfo) {
1032-
const fileUri = await (async () => {
1033-
if (path.isAbsolute(fileInfo[1])) {
1034-
return Uri.file(fileInfo[1]);
1032+
const fileUri: Uri = await (async () => {
1033+
const pathStr = fileInfo[1];
1034+
if (path.isAbsolute(pathStr)) {
1035+
return Uri.file(pathStr);
10351036
} else {
1036-
let uri = Uri.file(path.resolve(path.dirname(vimState.document.uri.fsPath), fileInfo[1]));
1037+
let uri = Uri.file(path.resolve(path.dirname(vimState.document.uri.fsPath), pathStr));
10371038
if (!(await doesFileExist(uri))) {
10381039
const workspaceRoot = vscode.workspace.getWorkspaceFolder(vimState.document.uri)?.uri;
10391040
if (workspaceRoot) {
1040-
uri = Uri.file(path.join(workspaceRoot.fsPath, fileInfo[1]));
1041+
uri = Uri.file(path.join(workspaceRoot.fsPath, pathStr));
10411042
if (!(await doesFileExist(uri))) {
1042-
return undefined;
1043+
throw VimError.fromCode(ErrorCode.CantFindFileInPath, pathStr);
10431044
}
10441045
}
10451046
}
10461047
return uri;
10471048
}
10481049
})();
10491050

1050-
if (fileUri) {
1051-
const line = parseInt(fileInfo[2], 10);
1052-
const fileCommand = new FileCommand({
1053-
name: 'edit',
1054-
bang: false,
1055-
opt: [],
1056-
file: fileUri.fsPath,
1057-
cmd: isNaN(line) ? undefined : { type: 'line_number', line: line - 1 },
1058-
createFileIfNotExists: false,
1059-
});
1060-
void fileCommand.execute(vimState);
1061-
}
1051+
const line = parseInt(fileInfo[2], 10);
1052+
const fileCommand = new FileCommand({
1053+
name: 'edit',
1054+
bang: false,
1055+
opt: [],
1056+
file: fileUri.fsPath,
1057+
cmd: isNaN(line) ? undefined : { type: 'line_number', line: line - 1 },
1058+
createFileIfNotExists: false,
1059+
});
1060+
void fileCommand.execute(vimState);
10621061
}
10631062
}
10641063
}

src/error.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export enum ErrorCode {
3030
SearchHitTop = 384,
3131
SearchHitBottom = 385,
3232
CannotCloseLastWindow = 444,
33+
CantFindFileInPath = 447,
3334
ArgumentRequired = 471,
3435
InvalidArgument474 = 474,
3536
InvalidArgument475 = 475,
@@ -111,6 +112,7 @@ export const ErrorMessage: IErrorMessage = {
111112
384: 'Search hit TOP without match for',
112113
385: 'Search hit BOTTOM without match for',
113114
444: 'Cannot close last window',
115+
447: 'Can\'t find file "{FILE_NAME}" in path',
114116
471: 'Argument required',
115117
474: 'Invalid argument',
116118
475: 'Invalid argument',
@@ -175,17 +177,23 @@ export class VimError extends Error {
175177
}
176178

177179
static fromCode(code: ErrorCode, extraValue?: string): VimError {
178-
if (ErrorMessage[code]) {
180+
let message = ErrorMessage[code];
181+
if (message) {
182+
// TODO: Come up with a more elegant solution
179183
if (extraValue) {
180184
if (code === ErrorCode.NothingInRegister) {
181185
extraValue = ` ${extraValue}`;
182186
} else if (code === ErrorCode.NoMatchingBuffer || code === ErrorCode.MultipleMatches) {
183187
extraValue = ` for ${extraValue}`;
188+
} else if (code === ErrorCode.CantFindFileInPath) {
189+
message = message.replace('{FILE_NAME}', extraValue);
190+
extraValue = '';
184191
} else {
185192
extraValue = `: ${extraValue}`;
186193
}
187194
}
188-
return new VimError(code, ErrorMessage[code] + (extraValue ?? ''));
195+
196+
return new VimError(code, message + (extraValue ?? ''));
189197
}
190198

191199
throw new Error('unknown error code: ' + code);

0 commit comments

Comments
 (0)