From e672f586a18a5a58cf781abfbbfb78c497bb62b3 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Fri, 11 Apr 2014 18:53:36 +0400 Subject: [PATCH] WEB-11278 Bad performance expanding emmet --- .../template/CustomTemplateCallback.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java b/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java index 7f9b9c389f2f..41d8aebc7117 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/CustomTemplateCallback.java @@ -15,6 +15,9 @@ */ package com.intellij.codeInsight.template; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.intellij.codeInsight.template.impl.TemplateImpl; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateSettings; @@ -33,6 +36,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutionException; /** * @author Eugene.Kudelevsky @@ -43,10 +47,14 @@ public class CustomTemplateCallback { private final PsiFile myFile; private final int myOffset; private final Project myProject; - private final boolean myInInjectedFragment; - - private FileType myFileType; + + private final LoadingCache myAvailabilityCache = CacheBuilder.newBuilder().weakKeys().build(new CacheLoader() { + @Override + public Boolean load(@NotNull TemplateImpl template) throws Exception { + return isAvailableTemplate(template); + } + }); public CustomTemplateCallback(@NotNull Editor editor, @NotNull PsiFile file, boolean wrapping) { myProject = file.getProject(); @@ -95,13 +103,22 @@ public class CustomTemplateCallback { public List filterApplicableCandidates(Collection candidates) { List result = new ArrayList(); for (TemplateImpl candidate : candidates) { - if (!candidate.isDeactivated() && TemplateManagerImpl.isApplicable(myFile, myOffset, candidate)) { - result.add(candidate); + try { + if (myAvailabilityCache.get(candidate)) { + result.add(candidate); + } + } + catch (ExecutionException ignore) { + // filter error } } return result; } + public boolean isAvailableTemplate(TemplateImpl template) { + return !template.isDeactivated() && TemplateManagerImpl.isApplicable(myFile, myOffset, template); + } + public void startTemplate(Template template, Map predefinedValues, TemplateEditingListener listener) { if(myInInjectedFragment) { template.setToReformat(false); @@ -136,10 +153,7 @@ public class CustomTemplateCallback { @NotNull public FileType getFileType() { - if (myFileType == null) { - myFileType = myFile.getFileType(); - } - return myFileType; + return myFile.getFileType(); } public Project getProject() {