Skip to content

Instantly share code, notes, and snippets.

@mingsai
Created March 13, 2022 15:23
Show Gist options
  • Save mingsai/9b46c013899798dc5d237675c91b5bf8 to your computer and use it in GitHub Desktop.
Save mingsai/9b46c013899798dc5d237675c91b5bf8 to your computer and use it in GitHub Desktop.
Convert VS Snippets to Android Studio/IntelliJ Live Templates

How to convert Visual Studio Code snippets for use in Android Studio

  1. Create a Dart scratch file (Cmd+Shift+N)
  2. Paste in the body of any snippet into the scratch file
  3. Remove the double quotes and extra commas (Cmd+R is the replace command)
  4. Replace the token placeholders with variable names (${1} => $placeholderName$)
  5. Highlight the transformed code, and go to Tools > Save as Live Template
  6. Add the abbreviation and description as seen in the dialog box below
  7. Make sure to add a default value to each variable used (see the edit variables button on live template dialog below)
  8. Once saved you can use the stkv template anywhere in the Flutter/Dart scope

For example (transform the body of stkv - line 31 from this)

"import 'package:flutter/material.dart';",
      "import 'package:stacked/stacked.dart';",
      "",
      "class ${1} extends StatelessWidget {",
      " const ${1}({Key? key}) : super(key: key);",
      "",
      " @override",
      " Widget build(BuildContext context) {",
      "   return ViewModelBuilder<${1}Model>.reactive(",
      "     builder: (context, model, child) => Scaffold(),",
      "     viewModelBuilder: () => ${1}Model(),",
      "   );",
      " }",
      "}"

The translated snippet should look something like this:

import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';

class $classname$ extends StatelessWidget {
 const $classname$({Key? key}) : super(key: key);

 @override
 Widget build(BuildContext context) {
   return ViewModelBuilder<$viewName$Model>.reactive(
     builder: (context, model, child) => Scaffold(),
     viewModelBuilder: () => $viewName$Model(),
   );
 }
}

Live Template Dialog box

Screen Shot 2022-03-13 at 10 49 40 AM

P.S. Pretty sure this will work for IntelliJ as well since AS is built on top

References Scratch Files Android Studio Code Snippets Editing live templates - IntelliJ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment