add API to listen for live-template start

This commit is contained in:
andrey zaytsev
2018-03-05 21:12:42 +03:00
parent da73b29697
commit ea3e2f5374
3 changed files with 24 additions and 2 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.util.PairProcessor;
import com.intellij.util.messages.Topic;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -27,6 +28,8 @@ import org.jetbrains.annotations.Nullable;
import java.util.Map;
public abstract class TemplateManager {
public static final Topic<TemplateManagerListener> TEMPLATE_STARTED_TOPIC = Topic.create("TEMPLATE_STARTED", TemplateManagerListener.class);
public static TemplateManager getInstance(Project project) {
return ServiceManager.getService(project, TemplateManager.class);
}
@@ -0,0 +1,11 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.template;
import com.intellij.codeInsight.template.impl.TemplateState;
import org.jetbrains.annotations.NotNull;
import java.util.EventListener;
public interface TemplateManagerListener extends EventListener {
void templateStarted(@NotNull TemplateState state);
}
@@ -42,7 +42,7 @@ import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.PairProcessor;
import com.intellij.util.containers.ConcurrentFactoryMap;
import com.intellij.util.containers.ContainerUtil;
import java.util.HashMap;
import com.intellij.util.messages.MessageBus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
@@ -53,12 +53,14 @@ import java.util.concurrent.ConcurrentMap;
public class TemplateManagerImpl extends TemplateManager implements Disposable {
private static final TemplateContextType[] ourContextTypes = Extensions.getExtensions(TemplateContextType.EP_NAME);
private final Project myProject;
private final MessageBus myMessageBus;
private boolean myTemplateTesting;
private static final Key<TemplateState> TEMPLATE_STATE_KEY = Key.create("TEMPLATE_STATE_KEY");
public TemplateManagerImpl(Project project) {
public TemplateManagerImpl(Project project, MessageBus messageBus) {
myProject = project;
myMessageBus = messageBus;
final EditorFactoryListener myEditorFactoryListener = new EditorFactoryAdapter() {
@Override
public void editorReleased(@NotNull EditorFactoryEvent event) {
@@ -187,6 +189,7 @@ public class TemplateManagerImpl extends TemplateManager implements Disposable {
editor.getSelectionModel().removeSelection();
}
templateState.start((TemplateImpl)template, processor, predefinedVarValues);
fireTemplateStarted(templateState);
};
if (inSeparateCommand) {
CommandProcessor.getInstance().executeCommand(myProject, r, CodeInsightBundle.message("insert.code.template.command"), null);
@@ -447,9 +450,14 @@ public class TemplateManagerImpl extends TemplateManager implements Disposable {
predefinedVarValues.put(TemplateImpl.ARG, argument);
}
templateState.start(template, processor, predefinedVarValues);
fireTemplateStarted(templateState);
}, CodeInsightBundle.message("insert.code.template.command"), null);
}
private void fireTemplateStarted(TemplateState templateState) {
myMessageBus.syncPublisher(TEMPLATE_STARTED_TOPIC).templateStarted(templateState);
}
private static List<TemplateImpl> filterApplicableCandidates(PsiFile file, int caretOffset, List<TemplateImpl> candidates) {
if (candidates.isEmpty()) {
return candidates;