Skip to content

Commit

Permalink
Refactor - move commands to a separate directory
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Nov 18, 2023
1 parent 5c36eee commit 815e2fb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 43 deletions.
42 changes: 42 additions & 0 deletions src/commands/generate_entries_for_directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as vscode from "vscode";
import { parseTextToClass } from "../util/dart_class_parser";
import { writeWidgetbookEntry } from "../util/file_util";
import path = require("path");

async function generateWidgetbookEntriesForDirectory(
uri: vscode.Uri
): Promise<void> {
const directoryPath = uri.fsPath;

await generateWidgetbookEntriesForDirectoryImpl(directoryPath);
}

// FIXME Refactor the naming
async function generateWidgetbookEntriesForDirectoryImpl(
directoryPath: string
): Promise<void> {
const files = await vscode.workspace.fs.readDirectory(
vscode.Uri.file(directoryPath)
);

for (const [fileName, fileType] of files) {
if (fileType === vscode.FileType.File) {
const filePath = path.join(directoryPath, fileName);

const fileContent = await vscode.workspace.fs.readFile(
vscode.Uri.file(filePath)
);
const fileContentString = new TextDecoder().decode(fileContent);

const clazz = parseTextToClass(fileContentString);

await writeWidgetbookEntry(clazz);
} else if (fileType === vscode.FileType.Directory) {
const subdirectoryPath = path.join(directoryPath, fileName);

await generateWidgetbookEntriesForDirectoryImpl(subdirectoryPath);
}
}
}

export { generateWidgetbookEntriesForDirectory };
21 changes: 21 additions & 0 deletions src/commands/generate_entry_for_widget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as vscode from "vscode";
import { parseTextToClass } from "../util/dart_class_parser";
import { writeWidgetbookEntry } from "../util/file_util";

async function generateWidgetbookEntryForWidgetInScope(): Promise<void> {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) return;

const text = activeEditor.document.getText();
const currentLineIndex = activeEditor.selection.active.line;
const fileContentFromCurrentLine = text
.split("\n")
.slice(currentLineIndex)
.join("\n");

const clazz = parseTextToClass(fileContentFromCurrentLine);

await writeWidgetbookEntry(clazz);
}

export { generateWidgetbookEntryForWidgetInScope };
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./generate_entries_for_directory";
export * from "./generate_entry_for_widget";
47 changes: 4 additions & 43 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from "vscode";
import { WidgetbookEntriesCodeActions } from "./code_actions/widgetbook_entries";
import { parseTextToClass } from "./util/dart_class_parser";
import { writeWidgetbookEntry } from "./util/file_util";
import path = require("path");
import {
generateWidgetbookEntriesForDirectory,
generateWidgetbookEntryForWidgetInScope,
} from "./commands";

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

Expand All @@ -23,44 +24,4 @@ function activate(context: vscode.ExtensionContext) {
);
}

async function generateWidgetbookEntryForWidgetInScope(): Promise<void> {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) return;

const text = activeEditor.document.getText();
const currentLineIndex = activeEditor.selection.active.line;
const fileContentFromCurrentLine = text
.split("\n")
.slice(currentLineIndex)
.join("\n");

const clazz = parseTextToClass(fileContentFromCurrentLine);

await writeWidgetbookEntry(clazz);
}
async function generateWidgetbookEntriesForDirectory(
uri: vscode.Uri
): Promise<void> {
const directoryPath = uri.fsPath;

const files = await vscode.workspace.fs.readDirectory(
vscode.Uri.file(directoryPath)
);

for (const [fileName, fileType] of files) {
if (fileType === vscode.FileType.File) {
const filePath = path.join(directoryPath, fileName);

const fileContent = await vscode.workspace.fs.readFile(
vscode.Uri.file(filePath)
);
const fileContentString = new TextDecoder().decode(fileContent);

const clazz = parseTextToClass(fileContentString);

await writeWidgetbookEntry(clazz);
}
}
}

export { activate };

0 comments on commit 815e2fb

Please sign in to comment.