diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy index 2a4df962e8d7..5ce0dd07c4d4 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -23,6 +23,7 @@ import com.intellij.codeInsight.lookup.impl.LookupImpl import com.intellij.codeInsight.lookup.impl.LookupManagerImpl import com.intellij.codeInsight.template.impl.* import com.intellij.codeInsight.template.macro.* +import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.impl.DocumentImpl @@ -956,6 +957,24 @@ class Foo { return calculateResult(params, context) } } + + public void "test escape shouldn't move caret to the end marker"() { + myFixture.configureByText 'a.java', """ +class Foo {{ + itar +}} +""" + myFixture.type '\ta' + myFixture.performEditorAction(IdeActions.ACTION_EDITOR_ESCAPE) + myFixture.checkResult """ +class Foo {{ + for (int a = 0; a < array.length; a++) { + = array[a]; + + } +}} +""" + } public void "test add new line on enter outside editing variable"() { myFixture.configureByText 'a.java', """ diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java index 5e25eb75f3b4..237c262ef4cf 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateState.java @@ -146,14 +146,14 @@ public class TemplateState implements Disposable { @Override public void caretAdded(CaretEvent e) { if (isMultiCaretMode()) { - finishTemplateEditing(false); + finishTemplateEditing(); } } @Override public void caretRemoved(CaretEvent e) { if (isMultiCaretMode()) { - finishTemplateEditing(false); + finishTemplateEditing(); } } }; @@ -401,7 +401,7 @@ public class TemplateState implements Disposable { } if (nextVariableNumber == -1) { - finishTemplateEditing(false); + finishTemplateEditing(); } else { setCurrentVariableNumber(nextVariableNumber); @@ -410,7 +410,7 @@ public class TemplateState implements Disposable { focusCurrentExpression(); currentVariableChanged(-1); if (isMultiCaretMode()) { - finishTemplateEditing(false); + finishTemplateEditing(); } } } @@ -668,7 +668,7 @@ public class TemplateState implements Disposable { final TextResult value = getVariableValue(variableName); if (value != null && !value.getText().isEmpty()) { if (!myProcessor.process(variableName, value.getText())) { - finishTemplateEditing(false); // nextTab(); ? + finishTemplateEditing(); // nextTab(); ? return; } } @@ -856,7 +856,7 @@ public class TemplateState implements Disposable { reformat(null); } }); - finishTemplateEditing(false); + finishTemplateEditing(); return; } focusCurrentHighlighter(false); @@ -931,36 +931,30 @@ public class TemplateState implements Disposable { public void gotoEnd(boolean brokenOff) { if (myTemplate == null) return; + LookupManager.getInstance(myProject).hideActiveLookup(); calcResults(false); if (!brokenOff) { doReformat(null); } - finishTemplateEditing(brokenOff); + setFinalEditorState(brokenOff); + cleanupTemplateState(brokenOff); } public void gotoEnd() { gotoEnd(true); } - public void cancelTemplate() { + private void finishTemplateEditing() { if (myTemplate == null) return; - LookupManager.getInstance(myProject).hideActiveLookup(); - - cleanupTemplateState(true); + setFinalEditorState(false); + cleanupTemplateState(false); } - private void finishTemplateEditing(boolean brokenOff) { - if (myTemplate == null) return; - - - LookupManager.getInstance(myProject).hideActiveLookup(); - - setFinalEditorState(); - cleanupTemplateState(brokenOff); - } - - private void setFinalEditorState() { + private void setFinalEditorState(boolean brokenOff) { + myEditor.getSelectionModel().removeSelection(); + if (brokenOff && !((TemplateManagerImpl)TemplateManager.getInstance(myProject)).shouldSkipInTests()) return; + int selectionSegment = myTemplate.getVariableSegmentNumber(TemplateImpl.SELECTION); int endSegmentNumber = selectionSegment >= 0 && getSelectionBeforeTemplate() == null ? selectionSegment : myTemplate.getEndSegmentNumber(); int offset = -1; @@ -981,8 +975,7 @@ public class TemplateState implements Disposable { myEditor.getCaretModel().moveToOffset(offset); myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } - - myEditor.getSelectionModel().removeSelection(); + int selStart = myTemplate.getSelectionStartSegmentNumber(); int selEnd = myTemplate.getSelectionEndSegmentNumber(); if (selStart >= 0 && selEnd >= 0) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/macro/BaseCompleteMacro.java b/platform/lang-impl/src/com/intellij/codeInsight/template/macro/BaseCompleteMacro.java index d2616efdd6e4..a947a3554d45 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/macro/BaseCompleteMacro.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/macro/BaseCompleteMacro.java @@ -115,7 +115,7 @@ public abstract class BaseCompleteMacro extends Macro { templateState.nextTab(); } else if (caret > range.getEndOffset()) { - templateState.cancelTemplate(); + templateState.gotoEnd(true); } } } diff --git a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java index 21e55ff2f4e2..effac153e74e 100644 --- a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java +++ b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -157,13 +157,13 @@ public class XmlTagInsertHandler implements InsertHandler { public void templateFinished(final Template template, boolean brokenOff) { final int offset = editor.getCaretModel().getOffset(); - if (chooseAttributeName && offset >= 3) { - char c = editor.getDocument().getCharsSequence().charAt(offset - 3); + if (chooseAttributeName && offset > 0) { + char c = editor.getDocument().getCharsSequence().charAt(offset - 1); if (c == '/' || (c == ' ' && brokenOff)) { new WriteCommandAction.Simple(project) { @Override protected void run() throws Throwable { - editor.getDocument().replaceString(offset - 2, offset + 1, ">"); + editor.getDocument().replaceString(offset, offset + 3, ">"); } }.execute(); }