diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml
index 85fa8eb9938..1c2b9b48355 100644
--- a/idea/src/META-INF/plugin.xml
+++ b/idea/src/META-INF/plugin.xml
@@ -82,7 +82,9 @@
implementationClass="org.jetbrains.jet.plugin.completion.JetCompletionContributor"/>
+
+
diff --git a/idea/src/org/jetbrains/jet/plugin/liveTemplates/JetLiveTemplateCompletionContributor.java b/idea/src/org/jetbrains/jet/plugin/liveTemplates/JetLiveTemplateCompletionContributor.java
new file mode 100644
index 00000000000..59c5c36ce4a
--- /dev/null
+++ b/idea/src/org/jetbrains/jet/plugin/liveTemplates/JetLiveTemplateCompletionContributor.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2000-2010 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.jet.plugin.liveTemplates;
+
+import com.intellij.codeInsight.completion.*;
+import com.intellij.codeInsight.lookup.LookupElement;
+import com.intellij.codeInsight.template.TemplateContextType;
+import com.intellij.codeInsight.template.impl.LiveTemplateLookupElement;
+import com.intellij.codeInsight.template.impl.TemplateImpl;
+import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
+import com.intellij.codeInsight.template.impl.TemplateSettings;
+import com.intellij.openapi.util.Ref;
+import com.intellij.patterns.PlatformPatterns;
+import com.intellij.psi.PsiFile;
+import com.intellij.util.Consumer;
+import com.intellij.util.ProcessingContext;
+import com.intellij.util.containers.CollectionFactory;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author peter (originally from IDEA platform)
+ * @author Evgeny Gerashchenko
+ * @since 2/2/2012
+ */
+public class JetLiveTemplateCompletionContributor extends CompletionContributor {
+ public JetLiveTemplateCompletionContributor() {
+ extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider() {
+ @Override
+ protected void addCompletions(@NotNull CompletionParameters parameters,
+ ProcessingContext context,
+ @NotNull final CompletionResultSet result) {
+ final PsiFile file = parameters.getPosition().getContainingFile();
+ final int offset = parameters.getOffset();
+ final List templates = listApplicableTemplates(file, offset);
+ final Ref templatesShown = Ref.create(false);
+
+ result.runRemainingContributors(parameters, new Consumer() {
+ @Override
+ public void consume(CompletionResult completionResult) {
+ result.passResult(completionResult);
+ ensureTemplatesShown(templatesShown, templates, result);
+ }
+ });
+
+ ensureTemplatesShown(templatesShown, templates, result);
+ }
+ });
+ }
+
+ private static void ensureTemplatesShown(Ref templatesShown, List templates, CompletionResultSet result) {
+ if (!templatesShown.get()) {
+ templatesShown.set(true);
+ for (final TemplateImpl possible : templates) {
+ result.addElement(new LiveTemplateLookupElement(possible, false));
+ }
+ }
+ }
+
+ private static List listApplicableTemplates(PsiFile file, int offset) {
+ Set contextTypes = TemplateManagerImpl.getApplicableContextTypes(file, offset);
+
+ final ArrayList result = CollectionFactory.arrayList();
+ for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) {
+ if (!template.isDeactivated() && !template.isSelectionTemplate() && TemplateManagerImpl.isApplicable(template, contextTypes)) {
+ result.add(template);
+ }
+ }
+ return result;
+ }
+
+ public static class Skipper extends CompletionPreselectSkipper {
+
+ @Override
+ public boolean skipElement(LookupElement element, CompletionLocation location) {
+ return element instanceof LiveTemplateLookupElement && ((LiveTemplateLookupElement) element).sudden;
+ }
+ }
+
+}