Open sandboxFocusImprove this doc

Exposing an aspect as a live template

A live template is a custom code action that appears in the code editor menu alongside other code suggestions or refactoring actions offered by your IDE. For more information on using live templates, see Using live templates.

Live templates are created using the Metalama Aspect Framework. Unlike traditional aspects that are executed at compile time by the compiler, live templates are interactively applied by you within the editor, modifying the source code.

Note

A key characteristic of an aspect is that it's applied at compile time and doesn't alter the source code. Consequently, a live template, despite being built with the aspect framework, can't be referred to as an aspect because it deviates from this fundamental principle. To prevent confusion, avoid using the term "aspects" when discussing live templates.

Writing a live template

  1. Write an aspect class as you normally would, keeping in mind the following differences:
    • The aspect class doesn't need to inherit from System.Attribute.
    • Generate idiomatic C# code.
    • Any diagnostics reported by the aspect are ignored.
    • Aspect ordering and requirements aren't considered.
  2. Add the Metalama.Extensions.CodeFixes package to your project.
  3. Ensure the aspect class has a default constructor.
  4. Annotate the class with [EditorExperience(SuggestAsLiveTemplate = true)].
  5. Define the aspect eligibility to ensure the code refactoring is suggested only for relevant declarations. For more details, see Defining the eligibility of aspects.