From 30c1fa393c0d00d24b0c8b28e020ebbacad9e528 Mon Sep 17 00:00:00 2001
From: Jinbo Wang <jinbwan@microsoft.com>
Date: Tue, 5 Dec 2023 02:31:55 +0800
Subject: [PATCH] Fix the document out-of-sync due to multi threads changing
 the current workingcopy buffer (#2969)

* Remove the duplicated method 'getStaticImportFavorites'
---
 .../corrections/SimilarElementsRequestor.java | 62 -------------------
 .../UnresolvedElementsSubProcessor.java       |  2 +-
 2 files changed, 1 insertion(+), 63 deletions(-)

diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/SimilarElementsRequestor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/SimilarElementsRequestor.java
index 5e84197cf3..7f01efe71f 100644
--- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/SimilarElementsRequestor.java
+++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/SimilarElementsRequestor.java
@@ -25,7 +25,6 @@
 import org.eclipse.jdt.core.IClasspathEntry;
 import org.eclipse.jdt.core.ICompilationUnit;
 import org.eclipse.jdt.core.IJavaProject;
-import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.JavaModelException;
 import org.eclipse.jdt.core.Signature;
 import org.eclipse.jdt.core.dom.ASTNode;
@@ -34,7 +33,6 @@
 import org.eclipse.jdt.core.dom.Name;
 import org.eclipse.jdt.core.dom.QualifiedName;
 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
-import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
 import org.eclipse.jdt.ls.core.internal.contentassist.TypeFilter;
 
 public class SimilarElementsRequestor extends CompletionRequestor {
@@ -256,66 +254,6 @@ public void accept(CompletionProposal proposal) {
 		}
 	}
 
-	public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
-		StringBuffer dummyCU= new StringBuffer();
-		String packName= cu.getParent().getElementName();
-		IType type= cu.findPrimaryType();
-		if (type == null) {
-			return new String[0];
-		}
-
-		if (packName.length() > 0) {
-			dummyCU.append("package ").append(packName).append(';'); //$NON-NLS-1$
-		}
-		dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName); // static initializer  //$NON-NLS-1$//$NON-NLS-2$
-		int offset= dummyCU.length();
-		dummyCU.append("\n}\n }"); //$NON-NLS-1$
-
-		ICompilationUnit newCU= null;
-		String contents = cu.getBuffer().getContents();
-		try {
-			newCU= cu.getWorkingCopy(null);
-			newCU.getBuffer().setContents(dummyCU.toString());
-
-			final HashSet<String> result= new HashSet<>();
-
-			CompletionRequestor requestor= new CompletionRequestor(true) {
-				@Override
-				public void accept(CompletionProposal proposal) {
-					if (elementName.equals(new String(proposal.getName()))) {
-						CompletionProposal[] requiredProposals= proposal.getRequiredProposals();
-						for (int i= 0; i < requiredProposals.length; i++) {
-							CompletionProposal curr= requiredProposals[i];
-							if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
-								result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
-							}
-						}
-					}
-				}
-			};
-
-			if (isMethod) {
-				requestor.setIgnored(CompletionProposal.METHOD_REF, false);
-				requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
-			} else {
-				requestor.setIgnored(CompletionProposal.FIELD_REF, false);
-				requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
-			}
-			requestor.setFavoriteReferences(favorites);
-
-			newCU.codeComplete(offset, requestor);
-			// if cu is working copy, we should restore the contents saved previously.
-			if (cu.isWorkingCopy()) {
-				cu.getBuffer().setContents(contents);
-			}
-			return result.toArray(new String[result.size()]);
-		} finally {
-			if (newCU != null && !cu.isWorkingCopy()) {
-				newCU.discardWorkingCopy();
-			}
-		}
-	}
-
 	@Override
 	public boolean isTestCodeExcluded() {
 		return fExcludeTestCode;
diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/UnresolvedElementsSubProcessor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/UnresolvedElementsSubProcessor.java
index 9879751bfe..7c12064b86 100644
--- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/UnresolvedElementsSubProcessor.java
+++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/UnresolvedElementsSubProcessor.java
@@ -980,7 +980,7 @@ private static void addStaticImportFavoriteProposals(IInvocationContext context,
 			AST ast= root.getAST();
 
 			String name= node.getIdentifier();
-			String[] staticImports= SimilarElementsRequestor.getStaticImportFavorites(context.getCompilationUnit(), name, isMethod, favourites);
+			String[] staticImports= JavaModelUtil.getStaticImportFavorites(context.getCompilationUnit(), name, isMethod, favourites);
 			for (int i= 0; i < staticImports.length; i++) {
 				String curr= staticImports[i];