Skip to content

Commit 56541f1

Browse files
committed
Refactors feedback button visibility logic for markdown AI
Introduces a context key and a tracker to control the visibility of AI feedback buttons in markdown previews based on both feedback context presence and telemetry settings. Streamlines menu conditions, ensuring buttons only appear when feedback is available and telemetry is enabled, improving maintainability and aligning UI with user permissions.
1 parent 47405d5 commit 56541f1

File tree

6 files changed

+139
-14
lines changed

6 files changed

+139
-14
lines changed

contributions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
"menus": {
150150
"editor/title": [
151151
{
152-
"when": "resourceScheme == gitlens-markdown && config.gitlens.telemetry.enabled && activeCustomEditorId == vscode.markdown.preview.editor",
152+
"when": "gitlens:markdown:feedback:available && activeCustomEditorId == vscode.markdown.preview.editor",
153153
"group": "navigation",
154154
"order": 2
155155
}
@@ -162,7 +162,7 @@
162162
"menus": {
163163
"editor/title": [
164164
{
165-
"when": "resourceScheme == gitlens-markdown && config.gitlens.telemetry.enabled && activeCustomEditorId == vscode.markdown.preview.editor",
165+
"when": "gitlens:markdown:feedback:available && activeCustomEditorId == vscode.markdown.preview.editor",
166166
"group": "navigation",
167167
"order": 1
168168
}

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14062,6 +14062,16 @@
1406214062
"when": "activeWebviewPanelId == gitlens.timeline && resourceScheme == webview-panel && config.gitlens.visualHistory.allowMultiple",
1406314063
"group": "navigation@-97"
1406414064
},
14065+
{
14066+
"command": "gitlens.ai.feedback.positive",
14067+
"when": "gitlens:markdown:feedback:available && activeCustomEditorId == vscode.markdown.preview.editor",
14068+
"group": "navigation@1"
14069+
},
14070+
{
14071+
"command": "gitlens.ai.feedback.negative",
14072+
"when": "gitlens:markdown:feedback:available && activeCustomEditorId == vscode.markdown.preview.editor",
14073+
"group": "navigation@2"
14074+
},
1406514075
{
1406614076
"command": "gitlens.clearFileAnnotations",
1406714077
"when": "resource in gitlens:tabs:blameable && (gitlens:window:annotated == computed || (resource in gitlens:tabs:annotated && resource not in gitlens:tabs:annotated:computing)) && config.gitlens.menus.editorGroup.blame",
@@ -14136,16 +14146,6 @@
1413614146
"when": "resourceScheme == gitlens-markdown && activeCustomEditorId == vscode.markdown.preview.editor",
1413714147
"group": "navigation@0"
1413814148
},
14139-
{
14140-
"command": "gitlens.ai.feedback.positive",
14141-
"when": "resourceScheme == gitlens-markdown && config.gitlens.telemetry.enabled && activeCustomEditorId == vscode.markdown.preview.editor",
14142-
"group": "navigation@1"
14143-
},
14144-
{
14145-
"command": "gitlens.ai.feedback.negative",
14146-
"when": "resourceScheme == gitlens-markdown && config.gitlens.telemetry.enabled && activeCustomEditorId == vscode.markdown.preview.editor",
14147-
"group": "navigation@2"
14148-
},
1414914149
{
1415014150
"command": "gitlens.openPatch",
1415114151
"when": "false && editorLangId == diff"

src/commands/generateRebase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ function generateRebaseMarkdown(
547547
},
548548
};
549549

550-
// Add feedback context and commands to toolbar if telemetry is enabled
551-
if (feedbackContext && telemetryEnabled) {
550+
// Always store feedback context if available, but only show UI when telemetry is enabled
551+
if (feedbackContext) {
552552
metadata.feedbackContext = feedbackContext as unknown as Record<string, unknown>;
553553
}
554554

src/constants.context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type ContextKeys = {
6161
'gitlens:vsls': boolean | 'host' | 'guest';
6262
'gitlens:window:annotated': AnnotationStatus;
6363
'gitlens:walkthroughSupported': boolean;
64+
'gitlens:markdown:feedback:available': boolean;
6465
} & Record<`gitlens:action:${string}`, number> &
6566
Record<`gitlens:feature:unsupported:${Features}`, boolean> &
6667
Record<`gitlens:key:${Keys}`, boolean> &

src/container.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { isWalkthroughSupported, WalkthroughStateProvider } from './telemetry/wa
6363
import { GitTerminalLinkProvider } from './terminal/linkProvider';
6464
import { GitDocumentTracker } from './trackers/documentTracker';
6565
import { LineTracker } from './trackers/lineTracker';
66+
import { MarkdownFeedbackTracker } from './trackers/markdownFeedbackTracker';
6667
import { DeepLinkService } from './uris/deepLinks/deepLinkService';
6768
import { UriService } from './uris/uriService';
6869
import { ViewFileDecorationProvider } from './views/viewDecorationProvider';
@@ -218,6 +219,7 @@ export class Container {
218219
this._disposables.push((this._actionRunners = new ActionRunners(this)));
219220
this._disposables.push((this._documentTracker = new GitDocumentTracker(this)));
220221
this._disposables.push((this._lineTracker = new LineTracker(this, this._documentTracker)));
222+
this._disposables.push(new MarkdownFeedbackTracker(this));
221223
this._disposables.push((this._keyboard = new Keyboard()));
222224
this._disposables.push((this._vsls = new VslsController(this)));
223225
this._disposables.push((this._eventBus = new EventBus()));
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import type { Disposable, TextDocument, TextEditor } from 'vscode';
2+
import { window, workspace } from 'vscode';
3+
import { Schemes } from '../constants';
4+
import type { Container } from '../container';
5+
import type { MarkdownContentMetadata } from '../documents/markdown';
6+
import { decodeGitLensRevisionUriAuthority } from '../git/gitUri.authority';
7+
import { setContext } from '../system/-webview/context';
8+
import { debug } from '../system/decorators/log';
9+
10+
/**
11+
* Tracks when a GitLens markdown document with feedback context is active
12+
* and sets the appropriate context variable for toolbar button visibility
13+
*/
14+
export class MarkdownFeedbackTracker implements Disposable {
15+
private readonly _disposables: Disposable[] = [];
16+
private _currentHasFeedback = false;
17+
18+
constructor(private readonly container: Container) {
19+
// Listen for text editor changes (for when editing markdown source)
20+
this._disposables.push(window.onDidChangeActiveTextEditor(this.onActiveEditorChanged, this));
21+
22+
// Listen for document opens (for when markdown documents are opened)
23+
this._disposables.push(workspace.onDidOpenTextDocument(this.onDocumentOpened, this));
24+
25+
// Listen for visible text editors changes (for when preview becomes active)
26+
this._disposables.push(window.onDidChangeVisibleTextEditors(this.onVisibleEditorsChanged, this));
27+
28+
// Check initial state
29+
this.checkCurrentState();
30+
}
31+
32+
dispose(): void {
33+
this._disposables.forEach(d => {
34+
d.dispose();
35+
});
36+
// Clear context on disposal
37+
void setContext('gitlens:markdown:feedback:available', false);
38+
}
39+
40+
@debug()
41+
private onActiveEditorChanged(editor: TextEditor | undefined): void {
42+
console.log('MarkdownFeedbackTracker: Active editor changed', {
43+
hasEditor: Boolean(editor),
44+
scheme: editor?.document.uri.scheme,
45+
});
46+
this.checkCurrentState();
47+
}
48+
49+
@debug()
50+
private onDocumentOpened(document: TextDocument): void {
51+
console.log('MarkdownFeedbackTracker: Document opened', {
52+
scheme: document.uri.scheme,
53+
authority: document.uri.authority,
54+
});
55+
if (document.uri.scheme === Schemes.GitLensMarkdown) {
56+
this.checkCurrentState();
57+
}
58+
}
59+
60+
@debug()
61+
private onVisibleEditorsChanged(editors: readonly TextEditor[]): void {
62+
console.log('MarkdownFeedbackTracker: Visible editors changed', {
63+
count: editors.length,
64+
schemes: editors.map(e => e.document.uri.scheme),
65+
});
66+
this.checkCurrentState();
67+
}
68+
69+
private checkCurrentState(): void {
70+
// Check all open documents for GitLens markdown with feedback context
71+
const hasFeedback = this.checkForFeedbackDocuments();
72+
73+
console.log('MarkdownFeedbackTracker: Checking current state', {
74+
hasFeedback: hasFeedback,
75+
telemetryEnabled: this.container.telemetry.enabled,
76+
});
77+
78+
if (hasFeedback !== this._currentHasFeedback) {
79+
this._currentHasFeedback = hasFeedback;
80+
console.log('MarkdownFeedbackTracker: Setting context to', hasFeedback);
81+
void setContext('gitlens:markdown:feedback:available', hasFeedback);
82+
}
83+
}
84+
85+
private checkForFeedbackDocuments(): boolean {
86+
// Check all open documents for GitLens markdown with feedback context
87+
for (const document of workspace.textDocuments) {
88+
if (this.documentHasFeedback(document)) {
89+
return true;
90+
}
91+
}
92+
return false;
93+
}
94+
95+
private documentHasFeedback(document: TextDocument): boolean {
96+
const uri = document.uri;
97+
if (uri.scheme !== Schemes.GitLensMarkdown) return false;
98+
99+
const authority = uri.authority;
100+
if (!authority) return false;
101+
102+
try {
103+
const metadata = decodeGitLensRevisionUriAuthority<MarkdownContentMetadata>(authority);
104+
105+
// Check if document has feedback context AND telemetry is enabled
106+
const hasFeedbackContext = Boolean(metadata.feedbackContext);
107+
const telemetryEnabled = this.container.telemetry.enabled;
108+
109+
console.log('MarkdownFeedbackTracker: Checking document feedback context', {
110+
uri: uri.toString(),
111+
hasFeedbackContext: hasFeedbackContext,
112+
telemetryEnabled: telemetryEnabled,
113+
metadata: metadata.feedbackContext ? 'present' : 'missing',
114+
});
115+
116+
return hasFeedbackContext && telemetryEnabled;
117+
} catch (error) {
118+
console.log('MarkdownFeedbackTracker: Error decoding metadata', error);
119+
return false;
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)