Search in sources :

Example 31 with TemplateState

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

the class InplaceIntroduceParameterTest method testLocalInsideAnonymous1.

public void testLocalInsideAnonymous1() throws Exception {
    final Pass<AbstractInplaceIntroducer> pass = new Pass<AbstractInplaceIntroducer>() {

        @Override
        public void pass(AbstractInplaceIntroducer inplaceIntroducePopup) {
        }
    };
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        //ensure extract local var
        final MyIntroduceHandler introduceHandler = createIntroduceHandler();
        introduceHandler.invokeImpl(LightPlatformTestCase.getProject(), getLocalVariableFromEditor(), getEditor());
        final AbstractInplaceIntroducer introducer = introduceHandler.getInplaceIntroducer();
        pass.pass(introducer);
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        assert state != null;
        state.gotoEnd(false);
        checkResultByFile(getBasePath() + name + "_after" + getExtension());
    } finally {
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : Pass(com.intellij.openapi.util.Pass) AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 32 with TemplateState

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

the class InplaceIntroduceVariableTest method doTestReplaceChoice.

private void doTestReplaceChoice(IntroduceVariableBase.JavaReplaceChoice choice, Pass<AbstractInplaceIntroducer> pass) {
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        MyIntroduceHandler handler = createIntroduceHandler();
        ((MyIntroduceVariableHandler) handler).setChoice(choice);
        final AbstractInplaceIntroducer introducer = invokeRefactoring(handler);
        if (pass != null) {
            pass.pass(introducer);
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        assert state != null;
        state.gotoEnd(false);
        checkResultByFile(getBasePath() + name + "_after" + getExtension());
    } finally {
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 33 with TemplateState

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

the class InplaceIntroduceVariableTest method doTestStopEditing.

private void doTestStopEditing(Pass<AbstractInplaceIntroducer> pass) {
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        final AbstractInplaceIntroducer introducer = invokeRefactoring();
        pass.pass(introducer);
        checkResultByFile(getBasePath() + name + "_after" + getExtension());
    } finally {
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        if (state != null) {
            state.gotoEnd(true);
        }
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 34 with TemplateState

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

the class AbstractInplaceIntroducer method stopIntroduce.

public void stopIntroduce(Editor editor) {
    final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    if (templateState != null) {
        final Runnable runnable = () -> templateState.gotoEnd(true);
        CommandProcessor.getInstance().executeCommand(myProject, runnable, getCommandName(), getCommandName());
    }
}
Also used : TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 35 with TemplateState

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

the class AbstractInplaceIntroducer method startInplaceIntroduceTemplate.

/**
   * Begins the in-place refactoring operation.
   *
   * @return true if the in-place refactoring was successfully started, false if it failed to start and a dialog should be shown instead.
   */
public boolean startInplaceIntroduceTemplate() {
    final boolean replaceAllOccurrences = isReplaceAllOccurrences();
    final Ref<Boolean> result = new Ref<>();
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        final String[] names = suggestNames(replaceAllOccurrences, getLocalVariable());
        final V variable = createFieldToStartTemplateOn(replaceAllOccurrences, names);
        boolean started = false;
        if (variable != null) {
            int caretOffset = getCaretOffset();
            myEditor.getCaretModel().moveToOffset(caretOffset);
            myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
            final LinkedHashSet<String> nameSuggestions = new LinkedHashSet<>();
            nameSuggestions.add(variable.getName());
            nameSuggestions.addAll(Arrays.asList(names));
            initOccurrencesMarkers();
            setElementToRename(variable);
            updateTitle(getVariable());
            started = super.performInplaceRefactoring(nameSuggestions);
            if (started) {
                onRenameTemplateStarted();
                myDocumentAdapter = new DocumentAdapter() {

                    @Override
                    public void documentChanged(DocumentEvent e) {
                        if (myPreview == null)
                            return;
                        final TemplateState templateState = TemplateManagerImpl.getTemplateState(myEditor);
                        if (templateState != null) {
                            final TextResult value = templateState.getVariableValue(InplaceRefactoring.PRIMARY_VARIABLE_NAME);
                            if (value != null) {
                                updateTitle(getVariable(), value.getText());
                            }
                        }
                    }
                };
                myEditor.getDocument().addDocumentListener(myDocumentAdapter);
                updateTitle(getVariable());
                if (TemplateManagerImpl.getTemplateState(myEditor) != null) {
                    myEditor.putUserData(ACTIVE_INTRODUCE, this);
                }
            }
        }
        result.set(started);
        if (!started) {
            finish(true);
        }
    }, getCommandName(), getCommandName());
    return result.get();
}
Also used : TextResult(com.intellij.codeInsight.template.TextResult) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Ref(com.intellij.openapi.util.Ref)

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