Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add the existing static imports in user code as the favorite static members #2903

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
package org.eclipse.jdt.ls.core.internal.contentassist;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;

public class CompletionProposalUtils {

Expand Down Expand Up @@ -62,4 +67,22 @@ public static CompletionProposal getRequiredTypeProposal(CompletionProposal prop

return requiredProposal;
}

public static void addStaticImportsAsFavoriteImports(ICompilationUnit unit) {
try {
List<String> staticImports = Arrays.stream(unit.getImports())
.filter(t -> {
try {
return Flags.isStatic(t.getFlags());
} catch (JavaModelException e) {
return false;
}
})
.map(t -> t.getElementName().replaceFirst("\\.[^\\.]+$", ".*"))
.toList();
Preferences.DISCOVERED_STATIC_IMPORTS.addAll(staticImports);
} catch (JavaModelException e) {
// ignore
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.eclipse.jdt.ls.core.internal.JobHelpers;
import org.eclipse.jdt.ls.core.internal.MovingAverage;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalUtils;
import org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper;
import org.eclipse.jdt.ls.core.internal.managers.InvisibleProjectImporter;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
Expand Down Expand Up @@ -334,12 +335,7 @@ public void didOpen(DidOpenTextDocumentParams params) {
handleOpen(params);
} else { // Open an unmanaged file, use a workspace runnable to mount it to default project or invisible project.
try {
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
handleOpen(params);
}
}, null, IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
ResourcesPlugin.getWorkspace().run((IWorkspaceRunnable) monitor -> handleOpen(params), null, IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Handle document open ", e);
}
Expand All @@ -360,12 +356,7 @@ public void didSave(DidSaveTextDocumentParams params) {
// some refactorings may be applied by the way, wrap those in a WorkspaceRunnable
try {
JobHelpers.waitForJobs(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor());
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
handleSaved(params);
}
}, null, IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
ResourcesPlugin.getWorkspace().run((IWorkspaceRunnable) monitor -> handleSaved(params), null, IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Handle document save ", e);
}
Expand Down Expand Up @@ -398,6 +389,8 @@ public ICompilationUnit handleOpen(DidOpenTextDocumentParams params) {
}
}

// Update the static imports of current file as the favorite static members.
CompletionProposalUtils.addStaticImportsAsFavoriteImports(unit);
// DiagnosticsHandler problemRequestor = new DiagnosticsHandler(connection, unit.getResource(), reportOnlySyntaxErrors);
unit.becomeWorkingCopy(new NullProgressMonitor());
IBuffer buffer = unit.getBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.contentassist.ChainCompletionProposalComputer;
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor;
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalUtils;
import org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal;
import org.eclipse.jdt.ls.core.internal.contentassist.SnippetCompletionProposal;
import org.eclipse.jdt.ls.core.internal.contentassist.SortTextHelper;
Expand Down Expand Up @@ -230,6 +231,7 @@ private CompletionList computeContentAssist(ICompilationUnit unit, CompletionPar
}
}

CompletionProposalUtils.addStaticImportsAsFavoriteImports(unit);
jdneo marked this conversation as resolved.
Show resolved Hide resolved
List<CompletionItem> proposals = new ArrayList<>();

final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), params.getPosition().getLine(), params.getPosition().getCharacter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -559,6 +560,9 @@ public class Preferences {
public static final String IMPLEMENTATION_ID = UUID.randomUUID().toString();
public static final String SELECTION_RANGE_ID = UUID.randomUUID().toString();
public static final String INLAY_HINT_ID = UUID.randomUUID().toString();

public static final Set<String> DISCOVERED_STATIC_IMPORTS = new LinkedHashSet<>();

private static final String GRADLE_OFFLINE_MODE = "gradle.offline.mode";
private static final int DEFAULT_TAB_SIZE = 4;

Expand Down Expand Up @@ -1610,7 +1614,9 @@ public List<String> getJavaImportExclusions() {
}

public String[] getJavaCompletionFavoriteMembers() {
return javaCompletionFavoriteMembers.toArray(new String[0]);
Set<String> favorites = new LinkedHashSet<>(javaCompletionFavoriteMembers);
favorites.addAll(DISCOVERED_STATIC_IMPORTS);
return favorites.toArray(new String[0]);
}

public String getJavaHome() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
import org.eclipse.jdt.ls.core.internal.JsonMessageHelper;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.junit.After;
Expand Down Expand Up @@ -54,6 +55,7 @@ public void setUp() {
lifeCycleHandler = new DocumentLifeCycleHandler(javaClient, preferenceManager, projectsManager, true);
preferences.setPostfixCompletionEnabled(false);
preferences.setChainCompletionEnabled(true);
Preferences.DISCOVERED_STATIC_IMPORTS.clear();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -128,6 +129,7 @@ public void setUp() {
lifeCycleHandler = new DocumentLifeCycleHandler(javaClient, preferenceManager, projectsManager, true);
preferences.setPostfixCompletionEnabled(false);
preferences.setCompletionLazyResolveTextEditEnabled(false);
Preferences.DISCOVERED_STATIC_IMPORTS.clear();
}

@After
Expand Down Expand Up @@ -3211,6 +3213,33 @@ public class Test {
}
}

@Test
public void testCompletion_autoAddStaticImportAsFavoriteImport() throws JavaModelException {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", """
package org.sample;
import static java.util.Arrays.sort;
public class Test {
public static void main(String[] args) {
asList
}
}""");
String[] oldFavorites = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getJavaCompletionFavoriteMembers();
Set<String> oldStaticImports = new LinkedHashSet<>(Preferences.DISCOVERED_STATIC_IMPORTS);
try {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setJavaCompletionFavoriteMembers(Arrays.asList("org.junit.Assert.*"));
Preferences.DISCOVERED_STATIC_IMPORTS.clear();
CompletionList list = requestCompletions(unit, "asList");
assertNotNull(list);
assertFalse(list.getItems().isEmpty());
CompletionItem item = list.getItems().stream().filter(i -> i.getDetail() != null && i.getDetail().startsWith("java.util.Arrays.asList(")).collect(Collectors.toList()).get(0);
assertNotNull(item);
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setJavaCompletionFavoriteMembers(Arrays.asList(oldFavorites));
Preferences.DISCOVERED_STATIC_IMPORTS.addAll(oldStaticImports);
PreferenceManager.getPrefs(null).setFilteredTypes(Collections.emptyList());
}
}

@Test
public void testCompletion_InvalidJavadoc() throws Exception {
importProjects("maven/aspose");
Expand Down