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 the Integrated Development Environment (IDE). For more information on using live templates, please refer to 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 the user within the editor, thereby modifying the source code.

Note

A key characteristic of an aspect is that it is applied at compile time and does not alter the source code. Consequently, a live template, despite being built with the aspect framework, cannot be referred to as an aspect as it deviates from this fundamental principle. To prevent any confusion, we recommend avoiding the use of the term 'aspects' when discussing live templates.

To write a live template

  1. Begin by writing an aspect class as you normally would, but keep in mind the following differences:
    • The aspect class does not need to inherit from System.Attribute.
    • Strive to generate idiomatic C# code.
    • Any diagnostics reported by the aspect will be disregarded.
    • Aspect ordering and requirements will not be considered.
  2. Ensure that the aspect class is equipped with a default constructor.
  3. Annotate the class with [EditorExperience(SuggestAsLiveTemplate = true)].
  4. Properly define the aspect eligibility to ensure that the code refactoring is suggested only for relevant declarations. For more details, refer to Defining the eligibility of aspects.