Skip to content

Commit 432475f

Browse files
committed
Adds regenerate command for explain summaries (wip)
1 parent 1333c5a commit 432475f

File tree

12 files changed

+188
-15
lines changed

12 files changed

+188
-15
lines changed

contributions.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,26 @@
36723672
]
36733673
}
36743674
},
3675+
"gitlens.regenerateMarkdownDocument": {
3676+
"label": "Regenerate",
3677+
"icon": "$(refresh)",
3678+
"menus": {
3679+
"editor/title": [
3680+
{
3681+
"when": "resourceScheme == gitlens-markdown && activeCustomEditorId == vscode.markdown.preview.editor",
3682+
"group": "navigation",
3683+
"order": 0
3684+
}
3685+
],
3686+
"editor/title/context": [
3687+
{
3688+
"when": "resourceScheme == gitlens-markdown && activeCustomEditorId == vscode.markdown.preview.editor",
3689+
"group": "1_gitlens",
3690+
"order": 0
3691+
}
3692+
]
3693+
}
3694+
},
36753695
"gitlens.reset": {
36763696
"label": "Reset Stored Data...",
36773697
"commandPalette": true

docs/telemetry-events.md

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7554,6 +7554,11 @@
75547554
"title": "Quick Open File History",
75557555
"category": "GitLens"
75567556
},
7557+
{
7558+
"command": "gitlens.regenerateMarkdownDocument",
7559+
"title": "Regenerate",
7560+
"icon": "$(refresh)"
7561+
},
75577562
{
75587563
"command": "gitlens.reset",
75597564
"title": "Reset Stored Data...",
@@ -11771,6 +11776,10 @@
1177111776
"command": "gitlens.quickOpenFileHistory",
1177211777
"when": "resource in gitlens:tabs:tracked"
1177311778
},
11779+
{
11780+
"command": "gitlens.regenerateMarkdownDocument",
11781+
"when": "false"
11782+
},
1177411783
{
1177511784
"command": "gitlens.revealCommitInView",
1177611785
"when": "resource in gitlens:tabs:blameable"
@@ -14110,6 +14119,11 @@
1411014119
"when": "resourceScheme =~ /^(gitlens|pr)$/ && gitlens:enabled && isInDiffEditor",
1411114120
"group": "navigation@-97"
1411214121
},
14122+
{
14123+
"command": "gitlens.regenerateMarkdownDocument",
14124+
"when": "resourceScheme == gitlens-markdown && activeCustomEditorId == vscode.markdown.preview.editor",
14125+
"group": "navigation@0"
14126+
},
1411314127
{
1411414128
"command": "gitlens.openPatch",
1411514129
"when": "false && editorLangId == diff"
@@ -14131,6 +14145,11 @@
1413114145
"when": "resourceScheme == gitlens && resourceScheme in gitlens:schemes:trackable",
1413214146
"group": "2_a_gitlens@0"
1413314147
},
14148+
{
14149+
"command": "gitlens.regenerateMarkdownDocument",
14150+
"when": "resourceScheme == gitlens-markdown && activeCustomEditorId == vscode.markdown.preview.editor",
14151+
"group": "1_gitlens@0"
14152+
},
1413414153
{
1413514154
"command": "gitlens.copyRemoteFileUrlWithoutRange",
1413614155
"when": "resourceScheme in gitlens:schemes:trackable && gitlens:enabled && gitlens:repos:withRemotes && config.gitlens.menus.editorTab.clipboard",

src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import './commands/patches';
5353
import './commands/quickWizard';
5454
import './commands/rebaseEditor';
5555
import './commands/refreshHover';
56+
import './commands/regenerateMarkdownDocument';
5657
import './commands/remoteProviders';
5758
import './commands/repositories';
5859
import './commands/resets';

src/commands/explainBranch.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TextEditor, Uri } from 'vscode';
22
import { ProgressLocation } from 'vscode';
33
import type { Container } from '../container';
4+
import type { MarkdownContentMetadata } from '../documents/markdown';
45
import { GitUri } from '../git/gitUri';
56
import type { GitBranchReference } from '../git/models/reference';
67
import { getBranchMergeTargetName } from '../git/utils/-webview/branch.utils';
@@ -139,7 +140,27 @@ export class ExplainBranchCommand extends GlCommandBase {
139140

140141
const content = `# Branch Summary\n\n> Generated by ${result.model.name}\n\n## ${branch.name}\n\n${result.parsed.summary}\n\n${result.parsed.body}`;
141142

142-
showMarkdownPreview(content);
143+
const contentMetadata = {
144+
header: {
145+
title: 'Branch Summary',
146+
subtitle: branch.name,
147+
aiModel: result.model.name,
148+
},
149+
command: {
150+
label: 'Explain Branch Changes',
151+
name: 'gitlens.ai.explainBranch',
152+
args: { repoPath: repository.path, ref: branch.ref, source: args.source },
153+
},
154+
};
155+
156+
const documentUri = this.container.markdown.openDocument(
157+
content,
158+
`/explain/branch/${branch.ref}/${result.model.id}`,
159+
branch.name,
160+
contentMetadata as MarkdownContentMetadata,
161+
);
162+
163+
showMarkdownPreview(documentUri);
143164
} catch (ex) {
144165
Logger.error(ex, 'ExplainBranchCommand', 'execute');
145166
void showGenericErrorMessage('Unable to explain branch');

src/commands/explainStash.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TextEditor, Uri } from 'vscode';
22
import { ProgressLocation } from 'vscode';
33
import type { Container } from '../container';
4+
import type { MarkdownContentMetadata } from '../documents/markdown';
45
import { GitUri } from '../git/gitUri';
56
import type { GitCommit, GitStashCommit } from '../git/models/commit';
67
import { showGenericErrorMessage } from '../messages';
@@ -101,8 +102,27 @@ export class ExplainStashCommand extends GlCommandBase {
101102
commit.message || commit.ref
102103
}\n\n${result.parsed.summary}\n\n${result.parsed.body}`;
103104

104-
// showMarkdownPreview(this.container, content, { filename: `stash-${commit.ref}` });
105-
showMarkdownPreview(content);
105+
const contentMetadata = {
106+
header: {
107+
title: 'Stash Summary',
108+
subtitle: commit.message || commit.ref,
109+
aiModel: result.model.name,
110+
},
111+
command: {
112+
label: 'Explain Stash Changes',
113+
name: 'gitlens.ai.explainStash',
114+
args: { repoPath: repository.path, ref: commit.ref, source: args.source },
115+
},
116+
};
117+
118+
const documentUri = this.container.markdown.openDocument(
119+
content,
120+
`/explain/stash/${commit.ref}/${result.model.id}`,
121+
commit.message || commit.ref,
122+
contentMetadata as MarkdownContentMetadata,
123+
);
124+
125+
showMarkdownPreview(documentUri);
106126
} catch (ex) {
107127
Logger.error(ex, 'ExplainStashCommand', 'execute');
108128
void showGenericErrorMessage('Unable to explain stash');

src/commands/explainWip.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TextEditor, Uri } from 'vscode';
22
import { ProgressLocation } from 'vscode';
33
import type { Container } from '../container';
4+
import type { MarkdownContentMetadata } from '../documents/markdown';
45
import type { GitRepositoryService } from '../git/gitRepositoryService';
56
import { GitUri } from '../git/gitUri';
67
import { uncommitted, uncommittedStaged } from '../git/models/revision';
@@ -67,6 +68,8 @@ export class ExplainWipCommand extends GlCommandBase {
6768
return;
6869
}
6970

71+
args.repoPath ??= svc.path;
72+
7073
try {
7174
let label;
7275
let to;
@@ -130,9 +133,30 @@ export class ExplainWipCommand extends GlCommandBase {
130133
}
131134

132135
const title = `${capitalize(label)} Changes Summary${worktreeDisplayName}`;
133-
const content = `# ${title}\n\n> Generated by ${result.model.name}\n\n## ${label} Changes\n\n${result.parsed.summary}\n\n${result.parsed.body}`;
136+
const subtitle = `${capitalize(label)} Changes`;
137+
const content = `# ${title}\n\n> Generated by ${result.model.name}\n\n## ${subtitle}\n\n${result.parsed.summary}\n\n${result.parsed.body}`;
138+
139+
const contentMetadata = {
140+
header: {
141+
title: title,
142+
subtitle: subtitle,
143+
aiModel: result.model.name,
144+
},
145+
command: {
146+
label: 'Explain Working Changes',
147+
name: 'gitlens.ai.explainWip',
148+
args: args,
149+
},
150+
};
151+
152+
const documentUri = this.container.markdown.openDocument(
153+
content,
154+
`/explain/wip/${result.model.id}`,
155+
subtitle,
156+
contentMetadata as MarkdownContentMetadata,
157+
);
134158

