|
| 1 | +import { commands, Range, workspace } from 'vscode'; |
| 2 | +import { Bookmark } from '../bookmark'; |
| 3 | + |
| 4 | +export enum BookmarkSelectionStyle { |
| 5 | + /** |
| 6 | + * Uses VS Code's default behavior: selects from the start of the first line |
| 7 | + * to the start of the last line (exclusive). |
| 8 | + */ |
| 9 | + Default = 'default', |
| 10 | + |
| 11 | + /** |
| 12 | + * Selects entire lines from start to end, including the last line fully. |
| 13 | + */ |
| 14 | + FullLine = 'fullLine', |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Open bookmark. |
| 19 | + */ |
| 20 | +export async function openBookmark( |
| 21 | + bookmark: Bookmark, |
| 22 | + selectionStyle?: BookmarkSelectionStyle, |
| 23 | +): Promise<void> { |
| 24 | + let args: any = undefined; |
| 25 | + |
| 26 | + const targetSelectionStyle = |
| 27 | + selectionStyle ?? |
| 28 | + workspace.getConfiguration().get('bookmarks.selectionStyle', BookmarkSelectionStyle.Default); |
| 29 | + if (targetSelectionStyle === BookmarkSelectionStyle.FullLine) { |
| 30 | + const selection = await getSelection(); |
| 31 | + |
| 32 | + args = { selection }; |
| 33 | + } |
| 34 | + |
| 35 | + await commands.executeCommand('vscode.open', bookmark.uri, args); |
| 36 | + |
| 37 | + async function getSelection() { |
| 38 | + let selection: Range | undefined; |
| 39 | + const document = await workspace |
| 40 | + .openTextDocument(bookmark.uri) |
| 41 | + .then(undefined, () => undefined); |
| 42 | + if (document) { |
| 43 | + const zeroStart = bookmark.start - 1; |
| 44 | + if (bookmark.end === undefined) { |
| 45 | + selection = document.lineAt(zeroStart).range; |
| 46 | + } else { |
| 47 | + const zeroEnd = bookmark.end - 1; |
| 48 | + selection = new Range(zeroStart, 0, zeroEnd, document.lineAt(zeroEnd).range.end.character); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return selection; |
| 53 | + } |
| 54 | +} |
0 commit comments