Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(copilot): optimize export with copilot experience VSCODE-655 #880

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions images/dark/export-to-language.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions images/light/export-to-language.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 20 additions & 67 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
"title": "MongoDB: Change Active Connection"
},
{
"command": "mdb.changeDriverSyntax",
"command": "mdb.changeDriverSyntaxForExportToLanguage",
"title": "MongoDB: Change Export To Language Driver Syntax"
},
{
Expand All @@ -283,40 +283,16 @@
}
},
{
"command": "mdb.exportCodeToPlayground",
"title": "Export Code to Playground"
},
{
"command": "mdb.exportToPython",
"title": "MongoDB: Export To Python 3"
},
{
"command": "mdb.exportToJava",
"title": "MongoDB: Export To Java"
},
{
"command": "mdb.exportToCsharp",
"title": "MongoDB: Export To C#"
},
{
"command": "mdb.exportToNode",
"title": "MongoDB: Export To Node.js"
},
{
"command": "mdb.exportToRuby",
"title": "MongoDB: Export To Ruby"
},
{
"command": "mdb.exportToGo",
"title": "MongoDB: Export To Go"
},
{
"command": "mdb.exportToRust",
"title": "MongoDB: Export To Rust"
"command": "mdb.selectTargetForExportToLanguage",
"title": "MongoDB: Select Target For Export to Language",
"icon": {
"light": "images/light/export-to-language.svg",
"dark": "images/dark/export-to-language.svg"
}
},
{
"command": "mdb.exportToPHP",
"title": "MongoDB: Export To PHP"
"command": "mdb.exportCodeToPlayground",
"title": "Export Code to Playground"
},
{
"command": "mdb.addConnection",
Expand Down Expand Up @@ -745,9 +721,14 @@
}
],
"editor/title": [
{
"command": "mdb.selectTargetForExportToLanguage",
"group": "navigation@1",
"when": "mdb.isPlayground == true"
},
{
"command": "mdb.runPlayground",
"group": "navigation",
"group": "navigation@2",
"when": "mdb.isPlayground == true"
}
],
Expand Down Expand Up @@ -808,38 +789,6 @@
"command": "mdb.runAllPlaygroundBlocks",
"when": "mdb.isPlayground == true"
},
{
"command": "mdb.exportToRuby",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToPython",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToJava",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToCsharp",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToNode",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToGo",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToRust",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.exportToPHP",
"when": "mdb.isPlayground == true && mdb.connectedToMongoDB == true && mdb.isAtlasStreams == false"
},
{
"command": "mdb.refreshPlaygroundsFromTreeView",
"when": "false"
Expand Down Expand Up @@ -873,7 +822,7 @@
"when": "mdb.isPlayground"
},
{
"command": "mdb.changeDriverSyntax",
"command": "mdb.changeDriverSyntaxForExportToLanguage",
"when": "false"
},
{
Expand Down Expand Up @@ -964,6 +913,10 @@
"command": "mdb.runPlayground",
"when": "false"
},
{
"command": "mdb.selectTargetForExportToLanguage",
"when": "false"
},
{
"command": "mdb.exportCodeToPlayground",
"when": "false"
Expand Down
12 changes: 3 additions & 9 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@ enum EXTENSION_COMMANDS {
MDB_FIX_THIS_INVALID_INTERACTIVE_SYNTAX = 'mdb.fixThisInvalidInteractiveSyntax',
MDB_FIX_ALL_INVALID_INTERACTIVE_SYNTAX = 'mdb.fixAllInvalidInteractiveSyntax',

MDB_EXPORT_TO_PYTHON = 'mdb.exportToPython',
MDB_EXPORT_TO_JAVA = 'mdb.exportToJava',
MDB_EXPORT_TO_CSHARP = 'mdb.exportToCsharp',
MDB_EXPORT_TO_NODE = 'mdb.exportToNode',
MDB_EXPORT_TO_RUBY = 'mdb.exportToRuby',
MDB_EXPORT_TO_GO = 'mdb.exportToGo',
MDB_EXPORT_TO_RUST = 'mdb.exportToRust',
MDB_EXPORT_TO_PHP = 'mdb.exportToPHP',
MDB_CHANGE_DRIVER_SYNTAX = 'mdb.changeDriverSyntax',
MDB_SELECT_TARGET_FOR_EXPORT_TO_LANGUAGE = 'mdb.selectTargetForExportToLanguage',
MDB_EXPORT_TO_LANGUAGE = 'mdb.exportToLanguage',
MDB_CHANGE_DRIVER_SYNTAX_FOR_EXPORT_TO_LANGUAGE = 'mdb.changeDriverSyntaxForExportToLanguage',

MDB_OPEN_MONGODB_DOCUMENT_FROM_CODE_LENS = 'mdb.openMongoDBDocumentFromCodeLens',
MDB_OPEN_MONGODB_DOCUMENT_FROM_TREE = 'mdb.openMongoDBDocumentFromTree',
Expand Down
13 changes: 6 additions & 7 deletions src/editors/exportToLanguageCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as vscode from 'vscode';

import EXTENSION_COMMANDS from '../commands';
import {
ExportToLanguage,
isExportToLanguageResult,
} from '../types/playgroundType';
import { isExportToLanguageResult } from '../types/playgroundType';
import type PlaygroundResultProvider from './playgroundResultProvider';

export const DEFAULT_EXPORT_TO_LANGUAGE_DRIVER_SYNTAX = true;

export default class ExportToLanguageCodeLensProvider
implements vscode.CodeLensProvider
{
Expand Down Expand Up @@ -45,15 +44,15 @@ export default class ExportToLanguageCodeLensProvider
}

if (
this._playgroundResultProvider._playgroundResult?.language !==
ExportToLanguage.CSHARP
this._playgroundResultProvider._playgroundResult?.language !== 'csharp'
) {
driverSyntaxCodeLens.command = {
title: this._playgroundResultProvider._playgroundResult
.includeDriverSyntax
? 'Exclude Driver Syntax'
: 'Include Driver Syntax',
command: EXTENSION_COMMANDS.MDB_CHANGE_DRIVER_SYNTAX,
command:
EXTENSION_COMMANDS.MDB_CHANGE_DRIVER_SYNTAX_FOR_EXPORT_TO_LANGUAGE,
arguments: [
!this._playgroundResultProvider._playgroundResult.includeDriverSyntax,
],
Expand Down
93 changes: 27 additions & 66 deletions src/editors/playgroundSelectionCodeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,15 @@ import * as vscode from 'vscode';
import EXTENSION_COMMANDS from '../commands';
import { isPlayground, getSelectedText } from '../utils/playground';

const selectionCommands = [
{
name: 'Run selected playground blocks',
command: EXTENSION_COMMANDS.MDB_RUN_SELECTED_PLAYGROUND_BLOCKS,
},
{
name: 'Export To Python 3',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_PYTHON,
isCopilotRequired: true,
},
{
name: 'Export To Java',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_JAVA,
isCopilotRequired: true,
},
{
name: 'Export To C#',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_CSHARP,
isCopilotRequired: true,
},
{
name: 'Export To Node.js',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_NODE,
isCopilotRequired: true,
},
{
name: 'Export To Ruby',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_RUBY,
isCopilotRequired: true,
},
{
name: 'Export To Go',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_GO,
isCopilotRequired: true,
},
{
name: 'Export To Rust',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_RUST,
isCopilotRequired: true,
},
{
name: 'Export To PHP',
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_PHP,
isCopilotRequired: true,
},
export const EXPORT_TO_LANGUAGE_ALIASES = [
{ id: 'csharp', alias: 'C#' },
{ id: 'go', alias: 'Go' },
{ id: 'java', alias: 'Java' },
{ id: 'javascript', alias: 'Node.js' },
{ id: 'php', alias: 'PHP' },
{ id: 'python', alias: 'Python 3' },
{ id: 'ruby', alias: 'Ruby' },
{ id: 'rust', alias: 'Rust' },
];

export default class PlaygroundSelectionCodeActionProvider
Expand All @@ -67,43 +31,40 @@ export default class PlaygroundSelectionCodeActionProvider
readonly onDidChangeCodeLenses: vscode.Event<void> =
this._onDidChangeCodeCodeAction.event;

createCodeAction({
codeActionName,
codeActionCommand,
}: {
codeActionName: string;
codeActionCommand: string;
}): vscode.CodeAction {
createCodeAction(command: vscode.Command): vscode.CodeAction {
const codeAction = new vscode.CodeAction(
codeActionName,
command.title,
vscode.CodeActionKind.Empty
);
codeAction.command = {
command: codeActionCommand,
title: codeActionName,
tooltip: codeActionName,
};
codeAction.command = command;
return codeAction;
}

provideCodeActions(): vscode.CodeAction[] | undefined {
const editor = vscode.window.activeTextEditor;
const codeActions: vscode.CodeAction[] = [];
const copilot = vscode.extensions.getExtension('github.copilot-chat');
let codeActions: vscode.CodeAction[] = [
this.createCodeAction({
title: 'Run selected playground blocks',
command: EXTENSION_COMMANDS.MDB_RUN_SELECTED_PLAYGROUND_BLOCKS,
}),
];

if (!isPlayground(editor?.document.uri) || !getSelectedText()) {
return;
}

for (const { name, command, isCopilotRequired } of selectionCommands) {
if (!isCopilotRequired || copilot?.isActive) {
codeActions.push(
if (copilot?.isActive) {
codeActions = [
...codeActions,
...EXPORT_TO_LANGUAGE_ALIASES.map(({ id, alias }) =>
this.createCodeAction({
codeActionName: name,
codeActionCommand: command,
title: `Export To ${alias}`,
command: EXTENSION_COMMANDS.MDB_EXPORT_TO_LANGUAGE,
arguments: [id],
})
);
}
),
];
}

return codeActions;
Expand Down
Loading
Loading