Search in sources :

Example 36 with TemplateState

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

the class InplaceRefactoring method navigateToStarted.

private static void navigateToStarted(final Document oldDocument, final Project project, @Messages.YesNoResult final int exitCode) {
    final PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(oldDocument);
    if (file != null) {
        final VirtualFile virtualFile = file.getVirtualFile();
        if (virtualFile != null) {
            final FileEditor[] editors = FileEditorManager.getInstance(project).getEditors(virtualFile);
            for (FileEditor editor : editors) {
                if (editor instanceof TextEditor) {
                    final Editor textEditor = ((TextEditor) editor).getEditor();
                    final TemplateState templateState = TemplateManagerImpl.getTemplateState(textEditor);
                    if (templateState != null) {
                        if (exitCode == Messages.YES) {
                            final TextRange range = templateState.getVariableRange(PRIMARY_VARIABLE_NAME);
                            if (range != null) {
                                new OpenFileDescriptor(project, virtualFile, range.getStartOffset()).navigate(true);
                                return;
                            }
                        } else if (exitCode > 0) {
                            templateState.gotoEnd();
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) TextEditor(com.intellij.openapi.fileEditor.TextEditor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 37 with TemplateState

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

the class InplaceRefactoring method revertState.

protected void revertState() {
    if (myOldName == null)
        return;
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
        ApplicationManager.getApplication().runWriteAction(() -> {
            final TemplateState state = TemplateManagerImpl.getTemplateState(topLevelEditor);
            assert state != null;
            final int segmentsCount = state.getSegmentsCount();
            final Document document = topLevelEditor.getDocument();
            for (int i = 0; i < segmentsCount; i++) {
                final TextRange segmentRange = state.getSegmentRange(i);
                document.replaceString(segmentRange.getStartOffset(), segmentRange.getEndOffset(), myOldName);
            }
        });
        if (!myProject.isDisposed() && myProject.isOpen()) {
            PsiDocumentManager.getInstance(myProject).commitDocument(topLevelEditor.getDocument());
        }
    }, getCommandName(), null);
}
Also used : TextEditor(com.intellij.openapi.fileEditor.TextEditor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 38 with TemplateState

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

the class MyLookupExpression method calculateResult.

@Override
public Result calculateResult(ExpressionContext context) {
    TemplateState templateState = TemplateManagerImpl.getTemplateState(context.getEditor());
    final TextResult insertedValue = templateState != null ? templateState.getVariableValue(InplaceRefactoring.PRIMARY_VARIABLE_NAME) : null;
    if (insertedValue != null) {
        if (!insertedValue.getText().isEmpty())
            return insertedValue;
    }
    return new TextResult(myName);
}
Also used : TextResult(com.intellij.codeInsight.template.TextResult) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 39 with TemplateState

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

the class VariableInplaceRenamer method restoreSelection.

@Override
protected void restoreSelection() {
    if (mySelectedRange != null) {
        Editor editor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
        TextRange selectedRange;
        if (myEditor instanceof EditorWindow) {
            PsiFile injected = ((EditorWindow) myEditor).getInjectedFile();
            selectedRange = InjectedLanguageManager.getInstance(editor.getProject()).injectedToHost(injected, mySelectedRange);
        } else {
            selectedRange = mySelectedRange;
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(editor);
        if (state != null) {
            for (int i = 0; i < state.getSegmentsCount(); i++) {
                final TextRange segmentRange = state.getSegmentRange(i);
                TextRange intersection = segmentRange.intersection(selectedRange);
                if (intersection != null) {
                    editor.getSelectionModel().setSelection(intersection.getStartOffset(), intersection.getEndOffset());
                    return;
                }
            }
        }
        myEditor.getSelectionModel().setSelection(mySelectedRange.getStartOffset(), mySelectedRange.getEndOffset());
    } else if (!shouldSelectAll()) {
        myEditor.getSelectionModel().removeSelection();
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) EditorWindow(com.intellij.injected.editor.EditorWindow)

Example 40 with TemplateState

use of com.intellij.codeInsight.template.impl.TemplateState 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)

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