mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
preselect lookup almost everywhere in Java
This commit is contained in:
+27
@@ -125,4 +125,31 @@ class CompletionAutoPopupTest extends LightCodeInsightFixtureTestCase {
|
||||
assertNull LookupManager.getActiveLookup(myFixture.getEditor())
|
||||
}
|
||||
|
||||
public void testGenerallyFocusLookupInJavaMethod() {
|
||||
myFixture.configureByText("a.java", """
|
||||
class Foo {
|
||||
String foo(String iterable) {
|
||||
return it<caret>;
|
||||
}
|
||||
}
|
||||
""")
|
||||
type 'e'
|
||||
final def lookup = LookupManager.getActiveLookup(myFixture.getEditor())
|
||||
assertNotNull lookup
|
||||
assertTrue lookup.focused
|
||||
}
|
||||
|
||||
public void testNoLookupFocusInJavaVariable() {
|
||||
myFixture.configureByText("a.java", """
|
||||
class Foo {
|
||||
String foo(String st<caret>) {
|
||||
}
|
||||
}
|
||||
""")
|
||||
type 'r'
|
||||
final def lookup = LookupManager.getActiveLookup(myFixture.getEditor())
|
||||
assertNotNull lookup
|
||||
assertFalse lookup.focused
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2000-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class AlwaysFocusLookup extends CompletionConfidence {
|
||||
@Override
|
||||
public Boolean shouldFocusLookup(@NotNull CompletionParameters parameters) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -16,13 +16,15 @@
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public abstract class CompletionConfidence {
|
||||
|
||||
public abstract boolean shouldFocusLookup(@NotNull CompletionParameters parameters);
|
||||
@Nullable
|
||||
public abstract Boolean shouldFocusLookup(@NotNull CompletionParameters parameters);
|
||||
|
||||
|
||||
}
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2000-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.completion;
|
||||
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiNameIdentifierOwner;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class UnfocusedNameIdentifier extends CompletionConfidence {
|
||||
@Override
|
||||
public Boolean shouldFocusLookup(@NotNull CompletionParameters parameters) {
|
||||
final PsiElement position = parameters.getPosition();
|
||||
final PsiElement parent = position.getParent();
|
||||
if (parent instanceof PsiNameIdentifierOwner && ((PsiNameIdentifierOwner)parent).getNameIdentifier() == position) {
|
||||
return false;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -95,7 +95,7 @@ public class AutoPopupController implements Disposable {
|
||||
if (!file.isValid()) return;
|
||||
|
||||
if (condition != null && !condition.value(file)) return;
|
||||
new CodeCompletionHandlerBase(CompletionType.BASIC, false, false).invoke(myProject, editor, file);
|
||||
new CodeCompletionHandlerBase(CompletionType.BASIC, false, true).invoke(myProject, editor, file);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+11
-10
@@ -78,16 +78,16 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.completion.CodeCompletionHandlerBase");
|
||||
private final CompletionType myCompletionType;
|
||||
final boolean invokedExplicitly;
|
||||
private final boolean myFocusLookup;
|
||||
final boolean autopopup;
|
||||
|
||||
public CodeCompletionHandlerBase(final CompletionType completionType) {
|
||||
this(completionType, true, true);
|
||||
this(completionType, true, false);
|
||||
}
|
||||
|
||||
public CodeCompletionHandlerBase(CompletionType completionType, boolean invokedExplicitly, boolean focusLookup) {
|
||||
public CodeCompletionHandlerBase(CompletionType completionType, boolean invokedExplicitly, boolean autopopup) {
|
||||
myCompletionType = completionType;
|
||||
this.invokedExplicitly = invokedExplicitly;
|
||||
this.myFocusLookup = focusLookup;
|
||||
this.autopopup = autopopup;
|
||||
}
|
||||
|
||||
public final void invoke(final Project project, final Editor editor) {
|
||||
@@ -100,7 +100,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
|
||||
}
|
||||
|
||||
try {
|
||||
invokeCompletion(project, editor, psiFile, myFocusLookup ? 1 : 0);
|
||||
invokeCompletion(project, editor, psiFile, autopopup ? 0 : 1);
|
||||
}
|
||||
catch (IndexNotReadyException e) {
|
||||
DumbService.getInstance(project).showDumbModeNotification("Code completion is not available here while indices are being built");
|
||||
@@ -173,7 +173,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
|
||||
ApplicationManager.getApplication().runWriteAction(runnable);
|
||||
}
|
||||
};
|
||||
if (!myFocusLookup) {
|
||||
if (autopopup) {
|
||||
CommandProcessor.getInstance().runUndoTransparentAction(initCmd);
|
||||
} else {
|
||||
CommandProcessor.getInstance().executeCommand(project, initCmd, null, null);
|
||||
@@ -188,14 +188,15 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
|
||||
}
|
||||
|
||||
private boolean shouldFocusLookup(CompletionParameters parameters) {
|
||||
if (myFocusLookup) {
|
||||
if (!autopopup) {
|
||||
return true;
|
||||
}
|
||||
|
||||
final Language language = PsiUtilBase.getLanguageAtOffset(parameters.getPosition().getContainingFile(), parameters.getOffset());
|
||||
for (CompletionConfidence confidence : CompletionConfidenceEP.forLanguage(language)) {
|
||||
if (confidence.shouldFocusLookup(parameters)) {
|
||||
return true;
|
||||
final Boolean result = confidence.shouldFocusLookup(parameters);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -206,7 +207,7 @@ public class CodeCompletionHandlerBase implements CodeInsightActionHandler {
|
||||
LookupImpl existing = (LookupImpl)LookupManager.getActiveLookup(editor);
|
||||
if (existing != null) {
|
||||
existing.markReused();
|
||||
if (myFocusLookup) {
|
||||
if (!autopopup) {
|
||||
existing.setFocused(true);
|
||||
}
|
||||
return existing;
|
||||
|
||||
+3
-2
@@ -539,7 +539,7 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
|
||||
|
||||
@Override
|
||||
public boolean isAutopopupCompletion() {
|
||||
return !myLookup.isFocused();
|
||||
return myHandler.autopopup;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -575,7 +575,8 @@ public class CompletionProgressIndicator extends ProgressIndicatorBase implement
|
||||
public void restartCompletion() {
|
||||
closeAndFinish(false);
|
||||
|
||||
final CodeCompletionHandlerBase newHandler = new CodeCompletionHandlerBase(myParameters.getCompletionType(), false, myLookup.isFocused());
|
||||
final CodeCompletionHandlerBase newHandler = new CodeCompletionHandlerBase(myParameters.getCompletionType(), false,
|
||||
!isAutopopupCompletion());
|
||||
final PsiFile psiFileInEditor = PsiUtilBase.getPsiFileInEditor(myEditor, getProject());
|
||||
newHandler.invokeCompletion(getProject(), myEditor, psiFileInEditor, myParameters.getInvocationCount());
|
||||
}
|
||||
|
||||
+1
-1
@@ -99,7 +99,7 @@ public class CompletionAutoPopupHandler extends TypedHandlerDelegate {
|
||||
if (editor.isDisposed() || isMainEditor && FileEditorManager.getInstance(project).getSelectedTextEditor() != editor) return;
|
||||
if (ApplicationManager.getApplication().isWriteAccessAllowed()) return; //it will fail anyway
|
||||
|
||||
new CodeCompletionHandlerBase(CompletionType.BASIC, false, false).invoke(project, editor);
|
||||
new CodeCompletionHandlerBase(CompletionType.BASIC, false, true).invoke(project, editor);
|
||||
|
||||
final AutoPopupState state = new AutoPopupState(project, editor);
|
||||
editor.putUserData(STATE_KEY, state);
|
||||
|
||||
@@ -59,4 +59,6 @@ public interface Lookup extends UserDataHolder{
|
||||
boolean isCompletion();
|
||||
|
||||
java.util.List<LookupElement> getItems();
|
||||
|
||||
boolean isFocused();
|
||||
}
|
||||
|
||||
@@ -147,6 +147,7 @@ public class LookupImpl extends LightweightHint implements Lookup, Disposable {
|
||||
myModel.setArranger(arranger);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFocused() {
|
||||
return myFocused;
|
||||
}
|
||||
|
||||
+1
-1
@@ -25,6 +25,6 @@ public class ClassNameCompleteMacro extends BaseCompleteMacro {
|
||||
}
|
||||
|
||||
CodeInsightActionHandler getCompletionHandler() {
|
||||
return new CodeCompletionHandlerBase(CompletionType.CLASS_NAME, false, true);
|
||||
return new CodeCompletionHandlerBase(CompletionType.CLASS_NAME, false, false);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,6 @@ public class CompleteMacro extends BaseCompleteMacro {
|
||||
}
|
||||
|
||||
CodeInsightActionHandler getCompletionHandler() {
|
||||
return new CodeCompletionHandlerBase(CompletionType.BASIC, ApplicationManager.getApplication().isUnitTestMode(), true);
|
||||
return new CodeCompletionHandlerBase(CompletionType.BASIC, ApplicationManager.getApplication().isUnitTestMode(), false);
|
||||
}
|
||||
}
|
||||
@@ -231,6 +231,9 @@
|
||||
<lookup.actionProvider implementation="com.intellij.codeInsight.completion.ExcludeFromCompletionLookupActionProvider"
|
||||
id="excludeFromCompletion" order="last"/>
|
||||
|
||||
<completion.confidence language="JAVA" implementationClass="com.intellij.codeInsight.completion.UnfocusedNameIdentifier" id="javaNameIdentifier"/>
|
||||
<completion.confidence language="JAVA" implementationClass="com.intellij.codeInsight.completion.AlwaysFocusLookup" id="javaTrue" order="last"/>
|
||||
|
||||
<completion.contributor language="any" implementationClass="com.intellij.codeInsight.completion.BasicToClassNameDelegator" id="basic2ClassName"
|
||||
order="first, before javaMemberName, after xml"/>
|
||||
<completion.contributor language="JAVA" implementationClass="com.intellij.codeInsight.completion.JavaCompletionContributor" id="javaLegacy"
|
||||
|
||||
Reference in New Issue
Block a user