本文整理汇总了Java中com.intellij.codeInsight.template.Template类的典型用法代码示例。如果您正苦于以下问题:Java Template类的具体用法?Java Template怎么用?Java Template使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Template类属于com.intellij.codeInsight.template包,在下文中一共展示了Template类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: insertMethod
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
protected void insertMethod(@NotNull Project project, @NotNull Editor editor) {
Template template = TemplateManager.getInstance(project).createTemplate("", "");
template.addTextSegment("public function test");
Expression nameExpr = new ConstantNode("");
template.addVariable("name", nameExpr, nameExpr, true);
template.addTextSegment("()");
PhpLanguageLevel languageLevel = PhpProjectConfigurationFacade.getInstance(project).getLanguageLevel();
if (languageLevel.hasFeature(PhpLanguageFeature.RETURN_VOID)) {
template.addTextSegment(": void");
}
template.addTextSegment("\n{\n");
template.addEndVariable();
template.addTextSegment("\n}");
template.setToIndent(true);
template.setToReformat(true);
template.setToShortenLongNames(true);
TemplateManager.getInstance(project).startTemplate(editor, template, null);
}
示例2: getType
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public PsiType getType() {
final Template template = getTemplate();
String text = template.getTemplateText();
StringBuilder resultingText = new StringBuilder(text);
int segmentsCount = template.getSegmentsCount();
for (int j = segmentsCount - 1; j >= 0; j--) {
if (template.getSegmentName(j).equals(TemplateImpl.END)) {
continue;
}
resultingText.insert(template.getSegmentOffset(j), "xxx");
}
try {
final PsiExpression templateExpression = JavaPsiFacade.getElementFactory(myContext.getProject()).createExpressionFromText(resultingText.toString(), myContext);
return templateExpression.getType();
}
catch (IncorrectOperationException e) { // can happen when text of the template does not form an expression
return null;
}
}
示例3: startTemplate
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
public static void startTemplate(@NotNull final Editor editor,
final Template template,
@NotNull final Project project,
final TemplateEditingListener listener,
final String commandName) {
Runnable runnable = new Runnable() {
@Override
public void run() {
if (project.isDisposed() || editor.isDisposed()) return;
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override
public void run() {
TemplateManager.getInstance(project).startTemplate(editor, template, listener);
}
}, commandName, commandName);
}
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
runnable.run();
}
else {
ApplicationManager.getApplication().invokeLater(runnable);
}
}
示例4: processText
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public void processText(final Project project, final Template template, final Document document, final RangeMarker templateRange,
final Editor editor) {
if (!template.isToShortenLongNames()) return;
try {
PsiDocumentManager.getInstance(project).commitDocument(document);
JavaCodeStyleManager javaStyle = JavaCodeStyleManager.getInstance(project);
final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
assert file != null;
javaStyle.shortenClassReferences(file, templateRange.getStartOffset(), templateRange.getEndOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
示例5: processText
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public void processText(Project project,
Template template,
Document document,
RangeMarker templateRange,
Editor editor) {
if (!template.isToReformat()) return;
PsiDocumentManager.getInstance(project).commitDocument(document);
PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
if (!(file instanceof PsiJavaFile)) return;
CharSequence text = document.getImmutableCharSequence();
int prevChar = CharArrayUtil.shiftBackward(text, templateRange.getStartOffset() - 1, " \t");
int nextChar = CharArrayUtil.shiftForward(text, templateRange.getEndOffset(), " \t");
if (prevChar > 0 && text.charAt(prevChar) == '{' && nextChar < text.length() && text.charAt(nextChar) == '}') {
PsiCodeBlock codeBlock = PsiTreeUtil.findElementOfClassAtOffset(file, prevChar, PsiCodeBlock.class, false);
if (codeBlock != null && codeBlock.getTextRange().getStartOffset() == prevChar) {
PsiJavaToken rBrace = codeBlock.getRBrace();
if (rBrace != null && rBrace.getTextRange().getStartOffset() == nextChar) {
CodeEditUtil.markToReformat(rBrace.getNode(), true);
}
}
}
}
示例6: updateDetails
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
private void updateDetails(final PatternDescriptor descriptor) {
new WriteCommandAction.Simple(myProject) {
@Override
protected void run() throws Throwable {
final Template template = descriptor.getTemplate();
if (template instanceof TemplateImpl) {
String text = ((TemplateImpl)template).getString();
myEditor.getDocument().replaceString(0, myEditor.getDocument().getTextLength(), text);
TemplateEditorUtil.setHighlighter(myEditor, ((TemplateImpl)template).getTemplateContext());
}
else {
myEditor.getDocument().replaceString(0, myEditor.getDocument().getTextLength(), "");
}
}
}.execute();
}
示例7: expandForChooseExpression
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public final void expandForChooseExpression(@NotNull PsiElement expr, @NotNull Editor editor) {
Project project = expr.getProject();
Document document = editor.getDocument();
PsiElement elementForRemoving = getElementToRemove(expr);
document.deleteString(elementForRemoving.getTextRange().getStartOffset(), elementForRemoving.getTextRange().getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
String templateString = getTemplateString(expr);
if (templateString == null) {
PostfixTemplatesUtils.showErrorHint(expr.getProject(), editor);
return;
}
Template template = createTemplate(manager, templateString);
if (shouldAddExpressionToContext()) {
template.addVariable("expr", new TextExpression(expr.getText()), false);
}
setVariables(template, expr);
manager.startTemplate(editor, template);
}
示例8: resetFrom
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
public void resetFrom(TemplateImpl another) {
removeAllParsed();
toParseSegments = another.toParseSegments;
myKey = another.getKey();
myString = another.myString;
myTemplateText = another.myTemplateText;
myGroupName = another.myGroupName;
myId = another.myId;
myDescription = another.myDescription;
myShortcutChar = another.myShortcutChar;
isToReformat = another.isToReformat;
isToShortenLongNames = another.isToShortenLongNames;
myIsInline = another.myIsInline;
myTemplateContext = another.myTemplateContext.createCopy();
isDeactivated = another.isDeactivated;
for (Property property : Property.values()) {
boolean value = another.getValue(property);
if (value != Template.getDefaultValue(property)) {
setValue(property, value);
}
}
for (Variable variable : another.myVariables) {
addVariable(variable.getName(), variable.getExpressionString(), variable.getDefaultValueString(), variable.isAlwaysStopAt());
}
}
示例9: invoke
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
final XmlTag rootTag = myTargetFile.getDocument().getRootTag();
OpenFileDescriptor descriptor = new OpenFileDescriptor(
project,
myTargetFile.getVirtualFile(),
rootTag.getValue().getTextRange().getEndOffset()
);
Editor targetEditor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
TemplateManager manager = TemplateManager.getInstance(project);
final Template template = manager.createTemplate("", "");
addTextTo(template, rootTag);
manager.startTemplate(targetEditor, template);
}
示例10: doFix
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
protected void doFix(@NotNull Project project,
@NotNull @GrModifier.ModifierConstant String[] modifiers,
@NotNull @NonNls String fieldName,
@NotNull TypeConstraint[] typeConstraints,
@NotNull PsiElement context) throws IncorrectOperationException {
JVMElementFactory factory = JVMElementFactories.getFactory(myTargetClass.getLanguage(), project);
if (factory == null) return;
PsiField field = factory.createField(fieldName, PsiType.INT);
if (myTargetClass instanceof GroovyScriptClass) {
field.getModifierList().addAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD);
}
for (@GrModifier.ModifierConstant String modifier : modifiers) {
PsiUtil.setModifierProperty(field, modifier, true);
}
field = CreateFieldFromUsageHelper.insertField(myTargetClass, field, context);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(field.getParent());
Editor newEditor = IntentionUtils.positionCursor(project, myTargetClass.getContainingFile(), field);
Template template = CreateFieldFromUsageHelper.setupTemplate(field, typeConstraints, myTargetClass, newEditor, context, false);
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(newEditor, template);
}
示例11: processText
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public void processText(final Project project,
final Template template,
final Document document,
final RangeMarker templateRange,
final Editor editor) {
if (!template.isToShortenLongNames()) return;
try {
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
if (file instanceof GroovyFile) {
JavaCodeStyleManager.getInstance(project).shortenClassReferences(file, templateRange.getStartOffset(),templateRange.getEndOffset());
}
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
示例12: addExprVariable
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
protected void addExprVariable(@NotNull PsiElement expr, Template template) {
ToStringIfNeedMacro toStringIfNeedMacro = new ToStringIfNeedMacro();
MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro);
macroCallNode.addParameter(new ConstantNode(expr.getText()));
MacroCallNode node = new MacroCallNode(new VariableOfTypeMacro());
node.addParameter(new ConstantNode(VIEW.toString()));
template.addVariable("view", node, new EmptyNode(), false);
template.addVariable("expr", macroCallNode, false);
if (mWithAction) {
template.addVariable("actionText", new EmptyNode(), false);
template.addVariable("action", new EmptyNode(), false);
}
}
示例13: getType
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public PsiType getType() {
final Template template = getObject();
String text = template.getTemplateText();
StringBuilder resultingText = new StringBuilder(text);
int segmentsCount = template.getSegmentsCount();
for (int j = segmentsCount - 1; j >= 0; j--) {
if (template.getSegmentName(j).equals(TemplateImpl.END)) {
continue;
}
int segmentOffset = template.getSegmentOffset(j);
resultingText.insert(segmentOffset, PLACEHOLDER);
}
try {
final PsiExpression templateExpression = JavaPsiFacade.getElementFactory(myContext.getProject()).createExpressionFromText(resultingText.toString(), myContext);
return templateExpression.getType();
}
catch (IncorrectOperationException e) { // can happen when text of the template does not form an expression
return null;
}
}
示例14: copy
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
@Override
public TemplateImpl copy() {
TemplateImpl template = new TemplateImpl(myKey, myString, myGroupName);
template.myId = myId;
template.myDescription = myDescription;
template.myShortcutChar = myShortcutChar;
template.isToReformat = isToReformat;
template.isToShortenLongNames = isToShortenLongNames;
template.myIsInline = myIsInline;
template.myTemplateContext = myTemplateContext.createCopy();
template.isDeactivated = isDeactivated;
for (Property property : Property.values()) {
boolean value = getValue(property);
if (value != Template.getDefaultValue(property)) {
template.setValue(property, value);
}
}
for (Variable variable : myVariables) {
template.addVariable(variable.getName(), variable.getExpressionString(), variable.getDefaultValueString(), variable.isAlwaysStopAt());
}
return template;
}
示例15: invoke
import com.intellij.codeInsight.template.Template; //导入依赖的package包/类
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return;
final XmlTag rootTag = myTargetFile.getDocument().getRootTag();
OpenFileDescriptor descriptor = new OpenFileDescriptor(
project,
myTargetFile.getVirtualFile(),
rootTag.getValue().getTextRange().getEndOffset()
);
Editor targetEditor = FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
TemplateManager manager = TemplateManager.getInstance(project);
final Template template = manager.createTemplate("", "");
addTextTo(template, rootTag);
manager.startTemplate(targetEditor, template);
}