Skip to content

Commit 581b9bf

Browse files
authored
[VS Code] Always show "Generate Operation" code lens (#9233)
* Always show generate code lens * ensure there is a new line before inserted query
1 parent d872941 commit 581b9bf

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

firebase-vscode/src/data-connect/code-lens-provider.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,30 @@ export class OperationCodeLensProvider extends ComputedCodeLensProvider {
123123
}
124124
}
125125

126-
if (projectId) {
127-
const comments = findCommentsBlocks(documentText);
128-
for (let i = 0; i < comments.length; i++) {
129-
const c = comments[i];
130-
const range = new vscode.Range(c.startLine, 0, c.startLine, 0);
131-
const queryDoc = documentNode.definitions.find((d) =>
132-
d.kind === Kind.OPERATION_DEFINITION &&
133-
// startToken.line is 1-indexed, endLine is 0-indexed
134-
d.loc?.startToken.line === c.endLine + 2
135-
);
136-
const arg: GenerateOperationInput = {
137-
projectId,
138-
document: document,
139-
description: c.text,
140-
insertPosition: c.endIndex + 1,
141-
existingQuery: queryDoc?.loc ? documentText.substring(c.endIndex + 1, queryDoc.loc.endToken.end) : '',
142-
};
143-
codeLenses.push(
144-
new vscode.CodeLens(range, {
145-
title: queryDoc ? `$(sparkle) Refine Operation` : `$(sparkle) Generate Operation`,
146-
command: "firebase.dataConnect.generateOperation",
147-
tooltip: "Generate the operation (⌘+enter or Ctrl+Enter)",
148-
arguments: [arg],
149-
}),
150-
);
151-
}
126+
const comments = findCommentsBlocks(documentText);
127+
for (let i = 0; i < comments.length; i++) {
128+
const c = comments[i];
129+
const range = new vscode.Range(c.startLine, 0, c.startLine, 0);
130+
const queryDoc = documentNode.definitions.find((d) =>
131+
d.kind === Kind.OPERATION_DEFINITION &&
132+
// startToken.line is 1-indexed, endLine is 0-indexed
133+
d.loc?.startToken.line === c.endLine + 2
134+
);
135+
const arg: GenerateOperationInput = {
136+
projectId,
137+
document: document,
138+
description: c.text,
139+
insertPosition: c.endIndex + 1,
140+
existingQuery: queryDoc?.loc ? documentText.substring(c.endIndex + 1, queryDoc.loc.endToken.end) : '',
141+
};
142+
codeLenses.push(
143+
new vscode.CodeLens(range, {
144+
title: queryDoc ? `$(sparkle) Refine Operation` : `$(sparkle) Generate Operation`,
145+
command: "firebase.dataConnect.generateOperation",
146+
tooltip: "Generate the operation (⌘+enter or Ctrl+Enter)",
147+
arguments: [arg],
148+
}),
149+
);
152150
}
153151
return codeLenses;
154152
}

firebase-vscode/src/data-connect/execution/execution.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface ExecutionInput {
5353
}
5454

5555
export interface GenerateOperationInput {
56-
projectId: string;
56+
projectId?: string;
5757
document: vscode.TextDocument;
5858
description: string;
5959
insertPosition: number;
@@ -314,6 +314,10 @@ export function registerExecution(
314314
}
315315

316316
async function generateOperation(arg: GenerateOperationInput) {
317+
if (!arg.projectId) {
318+
vscode.window.showErrorMessage(`Connect a Firebase project to use Gemini in Firebase features.`);
319+
return;
320+
}
317321
try {
318322
const schema = await dataConnectService.schema();
319323
const prompt = `Generate a Data Connect operation to match this description: ${arg.description}

firebase-vscode/src/data-connect/file-utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,16 @@ export async function insertQueryAt(uri: vscode.Uri, at: number, existing: strin
5959
const doc = await vscode.workspace.openTextDocument(uri);
6060
const text = doc.getText();
6161
if (!existing) {
62+
if (text[at-1] !== "\n") {
63+
replace = "\n" + replace;
64+
}
6265
const newText = text.slice(0, at) + replace + text.slice(at);
6366
await vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(newText));
6467
return;
6568
}
6669
if (text.slice(at, at + existing.length) !== existing) {
6770
throw new Error("The existing query was updated.");
6871
}
69-
// Adds a new line before if inserting at the end of the file
70-
if (at > text.length) {
71-
replace = "\n" + replace;
72-
}
7372
const newText = text.slice(0, at) + replace + text.slice(at + existing.length);
7473
await vscode.workspace.fs.writeFile(uri, new TextEncoder().encode(newText));
7574
}

0 commit comments

Comments
 (0)