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 5ce0dd07c4d4..2a4df962e8d7 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -23,7 +23,6 @@ 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 @@ -957,24 +956,6 @@ 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 237c262ef4cf..5e25eb75f3b4 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(); + finishTemplateEditing(false); } } @Override public void caretRemoved(CaretEvent e) { if (isMultiCaretMode()) { - finishTemplateEditing(); + finishTemplateEditing(false); } } }; @@ -401,7 +401,7 @@ public class TemplateState implements Disposable { } if (nextVariableNumber == -1) { - finishTemplateEditing(); + finishTemplateEditing(false); } else { setCurrentVariableNumber(nextVariableNumber); @@ -410,7 +410,7 @@ public class TemplateState implements Disposable { focusCurrentExpression(); currentVariableChanged(-1); if (isMultiCaretMode()) { - finishTemplateEditing(); + finishTemplateEditing(false); } } } @@ -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(); // nextTab(); ? + finishTemplateEditing(false); // nextTab(); ? return; } } @@ -856,7 +856,7 @@ public class TemplateState implements Disposable { reformat(null); } }); - finishTemplateEditing(); + finishTemplateEditing(false); return; } focusCurrentHighlighter(false); @@ -931,30 +931,36 @@ public class TemplateState implements Disposable { public void gotoEnd(boolean brokenOff) { if (myTemplate == null) return; - LookupManager.getInstance(myProject).hideActiveLookup(); calcResults(false); if (!brokenOff) { doReformat(null); } - setFinalEditorState(brokenOff); - cleanupTemplateState(brokenOff); + finishTemplateEditing(brokenOff); } public void gotoEnd() { gotoEnd(true); } - private void finishTemplateEditing() { + public void cancelTemplate() { if (myTemplate == null) return; + LookupManager.getInstance(myProject).hideActiveLookup(); - setFinalEditorState(false); - cleanupTemplateState(false); + + cleanupTemplateState(true); } - private void setFinalEditorState(boolean brokenOff) { - myEditor.getSelectionModel().removeSelection(); - if (brokenOff && !((TemplateManagerImpl)TemplateManager.getInstance(myProject)).shouldSkipInTests()) return; - + private void finishTemplateEditing(boolean brokenOff) { + if (myTemplate == null) return; + + + LookupManager.getInstance(myProject).hideActiveLookup(); + + setFinalEditorState(); + cleanupTemplateState(brokenOff); + } + + private void setFinalEditorState() { int selectionSegment = myTemplate.getVariableSegmentNumber(TemplateImpl.SELECTION); int endSegmentNumber = selectionSegment >= 0 && getSelectionBeforeTemplate() == null ? selectionSegment : myTemplate.getEndSegmentNumber(); int offset = -1; @@ -975,7 +981,8 @@ 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 a947a3554d45..d2616efdd6e4 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.gotoEnd(true); + templateState.cancelTemplate(); } } } diff --git a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java index effac153e74e..21e55ff2f4e2 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-2015 JetBrains s.r.o. + * Copyright 2000-2014 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 > 0) { - char c = editor.getDocument().getCharsSequence().charAt(offset - 1); + if (chooseAttributeName && offset >= 3) { + char c = editor.getDocument().getCharsSequence().charAt(offset - 3); if (c == '/' || (c == ' ' && brokenOff)) { new WriteCommandAction.Simple(project) { @Override protected void run() throws Throwable { - editor.getDocument().replaceString(offset, offset + 3, ">"); + editor.getDocument().replaceString(offset - 2, offset + 1, ">"); } }.execute(); }