Skip to content

Commit

Permalink
[WIP]: Custom wrap with
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkkiller committed Aug 22, 2024
1 parent 251abcb commit 4d67631
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,32 @@
"language": "dart",
"path": "./snippets/flutter.json"
}
]
],
"configuration": {
"title": "Custom Wraps",
"properties": {
"flutterplus.customWraps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"body": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
}
},
"scripts": {
"compile": "npm run check-types && npm run lint && node esbuild.js",
Expand Down
19 changes: 18 additions & 1 deletion src/code-actions/code-action-wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

import { CodeAction, CodeActionKind, CodeActionProvider, window } from "vscode";
import { getSelectedText } from "../utils";
import { CustomWrapConfig } from "../config/config";

export class CodeActionWrap implements CodeActionProvider {
// create constructor that takes Array<CustomWrapConfig>
// as a parameter and assign it to a private field
private customWraps: Array<CustomWrapConfig>;

constructor(customWraps: Array<CustomWrapConfig>) {
this.customWraps = customWraps;
}

public provideCodeActions(): CodeAction[] {
const editor = window.activeTextEditor;
if (!editor) return [];

const selectedText = editor.document.getText(getSelectedText(editor));
if (selectedText === "") return [];

const customCommands = this.customWraps.map((wrap) => {
return {
command: wrap.id,
title: wrap.name,
};
});

return [
{
command: "flutter-plus.wrap-sizedbox",
Expand All @@ -28,7 +44,8 @@ export class CodeActionWrap implements CodeActionProvider {
{
command: "flutter-plus.wrap-repaintboundary",
title: "Wrap with RepaintBoundary",
}
},
...customCommands,
].map((c) => {
let action = new CodeAction(c.title, CodeActionKind.Refactor);
action.command = {
Expand Down
25 changes: 25 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { workspace } from "vscode";

export type CustomWrapConfig = {
name: string,
id: string,
body: Array<string>,
};

export class FlutterPlusConfig {
private static instance: FlutterPlusConfig;

public static getInstance(): FlutterPlusConfig {
if (!FlutterPlusConfig.instance) {
FlutterPlusConfig.instance = new FlutterPlusConfig();
}
return FlutterPlusConfig.instance;
}

public getCustomWraps(): Array<CustomWrapConfig> {
const config = workspace.getConfiguration('flutterplus');
const customWraps = config.get<Array<CustomWrapConfig>>('customWraps');

return customWraps || [];
}
}
50 changes: 40 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as vscode from 'vscode';

import {
commands,
languages
languages,
Disposable
} from "vscode";

import {
Expand All @@ -16,27 +17,56 @@ import {
import {
CodeActionWrap,
} from './code-actions';
import { FlutterPlusConfig } from './config/config';
import { wrapWith } from './utils';

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

export function activate(context: vscode.ExtensionContext) {
//console.log('Congratulations, your extension "flutter-plus" is now active!');
var commands = $registerCommands();

const disposable = vscode.workspace.onDidChangeConfiguration(event => {
if (!event.affectsConfiguration('flutterplus')) {
return;
}

$unregisterCommands(commands);

commands = $registerCommands();

context.subscriptions.push(
...commands
);
});

context.subscriptions.push(
/* vscode.commands.registerCommand('flutter-plus.helloWorld', () => {
vscode.window.showInformationMessage('Hello World from Flutter Plus!');
}), */
disposable,
...commands,
);
}

function $unregisterCommands(disposables: Array<Disposable>) {
disposables.forEach((disposable) => disposable.dispose());
}

function $registerCommands(): Array<Disposable> {
const customWraps = FlutterPlusConfig.getInstance().getCustomWraps();

const customCommands = customWraps.map((wrap) => {
return commands.registerCommand(wrap.id, () => wrapWith((widget) => wrap.body.join('\n').replace("${widget}", widget)),);
});

return [
...customCommands,
commands.registerCommand("flutter-plus.sealed-states", sealedStates),
commands.registerCommand("flutter-plus.wrap-sizedbox", wrapWithSizedBox),
commands.registerCommand("flutter-plus.wrap-listenablebuilder", wrapWithListenableBuilder),
commands.registerCommand("flutter-plus.wrap-valuelistenablebuilder", wrapWithValueListenableBuilder),
commands.registerCommand("flutter-plus.wrap-repaintboundary", wrapWithRepaintBoundary),

languages.registerCodeActionsProvider(
DART_MODE,
new CodeActionWrap(),
),
);
new CodeActionWrap(customWraps),
),];
}

export function deactivate() { }
export function deactivate() { }

0 comments on commit 4d67631

Please sign in to comment.