mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
WI-14545 Live Template folowed by autocomplete doesn't work as expected
This commit is contained in:
+2
-12
@@ -31,18 +31,8 @@ public class JavaTemplateCompletionProcessor implements TemplateCompletionProces
|
||||
@Override
|
||||
public boolean nextTabOnItemSelected(final ExpressionContext context, final LookupElement item) {
|
||||
final List<? extends PsiElement> elements = JavaCompletionUtil.getAllPsiElements(item);
|
||||
if (elements != null) {
|
||||
if (elements.size() != 1) return false;
|
||||
final PsiElement element = elements.get(0);
|
||||
if (element instanceof PsiPackage) {
|
||||
return false;
|
||||
}
|
||||
if (element instanceof PsiMethod) {
|
||||
PsiMethod method = (PsiMethod)element;
|
||||
if (method.getParameterList().getParametersCount() != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (elements != null && elements.size() == 1 && elements.get(0) instanceof PsiPackage) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.intellij.codeInsight.CodeInsightSettings
|
||||
import com.intellij.codeInsight.lookup.LookupManager
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl
|
||||
import com.intellij.codeInsight.lookup.impl.LookupManagerImpl
|
||||
import com.intellij.codeInsight.template.macro.CompleteMacro
|
||||
import com.intellij.openapi.application.AccessToken
|
||||
import com.intellij.openapi.application.WriteAction
|
||||
import com.intellij.openapi.command.CommandProcessor
|
||||
@@ -134,6 +135,27 @@ public class LiveTemplateTest extends LightCodeInsightFixtureTestCase {
|
||||
checkResult();
|
||||
}
|
||||
|
||||
public void "test honor custom completion caret placement"() {
|
||||
myFixture.configureByText 'a.java', '''
|
||||
class Foo {
|
||||
void foo(int a) {}
|
||||
{ <caret> }
|
||||
}
|
||||
'''
|
||||
final TemplateManager manager = TemplateManager.getInstance(getProject());
|
||||
final Template template = manager.createTemplate("frm", "user", '$VAR$');
|
||||
template.addVariable('VAR', new MacroCallNode(new CompleteMacro()), new EmptyNode(), true)
|
||||
manager.startTemplate(getEditor(), template);
|
||||
myFixture.type('fo\n')
|
||||
myFixture.checkResult '''
|
||||
class Foo {
|
||||
void foo(int a) {}
|
||||
{ foo(<caret>); }
|
||||
}
|
||||
'''
|
||||
assert !state.finished
|
||||
}
|
||||
|
||||
private Editor getEditor() {
|
||||
return myFixture.getEditor();
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ public interface ExpressionContext {
|
||||
@NonNls Key<String> SELECTION = Key.create("SELECTION");
|
||||
|
||||
Project getProject();
|
||||
|
||||
@Nullable
|
||||
Editor getEditor();
|
||||
int getStartOffset();
|
||||
int getTemplateStartOffset();
|
||||
|
||||
+22
-16
@@ -18,10 +18,10 @@ package com.intellij.codeInsight.template.macro;
|
||||
|
||||
import com.intellij.codeInsight.lookup.*;
|
||||
import com.intellij.codeInsight.template.*;
|
||||
import com.intellij.codeInsight.template.Result;
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateState;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.ModalityState;
|
||||
import com.intellij.openapi.application.*;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -88,13 +88,7 @@ public abstract class BaseCompleteMacro extends Macro {
|
||||
lookup.addLookupListener(new MyLookupListener(context));
|
||||
}
|
||||
else {
|
||||
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
|
||||
if (templateState != null) {
|
||||
TextRange range = templateState.getCurrentVariableRange();
|
||||
if (range != null && range.getLength() > 0) {
|
||||
templateState.nextTab();
|
||||
}
|
||||
}
|
||||
considerNextTab(editor);
|
||||
}
|
||||
}
|
||||
}, "", null);
|
||||
@@ -107,6 +101,16 @@ public abstract class BaseCompleteMacro extends Macro {
|
||||
}
|
||||
}
|
||||
|
||||
private static void considerNextTab(Editor editor) {
|
||||
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
|
||||
if (templateState != null) {
|
||||
TextRange range = templateState.getCurrentVariableRange();
|
||||
if (range != null && range.getLength() > 0 && editor.getCaretModel().getOffset() == range.getEndOffset()) {
|
||||
templateState.nextTab();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void invokeCompletionHandler(Project project, Editor editor);
|
||||
|
||||
private static class MyLookupListener extends LookupAdapter {
|
||||
@@ -133,23 +137,25 @@ public abstract class BaseCompleteMacro extends Macro {
|
||||
}
|
||||
|
||||
final Project project = myContext.getProject();
|
||||
ApplicationManager.getApplication().invokeLater(new Runnable() {
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
new WriteCommandAction(project) {
|
||||
@Override
|
||||
protected void run(com.intellij.openapi.application.Result result) throws Throwable {
|
||||
final Editor editor = myContext.getEditor();
|
||||
Editor editor = myContext.getEditor();
|
||||
if (editor != null) {
|
||||
TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
|
||||
if (templateState != null) {
|
||||
templateState.nextTab();
|
||||
}
|
||||
considerNextTab(editor);
|
||||
}
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
}, ModalityState.current(), project.getDisposed());
|
||||
};
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
runnable.run();
|
||||
} else {
|
||||
ApplicationManager.getApplication().invokeLater(runnable, ModalityState.current(), project.getDisposed());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+3
-5
@@ -26,11 +26,9 @@ import com.intellij.psi.PsiFileSystemItem;
|
||||
public class DirectoryTemplateCompletionProcessor implements TemplateCompletionProcessor {
|
||||
@Override
|
||||
public boolean nextTabOnItemSelected(final ExpressionContext context, final LookupElement item) {
|
||||
if (item.getObject() instanceof PsiFileSystemItem) {
|
||||
final PsiFileSystemItem fileSystemItem = (PsiFileSystemItem)item.getObject();
|
||||
if (fileSystemItem.isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
Object object = item.getObject();
|
||||
if (object instanceof PsiFileSystemItem && ((PsiFileSystemItem)object).isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user