135-
showMarkdownPreview(content);
159+
showMarkdownPreview(documentUri);
136160
} catch (ex) {
137161
Logger.error(ex, 'ExplainWipCommand', 'execute');
138162
void showGenericErrorMessage('Unable to explain working changes');
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { TextEditor, Uri } from 'vscode';
2+
import { window } from 'vscode';
3+
import { Schemes } from '../constants';
4+
import type { MarkdownContentMetadata } from '../documents/markdown';
5+
import { decodeGitLensRevisionUriAuthority } from '../git/gitUri.authority';
6+
import { command, executeCommand } from '../system/-webview/command';
7+
import { Logger } from '../system/logger';
8+
import { ActiveEditorCommand } from './commandBase';
9+
import { getCommandUri } from './commandBase.utils';
10+
11+
@command()
12+
export class RegenerateMarkdownDocumentCommand extends ActiveEditorCommand {
13+
constructor() {
14+
super('gitlens.regenerateMarkdownDocument');
15+
}
16+
17+
async execute(editor?: TextEditor, uri?: Uri): Promise<void> {
18+
uri = getCommandUri(uri, editor);
19+
if (uri == null) return;
20+
21+
// Only work with gitlens-markdown scheme documents
22+
if (uri.scheme !== Schemes.GitLensMarkdown) {
23+
void window.showErrorMessage('This action can only be used on GitLens markdown documents.');
24+
return;
25+
}
26+
27+
// Extract the command from the authority
28+
const authority = uri.authority;
29+
if (authority == null || authority.length === 0) {
30+
void window.showErrorMessage('No regeneration command found for this document.');
31+
return;
32+
}
33+
34+
try {
35+
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
36+
37+
if (metadata.command == null) {
38+
void window.showErrorMessage('No regeneration command found for this document.');
39+
return;
40+
}
41+
42+
// Execute the command that was encoded in the authority
43+
// The openDocument method in the regeneration command will automatically
44+
// detect content changes and fire the _onDidChange event to refresh the preview
45+
await executeCommand(metadata.command.name, metadata.command.args);
46+
} catch (ex) {
47+
Logger.error(ex, 'RegenerateMarkdownDocumentCommand');
48+
void window.showErrorMessage('Failed to regenerate document. See output for more details.');
49+
}
50+
}
51+
}

src/constants.commands.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export type ContributedCommands =
151151
| 'gitlens.openFolderHistory'
152152
| 'gitlens.openPullRequestOnRemote'
153153
| 'gitlens.plus.cloudIntegrations.connect'
154+
| 'gitlens.regenerateMarkdownDocument'
154155
| 'gitlens.showInCommitGraph'
155156
| 'gitlens.showInCommitGraphView'
156157
| 'gitlens.showInDetailsView'

src/constants.commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ type InternalGlCommands =
127127
| 'gitlens.openOnRemote'
128128
| 'gitlens.openWalkthrough'
129129
| 'gitlens.refreshHover'
130+
| 'gitlens.regenerateMarkdownDocument'
130131
| 'gitlens.visualizeHistory'
131132
| InternalGraphWebviewCommands
132133
| InternalHomeWebviewCommands

0 commit comments

Comments
 (0)