vscode-java icon indicating copy to clipboard operation
vscode-java copied to clipboard

Generate toString() source action should support inclusion of methods

Open rohansatapathy opened this issue 3 years ago • 1 comments

It would be helpful to add an option to generate a toString with getters instead of attribute names because it would allow the toString to work automatically in subclasses where the getters might be overridden. Currently, the dialog only allows you to select which attributes to include in the toString. It seems like it would be relatively simple to add a checkbox (or maybe a separate dialog box) that allows the user to select whether to use attribute names or getters.

rohansatapathy avatar Aug 22 '22 15:08 rohansatapathy

I think this is possible with just adjusting how we use the GenerateToStringOperation and its underlying API. From Eclipse IDE, I see :

Screenshot from 2022-08-22 15-51-31

We don't need to support all of these, but clearly generation from methods is supported. Also, in case anyone is interested, here is where we set defaults for most of these settings : https://github.com/eclipse/eclipse.jdt.ls/blob/a33760778e3c6e5433cde02f45cba972e6c20159/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java#L115 .

Update :

  1. At https://github.com/eclipse/eclipse.jdt.ls/blob/a33760778e3c6e5433cde02f45cba972e6c20159/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java#L81 , we only gather all fields, and not methods for the quick-pick prompt. It's easy enough to get all methods from a given IType (we have have the ITypeBinding!).
  2. From the vscode-java side, the code needs to change so the quick-pick can display the methods being considered : https://github.com/redhat-developer/vscode-java/blob/60b41ea8d8d2f6f7b53eb4a2ba506a507d2d0d30/src/sourceAction.ts#L184-L202

Once all of that is done, https://github.com/eclipse/eclipse.jdt.ls/blob/a33760778e3c6e5433cde02f45cba972e6c20159/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/GenerateToStringHandler.java#L149 is where the selected members get passed in for the toString() method generation. Although we pass in just fields, the javadoc clearly indicates it can accept a member.

rgrunber avatar Aug 22 '22 19:08 rgrunber