A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://plugins.jetbrains.com/docs/intellij/creating-live-template-functions.html below:

Creating New Functions for Live Templates

Creating New Functions for Live Templates

The Predefined Functions are the building blocks for creating Parameterized Templates and Surround Templates. However, sometimes the Predefined Functions are not enough.

This tutorial illustrates how to add custom functions to an IntelliJ Platform plugin and make them available for use by Live Templates. As an example, a function is created to convert a selection to Title Case. Refer to the SDK code sample live_templates.

Implementing a New Function

Under the hood, the predefined functions for Live Templates are called macros. A new custom function for Live Templates is implemented in TitleCaseMacro, which extends MacroBase. Three TitleCaseMacro methods are of particular interest:

final class TitleCaseMacro extends MacroBase { public TitleCaseMacro() { super("titleCase", "titleCase(String)"); } /** * Strictly to uphold contract for constructors in base class. */ private TitleCaseMacro(String name, String description) { super(name, description); } @Override protected Result calculateResult(Expression @NotNull [] params, ExpressionContext context, boolean quick) { // Retrieve the text from the macro or selection, if any is available. String text = getTextResult(params, context, true); if (text == null) { return null; } if (!text.isEmpty()) { // Capitalize the start of every word text = StringUtil.toTitleCase(text); } return new TextResult(text); } @Override public boolean isAcceptableInContext(TemplateContextType context) { // Might want to be less restrictive in future return (context instanceof MarkdownContext); } }

Adding a Live Template

Using the procedures previously discussed for Template Creation and Export the Live Template, add a Live Template to the Markdown.xml file for the plugin. The XML representation of an example Live Template using the new titleCase function is listed below.

There is only one variable, TITLE. The expression for TITLE evaluates to the titleCase function provided by the plugin. The argument to the titleCase function is SELECTION, which tells the IntelliJ Platform to operate on the current selection.

<template name="mc" value="$TITLE$" description="SDK: Convert to title case" toReformat="true" toShortenFQNames="false"> <variable name="TITLE" expression="titleCase(SELECTION)" defaultValue="the quick brown fox" alwaysStopAt="true"/> <context> <option name="MARKDOWN" value="true"/> </context> </template>

Register Extension Point

Using the com.intellij.liveTemplateMacro extension point, register the implementation with the IntelliJ Platform.

<extensions defaultExtensionNs="com.intellij"> <liveTemplateMacro implementation="org.intellij.sdk.liveTemplates.TitleCaseMacro"/> </extensions>

Check Plugin

Now verify the plugin is working correctly.

07 April 2023


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4