Search in sources :

Example 16 with TemplateState

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

the class ShowIntentionsPass method doCollectInformation.

@Override
public void doCollectInformation(@NotNull ProgressIndicator progress) {
    if (!ApplicationManager.getApplication().isUnitTestMode() && !myEditor.getContentComponent().hasFocus())
        return;
    TemplateState state = TemplateManagerImpl.getTemplateState(myEditor);
    if (state != null && !state.isFinished())
        return;
    DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(myProject);
    getIntentionActionsToShow();
    updateActions(codeAnalyzer);
}
Also used : TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 17 with TemplateState

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

the class SelectAllHandler method execute.

@Override
public void execute(Editor editor, DataContext dataContext) {
    final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null && !templateState.isFinished()) {
        final TextRange range = templateState.getCurrentVariableRange();
        final int caretOffset = editor.getCaretModel().getOffset();
        if (range != null && range.getStartOffset() <= caretOffset && caretOffset <= range.getEndOffset()) {
            editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
            return;
        }
    }
    myOriginalHandler.execute(editor, dataContext);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 18 with TemplateState

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

the class TemplateLineStartEndHandler method isEnabledForCaret.

@Override
protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null && !templateState.isFinished()) {
        TextRange range = templateState.getCurrentVariableRange();
        int caretOffset = editor.getCaretModel().getOffset();
        if (range != null && range.containsOffset(caretOffset))
            return true;
    }
    return myOriginalHandler.isEnabled(editor, caret, dataContext);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 19 with TemplateState

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

the class JavaMethodCallElement method setupNonFilledArgumentRemoving.

private static void setupNonFilledArgumentRemoving(final Editor editor, final TemplateState templateState) {
    AtomicInteger maxEditedVariable = new AtomicInteger(-1);
    editor.getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            maxEditedVariable.set(Math.max(maxEditedVariable.get(), templateState.getCurrentVariableNumber()));
        }
    }, templateState);
    templateState.addTemplateStateListener(new TemplateEditingAdapter() {

        @Override
        public void currentVariableChanged(TemplateState templateState, Template template, int oldIndex, int newIndex) {
            maxEditedVariable.set(Math.max(maxEditedVariable.get(), oldIndex));
        }

        @Override
        public void beforeTemplateFinished(TemplateState state, Template template, boolean brokenOff) {
            if (brokenOff) {
                removeUntouchedArguments((TemplateImpl) template);
            }
        }

        private void removeUntouchedArguments(TemplateImpl template) {
            int firstUnchangedVar = maxEditedVariable.get() + 1;
            if (firstUnchangedVar >= template.getVariableCount())
                return;
            TextRange startRange = templateState.getVariableRange(template.getVariableNameAt(firstUnchangedVar));
            TextRange endRange = templateState.getVariableRange(template.getVariableNameAt(template.getVariableCount() - 1));
            if (startRange == null || endRange == null)
                return;
            WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> editor.getDocument().deleteString(startRange.getStartOffset(), endRange.getEndOffset()));
        }
    });
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) TextRange(com.intellij.openapi.util.TextRange) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 20 with TemplateState

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

the class JavaMethodCallElement method startArgumentLiveTemplate.

public static boolean startArgumentLiveTemplate(InsertionContext context, PsiMethod method) {
    if (method.getParameterList().getParametersCount() == 0 || context.getCompletionChar() == Lookup.COMPLETE_STATEMENT_SELECT_CHAR || !Registry.is("java.completion.argument.live.template")) {
        return false;
    }
    Editor editor = context.getEditor();
    context.commitDocument();
    PsiCall call = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiCall.class, false);
    PsiExpressionList argList = call == null ? null : call.getArgumentList();
    if (argList == null || argList.getExpressions().length > 0) {
        return false;
    }
    TextRange argRange = argList.getTextRange();
    int caretOffset = editor.getCaretModel().getOffset();
    if (!argRange.contains(caretOffset)) {
        return false;
    }
    Template template = createArgTemplate(method, caretOffset, argList, argRange);
    context.getDocument().deleteString(caretOffset, argRange.getEndOffset());
    TemplateManager.getInstance(method.getProject()).startTemplate(editor, template);
    TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState == null)
        return false;
    setupNonFilledArgumentRemoving(editor, templateState);
    editor.putUserData(ARGUMENT_TEMPLATE_ACTIVE, method);
    Disposer.register(templateState, () -> {
        if (editor.getUserData(ARGUMENT_TEMPLATE_ACTIVE) == method) {
            editor.putUserData(ARGUMENT_TEMPLATE_ACTIVE, null);
        }
    });
    return true;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

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