package net.imagej.legacy.plugin;

import net.imagej.ImageJService;
import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.CompletionProvider;
import org.scijava.plugin.AbstractPTService;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.PluginInfo;
import org.scijava.service.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * MacroExtensionAutoCompletionService
 *
 * This service manages extensions to the auto-complete pull down of the script editor.
 *
 * Author: @haesleinhuepf
 * December 2018
 */


@Plugin(type = Service.class)
public class MacroExtensionAutoCompletionService  extends AbstractPTService<MacroExtensionAutoCompletionPlugin> implements ImageJService {

    private HashMap<String, PluginInfo<MacroExtensionAutoCompletionPlugin>> macroExtensionAutoCompletionPlugins = new HashMap<>();

    @Override
    public void initialize() {
        for (final PluginInfo<MacroExtensionAutoCompletionPlugin> info : getPlugins()) {
            String name = info.getName();
            if (name == null || name.isEmpty()) {
                name = info.getClassName();
            }
            macroExtensionAutoCompletionPlugins.put(name, info);
        }
    }

    public List<BasicCompletion> getCompletions(CompletionProvider completionProvider) {
        ArrayList<BasicCompletion> completions = new ArrayList<BasicCompletion>();
        System.out.println("Completions search");
        for (String key : macroExtensionAutoCompletionPlugins.keySet()) {
            System.out.println("macroext " + key);
            PluginInfo<MacroExtensionAutoCompletionPlugin> info = macroExtensionAutoCompletionPlugins.get(key);

            List<BasicCompletion> list = pluginService().createInstance(info).getCompletions(completionProvider);
            completions.addAll(list);

        }
        return completions;
    }

    @Override
    public Class<MacroExtensionAutoCompletionPlugin> getPluginType() {
        return MacroExtensionAutoCompletionPlugin.class;
    }
}

@@ -890,48 +890,133 @@
     
-@Override%0A    public void initialize() %7B
+boolean initialized = false;%0A%0A    private void initializeService() %7B%0A        if (initialized) %7B%0A            return;%0A        %7D
 %0A   
@@ -1322,24 +1322,52 @@
 ;%0A        %7D%0A
+        initialized = true;%0A
     %7D%0A%0A    p
@@ -1446,24 +1446,53 @@
 Provider) %7B%0A
+        initializeService();%0A
         Arra

make the MacroExtensionAutoCompletionService initialize the lazy way