| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Repository} from 'isl-server/src/Repository'; |
| b69ab31 | | | 9 | import type {AbsolutePath, RepoRelativePath} from 'isl/src/types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import * as pathModule from 'node:path'; |
| b69ab31 | | | 12 | import * as vscode from 'vscode'; |
| b69ab31 | | | 13 | import {shouldOpenBeside} from './config'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | const IMAGE_EXTENSIONS = new Set(['.bmp', '.gif', '.ico', '.jpeg', '.jpg', '.png', '.webp']); |
| b69ab31 | | | 16 | function looksLikeImageUri(uri: vscode.Uri): boolean { |
| b69ab31 | | | 17 | const ext = pathModule.extname(uri.path).toLowerCase(); |
| b69ab31 | | | 18 | return IMAGE_EXTENSIONS.has(ext); |
| b69ab31 | | | 19 | } |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | /** |
| b69ab31 | | | 22 | * Opens a file in the editor within a provided repository, optionally at a specific line number. |
| b69ab31 | | | 23 | * The file path should be relative to the repository root. |
| b69ab31 | | | 24 | */ |
| b69ab31 | | | 25 | export function openFileInRepo( |
| b69ab31 | | | 26 | repo: Repository, |
| b69ab31 | | | 27 | filePath: RepoRelativePath, |
| b69ab31 | | | 28 | line?: number, |
| b69ab31 | | | 29 | preview?: boolean, |
| b69ab31 | | | 30 | onError?: (err: Error) => void, |
| b69ab31 | | | 31 | onOpened?: (editor: vscode.TextEditor) => void, |
| b69ab31 | | | 32 | disableScroll: boolean = false, |
| b69ab31 | | | 33 | ) { |
| b69ab31 | | | 34 | const path: AbsolutePath = pathModule.join(repo.info.repoRoot, filePath); |
| b69ab31 | | | 35 | openFileImpl(path, line, preview, onError, onOpened, disableScroll); |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | /** |
| b69ab31 | | | 39 | * Opens a file in the editor, optionally at a specific line number. |
| b69ab31 | | | 40 | * The file path should be absolute. |
| b69ab31 | | | 41 | */ |
| b69ab31 | | | 42 | export function openFile( |
| b69ab31 | | | 43 | filePath: AbsolutePath, |
| b69ab31 | | | 44 | line?: number, |
| b69ab31 | | | 45 | preview?: boolean, |
| b69ab31 | | | 46 | onError?: (err: Error) => void, |
| b69ab31 | | | 47 | onOpened?: (editor: vscode.TextEditor) => void, |
| b69ab31 | | | 48 | disableScroll: boolean = false, |
| b69ab31 | | | 49 | ): void { |
| b69ab31 | | | 50 | openFileImpl(filePath, line, preview, onError, onOpened, disableScroll); |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | function openFileImpl( |
| b69ab31 | | | 54 | filePath: AbsolutePath, |
| b69ab31 | | | 55 | line?: number, |
| b69ab31 | | | 56 | preview?: boolean, |
| b69ab31 | | | 57 | onError?: (err: Error) => void, |
| b69ab31 | | | 58 | onOpened?: (editor: vscode.TextEditor) => void, |
| b69ab31 | | | 59 | disableScroll: boolean = false, |
| b69ab31 | | | 60 | ): void { |
| b69ab31 | | | 61 | const uri = vscode.Uri.file(filePath); |
| b69ab31 | | | 62 | if (looksLikeImageUri(uri)) { |
| b69ab31 | | | 63 | vscode.commands.executeCommand('vscode.open', uri).then(undefined, err => { |
| b69ab31 | | | 64 | vscode.window.showErrorMessage('cannot open file' + (err.message ?? String(err))); |
| b69ab31 | | | 65 | }); |
| b69ab31 | | | 66 | return; |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | vscode.window |
| b69ab31 | | | 69 | .showTextDocument(uri, { |
| b69ab31 | | | 70 | preview, |
| b69ab31 | | | 71 | viewColumn: shouldOpenBeside() ? vscode.ViewColumn.Beside : undefined, |
| b69ab31 | | | 72 | }) |
| b69ab31 | | | 73 | .then( |
| b69ab31 | | | 74 | editor => { |
| b69ab31 | | | 75 | if (!disableScroll && line != null) { |
| b69ab31 | | | 76 | const lineZeroIndexed = line - 1; // vscode uses 0-indexed line numbers |
| b69ab31 | | | 77 | editor.selections = [new vscode.Selection(lineZeroIndexed, 0, lineZeroIndexed, 0)]; // move cursor to line |
| b69ab31 | | | 78 | editor.revealRange( |
| b69ab31 | | | 79 | new vscode.Range(lineZeroIndexed, 0, lineZeroIndexed, 0), |
| b69ab31 | | | 80 | vscode.TextEditorRevealType.InCenterIfOutsideViewport, |
| b69ab31 | | | 81 | ); // scroll to line |
| b69ab31 | | | 82 | } |
| b69ab31 | | | 83 | onOpened?.(editor); |
| b69ab31 | | | 84 | }, |
| b69ab31 | | | 85 | err => { |
| b69ab31 | | | 86 | if (onError) { |
| b69ab31 | | | 87 | onError(err); |
| b69ab31 | | | 88 | } else { |
| b69ab31 | | | 89 | vscode.window.showErrorMessage(err.message ?? String(err)); |
| b69ab31 | | | 90 | } |
| b69ab31 | | | 91 | }, |
| b69ab31 | | | 92 | ); |
| b69ab31 | | | 93 | } |