From 28692f4be7985b5021a2a81644ee1cb6d5e986e9 Mon Sep 17 00:00:00 2001 From: Michael Lazebny Date: Wed, 28 Aug 2024 11:32:23 +0200 Subject: [PATCH 1/4] fix: added filter for wraps --- src/extension.ts | 126 ++++++++++++++++------------------------------- 1 file changed, 43 insertions(+), 83 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 7525122..46c34e8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,29 +1,11 @@ import * as vscode from 'vscode'; - -import { - Disposable -} from "vscode"; - -import { - sealedStates -} from "./commands"; - +import { Disposable } from "vscode"; +import { sealedStates } from "./commands"; import { FlutterPlusConfig } from './config/config'; import { wrapWith } from './utils'; - - -import { - dartCodeExtensionIdentifier, - flutterExtensionIdentifier, -} from "./constants"; - -/* import fs from 'fs'; -import path from 'path'; */ - +import { dartCodeExtensionIdentifier, flutterExtensionIdentifier } from "./constants"; import { CodeActionWrap } from './code-actions'; -import { - SdkCommands, -} from './utils'; +import { SdkCommands } from './utils'; const DART_MODE = { language: "dart", scheme: "file" }; @@ -31,32 +13,28 @@ export async function activate(context: vscode.ExtensionContext): Promise // Ensure we have a Dart extension. const dartExt = vscode.extensions.getExtension(dartCodeExtensionIdentifier); if (!dartExt) { - // This should not happen since the Flutter extension has a dependency on the Dart one - // but just in case, we'd like to give a useful error message. - throw new Error("The Dart extension is not installed, Flutter extension is unable to activate."); + vscode.window.showErrorMessage("The Dart extension is not installed. Flutter extension cannot be activated."); + return; } await dartExt.activate(); + if (!dartExt.exports) { - console.error("The Dart extension did not provide an exported API. Maybe it failed to activate or is not the latest version?"); + console.error("The Dart extension did not provide an exported API. It may have failed to activate or is not the latest version."); return; } // Ensure we have a Flutter extension. const flutterExt = vscode.extensions.getExtension(flutterExtensionIdentifier); if (!flutterExt) { - // This should not happen since the Flutter extension has a dependency on the Dart one - // but just in case, we'd like to give a useful error message. - throw new Error("The Flutter extension is not installed, Flutter Plus extension is unable to activate."); + vscode.window.showErrorMessage("The Flutter extension is not installed. Flutter Plus extension cannot be activated."); + return; } await flutterExt.activate(); // Register SDK commands. const sdkCommands = new SdkCommands(context, dartExt.exports); - //console.log('Congratulations, your extension "flutter-plus" is now active!'); - registerCommands(context); - //registerActionButtons(context); registerWrappers(context); } @@ -67,70 +45,52 @@ function registerCommands(context: vscode.ExtensionContext) { ); } -/* function registerActionButtons(context: vscode.ExtensionContext) { - function runPubGet() { - // Example of running `dart pub get` or `flutter pub get` - const pubspecPath = path.join(vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || '', 'pubspec.yaml'); - if (fs.existsSync(pubspecPath)) { - const pubspecContent = fs.readFileSync(pubspecPath, 'utf8'); - const isFlutterApp = pubspecContent.includes('flutter:'); - const command = isFlutterApp ? 'flutter pub get' : 'dart pub get'; - executeCommand(command); - } - } - context.subscriptions.push(vscode.commands.registerCommand('flutter-plus.pub-get', () => { - runPubGet(); - })); -} */ - /// Register all wrappers (Wrap With...). function registerWrappers(context: vscode.ExtensionContext) { - var wraps = $registerWrappers(context); + let wrappers = registerWrapperCommands(context); const disposable = vscode.workspace.onDidChangeConfiguration(event => { - if (!event.affectsConfiguration('flutter-plus')) { - return; + if (event.affectsConfiguration('flutter-plus')) { + unregisterWrappers(wrappers); + wrappers = registerWrapperCommands(context); } - - $unregisterWrappers(wraps); - wraps = $registerWrappers(context); }); context.subscriptions.push(disposable); } -function $unregisterWrappers(disposables: Array) { - disposables.forEach((disposable) => disposable.dispose()); +function unregisterWrappers(disposables: Disposable[]) { + disposables.forEach(disposable => disposable.dispose()); } -function $registerWrappers(context: vscode.ExtensionContext): Array { - const configWraps = FlutterPlusConfig.getInstance().getCustomWraps(); - const wraps: Array = configWraps.map((wrap) => { - return { - commandId: "flutter-plus.wrapWith." + wrap.name.toLowerCase().replace(/\s/g, "-"), - title: "Wrap with " + wrap.name, - command: () => wrapWith((selectedText) => wrap.body.join("\n").replace("\${widget}", selectedText)), - }; - }); - - const subscriptions = [ - ...wraps.map((wrap) => { - return vscode.commands.registerCommand(wrap.commandId, wrap.command); - }), - vscode.languages.registerCodeActionsProvider(DART_MODE, new CodeActionWrap(wraps)), - ]; - - context.subscriptions.push( - ...subscriptions, - ); - - return subscriptions; +function registerWrapperCommands(context: vscode.ExtensionContext): Disposable[] { + try { + const configWraps = FlutterPlusConfig.getInstance().getCustomWraps(); + const wraps: CodeWrap[] = configWraps.map(wrap => ({ + commandId: `flutter-plus.wrapWith.${wrap.name.toLowerCase().replace(/\s/g, "-")}`, + title: `Wrap with ${wrap.name}`, + command: () => wrapWith(selectedText => wrap.body.join("\n").replace("${widget}", selectedText)), + })).filter((wrap, index, self) => + index === self.findIndex(t => t.commandId === wrap.commandId) + ); + + const subscriptions = wraps.map(wrap => + vscode.commands.registerCommand(wrap.commandId, wrap.command) + ); + + subscriptions.push(vscode.languages.registerCodeActionsProvider(DART_MODE, new CodeActionWrap(wraps))); + context.subscriptions.push(...subscriptions); + + return subscriptions; + } catch (error) { + vscode.window.showErrorMessage(`Error registering wraps: ${error}`); + return []; + } } - -export function deactivate() { } +export function deactivate() {} export type CodeWrap = { - commandId: string, - title: string, - command: () => void, + commandId: string; + title: string; + command: () => void; }; \ No newline at end of file From d58c4403eb0e859b95b52f314f70f34576a24c07 Mon Sep 17 00:00:00 2001 From: Michael Lazebny Date: Wed, 28 Aug 2024 13:19:51 +0200 Subject: [PATCH 2/4] Add warning --- src/extension.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 46c34e8..0aed833 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -69,11 +69,21 @@ function registerWrapperCommands(context: vscode.ExtensionContext): Disposable[] commandId: `flutter-plus.wrapWith.${wrap.name.toLowerCase().replace(/\s/g, "-")}`, title: `Wrap with ${wrap.name}`, command: () => wrapWith(selectedText => wrap.body.join("\n").replace("${widget}", selectedText)), - })).filter((wrap, index, self) => + })); + + const filteredWraps = wraps.filter((wrap, index, self) => index === self.findIndex(t => t.commandId === wrap.commandId) ); - const subscriptions = wraps.map(wrap => + if (filteredWraps.length < wraps.length) { + const duplicates = wraps.filter((wrap, index, self) => + index !== self.findIndex(t => t.commandId === wrap.commandId) + ); + + vscode.window.showWarningMessage(`Multiple wraps with the same command ID found: ${duplicates.map(d => d.commandId).join(", ")}`); + } + + const subscriptions = filteredWraps.map(wrap => vscode.commands.registerCommand(wrap.commandId, wrap.command) ); @@ -87,7 +97,7 @@ function registerWrapperCommands(context: vscode.ExtensionContext): Disposable[] } } -export function deactivate() {} +export function deactivate() { } export type CodeWrap = { commandId: string; From a632ba82f02df9186d80830291eab23819ee1116 Mon Sep 17 00:00:00 2001 From: Michael Lazebny Date: Wed, 28 Aug 2024 13:24:20 +0200 Subject: [PATCH 3/4] Raise version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 67b5796..bf20702 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Flutter Plus", "description": "Extension with various improvements for Flutter", "icon": "assets/logo.png", - "version": "0.6.0", + "version": "0.6.1", "pricing": "Free", "engines": { "vscode": "^1.92.0" From d3253c4325311ad690fda36a2cce4b5b5d197b16 Mon Sep 17 00:00:00 2001 From: Michael Lazebny Date: Wed, 28 Aug 2024 13:28:17 +0200 Subject: [PATCH 4/4] style changes --- src/extension.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 0aed833..0e592ae 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -100,7 +100,7 @@ function registerWrapperCommands(context: vscode.ExtensionContext): Disposable[] export function deactivate() { } export type CodeWrap = { - commandId: string; - title: string; - command: () => void; -}; \ No newline at end of file + commandId: string, + title: string, + command: () => void, +};