postfix templates: doesn't start write action if it isn't necessary

This commit is contained in:
Andrey Starovoyt
2016-12-19 14:34:47 +03:00
parent b99bb074c2
commit eb5ed0f3f1
3 changed files with 31 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -141,7 +141,7 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase {
}
// don't care about errors in multiCaret mode
else if (editor.getCaretModel().getAllCarets().size() == 1) {
LOG.error("Template not found by key: " + key + "; offset = " + callback.getOffset(),
LOG.error("Template not found by key: " + key + "; offset = " + callback.getOffset(),
AttachmentFactory.createAttachment(callback.getFile().getVirtualFile()));
}
return;
@@ -150,7 +150,7 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase {
// don't care about errors in multiCaret mode
if (editor.getCaretModel().getAllCarets().size() == 1) {
LOG.error("Template not found by key: " + key + "; offset = " + callback.getOffset(),
LOG.error("Template not found by key: " + key + "; offset = " + callback.getOffset(),
AttachmentFactory.createAttachment(callback.getFile().getVirtualFile()));
}
}
@@ -222,7 +222,13 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase {
private static void expandTemplate(@NotNull final PostfixTemplate template,
@NotNull final Editor editor,
@NotNull final PsiElement context) {
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(context.getProject(), () -> template.expand(context, editor), "Expand postfix template", POSTFIX_TEMPLATE_ID));
if (template.startInWriteAction()) {
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance()
.executeCommand(context.getProject(), () -> template.expand(context, editor), "Expand postfix template", POSTFIX_TEMPLATE_ID));
}
else {
template.expand(context, editor);
}
}
@@ -273,7 +279,7 @@ public class PostfixLiveTemplate extends CustomLiveTemplateBase {
PsiFile copy = psiFileFactory.createFileFromText(file.getName(), file.getFileType(), fileContentWithoutKey);
if (copy instanceof PsiFileImpl) {
((PsiFileImpl) copy).setOriginalFile(TemplateLanguageUtil.getBaseFile(file));
((PsiFileImpl)copy).setOriginalFile(TemplateLanguageUtil.getBaseFile(file));
}
VirtualFile vFile = copy.getVirtualFile();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,6 +69,10 @@ public abstract class PostfixTemplate {
return myExample;
}
public boolean startInWriteAction() {
return true;
}
public boolean isEnabled(PostfixTemplateProvider provider) {
final PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
return settings != null && settings.isPostfixTemplatesEnabled() && settings.isTemplateEnabled(this, provider);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,14 +66,14 @@ public abstract class PostfixTemplateWithExpressionSelector extends PostfixTempl
}
if (expressions.size() == 1) {
expandForChooseExpression(expressions.get(0), editor);
startExpandForChooseExpression(expressions.get(0), editor);
return;
}
if (ApplicationManager.getApplication().isUnitTestMode()) {
PsiElement item = ContainerUtil.getLastItem(expressions);
assert item != null;
expandForChooseExpression(item, editor);
startExpandForChooseExpression(item, editor);
return;
}
@@ -81,7 +81,7 @@ public abstract class PostfixTemplateWithExpressionSelector extends PostfixTempl
editor, expressions,
new Pass<PsiElement>() {
public void pass(@NotNull final PsiElement e) {
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(e.getProject(), () -> expandForChooseExpression(e, editor), "Expand postfix template", PostfixLiveTemplate.POSTFIX_TEMPLATE_ID));
startExpandForChooseExpression(e, editor);
}
},
mySelector.getRenderer(),
@@ -89,5 +89,16 @@ public abstract class PostfixTemplateWithExpressionSelector extends PostfixTempl
);
}
protected void startExpandForChooseExpression(@NotNull PsiElement expression, @NotNull Editor editor) {
ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance()
.executeCommand(expression.getProject(), () -> expandForChooseExpression(expression, editor), "Expand postfix template",
PostfixLiveTemplate.POSTFIX_TEMPLATE_ID));
}
@Override
public boolean startInWriteAction() {
return false;
}
protected abstract void expandForChooseExpression(@NotNull PsiElement expression, @NotNull Editor editor);
}