|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { getCurrentWorkspaceRoot } from '@zardoy/vscode-utils/build/fs' |
| 3 | +import { extensionCtx, registerActiveDevelopmentCommand, registerExtensionCommand, showQuickPick } from 'vscode-framework' |
| 4 | +import { omitObj } from '@zardoy/utils' |
| 5 | +import { GitRepository, getGitActiveRepoOrThrow } from '../git-api' |
| 6 | + |
| 7 | +interface Location { |
| 8 | + file: string |
| 9 | + line: number |
| 10 | + char?: number |
| 11 | + commitHash?: string |
| 12 | +} |
| 13 | + |
| 14 | +interface BreakpointLocations extends Location { |
| 15 | + breakpoint: Omit<vscode.Breakpoint, 'id'> |
| 16 | +} |
| 17 | + |
| 18 | +interface RepoLocations { |
| 19 | + [location: string]: { |
| 20 | + breakpointsPacks: { |
| 21 | + [name: string]: BreakpointLocations[] |
| 22 | + } |
| 23 | + bookmarks: { |
| 24 | + [name: string]: Location |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const getPicks = (locations: { [name: string]: BreakpointLocations[] }) => { |
| 30 | + const breakpointPacks = Object.keys(locations) |
| 31 | + return breakpointPacks.map(breakpointPack => ({ |
| 32 | + label: breakpointPack, |
| 33 | + value: breakpointPack, |
| 34 | + description: `${+locations[breakpointPack]!.length} breakpoints`, |
| 35 | + })) |
| 36 | +} |
| 37 | + |
| 38 | +export default () => { |
| 39 | + extensionCtx.globalState.setKeysForSync(['repoLocations']) |
| 40 | + const repoLocations = extensionCtx.globalState.get<RepoLocations>('repoLocations') ?? ({} as RepoLocations) |
| 41 | + const saveRepoLocations = () => extensionCtx.globalState.update('repoLocations', repoLocations) |
| 42 | + |
| 43 | + const getRepoLocationsKey = () => { |
| 44 | + let repo: GitRepository | undefined |
| 45 | + try { |
| 46 | + repo = getGitActiveRepoOrThrow() |
| 47 | + } catch (error) { |
| 48 | + console.warn(error) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + const originUrl = repo?.state.remotes.find(remote => remote.name === 'origin')?.fetchUrl |
| 53 | + return originUrl |
| 54 | + } |
| 55 | + |
| 56 | + const getRepoLocations = () => { |
| 57 | + const key = getRepoLocationsKey() ?? `local:${getCurrentWorkspaceRoot().name}` |
| 58 | + // eslint-disable-next-line no-return-assign |
| 59 | + return repoLocations[key] ?? (repoLocations[key] = { breakpointsPacks: {}, bookmarks: {} }) |
| 60 | + } |
| 61 | + |
| 62 | + registerExtensionCommand('restoreBreakpointsPack', async () => { |
| 63 | + const locations = getRepoLocations().breakpointsPacks |
| 64 | + const selected = await showQuickPick(getPicks(locations)) |
| 65 | + if (!selected) return |
| 66 | + const breakpoints = locations[selected]! |
| 67 | + const newBreakpoints = breakpoints.map(location => { |
| 68 | + const breakpoint = new vscode.SourceBreakpoint( |
| 69 | + new vscode.Location(vscode.Uri.joinPath(getCurrentWorkspaceRoot().uri, location.file), new vscode.Position(location.line, location.char ?? 0)), |
| 70 | + ) |
| 71 | + Object.assign(breakpoint, location.breakpoint) |
| 72 | + return breakpoint |
| 73 | + }) |
| 74 | + vscode.debug.addBreakpoints(newBreakpoints) |
| 75 | + }) |
| 76 | + |
| 77 | + registerExtensionCommand('saveBreakpointsPack', async () => { |
| 78 | + const locations = getRepoLocations().breakpointsPacks |
| 79 | + const newValueSymbol = Symbol('new') |
| 80 | + const selected = await showQuickPick([...getPicks(locations), { label: 'New', value: newValueSymbol as any }]) |
| 81 | + if (!selected) return |
| 82 | + const locationName = selected === newValueSymbol ? await vscode.window.showInputBox({ prompt: 'Enter a name for the breakpoint pack' }) : selected |
| 83 | + if (!locationName) return |
| 84 | + locations[locationName] = vscode.debug.breakpoints |
| 85 | + .map(breakpoint => { |
| 86 | + if (!(breakpoint instanceof vscode.SourceBreakpoint)) return undefined! |
| 87 | + const { uri, range } = breakpoint.location |
| 88 | + return { |
| 89 | + file: vscode.workspace.asRelativePath(uri.path), |
| 90 | + line: range.start.line, |
| 91 | + char: range.start.character, |
| 92 | + breakpoint: omitObj(breakpoint, 'id', 'location'), |
| 93 | + } |
| 94 | + }) |
| 95 | + .filter(Boolean) |
| 96 | + |
| 97 | + await saveRepoLocations() |
| 98 | + }) |
| 99 | +} |
0 commit comments