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

fix: filter wraps for uniqueness #10

Merged
merged 4 commits into from
Aug 28, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
124 changes: 47 additions & 77 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,40 @@
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" };

export async function activate(context: vscode.ExtensionContext): Promise<void> {
// 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);
}

Expand All @@ -67,70 +45,62 @@ 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<Disposable>) {
disposables.forEach((disposable) => disposable.dispose());
function unregisterWrappers(disposables: Disposable[]) {
disposables.forEach(disposable => disposable.dispose());
}

function $registerWrappers(context: vscode.ExtensionContext): Array<Disposable> {
const configWraps = FlutterPlusConfig.getInstance().getCustomWraps();
const wraps: Array<CodeWrap> = 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)),
};
});
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)),
}));

const filteredWraps = wraps.filter((wrap, index, self) =>
index === self.findIndex(t => t.commandId === wrap.commandId)
);

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 = [
...wraps.map((wrap) => {
return vscode.commands.registerCommand(wrap.commandId, wrap.command);
}),
vscode.languages.registerCodeActionsProvider(DART_MODE, new CodeActionWrap(wraps)),
];
const subscriptions = filteredWraps.map(wrap =>
vscode.commands.registerCommand(wrap.commandId, wrap.command)
);

context.subscriptions.push(
...subscriptions,
);
subscriptions.push(vscode.languages.registerCodeActionsProvider(DART_MODE, new CodeActionWrap(wraps)));
context.subscriptions.push(...subscriptions);

return subscriptions;
return subscriptions;
} catch (error) {
vscode.window.showErrorMessage(`Error registering wraps: ${error}`);
return [];
}
}


export function deactivate() { }

export type CodeWrap = {
commandId: string,
title: string,
command: () => void,
};
};