tomaytotomato
tomaytotomato

Reputation: 4028

IntelliJ copy class field names and paste them into method as arguments?

I am designing a Vaadin UI component with around 30 components

e.g.

public class MyView {


private TextField foo;
private TextField bar;
private TextField bobby;
private TextField x;
private TextField y;
private TextField z;
private TextField ad;
private TextField nauseum;
private TextField thisisboring;


    public MyView() {
        formview.add(foo,bar,bobby,tables,x,y,z,ad,nauseum,thisisboring);
    }

}

I want to add all these fields as arguments into the formview.add() method. At the moment I am manually adding each field to this method.

Is there an easier way for IntelliJ to generate all these field names as arguments of a function?

(I really don't want to type 30 fields into a method)

Upvotes: 0

Views: 481

Answers (2)

Roman Kovařík
Roman Kovařík

Reputation: 81

I can think of one naive approach: Declaring it like

private AbstractField<String> foo, bar, bobby, x, y, z, ad, nauseum, thisisboring;

and just copy/paste (or if there are multiple field types, at least group the same types)?

Or maybe better approach: as the root of the problem is probably too many parameters, we can encapsulate it:

        private FormFields formFields;

        public MyView() {
            formview.add(formFields.asArray());
        }

        public static class FormFields {

            private TextField foo;
            ...

            public Component[] asArray() {
                return new Component[]{foo, bar, bobby, x, y, z, ad, nauseum, thisisboring};
            }
            
            ... //getters etc.
        }

Upvotes: 0

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26482

Not a perfect solution, but use multi-caret selection:

First double click on the first identifier you need, then double click on each following identifier while holding Alt+Shift down. You should have all the identifiers you need selected. Copy them to the clipboard using Cmd/Ctrl+C. Put the caret between the parentheses of formview.add(). Don't hold any keys down, so you have only a single caret left. Paste Cmd/Ctrl+V. All identifiers will be pasted on separate lines. Hold Alt and click and drag the mouse past the end of the selected lines (except the last one). This should add a caret at the end of every line. Type a , to add a comma between the pasted identifiers and optionally join lines Ctrl+Shift+J to put the call on a single line.

This should be quite a bit less work than typing everything out.

Upvotes: 1

Related Questions