Skip to content

Commit 1740afb

Browse files
authored
Merge pull request #10 from PlugFox/fix/add-filter
fix: filter wraps for uniqueness
2 parents dbee06a + d3253c4 commit 1740afb

File tree

2 files changed

+48
-78
lines changed

2 files changed

+48
-78
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Flutter Plus",
44
"description": "Extension with various improvements for Flutter",
55
"icon": "assets/logo.png",
6-
"version": "0.6.0",
6+
"version": "0.6.1",
77
"pricing": "Free",
88
"engines": {
99
"vscode": "^1.92.0"

src/extension.ts

Lines changed: 47 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,40 @@
11
import * as vscode from 'vscode';
2-
3-
import {
4-
Disposable
5-
} from "vscode";
6-
7-
import {
8-
sealedStates
9-
} from "./commands";
10-
2+
import { Disposable } from "vscode";
3+
import { sealedStates } from "./commands";
114
import { FlutterPlusConfig } from './config/config';
125
import { wrapWith } from './utils';
13-
14-
15-
import {
16-
dartCodeExtensionIdentifier,
17-
flutterExtensionIdentifier,
18-
} from "./constants";
19-
20-
/* import fs from 'fs';
21-
import path from 'path'; */
22-
6+
import { dartCodeExtensionIdentifier, flutterExtensionIdentifier } from "./constants";
237
import { CodeActionWrap } from './code-actions';
24-
import {
25-
SdkCommands,
26-
} from './utils';
8+
import { SdkCommands } from './utils';
279

2810
const DART_MODE = { language: "dart", scheme: "file" };
2911

3012
export async function activate(context: vscode.ExtensionContext): Promise<void> {
3113
// Ensure we have a Dart extension.
3214
const dartExt = vscode.extensions.getExtension(dartCodeExtensionIdentifier);
3315
if (!dartExt) {
34-
// This should not happen since the Flutter extension has a dependency on the Dart one
35-
// but just in case, we'd like to give a useful error message.
36-
throw new Error("The Dart extension is not installed, Flutter extension is unable to activate.");
16+
vscode.window.showErrorMessage("The Dart extension is not installed. Flutter extension cannot be activated.");
17+
return;
3718
}
3819
await dartExt.activate();
20+
3921
if (!dartExt.exports) {
40-
console.error("The Dart extension did not provide an exported API. Maybe it failed to activate or is not the latest version?");
22+
console.error("The Dart extension did not provide an exported API. It may have failed to activate or is not the latest version.");
4123
return;
4224
}
4325

4426
// Ensure we have a Flutter extension.
4527
const flutterExt = vscode.extensions.getExtension(flutterExtensionIdentifier);
4628
if (!flutterExt) {
47-
// This should not happen since the Flutter extension has a dependency on the Dart one
48-
// but just in case, we'd like to give a useful error message.
49-
throw new Error("The Flutter extension is not installed, Flutter Plus extension is unable to activate.");
29+
vscode.window.showErrorMessage("The Flutter extension is not installed. Flutter Plus extension cannot be activated.");
30+
return;
5031
}
5132
await flutterExt.activate();
5233

5334
// Register SDK commands.
5435
const sdkCommands = new SdkCommands(context, dartExt.exports);
5536

56-
//console.log('Congratulations, your extension "flutter-plus" is now active!');
57-
5837
registerCommands(context);
59-
//registerActionButtons(context);
6038
registerWrappers(context);
6139
}
6240

@@ -67,70 +45,62 @@ function registerCommands(context: vscode.ExtensionContext) {
6745
);
6846
}
6947

