Search in sources :

Example 41 with TemplateState

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

the class TemplateLineStartEndHandler method doExecute.

@Override
protected void doExecute(Editor editor, @Nullable Caret caret, 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 && shouldStayInsideVariable(range, caretOffset)) {
            int selectionOffset = editor.getSelectionModel().getLeadSelectionOffset();
            int offsetToMove = myIsHomeHandler ? range.getStartOffset() : range.getEndOffset();
            LogicalPosition logicalPosition = editor.offsetToLogicalPosition(offsetToMove).leanForward(myIsHomeHandler);
            editor.getCaretModel().moveToLogicalPosition(logicalPosition);
            EditorModificationUtil.scrollToCaret(editor);
            if (myWithSelection) {
                editor.getSelectionModel().setSelection(selectionOffset, offsetToMove);
            } else {
                editor.getSelectionModel().removeSelection();
            }
            return;
        }
    }
    myOriginalHandler.execute(editor, caret, dataContext);
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) TextRange(com.intellij.openapi.util.TextRange) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 42 with TemplateState

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

the class ClassNameCompletionTest method testTypeParametersTemplate.

public void testTypeParametersTemplate() throws Exception {
    createClass("package pack; public interface Foo<T> {void foo(T t};");
    String path = "/template";
    TemplateManagerImpl.setTemplateTesting(getProject(), myFixture.getTestRootDisposable());
    configureByFile(path + "/before1.java");
    selectItem(myItems[0]);
    TemplateState state = TemplateManagerImpl.getTemplateState(myFixture.getEditor());
    type("String");
    assert state != null;
    state.gotoEnd(false);
    checkResultByFile(path + "/after1.java");
    configureByFile(path + "/before2.java");
    selectItem(myItems[0]);
    assert TemplateManagerImpl.getTemplateState(myFixture.getEditor()) == null;
    checkResultByFile(path + "/after2.java");
}
Also used : TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 43 with TemplateState

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

the class RenameLocalTest method doRenameWrongRef.

private void doRenameWrongRef(final String newName) throws Exception {
    final String name = getTestName(false);
    configureByFile(BASE_PATH + name + ".java");
    TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
    new RenameWrongRefHandler().invoke(getProject(), getEditor(), getFile(), null);
    final TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
    assert state != null;
    final TextRange range = state.getCurrentVariableRange();
    assert range != null;
    new WriteCommandAction.Simple(getProject()) {

        @Override
        protected void run() throws Throwable {
            getEditor().getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), newName);
        }
    }.execute().throwException();
    state.gotoEnd(false);
    checkResultByFile(BASE_PATH + name + "_after.java");
}
Also used : RenameWrongRefHandler(com.intellij.refactoring.rename.RenameWrongRefHandler) TextRange(com.intellij.openapi.util.TextRange) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 44 with TemplateState

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

the class RenameMembersInplaceTest method testSameNamedMethodsInOneFile.

public void testSameNamedMethodsInOneFile() throws Exception {
    configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
    final PsiElement element = TargetElementUtil.findTargetElement(myEditor, TargetElementUtil.getInstance().getAllAccepted());
    assertNotNull(element);
    Editor editor = getEditor();
    Project project = editor.getProject();
    TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
    new MemberInplaceRenameHandler().doRename(element, editor, DataManager.getInstance().getDataContext(editor.getComponent()));
    TemplateState state = TemplateManagerImpl.getTemplateState(editor);
    assert state != null;
    assertEquals(2, state.getSegmentsCount());
    final TextRange range = state.getCurrentVariableRange();
    assert range != null;
    final Editor finalEditor = editor;
    new WriteCommandAction.Simple(project) {

        @Override
        protected void run() throws Throwable {
            finalEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), "newDoSomething");
        }
    }.execute().throwException();
    state = TemplateManagerImpl.getTemplateState(editor);
    assert state != null;
    state.gotoEnd(false);
    checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
Also used : Project(com.intellij.openapi.project.Project) MemberInplaceRenameHandler(com.intellij.refactoring.rename.inplace.MemberInplaceRenameHandler) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) PsiElement(com.intellij.psi.PsiElement)

Example 45 with TemplateState

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

the class ShowIntentionsPass method doApplyInformationToEditor.

@Override
public void doApplyInformationToEditor() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    if (!ApplicationManager.getApplication().isUnitTestMode() && !myEditor.getContentComponent().hasFocus())
        return;
    // do not show intentions if caret is outside visible area
    LogicalPosition caretPos = myEditor.getCaretModel().getLogicalPosition();
    Rectangle visibleArea = myEditor.getScrollingModel().getVisibleArea();
    Point xy = myEditor.logicalPositionToXY(caretPos);
    if (!visibleArea.contains(xy))
        return;
    TemplateState state = TemplateManagerImpl.getTemplateState(myEditor);
    if (myShowBulb && (state == null || state.isFinished()) && !HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(false)) {
        DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(myProject);
        codeAnalyzer.setLastIntentionHint(myProject, myFile, myEditor, myIntentionsInfo, myHasToRecreate);
    }
}
Also used : LogicalPosition(com.intellij.openapi.editor.LogicalPosition) 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