Search in sources :

Example 11 with TemplateState

use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.

the class ShowIntentionActionsHandler method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (editor instanceof EditorWindow) {
        editor = ((EditorWindow) editor).getDelegate();
        file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
    }
    final LookupEx lookup = LookupManager.getActiveLookup(editor);
    if (lookup != null) {
        lookup.showElementActions();
        return;
    }
    final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(project);
    letAutoImportComplete(editor, file, codeAnalyzer);
    ShowIntentionsPass.IntentionsInfo intentions = new ShowIntentionsPass.IntentionsInfo();
    ShowIntentionsPass.getActionsToShow(editor, file, intentions, -1);
    IntentionHintComponent hintComponent = codeAnalyzer.getLastIntentionHint();
    if (hintComponent != null) {
        IntentionHintComponent.PopupUpdateResult result = hintComponent.isForEditor(editor) ? hintComponent.updateActions(intentions) : IntentionHintComponent.PopupUpdateResult.HIDE_AND_RECREATE;
        if (result == IntentionHintComponent.PopupUpdateResult.HIDE_AND_RECREATE) {
            hintComponent.hide();
        }
    }
    if (HintManagerImpl.getInstanceImpl().performCurrentQuestionAction())
        return;
    //intentions check isWritable before modification: if (!file.isWritable()) return;
    TemplateState state = TemplateManagerImpl.getTemplateState(editor);
    if (state != null && !state.isFinished()) {
        return;
    }
    if (!intentions.isEmpty()) {
        IntentionHintComponent.showIntentionHint(project, file, editor, intentions, true);
    }
}
Also used : ShowIntentionsPass(com.intellij.codeInsight.daemon.impl.ShowIntentionsPass) DaemonCodeAnalyzerImpl(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl) LookupEx(com.intellij.codeInsight.lookup.LookupEx) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 12 with TemplateState

use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.

the class MyLookupExpression method initLookupItems.

private static LookupElement[] initLookupItems(LinkedHashSet<String> names, PsiNamedElement elementToRename, PsiElement nameSuggestionContext, final boolean shouldSelectAll) {
    if (names == null) {
        names = new LinkedHashSet<>();
        for (NameSuggestionProvider provider : Extensions.getExtensions(NameSuggestionProvider.EP_NAME)) {
            final SuggestedNameInfo suggestedNameInfo = provider.getSuggestedNames(elementToRename, nameSuggestionContext, names);
            if (suggestedNameInfo != null && provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers()) {
                break;
            }
        }
    }
    final LookupElement[] lookupElements = new LookupElement[names.size()];
    final Iterator<String> iterator = names.iterator();
    for (int i = 0; i < lookupElements.length; i++) {
        final String suggestion = iterator.next();
        lookupElements[i] = LookupElementBuilder.create(suggestion).withInsertHandler(new InsertHandler<LookupElement>() {

            @Override
            public void handleInsert(InsertionContext context, LookupElement item) {
                if (shouldSelectAll)
                    return;
                final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor());
                final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
                if (templateState != null) {
                    final TextRange range = templateState.getCurrentVariableRange();
                    if (range != null) {
                        topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), suggestion);
                    }
                }
            }
        });
    }
    return lookupElements;
}
Also used : PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) TextRange(com.intellij.openapi.util.TextRange) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) LookupElement(com.intellij.codeInsight.lookup.LookupElement) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) Editor(com.intellij.openapi.editor.Editor)

Example 13 with TemplateState

use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.

the class CompletionContributorForInplaceRename method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    final Editor editor = parameters.getEditor();
    final TemplateState state = TemplateManagerImpl.getTemplateState(editor);
    if (state != null) {
        if (editor.getUserData(InplaceRefactoring.INPLACE_RENAMER) != null && parameters.getInvocationCount() == 0) {
            result.stopHere();
        }
    }
}
Also used : Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 14 with TemplateState

use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.

the class InplaceRefactoring method buildTemplateAndStart.

