UNPKG

filetree-pro

Version:

A powerful file tree generator for VS Code and Cursor. Generate beautiful file trees in multiple formats with smart exclusions and custom configurations.

97 lines (79 loc) 2.73 kB
/** * Convert Text to Tree Command - Converts selected text to tree format * Simple text-to-tree conversion utility * * @module commands * @since 0.3.0 */ import * as vscode from 'vscode'; /** * Convert text to tree command handler * Implements Single Responsibility Principle */ export class ConvertTextCommand { /** * Execute the convert text to tree command */ async execute(): Promise<void> { const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor found'); return; } const selection = editor.selection; const text = editor.document.getText(selection); if (!text.trim()) { vscode.window.showErrorMessage('Please select some text to convert to tree format'); return; } try { const treeText = this.convertTextToTree(text); // Create a new document with the tree const document = await vscode.workspace.openTextDocument({ content: treeText, language: 'markdown', }); await vscode.window.showTextDocument(document); vscode.window.showInformationMessage('Text converted to tree format successfully!'); } catch (error) { vscode.window.showErrorMessage(`Failed to convert text: ${error}`); } } /** * Convert plain text to tree format * @param text - Input text * @returns Formatted tree text * @private */ private convertTextToTree(text: string): string { const lines = text.split('\n').filter(line => line.trim()); const treeLines: string[] = []; treeLines.push('# File Tree from Text'); treeLines.push(''); for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (!line) continue; // Determine if it's a file or folder based on extension or trailing slash const isFolder = line.endsWith('/') || line.endsWith('\\') || !line.includes('.'); const icon = isFolder ? '📁' : '📄'; // Create tree structure with proper indentation const indent = ' '.repeat(Math.min(i, 3)); // Limit depth for readability const prefix = i === lines.length - 1 ? '└── ' : '├── '; treeLines.push(`${indent}${prefix}${icon} ${line}`); } treeLines.push(''); treeLines.push('*Generated by FileTree Pro Extension*'); return treeLines.join('\n'); } /** * Register the command with VS Code * @param context - Extension context * @returns Disposable */ static register(context: vscode.ExtensionContext): vscode.Disposable { const command = new ConvertTextCommand(); return vscode.commands.registerCommand('filetree-pro.convertTextToTree', () => command.execute() ); } }