Live templates: list template action should work in multi-carets mode (IDEA-182741)

GitOrigin-RevId: 7617b4d3c2bcc7bafef15ef0199eac08f6cb8491
This commit is contained in:
Alexander Zolotov
2019-12-27 18:21:22 +03:00
committed by intellij-monorepo-bot
parent 0585365e24
commit 8ccd4547f5
6 changed files with 51 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
class A {
public static void main() {
<caret>
<caret>
}
}

View File

@@ -0,0 +1,6 @@
class A {
public static void main() {
si<caret>
si<caret>
}
}

View File

@@ -0,0 +1,6 @@
class A {
public static void main() {
simple template text<caret>
simple template text<caret>
}
}

View File

@@ -0,0 +1,6 @@
class A {
public static void main() {
simple template text<caret>
simple template text<caret>
}
}

View File

@@ -97,6 +97,14 @@ public class ListTemplateActionTest extends LightJavaCodeInsightFixtureTestCase
doTest("template.with.desc");
}
public void testMulticaret() {
doTest("simple");
}
public void testMulticaretWithPrefix() {
doTest("simple");
}
private void doTest(@NotNull String lookupText) {
myFixture.configureByFile(getTestName(false) + ".java");
new ListTemplatesAction().actionPerformedImpl(myFixture.getProject(), myFixture.getEditor());

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.template.impl;
@@ -236,11 +236,26 @@ public class ListTemplatesHandler implements CodeInsightActionHandler {
if (item instanceof LiveTemplateLookupElementImpl) {
final TemplateImpl template = ((LiveTemplateLookupElementImpl)item).getTemplate();
final String argument = myTemplate2Argument != null ? myTemplate2Argument.get(template) : null;
WriteCommandAction.writeCommandAction(project).run(() -> ((TemplateManagerImpl)TemplateManager.getInstance(project)).startTemplateWithPrefix(lookup.getEditor(), template, null, argument));
WriteCommandAction.writeCommandAction(project).run(() -> {
Editor editor = lookup.getEditor();
if (!editor.isDisposed()) {
editor.getCaretModel().runForEachCaret(caret -> {
((TemplateManagerImpl)TemplateManager.getInstance(project))
.startTemplateWithPrefix(caret.getEditor(), template, null, argument);
});
}
});
}
else if (item instanceof CustomLiveTemplateLookupElement) {
if (myFile != null) {
WriteCommandAction.writeCommandAction(project).run(() -> ((CustomLiveTemplateLookupElement)item).expandTemplate(lookup.getEditor(), myFile));
WriteCommandAction.writeCommandAction(project).run(() -> {
Editor editor = lookup.getEditor();
if (!editor.isDisposed()) {
editor.getCaretModel().runForEachCaret(caret -> {
((CustomLiveTemplateLookupElement)item).expandTemplate(lookup.getEditor(), myFile);
});
}
});
}
}
}
@@ -260,7 +275,7 @@ public class ListTemplatesHandler implements CodeInsightActionHandler {
result.addAll(items);
ArrayList<LookupElement> list = new ArrayList<>(result);
int selected = lookup.isSelectionTouched() ? list.indexOf(lookup.getCurrentItem()) : 0;
return new Pair<>(list, selected >= 0 ? selected : 0);
return new Pair<>(list, Math.max(selected, 0));
}
@Override