diff --git a/package.json b/package.json index 8d07ee4..04e003f 100644 --- a/package.json +++ b/package.json @@ -190,6 +190,10 @@ { "command": "magit.unstage-file", "title": "Magit Unstage File" + }, + { + "command": "magit.copy-section-value", + "title": "Magit Copy Section value" } ], "menus": { diff --git a/src/commands/copySectionValueCommands.ts b/src/commands/copySectionValueCommands.ts new file mode 100644 index 0000000..678863b --- /dev/null +++ b/src/commands/copySectionValueCommands.ts @@ -0,0 +1,44 @@ +import { env, window } from 'vscode'; +import * as Constants from '../common/constants'; +import { MagitRepository } from '../models/magitRepository'; +import { BranchListingView } from '../views/branches/branchListingView'; +import { ChangeHeaderView } from '../views/changes/changeHeaderView'; +import { CommitItemView } from '../views/commits/commitSectionView'; +import { DocumentView } from '../views/general/documentView'; +import { RemoteBranchListingView } from '../views/remotes/remoteBranchListingView'; +import { StashItemView } from '../views/stashes/stashSectionView'; +import { TagListingView } from '../views/tags/tagListingView'; +import { ChangeView } from '../views/changes/changeView'; + +export async function copySectionValueCommand(repository: MagitRepository, currentView: DocumentView) { + + const activePosition = window.activeTextEditor?.selection.active; + + if (!activePosition) { + return; + } + + let sectionValue: string | undefined; + const selectedView = currentView.click(activePosition); + if (selectedView instanceof CommitItemView) { + sectionValue = selectedView.commit.hash; + } else if ( + selectedView instanceof BranchListingView || + selectedView instanceof RemoteBranchListingView || + selectedView instanceof TagListingView + ) { + sectionValue = selectedView.ref.commit; + } else if (selectedView instanceof StashItemView) { + sectionValue = selectedView.section; + } else if ( + selectedView instanceof ChangeView || + selectedView instanceof ChangeHeaderView + ) { + sectionValue = selectedView.change.relativePath; + } + + if (sectionValue) { + env.clipboard.writeText(sectionValue); + window.setStatusBarMessage(sectionValue, Constants.StatusMessageDisplayTimeout); + } +} diff --git a/src/extension.ts b/src/extension.ts index 7fe5d5a..4de50ce 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -38,6 +38,7 @@ import { cherryPicking } from './commands/cherryPickingCommands'; import { reverting } from './commands/revertingCommands'; import { reverseAtPoint } from './commands/reverseAtPointCommands'; import { blameFile } from './commands/blamingCommands'; +import { copySectionValueCommand } from './commands/copySectionValueCommands'; export const magitRepositories: Map = new Map(); export const views: Map = new Map(); @@ -120,6 +121,8 @@ export function activate(context: ExtensionContext) { context.subscriptions.push(commands.registerTextEditorCommand('magit.stage-file', Command.primeFileCommand(stageFile))); context.subscriptions.push(commands.registerTextEditorCommand('magit.unstage-file', Command.primeFileCommand(unstageFile))); + context.subscriptions.push(commands.registerTextEditorCommand('magit.copy-section-value', Command.primeRepoAndView(copySectionValueCommand))); + context.subscriptions.push(commands.registerCommand('magit.dispatch', async () => { const editor = window.activeTextEditor; const repository = await MagitUtils.getCurrentMagitRepo(editor?.document.uri); diff --git a/src/views/changes/changeHeaderView.ts b/src/views/changes/changeHeaderView.ts index 905a6a5..35e60aa 100644 --- a/src/views/changes/changeHeaderView.ts +++ b/src/views/changes/changeHeaderView.ts @@ -4,7 +4,7 @@ import { Status } from '../../typings/git'; export class ChangeHeaderView extends TextView { - constructor(private change: MagitChange) { + constructor(public change: MagitChange) { super(); const statusLabel = mapFileStatusToLabel(this.change.status); const mergingStatusLabel = mapFileStatusToMergingLabel(this.change.status); diff --git a/src/views/stashes/stashSectionView.ts b/src/views/stashes/stashSectionView.ts index 345ebe1..d92c433 100644 --- a/src/views/stashes/stashSectionView.ts +++ b/src/views/stashes/stashSectionView.ts @@ -21,7 +21,15 @@ export class StashSectionView extends View { export class StashItemView extends TextView { + public get section() { + return StashItemView.getSection(this.stash); + } + + private static getSection(stash: Stash) { + return `stash@{${stash.index}}`; + } + constructor(public stash: Stash) { - super(`stash@{${stash.index}} ${stash.description}`); + super(`${StashItemView.getSection(stash)} ${stash.description}`); } } \ No newline at end of file