mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
[live templates] Add custom macro
This commit is contained in:
parent
9336f8ff87
commit
4e80e8a17b
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -34,8 +34,12 @@
|
||||
<vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<!-- Declare where the IntelliJ Platform should look to find XML-based Live Templates -->
|
||||
<defaultLiveTemplates file="/liveTemplates/Markdown"/>
|
||||
<!-- Declare the implementation for determining the context for applying Live Templates -->
|
||||
<liveTemplateContext implementation="org.intellij.sdk.liveTemplates.MarkdownContext"/>
|
||||
<!-- Declare implementation-based Live Template -->
|
||||
<liveTemplateMacro implementation="org.intellij.sdk.liveTemplates.TitleCaseMacro"/>
|
||||
</extensions>
|
||||
|
||||
</idea-plugin>
|
@ -10,4 +10,15 @@
|
||||
<option name="MARKDOWN" value="true"/>
|
||||
</context>
|
||||
</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>
|
Loading…
x
Reference in New Issue
Block a user