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

Escape java hint fix String for html and fill in multi variables. #7075

Merged
merged 1 commit into from
Mar 19, 2024

Conversation

mbien
Copy link
Member

@mbien mbien commented Feb 17, 2024

fixes a few small issues and one little performance improvement:

  • The hint fix contains code segments which may contain angle brackets. This can break the formatting of the apply-fix dropdown.
  • Multi variables are now replaced with their values in simple cases.
  • Sort vars by length before replacement so that they don't replace each other. Multi-vars go first.
  • can use String.replace() instead of replaceAll() since no regex is involved

before:
escape-hints-fix_before

after:
escape-hints-fix_after

hint:

$name = new java.util.Vector<$T$>();     =>
$name = new java.util.ArrayList<$T$>();  ;;

note:

  • multi-variables like $T$ (zero-or-many) were not substituted before this change
  • angle brackets were either ignored by the html renderer or caused undesired formatting

non-trivial case:

if ($b < $b1) {
    $body$;    
}
=>
if ($b1 < $b) {
    $body$;    
}
;;

note: some vars are prefixes of others

empty body:
jackpot-substitution-empty

single entry in multivar:
jackpot-substitution-single

multiple entries in multivar:
jackpot-substitution-multiple

@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) hints ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Jackpot labels Feb 17, 2024
@mbien mbien added this to the NB22 milestone Feb 17, 2024
@mbien mbien requested a review from lahodaj February 17, 2024 15:59
@mbien mbien linked an issue Feb 27, 2024 that may be closed by this pull request
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me. Thanks!

@mbien
Copy link
Member Author

mbien commented Mar 1, 2024

@lahodaj do you think it is possible to compute the display name lazily in the getter of JavaFixRealImpl? I feel bad that this has so much string replacement and it runs during the hint matching phase and not during the apply phase.

Is it legal to call treepath.getLeaf().toString() without CompilationInfo or any other context? It would be fairly easy to move it into the getter if yes.

@mbien mbien force-pushed the escape-jackpot-fix branch from ef653e6 to cd18599 Compare March 1, 2024 22:36
@mbien
Copy link
Member Author

mbien commented Mar 1, 2024

after testing a bit more I had to make some more adjustments

  • the VariableTree -> identifier name conversion can't be done for multi vars, since those can have full statements in them
  • improved the whitespace situation a bit so that it looks better as single-line-String
  • cd18599 computes the display name of the fix lazily, so that it runs after the matching phase and does not affect editor performance

I noticed during testing that variables for method names are missing in the list, example:

java.util.Iterator<$T> $it = $col.iterator();
while ($it.hasNext()) {
    if ($it.next().$condition($params$)) {
        $it.remove();
    }
} :: $col instanceof java.util.Collection
=>
$col.removeIf(e -> e.$condition($params$));
;;

$condition would be missing, the refactoring works fine though.
image

@mbien
Copy link
Member Author

mbien commented Mar 19, 2024

rebasing/squashing for a fresh run and preparing for merge

 - The hint fix contains code segments which may contain angle
   brackets. This can break the formatting of the apply-fix dropdown.
 - Multi variables are now replaced with their values in simple cases.
 - sort vars by length before replacement so that they don't replace
   each other. Multi-vars go first.
 - VariableTree should be converted into an identifier name in some
   cases
 - some String cleanup so that it is presentable as one-liner in UI
 - compute display name for fix lazily so that it does not run in
   the hint matching phase
@mbien mbien force-pushed the escape-jackpot-fix branch from cd18599 to 57e3c10 Compare March 19, 2024 06:22
@mbien
Copy link
Member Author

mbien commented Mar 19, 2024

I looked through com.sun.tools.javac.tree.Pretty which is called by JCTree#toString and I think it should be fine to call TreePath#toString lazily on EDT -> merging once green again

@mbien mbien merged commit e35c66f into apache:master Mar 19, 2024
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) hints Jackpot Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Declarative hint variable seems to capture too much
2 participants