From bebc20f0865cc5aebf4216eb0a8cb4ade0dbb1e6 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Tue, 7 Feb 2017 15:09:32 +0300 Subject: [PATCH] Live Templates: bulk update should change later segment first (IDEA-167694) --- .../codeInsight/template/LiveTemplateTest.groovy | 12 +++++++++++- .../codeInsight/template/impl/TemplateState.java | 6 +++--- 2 files changed, 14 insertions(+), 4 deletions(-) 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 5ca52587214e..e46bca927635 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -113,6 +113,16 @@ class LiveTemplateTest extends LightCodeInsightFixtureTestCase { doTestTemplateWithThreeVariables("", "DefaultValue", "", "class A { void test() { for(TestValue1DefaultValueTestValue3) {} } }") } + void testTemplateWithSegmentsAtTheSamePosition_4() { + configureFromFileText("dummy.java", "class A { void test() { } }") + TemplateManager manager = TemplateManager.getInstance(getProject()) + final Template template = manager.createTemplate("test_template", "user_group", '$A$$B$ then "$A$$B$"') + template.addVariable("A", "", "\"Def1\"", true) + template.addVariable("B", "", "\"Def2\"", true) + startTemplate(template) + checkResultByText("class A { void test() { Def1Def2 then \"Def1Def2\" } }") + } + private void doTestTemplateWithThreeVariables(String firstDefaultValue, String secondDefaultValue, String thirdDefaultValue, String expectedText) { configureFromFileText("dummy.java", "class A { void test() { } }") 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 aaf4b4b270c2..a3b2b41ad2f4 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 @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -786,7 +786,7 @@ public class TemplateState implements Disposable { if (changes.size() > 1) { ContainerUtil.sort(changes, (o1, o2) -> { int startDiff = o2.startOffset - o1.startOffset; - return startDiff != 0 ? startDiff : o2.endOffset - o1.endOffset; + return startDiff != 0 ? startDiff : o2.segmentNumber - o1.segmentNumber; }); } DocumentUtil.executeInBulk(myDocument, true, () -> { @@ -797,7 +797,7 @@ public class TemplateState implements Disposable { } /** - * Must be invoked on every segment change in order to avoid ovelapping editing segment with its neibours + * Must be invoked on every segment change in order to avoid overlapping editing segment with its neighbours */ private void fixOverlappedSegments(int currentSegment) { if (currentSegment >= 0) {