70-
/* function registerActionButtons(context: vscode.ExtensionContext) {
71-
function runPubGet() {
72-
// Example of running `dart pub get` or `flutter pub get`
73-
const pubspecPath = path.join(vscode.workspace.workspaceFolders?.[0]?.uri.fsPath || '', 'pubspec.yaml');
74-
if (fs.existsSync(pubspecPath)) {
75-
const pubspecContent = fs.readFileSync(pubspecPath, 'utf8');
76-
const isFlutterApp = pubspecContent.includes('flutter:');
77-
const command = isFlutterApp ? 'flutter pub get' : 'dart pub get';
78-
executeCommand(command);
79-
}
80-
}
81-
context.subscriptions.push(vscode.commands.registerCommand('flutter-plus.pub-get', () => {
82-
runPubGet();
83-
}));
84-
} */
85-
8648
/// Register all wrappers (Wrap With...).
8749
function registerWrappers(context: vscode.ExtensionContext) {
88-
var wraps = $registerWrappers(context);
50+
let wrappers = registerWrapperCommands(context);
8951
const disposable = vscode.workspace.onDidChangeConfiguration(event => {
90-
if (!event.affectsConfiguration('flutter-plus')) {
91-
return;
52+
if (event.affectsConfiguration('flutter-plus')) {
53+
unregisterWrappers(wrappers);
54+
wrappers = registerWrapperCommands(context);
9255
}
93-
94-
$unregisterWrappers(wraps);
95-
wraps = $registerWrappers(context);
9656
});
9757

9858
context.subscriptions.push(disposable);
9959
}
10060

101-
function $unregisterWrappers(disposables: Array<Disposable>) {
102-
disposables.forEach((disposable) => disposable.dispose());
61+
function unregisterWrappers(disposables: Disposable[]) {
62+
disposables.forEach(disposable => disposable.dispose());
10363
}
10464

105-
function $registerWrappers(context: vscode.ExtensionContext): Array<Disposable> {
106-
const configWraps = FlutterPlusConfig.getInstance().getCustomWraps();
107-
const wraps: Array<CodeWrap> = configWraps.map((wrap) => {
108-
return {
109-
commandId: "flutter-plus.wrapWith." + wrap.name.toLowerCase().replace(/\s/g, "-"),
110-
title: "Wrap with " + wrap.name,
111-
command: () => wrapWith((selectedText) => wrap.body.join("\n").replace("\${widget}", selectedText)),
112-
};
113-
});
65+
function registerWrapperCommands(context: vscode.ExtensionContext): Disposable[] {
66+
try {
67+
const configWraps = FlutterPlusConfig.getInstance().getCustomWraps();
68+
const wraps: CodeWrap[] = configWraps.map(wrap => ({
69+
commandId: `flutter-plus.wrapWith.${wrap.name.toLowerCase().replace(/\s/g, "-")}`,
70+
title: `Wrap with ${wrap.name}`,
71+
command: () => wrapWith(selectedText => wrap.body.join("\n").replace("${widget}", selectedText)),
72+
}));
73+
74+
const filteredWraps = wraps.filter((wrap, index, self) =>
75+
index === self.findIndex(t => t.commandId === wrap.commandId)
76+
);
77+
78+
if (filteredWraps.length < wraps.length) {
79+
const duplicates = wraps.filter((wrap, index, self) =>
80+
index !== self.findIndex(t => t.commandId === wrap.commandId)
81+
);
82+
83+
vscode.window.showWarningMessage(`Multiple wraps with the same command ID found: ${duplicates.map(d => d.commandId).join(", ")}`);
84+
}
11485

115-
const subscriptions = [
116-
...wraps.map((wrap) => {
117-
return vscode.commands.registerCommand(wrap.commandId, wrap.command);
118-
}),
119-
vscode.languages.registerCodeActionsProvider(DART_MODE, new CodeActionWrap(wraps)),
120-
];
86+
const subscriptions = filteredWraps.map(wrap =>
87+
vscode.commands.registerCommand(wrap.commandId, wrap.command)
88+
);
12189

122-
context.subscriptions.push(
123-
...subscriptions,
124-
);
90+
subscriptions.push(vscode.languages.registerCodeActionsProvider(DART_MODE, new CodeActionWrap(wraps)));
91+
context.subscriptions.push(...subscriptions);
12592

126-
return subscriptions;
93+
return subscriptions;
94+
} catch (error) {
95+
vscode.window.showErrorMessage(`Error registering wraps: ${error}`);
96+
return [];
97+
}
12798
}
12899

129-
130100
export function deactivate() { }
131101

132102
export type CodeWrap = {
133103
commandId: string,
134104
title: string,
135105
command: () => void,
136-
};
106+
};

0 commit comments

Comments
 (0)