Skip to content

Commit

Permalink
Fix the document out-of-sync due to multi threads changing the curren…
Browse files Browse the repository at this point in the history
…t workingcopy buffer (#2969)

* Remove the duplicated method 'getStaticImportFavorites'
  • Loading branch information
testforstephen authored Dec 4, 2023
1 parent 0a708d2 commit 30c1fa3
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down

0 comments on commit 30c1fa3

Please sign in to comment.