netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

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

Open mbien opened this issue 1 year ago • 2 comments

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 avatar Feb 17 '24 15:02 mbien

@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 avatar Mar 01 '24 20:03 mbien

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 avatar Mar 01 '24 22:03 mbien

rebasing/squashing for a fresh run and preparing for merge

mbien avatar Mar 19 '24 06:03 mbien

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 avatar Mar 19 '24 07:03 mbien