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

Generate toString() source action should support inclusion of methods #2639

Closed
rohansatapathy opened this issue Aug 22, 2022 · 1 comment · Fixed by eclipse-jdtls/eclipse.jdt.ls#3055 or #3495

Comments

@rohansatapathy
Copy link

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.

@rgrunber
Copy link
Member

rgrunber commented Aug 22, 2022

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 :
    let fields: VariableBinding[] = [];
    if (result.fields && result.fields.length) {
    const fieldItems = result.fields.map((field) => {
    return {
    label: `${field.name}: ${field.type}`,
    picked: true,
    originalField: field
    };
    });
    const selectedFields = await window.showQuickPick(fieldItems, {
    canPickMany: true,
    placeHolder: 'Select the fields to include in the toString() method.'
    });
    if (!selectedFields) {
    return;
    }
    fields = selectedFields.map((item) => item.originalField);
    }

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 rgrunber changed the title [enhancement] Add option to generate toString() with getters instead of attribute names Add option to generate toString() with getters instead of attribute names Aug 22, 2022
@rgrunber rgrunber changed the title Add option to generate toString() with getters instead of attribute names Generate toString() source action should support inclusion of methods Aug 22, 2022
@snjeza snjeza self-assigned this Feb 6, 2024
@rgrunber rgrunber added this to the Mid March 2024 milestone Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment