make Tab navigate to next parameter, if completion changes nothing

This commit is contained in:
Dmitry Batrak
2017-10-03 16:04:02 +03:00
parent 05a4a74695
commit 413ce9c3bd
3 changed files with 70 additions and 0 deletions
@@ -691,6 +691,17 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase {
checkHintContents(null);
}
public void testNextParameterWorksWhenTabCompletionDoesntChangeAnything() throws Exception {
configureJava("class C { void m() { String local = \"a\"; String local2 = \"b\"; System.getPro<caret> } }");
complete("getProperty(String key, String def)");
type("local");
complete();
assertEquals("local", myFixture.getLookupElements()[0].getLookupString());
myFixture.performEditorAction(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_REPLACE);
waitForAllAsyncStuff();
checkResultWithInlays("class C { void m() { String local = \"a\"; String local2 = \"b\"; System.getProperty(<hint text=\"key:\"/>local, <HINT text=\"def:\"/><caret>) } }");
}
private void checkResult(String text) {
myFixture.checkResult(text);
}
@@ -0,0 +1,56 @@
// Copyright 2000-2017 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.hint;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.actionSystem.KeyboardShortcut;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.util.Ref;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class NextParameterAfterCompletionHandler extends EditorActionHandler {
private final EditorActionHandler myOriginalHandler;
public NextParameterAfterCompletionHandler(EditorActionHandler originalHandler) {
myOriginalHandler = originalHandler;
}
@Override
protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) {
return myOriginalHandler.isEnabled(editor, caret, dataContext);
}
@Override
protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) {
Ref<Boolean> documentChanged = new Ref<>();
DocumentListener listener = new DocumentListener() {
@Override
public void documentChanged(DocumentEvent event) {
if (event.getOldLength() > 0 || event.getNewLength() > 0) documentChanged.set(Boolean.TRUE);
}
};
editor.getDocument().addDocumentListener(listener);
try {
myOriginalHandler.execute(editor, caret, dataContext);
}
finally {
editor.getDocument().removeDocumentListener(listener);
}
if (documentChanged.isNull()) {
ActionManager actionManager = ActionManager.getInstance();
KeyboardShortcut completionShortcut = actionManager.getKeyboardShortcut(IdeActions.ACTION_CHOOSE_LOOKUP_ITEM_REPLACE);
KeyboardShortcut parameterShortcut = actionManager.getKeyboardShortcut(IdeActions.ACTION_EDITOR_NEXT_PARAMETER);
if (completionShortcut != null && completionShortcut.equals(parameterShortcut)) {
EditorActionHandler parameterHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_NEXT_PARAMETER);
if (parameterHandler.isEnabled(editor, caret, dataContext)) parameterHandler.execute(editor, caret, dataContext);
}
}
}
}
@@ -387,6 +387,9 @@
<experimentalFeature id="project.view.async.tree.model" internalFeature="true" percentOfUsers="10" requireRestart="true">
<description>New implementation based on AsyncTreeModel + StructureTreeModel</description>
</experimentalFeature>
<editorActionHandler action="EditorChooseLookupItemReplace"
implementationClass="com.intellij.codeInsight.hint.NextParameterAfterCompletionHandler"/>
</extensions>
<extensions defaultExtensionNs="org.jetbrains">
<webServerRootsProvider implementation="org.jetbrains.builtInWebServer.ArtifactWebServerRootsProvider" order="last"/>