[live templates] Add custom macro

This commit is contained in:
JohnHake 2020-03-11 16:09:13 -07:00
parent 9336f8ff87
commit 4e80e8a17b
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,41 @@
// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.intellij.sdk.liveTemplates;
import com.intellij.codeInsight.template.*;
import com.intellij.codeInsight.template.macro.MacroBase;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
public 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(@NotNull Expression[] params, ExpressionContext context, boolean quick) {
String text = getTextResult(params, context, true);
if (text != null) {
if (text.length() > 0) {
text = StringUtil.toTitleCase(text);
}
return new TextResult(text);
}
return null;
}
@Override
public boolean isAcceptableInContext(TemplateContextType context) {
// Might want to be less restrictive in future
return (context instanceof MarkdownContext);
}
}

View File

@ -34,8 +34,12 @@
<vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor> <vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor>
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
<!-- Declare where the IntelliJ Platform should look to find XML-based Live Templates -->
<defaultLiveTemplates file="/liveTemplates/Markdown"/> <defaultLiveTemplates file="/liveTemplates/Markdown"/>
<!-- Declare the implementation for determining the context for applying Live Templates -->
<liveTemplateContext implementation="org.intellij.sdk.liveTemplates.MarkdownContext"/> <liveTemplateContext implementation="org.intellij.sdk.liveTemplates.MarkdownContext"/>
<!-- Declare implementation-based Live Template -->
<liveTemplateMacro implementation="org.intellij.sdk.liveTemplates.TitleCaseMacro"/>
</extensions> </extensions>
</idea-plugin> </idea-plugin>

View File

@ -10,4 +10,15 @@
<option name="MARKDOWN" value="true"/> <option name="MARKDOWN" value="true"/>
</context> </context>
</template> </template>
<template name="tc"
value="$TITLE$"
shortcut="NONE"
description="SDK: Convert to title case"
toReformat="true"
toShortenFQNames="false">
<variable name="TITLE" expression="TitleCase(String)" defaultValue="" alwaysStopAt="true" />
<context>
<option name="MARKDOWN" value="true" />
</context>
</template>
</templateSet> </templateSet>