From ecadc2836d2bf1287f13cde7827d0d914b0bae92 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 19 Mar 2018 18:38:15 +0800 Subject: [PATCH] support trigger test command in file explorer --- package.json | 12 +++++++++--- src/commands/submit.ts | 17 ++++------------- src/commands/test.ts | 14 ++++---------- src/extension.ts | 4 ++-- src/utils/workspaceUtils.ts | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 937853c1..e84812dd 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ }, { "command": "leetcode.testSolution", - "title": "Test", + "title": "Test in LeetCode", "category": "LeetCode" }, { @@ -115,7 +115,7 @@ { "command": "leetcode.showProblem", "when": "view == leetCodeExplorer && viewItem == problem", - "group": "1@1" + "group": "leetcode-explorer@1" } ], "commandPalette": [ @@ -125,9 +125,15 @@ } ], "explorer/context": [ + { + "command": "leetcode.testSolution", + "when": "explorerResourceIsFolder == false", + "group": "file-explorer@1" + }, { "command": "leetcode.submitSolution", - "when": "explorerResourceIsFolder == false" + "when": "explorerResourceIsFolder == false", + "group": "file-explorer@2" } ] }, diff --git a/src/commands/submit.ts b/src/commands/submit.ts index d45382df..044e9b22 100644 --- a/src/commands/submit.ts +++ b/src/commands/submit.ts @@ -5,6 +5,7 @@ import { leetCodeManager } from "../leetCodeManager"; import { leetCodeBinaryPath } from "../shared"; import { executeCommand } from "../utils/cpUtils"; import { DialogType, promptForOpenOutputChannel, promptForSignIn, showResultFile } from "../utils/uiUtils"; +import { getActivefilePath } from "../utils/workspaceUtils"; export async function submitSolution(channel: vscode.OutputChannel, uri?: vscode.Uri): Promise { if (!leetCodeManager.getUser()) { @@ -12,19 +13,9 @@ export async function submitSolution(channel: vscode.OutputChannel, uri?: vscode return; } - let filePath: string; - if (uri) { - filePath = uri.fsPath; - } else { - const textEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor; - if (!textEditor) { - return; - } - if (!textEditor.document.save()) { - vscode.window.showWarningMessage("Please save the solution file first."); - return; - } - filePath = textEditor.document.uri.fsPath; + const filePath: string | undefined = await getActivefilePath(uri); + if (!filePath) { + return; } try { diff --git a/src/commands/test.ts b/src/commands/test.ts index 21a24504..f9cb73f0 100644 --- a/src/commands/test.ts +++ b/src/commands/test.ts @@ -6,24 +6,18 @@ import { leetCodeManager } from "../leetCodeManager"; import { IQuickItemEx, leetCodeBinaryPath, UserStatus } from "../shared"; import { executeCommand } from "../utils/cpUtils"; import { DialogType, promptForOpenOutputChannel, showFileSelectDialog, showResultFile } from "../utils/uiUtils"; +import { getActivefilePath } from "../utils/workspaceUtils"; -export async function testSolution(channel: vscode.OutputChannel): Promise { +export async function testSolution(channel: vscode.OutputChannel, uri?: vscode.Uri): Promise { try { if (leetCodeManager.getStatus() === UserStatus.SignedOut) { return; } - const activeText: vscode.TextEditor | undefined = vscode.window.activeTextEditor; - if (!activeText) { - vscode.window.showErrorMessage("Please open a LeetCode solution file first."); + const filePath: string | undefined = await getActivefilePath(uri); + if (!filePath) { return; } - if (!activeText.document.save()) { - vscode.window.showWarningMessage("Please save the solution file first."); - return; - } - - const filePath = activeText.document.uri.fsPath; const picks: Array> = []; picks.push( { diff --git a/src/extension.ts b/src/extension.ts index 3157b8b4..1422c7c3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,8 +27,8 @@ export async function activate(context: vscode.ExtensionContext) { vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(channel, node)), vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem(channel)), vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()), - vscode.commands.registerCommand("leetcode.testSolution", () => test.testSolution(channel)), - vscode.commands.registerCommand("leetcode.submitSolution", (uri: vscode.Uri) => submit.submitSolution(channel, uri)), + vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(channel, uri)), + vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(channel, uri)), ); leetCodeManager.on("statusChanged", () => { diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index b90991e5..f8958a54 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -17,3 +17,21 @@ export async function selectWorkspaceFolder(): Promise { } return folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode"); } + +export async function getActivefilePath(uri?: vscode.Uri): Promise { + let textEditor: vscode.TextEditor | undefined; + if (uri) { + textEditor = await vscode.window.showTextDocument(uri, { preview: false }); + } else { + textEditor = vscode.window.activeTextEditor; + } + + if (!textEditor) { + return undefined; + } + if (textEditor.document.isDirty && !await textEditor.document.save()) { + vscode.window.showWarningMessage("Please save the solution file first."); + return undefined; + } + return textEditor.document.uri.fsPath; +}