Comments (2)
After a sync with the original author @CsCherrYY , we found it's safe to revert the changes against the SimilarElementsRequestor#getStaticImportFavorites(...) in the PR Add missing import on paste by CsCherrYY · Pull Request #2320 · eclipse-jdtls/eclipse.jdt.ls (github.com).
Here is the old call hierarchy of SimilarElementsRequestor#getStaticImportFavorites(...)
when I checkout to PR #2320. The PasteEventHandler.handlePasteEvent()
-> getMissingImportsWorkspaceEdit()
-> OrganizeImportsHandler.organizeImports()
-> wrapStaticImports()
-> addImports()
-> SimilarElementsRequestor.getStaticImportFavorites()
.
Here is the new call hierarchy of SimilarElementsRequestor#getStaticImportFavorites(...)
with latest source code. Only code action will call SimilarElementsRequestor#getStaticImportFavorites(...)
, no calls from OrganizeImportsHandler
and PasteEventHandler
any more.
This is because a later PR organize imports remove static imports (#2396) · eclipse-jdtls/eclipse.jdt.ls@2e430fd (github.com), which deleted the wrapStaticImports()
method from OrganizeImportsHandler
. So the previous reason to change SimilarElementsRequestor#getStaticImportFavorites(...) for adding missing import on paste becomes invalid.
Next, let ask the second question why the previous PR wanted to add code to restore the CU buffer in SimilarElementsRequestor#getStaticImportFavorites(...)?
This is related to the implementation of cu.getWorkingCopy(null)
. Depending on whether the original cu was a primary workingcopy or not, it would either create a new workingcopy or return the old one. For the compilationUnit created from the API JDTUtils.resolveCompilationUnit()
(which is the case for most usages such as CodeActionHandler
in JDT LS), it's a primary unit and therefore it received a new workingcopy and did not affect the original buffer. However, for the old case of the PasteEventHandler
, the cu that it passed was a non-primary tempUnit, which meant that it directly manipulated the original buffer in the static-import calculation. That's why that PR #2320 wanted to restore the previous buffer.
Based on the analysis above, it's fine to revert the changes back in SimilarElementsRequestor#getStaticImportFavorites(...) since both the code action of static import and adding miss imports on paste still work fine.
from eclipse.jdt.ls.
Doing a comparison between
--- jdt-ls.txt 2023-11-22 10:29:37.696189721 -0500
+++ jdt-core.txt 2023-11-22 10:29:31.259087720 -0500
@@ -1,10 +1,9 @@
public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
- StringBuffer dummyCU= new StringBuffer();
+ StringBuilder dummyCU= new StringBuilder();
String packName= cu.getParent().getElementName();
IType type= cu.findPrimaryType();
- if (type == null) {
+ if (type == null)
return new String[0];
- }
if (packName.length() > 0) {
dummyCU.append("package ").append(packName).append(';'); //$NON-NLS-1$
@@ -14,7 +13,6 @@
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());
@@ -25,11 +23,9 @@
@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];
+ for (CompletionProposal curr : proposal.getRequiredProposals()) {
if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
- result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
+ result.add(concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
}
}
}
@@ -45,14 +41,11 @@
}
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);
- }
+ newCU.codeComplete(offset, requestor, new NullProgressMonitor());
+
return result.toArray(new String[result.size()]);
} finally {
- if (newCU != null && !cu.isWorkingCopy()) {
+ if (newCU != null) {
newCU.discardWorkingCopy();
}
}
I think your PR basically brings the duplicated method completely in line with JavaModelUtil.getStaticImportFavorites(...)
, so we should be able to replace its usage.
from eclipse.jdt.ls.
Related Issues (20)
- Doesn't revalidate project files after classpath changes if autobuild is off. HOT 6
- [Feature, Performance] - Native GraalWM Executable HOT 2
- references stop working; index file not found errors; deadlocks with autobuild.enabled = false and maxConcurrentBuilds=8 HOT 1
- [Feature] Browsing jar dependencies contents HOT 5
- Disabling maven importer prevents project from being imported at all. HOT 5
- Run the build test suite against the incubator javac branch HOT 2
- result of `textDocument/declaration` is [] HOT 2
- Enable PGP signing, instead of Eclipse CBI jar signing
- Gradle tests fail when running the build with Java 22 HOT 2
- Unsupported class file major version 61
- Autobuilding setting isn't applied until didChangeConfiguration is received.
- codeAction/resolve throws errors when your machine's processor count is too low. HOT 1
- Java Model Exception: Error in Java Model (code 969): src/main/.... [in app] does not exist HOT 13
- Maven support HOT 3
- Filter reported for workspace/willRename excludes folders/packages HOT 8
- Incoming call's hierarchy item's selectionRange behaves differently to all other (tested) servers HOT 1
- Outgoing calls hierarchies runs into NullPointerException if hashCode() is called HOT 4
- Outgoing call hierarchies sometimes duplicate the results HOT 2
- can't build project after latest jdt core updates (ArrayBinding cannot be cast to class ReferenceBinding) HOT 3
- very slow on large project comparing to intellij HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eclipse.jdt.ls.