Search in sources :

Example 26 with TemplateState

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

the class DartLiveTemplatesTest method expandTemplate.

public void expandTemplate(final Editor editor) {
    final Project project = editor.getProject();
    assertNotNull(project);
    new ListTemplatesAction().actionPerformedImpl(project, editor);
    final LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(editor);
    assertNotNull(lookup);
    lookup.finishLookup(Lookup.NORMAL_SELECT_CHAR);
    TemplateState template = TemplateManagerImpl.getTemplateState(editor);
    if (template != null) {
        Disposer.dispose(template);
    }
}
Also used : Project(com.intellij.openapi.project.Project) LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) ListTemplatesAction(com.intellij.codeInsight.template.impl.actions.ListTemplatesAction) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 27 with TemplateState

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

the class DartIntroduceHandler method performAction.

public void performAction(DartIntroduceOperation operation) {
    final PsiFile file = operation.getFile();
    if (!CommonRefactoringUtil.checkReadOnlyStatus(file)) {
        return;
    }
    final Editor editor = operation.getEditor();
    if (editor.getSettings().isVariableInplaceRenameEnabled()) {
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(operation.getEditor());
        if (templateState != null && !templateState.isFinished()) {
            return;
        }
    }
    PsiElement element1 = null;
    PsiElement element2 = null;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        element1 = file.findElementAt(selectionModel.getSelectionStart());
        element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
        if (element1 instanceof PsiWhiteSpace) {
            int startOffset = element1.getTextRange().getEndOffset();
            element1 = file.findElementAt(startOffset);
        }
        if (element2 instanceof PsiWhiteSpace) {
            int endOffset = element2.getTextRange().getStartOffset();
            element2 = file.findElementAt(endOffset - 1);
        }
    } else {
        if (smartIntroduce(operation)) {
            return;
        }
        final CaretModel caretModel = editor.getCaretModel();
        final Document document = editor.getDocument();
        int lineNumber = document.getLineNumber(caretModel.getOffset());
        if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
            element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
            element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
        }
    }
    final Project project = operation.getProject();
    if (element1 == null || element2 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    element1 = DartRefactoringUtil.getSelectedExpression(project, file, element1, element2);
    if (element1 == null) {
        showCannotPerformError(project, editor);
        return;
    }
    if (!checkIntroduceContext(file, editor, element1)) {
        return;
    }
    operation.setElement(element1);
    performActionOnElement(operation);
}
Also used : Project(com.intellij.openapi.project.Project) CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 28 with TemplateState

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

the class AbstractStepDefinitionCreator method closeActiveTemplateBuilders.

protected void closeActiveTemplateBuilders(PsiFile file) {
    final Project project = file.getProject();
    final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
    final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
    FileEditorManager.getInstance(project).getAllEditors(vFile);
    FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    assert editor != null;
    final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
    final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
    final Template template = templateManager.getActiveTemplate(editor);
    if (templateState != null && template != null) {
        templateState.gotoEnd(false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) TemplateManager(com.intellij.codeInsight.template.TemplateManager) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Template(com.intellij.codeInsight.template.Template)

Example 29 with TemplateState

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

the class AbstractInplaceIntroduceTest method doTestEscape.

protected void doTestEscape(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();
        if (pass != null) {
            pass.pass(introducer);
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        assert state != null;
        state.gotoEnd(true);
        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 30 with TemplateState

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

the class ChangeClassParametersIntention method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) throws IncorrectOperationException {
    final PsiTypeElement typeElement = PsiTreeUtil.getTopmostParentOfType(element, PsiTypeElement.class);
    final PsiReferenceParameterList parameterList = PsiTreeUtil.getParentOfType(typeElement, PsiReferenceParameterList.class);
    if (parameterList != null) {
        final PsiClass aClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
        if (aClass instanceof PsiAnonymousClass) {
            editor.getCaretModel().moveToOffset(aClass.getTextOffset());
            final PsiTypeElement[] typeElements = parameterList.getTypeParameterElements();
            final int changeIdx = ArrayUtil.find(typeElements, typeElement);
            final PsiClassType.ClassResolveResult result = ((PsiAnonymousClass) aClass).getBaseClassType().resolveGenerics();
            final PsiClass baseClass = result.getElement();
            LOG.assertTrue(baseClass != null);
            final PsiTypeParameter typeParameter = baseClass.getTypeParameters()[changeIdx];
            final TemplateBuilderImpl templateBuilder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(aClass);
            final String oldTypeText = typeElement.getText();
            final String varName = "param";
            templateBuilder.replaceElement(typeElement, varName, new TypeExpression(project, new PsiType[] { typeElement.getType() }), true);
            final Template template = templateBuilder.buildInlineTemplate();
            TemplateManager.getInstance(project).startTemplate(editor, template, false, null, new TemplateEditingAdapter() {

                private String myNewType;

                @Override
                public void beforeTemplateFinished(TemplateState state, Template template) {
                    final TextResult value = state.getVariableValue(varName);
                    myNewType = value != null ? value.getText() : "";
                    final int segmentsCount = state.getSegmentsCount();
                    final Document document = state.getEditor().getDocument();
                    ApplicationManager.getApplication().runWriteAction(() -> {
                        for (int i = 0; i < segmentsCount; i++) {
                            final TextRange segmentRange = state.getSegmentRange(i);
                            document.replaceString(segmentRange.getStartOffset(), segmentRange.getEndOffset(), oldTypeText);
                        }
                    });
                }

                @Override
                public void templateFinished(Template template, boolean brokenOff) {
                    if (!brokenOff) {
                        final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
                        try {
                            final PsiType targetParam = elementFactory.createTypeFromText(myNewType, aClass);
                            if (!(targetParam instanceof PsiClassType)) {
                                HintManager.getInstance().showErrorHint(editor, JavaErrorMessages.message("generics.type.argument.cannot.be.of.primitive.type"));
                                return;
                            }
                            final PsiClassType classType = (PsiClassType) targetParam;
                            final PsiClass target = classType.resolve();
                            if (target == null) {
                                HintManager.getInstance().showErrorHint(editor, JavaErrorMessages.message("cannot.resolve.symbol", classType.getPresentableText()));
                                return;
                            }
                            final TypeMigrationRules myRules = new TypeMigrationRules();
                            final PsiSubstitutor substitutor = result.getSubstitutor().put(typeParameter, targetParam);
                            final PsiType targetClassType = elementFactory.createType(baseClass, substitutor);
                            myRules.setBoundScope(new LocalSearchScope(aClass));
                            TypeMigrationProcessor.runHighlightingTypeMigration(project, editor, myRules, ((PsiAnonymousClass) aClass).getBaseClassReference().getParameterList(), targetClassType);
                        } catch (IncorrectOperationException e) {
                            HintManager.getInstance().showErrorHint(editor, "Incorrect type");
                        }
                    }
                }
            });
        }
    }
}
Also used : TypeMigrationRules(com.intellij.refactoring.typeMigration.TypeMigrationRules) Document(com.intellij.openapi.editor.Document) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) TypeExpression(com.intellij.codeInsight.intention.impl.TypeExpression) TextRange(com.intellij.openapi.util.TextRange) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

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