本文整理汇总了Java中com.intellij.codeInsight.template.TemplateManager类的典型用法代码示例。如果您正苦于以下问题:Java TemplateManager类的具体用法?Java TemplateManager怎么用?Java TemplateManager使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TemplateManager类属于com.intellij.codeInsight.template包,在下文中一共展示了TemplateManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: insertMethod
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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: startTemplate
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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);
}
}
示例3: expandForChooseExpression
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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);
}
示例4: startLiveTemplate
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
public static void startLiveTemplate(PsiFile file) {
Project project = file.getProject();
final Editor editor = EditorHelper.openInEditor(file);
if (editor == null) return;
final TemplateImpl template = new TemplateImpl("", file.getText(), "");
template.setInline(true);
int count = template.getSegmentsCount();
if (count == 0) return;
Set<String> variables = new HashSet<String>();
for (int i = 0; i < count; i++) {
variables.add(template.getSegmentName(i));
}
variables.removeAll(TemplateImpl.INTERNAL_VARS_SET);
for (String variable : variables) {
template.addVariable(variable, null, "\"" + variable + "\"", true);
}
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
@Override
public void run() {
editor.getDocument().setText(template.getTemplateText());
}
});
TemplateManager.getInstance(project).startTemplate(editor, template);
}
示例5: change
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
private void change(PsiElement child) {
if (myDeaf) return;
if (child == null || !child.isValid()) return;
final PsiFile file = child.getContainingFile();
if (file != null) {
final MyDocumentChangeAdapter changeBean = myListenerMap.get(file.getVirtualFile());
if (changeBean != null && changeBean.getInitialText() != null) {
final Editor editor = myFileEditorManager.getSelectedTextEditor();
if (editor != null && TemplateManager.getInstance(myProject).getActiveTemplate(editor) != null) return;
final LanguageChangeSignatureDetector detector = LanguageChangeSignatureDetectors.INSTANCE.forLanguage(child.getLanguage());
if (detector == null) return;
if (detector.ignoreChanges(child)) return;
final String currentSignature = detector.extractSignature(child, changeBean.getInitialChangeInfo());
if (currentSignature == null) {
changeBean.reinit();
} else {
changeBean.addSignature(currentSignature);
}
}
}
}
示例6: invoke
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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);
}
示例7: handleInsert
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
@Override
public void handleInsert(final InsertionContext context, LookupElement item) {
if (TemplateManager.getInstance(context.getProject()).getActiveTemplate(context.getEditor()) != null) {
return; // Don't brake the template.
}
context.commitDocument();
XmlFile xmlFile = (XmlFile)context.getFile();
PsiElement element = xmlFile.findElementAt(context.getStartOffset());
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
if (tag == null) return;
XmlTag dependencyTag = tag.getParentTag();
DomElement domElement = DomManager.getDomManager(context.getProject()).getDomElement(dependencyTag);
if (!(domElement instanceof MavenDomDependency)) return;
MavenDomDependency dependency = (MavenDomDependency)domElement;
String artifactId = dependency.getArtifactId().getStringValue();
if (StringUtil.isEmpty(artifactId)) return;
MavenDependencyCompletionUtil.addTypeAndClassifierAndVersion(context, dependency, item.getLookupString(), artifactId);
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:27,代码来源:MavenArtifactCoordinatesGroupIdConverter.java
示例8: doFix
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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);
}
示例9: execute
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
@Override
public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) {
if (charTyped == ' ') {
Project project = PlatformDataKeys.PROJECT.getData(dataContext);
if (project != null) {
TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(project);
if (templateManager != null && templateManager.startTemplate(editor, TemplateSettings.SPACE_CHAR)) {
return;
}
}
}
if (myOriginalHandler != null) {
myOriginalHandler.execute(editor, charTyped, dataContext);
}
}
示例10: hasTemplatePrefix
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
public static boolean hasTemplatePrefix(LookupImpl lookup, char shortcutChar) {
lookup.refreshUi(false, false); // to bring the list model up to date
CompletionProcess completion = CompletionService.getCompletionService().getCurrentCompletion();
if (completion == null || !completion.isAutopopupCompletion()) {
return false;
}
if (lookup.isSelectionTouched()) {
return false;
}
final PsiFile file = lookup.getPsiFile();
if (file == null) return false;
final Editor editor = lookup.getEditor();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
return ((TemplateManagerImpl)TemplateManager.getInstance(file.getProject())).prepareTemplate(editor, shortcutChar, null) != null;
}
示例11: invoke
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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);
}
示例12: doFix
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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 = QuickfixUtil.positionCursor(project, myTargetClass.getContainingFile(), field);
Template template = CreateFieldFromUsageHelper.setupTemplate(field, typeConstraints, myTargetClass, newEditor, context, false);
TemplateManager manager = TemplateManager.getInstance(project);
manager.startTemplate(newEditor, template);
}
示例13: startLiveTemplate
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
public static void startLiveTemplate(@Nonnull PsiFile file, @Nonnull Map<String, String> defaultValues) {
Editor editor = EditorHelper.openInEditor(file);
if (editor == null) return;
TemplateImpl template = new TemplateImpl("", file.getText(), "");
template.setInline(true);
int count = template.getSegmentsCount();
if (count == 0) return;
Set<String> variables = new HashSet<>();
for (int i = 0; i < count; i++) {
variables.add(template.getSegmentName(i));
}
variables.removeAll(TemplateImpl.INTERNAL_VARS_SET);
for (String variable : variables) {
String defaultValue = defaultValues.getOrDefault(variable, variable);
template.addVariable(variable, null, '"' + defaultValue + '"', true);
}
Project project = file.getProject();
WriteCommandAction.runWriteCommandAction(project, () -> editor.getDocument().setText(template.getTemplateText()));
editor.getCaretModel().moveToOffset(0); // ensures caret at the start of the template
TemplateManager.getInstance(project).startTemplate(editor, template);
}
示例14: invoke
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的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);
}
示例15: applyFix
import com.intellij.codeInsight.template.TemplateManager; //导入依赖的package包/类
@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor)
{
final PsiElement element = descriptor.getPsiElement();
final Editor editor = BaseCreateFix.getEditor(project, element.getContainingFile());
if(editor == null)
{
return;
}
final String shortenedType = JSResolveUtil.getShortenedType(this.type, element);
final TemplateManager templateManager = TemplateManager.getInstance(project);
Template template = templateManager.createTemplate("", "", shortenedType + "($SELECTION$)");
template.setToReformat(true);
final int offset = element.getTextOffset();
editor.getSelectionModel().setSelection(offset, offset + element.getTextLength());
editor.getCaretModel().moveToOffset(offset);
templateManager.startTemplate(editor, element.getText(), template);
}