Search in sources :

Example 31 with LookupImpl

use of com.intellij.codeInsight.lookup.impl.LookupImpl in project intellij-community by JetBrains.

the class LightCompletionTestCase method selectItem.

protected void selectItem(LookupElement item, char completionChar) {
    final LookupImpl lookup = (LookupImpl) LookupManager.getInstance(getProject()).getActiveLookup();
    lookup.setCurrentItem(item);
    if (completionChar == 0 || completionChar == '\n' || completionChar == '\t') {
        lookup.finishLookup(completionChar);
    } else {
        type(completionChar);
    }
}
Also used : LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl)

Example 32 with LookupImpl

use of com.intellij.codeInsight.lookup.impl.LookupImpl in project intellij-community by JetBrains.

the class ListTemplatesHandler method showTemplatesLookup.

private static void showTemplatesLookup(final Project project, final Editor editor, final PsiFile file, @NotNull Map<TemplateImpl, String> matchingTemplates, @NotNull MultiMap<String, CustomLiveTemplateLookupElement> customTemplatesLookupElements) {
    LookupImpl lookup = (LookupImpl) LookupManager.getInstance(project).createLookup(editor, LookupElement.EMPTY_ARRAY, "", new TemplatesArranger());
    for (Map.Entry<TemplateImpl, String> entry : matchingTemplates.entrySet()) {
        TemplateImpl template = entry.getKey();
        lookup.addItem(createTemplateElement(template), new PlainPrefixMatcher(StringUtil.notNullize(entry.getValue())));
    }
    for (Map.Entry<String, Collection<CustomLiveTemplateLookupElement>> entry : customTemplatesLookupElements.entrySet()) {
        for (CustomLiveTemplateLookupElement lookupElement : entry.getValue()) {
            lookup.addItem(lookupElement, new PlainPrefixMatcher(entry.getKey()));
        }
    }
    showLookup(lookup, file);
}
Also used : LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) PlainPrefixMatcher(com.intellij.codeInsight.completion.PlainPrefixMatcher) MultiMap(com.intellij.util.containers.MultiMap)

Example 33 with LookupImpl

use of com.intellij.codeInsight.lookup.impl.LookupImpl in project intellij-community by JetBrains.

the class EscapeHandler method execute.

@Override
public void execute(Editor editor, DataContext dataContext) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null && !templateState.isFinished()) {
        SelectionModel selectionModel = editor.getSelectionModel();
        LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
        // the idea behind lookup checking is that if there is a preselected value in lookup 
        // then user might want just to close lookup but not finish a template.
        // E.g. user wants to move to the next template segment by Tab without completion invocation.
        // If there is no selected value in completion that user definitely wants to finish template
        boolean lookupIsEmpty = lookup == null || lookup.getCurrentItem() == null;
        if (!selectionModel.hasSelection() && lookupIsEmpty) {
            CommandProcessor.getInstance().setCurrentCommandName(CodeInsightBundle.message("finish.template.command"));
            templateState.gotoEnd(true);
            return;
        }
    }
    if (myOriginalHandler.isEnabled(editor, dataContext)) {
        myOriginalHandler.execute(editor, dataContext);
    }
}
Also used : LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) SelectionModel(com.intellij.openapi.editor.SelectionModel) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 34 with LookupImpl

use of com.intellij.codeInsight.lookup.impl.LookupImpl in project intellij-community by JetBrains.

the class TemplateState method initListeners.

private void initListeners() {
    if (isDisposed())
        return;
    myEditorDocumentListener = new DocumentAdapter() {

        @Override
        public void beforeDocumentChange(DocumentEvent e) {
            myDocumentChanged = true;
        }
    };
    myLookupListener = new LookupAdapter() {

        @Override
        public void itemSelected(LookupEvent event) {
            if (isCaretOutsideCurrentSegment(null)) {
                if (isCaretInsideNextVariable()) {
                    nextTab();
                } else {
                    gotoEnd(true);
                }
            }
        }
    };
    LookupManager.getInstance(myProject).addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (LookupManager.PROP_ACTIVE_LOOKUP.equals(evt.getPropertyName())) {
                Lookup lookup = (Lookup) evt.getNewValue();
                if (lookup != null) {
                    lookup.addLookupListener(myLookupListener);
                }
            }
        }
    }, this);
    myCommandListener = new CommandAdapter() {

        boolean started = false;

        @Override
        public void commandStarted(CommandEvent event) {
            myDocumentChangesTerminateTemplate = isCaretOutsideCurrentSegment(event.getCommandName());
            started = true;
        }

        @Override
        public void beforeCommandFinished(CommandEvent event) {
            if (started && !isDisposed()) {
                Runnable runnable = () -> afterChangedUpdate();
                final LookupImpl lookup = myEditor != null ? (LookupImpl) LookupManager.getActiveLookup(myEditor) : null;
                if (lookup != null) {
                    lookup.performGuardedChange(runnable);
                } else {
                    runnable.run();
                }
            }
        }
    };
    myCaretListener = new CaretAdapter() {

        @Override
        public void caretAdded(CaretEvent e) {
            if (isMultiCaretMode()) {
                finishTemplateEditing();
            }
        }

        @Override
        public void caretRemoved(CaretEvent e) {
            if (isMultiCaretMode()) {
                finishTemplateEditing();
            }
        }
    };
    if (myEditor != null) {
        myEditor.getCaretModel().addCaretListener(myCaretListener);
    }
    myDocument.addDocumentListener(myEditorDocumentListener, this);
    CommandProcessor.getInstance().addCommandListener(myCommandListener, this);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) CommandAdapter(com.intellij.openapi.command.CommandAdapter) LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) CommandEvent(com.intellij.openapi.command.CommandEvent)

Example 35 with LookupImpl

use of com.intellij.codeInsight.lookup.impl.LookupImpl in project intellij-community by JetBrains.

the class TemplateState method dispose.

@Override
public synchronized void dispose() {
    if (myLookupListener != null) {
        final LookupImpl lookup = myEditor != null ? (LookupImpl) LookupManager.getActiveLookup(myEditor) : null;
        if (lookup != null) {
            lookup.removeLookupListener(myLookupListener);
        }
        myLookupListener = null;
    }
    myEditorDocumentListener = null;
    myCommandListener = null;
    myCaretListener = null;
    myProcessor = null;
    //Avoid the leak of the editor
    releaseAll();
    myDocument = null;
}
Also used : LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl)

Aggregations

LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)39 ListTemplatesAction (com.intellij.codeInsight.template.impl.actions.ListTemplatesAction)5 Document (com.intellij.openapi.editor.Document)5 Editor (com.intellij.openapi.editor.Editor)5 Project (com.intellij.openapi.project.Project)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 PlainPrefixMatcher (com.intellij.codeInsight.completion.PlainPrefixMatcher)2 LookupEx (com.intellij.codeInsight.lookup.LookupEx)2 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 CamelHumpMatcher (com.intellij.codeInsight.completion.impl.CamelHumpMatcher)1 CompletionSorterImpl (com.intellij.codeInsight.completion.impl.CompletionSorterImpl)1 Lookup (com.intellij.codeInsight.lookup.Lookup)1 LookupManager (com.intellij.codeInsight.lookup.LookupManager)1 LiveTemplateLookupElement (com.intellij.codeInsight.template.impl.LiveTemplateLookupElement)1 CommandAdapter (com.intellij.openapi.command.CommandAdapter)1 CommandEvent (com.intellij.openapi.command.CommandEvent)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 DocCommandGroupId (com.intellij.openapi.editor.actionSystem.DocCommandGroupId)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1