protected boolean buildTemplateAndStart(final Collection<PsiReference> refs, final Collection<Pair<PsiElement, TextRange>> stringUsages, final PsiElement scope, final PsiFile containingFile) {
    final PsiElement context = InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile);
    myScope = context != null ? context.getContainingFile() : scope;
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(myScope);
    PsiElement nameIdentifier = getNameIdentifier();
    int offset = InjectedLanguageUtil.getTopLevelEditor(myEditor).getCaretModel().getOffset();
    PsiElement selectedElement = getSelectedInEditorElement(nameIdentifier, refs, stringUsages, offset);
    boolean subrefOnPrimaryElement = false;
    boolean hasReferenceOnNameIdentifier = false;
    for (PsiReference ref : refs) {
        if (isReferenceAtCaret(selectedElement, ref)) {
            builder.replaceElement(ref.getElement(), getRangeToRename(ref), PRIMARY_VARIABLE_NAME, createLookupExpression(selectedElement), true);
            subrefOnPrimaryElement = true;
            continue;
        }
        addVariable(ref, selectedElement, builder, offset);
        hasReferenceOnNameIdentifier |= isReferenceAtCaret(nameIdentifier, ref);
    }
    if (nameIdentifier != null) {
        hasReferenceOnNameIdentifier |= selectedElement.getTextRange().contains(nameIdentifier.getTextRange());
        if (!subrefOnPrimaryElement || !hasReferenceOnNameIdentifier) {
            addVariable(nameIdentifier, selectedElement, builder);
        }
    }
    for (Pair<PsiElement, TextRange> usage : stringUsages) {
        addVariable(usage.first, usage.second, selectedElement, builder);
    }
    addAdditionalVariables(builder);
    int segmentsLimit = Registry.intValue("inplace.rename.segments.limit", -1);
    if (segmentsLimit != -1 && builder.getElementsCount() > segmentsLimit) {
        return false;
    }
    try {
        myMarkAction = startRename();
    } catch (final StartMarkAction.AlreadyStartedException e) {
        final Document oldDocument = e.getDocument();
        if (oldDocument != myEditor.getDocument()) {
            final int exitCode = Messages.showYesNoCancelDialog(myProject, e.getMessage(), getCommandName(), "Navigate to Started", "Cancel Started", "Cancel", Messages.getErrorIcon());
            if (exitCode == Messages.CANCEL)
                return true;
            navigateToAlreadyStarted(oldDocument, exitCode);
            return true;
        } else {
            if (!ourRenamersStack.isEmpty() && ourRenamersStack.peek() == this) {
                ourRenamersStack.pop();
                if (!ourRenamersStack.empty()) {
                    myOldName = ourRenamersStack.peek().myOldName;
                }
            }
            revertState();
            final TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
            if (templateState != null) {
                templateState.gotoEnd(true);
            }
        }
        return false;
    }
    beforeTemplateStart();
    new WriteCommandAction(myProject, getCommandName()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            startTemplate(builder);
        }
    }.execute();
    if (myBalloon == null) {
        showBalloon();
    }
    return true;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) RelativePoint(com.intellij.ui.awt.RelativePoint) Result(com.intellij.openapi.application.Result) StartMarkAction(com.intellij.openapi.command.impl.StartMarkAction)

Example 15 with TemplateState

use of com.intellij.codeInsight.template.impl.TemplateState in project intellij-community by JetBrains.

the class InplaceRefactoring method highlightTemplateVariables.

private void highlightTemplateVariables(Template template, Editor topLevelEditor) {
    //add highlights
    if (myHighlighters != null) {
        // can be null if finish is called during testing
        Map<TextRange, TextAttributes> rangesToHighlight = new HashMap<>();
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
        if (templateState != null) {
            EditorColorsManager colorsManager = EditorColorsManager.getInstance();
            for (int i = 0; i < templateState.getSegmentsCount(); i++) {
                final TextRange segmentOffset = templateState.getSegmentRange(i);
                final String name = template.getSegmentName(i);
                TextAttributes attributes = null;
                if (name.equals(PRIMARY_VARIABLE_NAME)) {
                    attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES);
                } else if (name.equals(OTHER_VARIABLE_NAME)) {
                    attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
                }
                if (attributes == null)
                    continue;
                rangesToHighlight.put(segmentOffset, attributes);
            }
        }
        addHighlights(rangesToHighlight, topLevelEditor, myHighlighters, HighlightManager.getInstance(myProject));
    }
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) RelativePoint(com.intellij.ui.awt.RelativePoint)

Aggregations

TemplateState (com.intellij.codeInsight.template.impl.TemplateState)47 TextRange (com.intellij.openapi.util.TextRange)14 Editor (com.intellij.openapi.editor.Editor)11 Project (com.intellij.openapi.project.Project)8 AbstractInplaceIntroducer (com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer)6 EditorWindow (com.intellij.injected.editor.EditorWindow)3 Document (com.intellij.openapi.editor.Document)3 SelectionModel (com.intellij.openapi.editor.SelectionModel)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 RelativePoint (com.intellij.ui.awt.RelativePoint)3 TypeExpression (com.intellij.codeInsight.intention.impl.TypeExpression)2 LookupImpl (com.intellij.codeInsight.lookup.impl.LookupImpl)2 TextResult (com.intellij.codeInsight.template.TextResult)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 CaretModel (com.intellij.openapi.editor.CaretModel)2 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)2 DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)2