I would like to have a snippet in VS Coder for dart that would create for me the content of an export file for all the files contained in a folder. I will explain more:
I would like to do this for Flutter/dart. Currently, when I work on my projects I create a folder structure similar to this:
(I will use pages
as an example but I apply the same mechanism to other folders)
Proyect name
└── .../
└── lib/
├── routes
├── services
├── ...
├── ui/
│ ├── pages/
│ │ ├── page_1.dart
│ │ ├── page_2.dart
│ │ ├── ...
│ │ └── pages.dart
│ └── ...
├── ...
└── main.dart
In the pages.dart
file, I would like to export all the pages contained in the folder, so when I want to use them along the app, I don't need to import them one by one, so I do import 'package:proyect/ui/pages/pages.dart'
and I have available all the pages.
Currently, I have several snippets that create for me parts of code, for example:
"Flutter Page": {
"prefix": "fl-page",
"body": [
"import 'package:flutter/material.dart';",
"",
"class ${TM_FILENAME_BASE/((^[a-z])|_([a-z]))/${2:/upcase}${3:/upcase}/g} extends StatelessWidget {",
" static const String route = '${TM_FILENAME_BASE/^([a-zA-Z]+)_.*\.dart$/${1}/}';",
" const ${TM_FILENAME_BASE/((^[a-z])|_([a-z]))/${2:/upcase}${3:/upcase}/g}({Key? key}) : super(key: key);",
" @override",
" Widget build(BuildContext context) {",
" return const Scaffold(",
" body: Center(",
" child: ${2:Text('${TM_FILENAME_BASE/((^[a-z])|_([a-z]))/${2:/upcase}${3:/upcase}/g}')},",
" ),",
" );",
" }",
"}",
],
"description": "Crea una página de flutter fácilmente"
}
Which creates the page directly taking the name of the file as the widget name removing the underscore and using UpperCamelCase, a route and so on. Well, I would like to have one such that when I use it in pages.dart
I get:
export 'package:<project>/ui/pages/page_1.dart';
export 'package:<project>/ui/pages/page_2.dart';
export 'package:<project>/ui/pages/page_3.dart';
...
I tried looking into the documentation of VS Code, reading about variables such WORKSPACE_NAME
, TM_FILENAME
, TM_FILENAME_BASE
, etc... But I can't get anything clear.
Is there any way I could list all the files inside the pages
directory so it will create all the exports needed?
If it's not using the snippets, would you have any idea or suggestion?
Also, in case you are flutter developer or you are familiar with it, do you have snippets you would like to share or you would not mind?
Thanks a lot in advance!!
I have written the extension File Templates and v1.16.0 contains a feature to iterate a snippet over a glob pattern file list.
If you add the following code to a template or a key binding to the command
templates.pasteTemplate
The files that match the include/exclude glob pattern will be used to insert the snippet for each file with the workspace relative file path set as the clipboard content.
The result will be