When adding elements to an array, is it possible to somehow automatically create the next element and move cursor to it?

Hey there,

    I was wondering if it would be possible somehow, say I am adding elements a := []string {"something~" , "" }, when I finish typing "something" and my cursor gets to the ~ (just a placeholder for reference), I could hit something, such as ctrl + tab, or ctrl + space, etc and it would add a comma and a new set of quotation marks and place the cursor in them automatically so I can quickly add multiple items?

Thanks!

-MH

4 comments
Comment actions Permalink

After some more research, I came across a plugin called "Easy Literal", which lets you type out a list of words such as { element1 element2 element3 } and then if you highlight them all at once and hit control + comma, it automatically adds quotes to them and separates by a comma like this { "element1", "element2", "ement3" } which is actually a pretty nice solution if I just type them all out to the end and then hit ctrl + shift + [ to highlight everything within the braces.

I still wonder if there is a way to do something like my original thought natively, adding a comma and a pair of quotes and moving the mouse into them. Would that have to be some sort of custom macro, perhaps? There are a few things I could see being useful to be able to have specific sets of actions be performed. 

0
Comment actions Permalink

Hi,

the closest you can get with built-in tools is to run the 'Complete current statement" action inside a string (default keybinding is Ctrl+Shift+Enter). It adds a comma after a string literal and places a cursor after the comma. So after that you need to type enter and start a new element if needed. I'd say it is a more general action since the next element is not always needed. If you think a dedicated action for adding one more element would be much more useful, please file a feature request in youtrack.

0
Comment actions Permalink

That's not too bad and works well considering when you create a new set of quotes it automatically places the cursor within them. I am just trying to be efficient as possible, yet end up spending a bunch of time researching if I can do the thing I think would save me some time, or making long posts about it. Instead of saving the 1/4 of a second I waste 30+ minutes, lol.

Speaking of which, is there any sort of way to add your own custom hot key? I wanted to make it so that when I press ctrl + "=" it adds ":=" instead of just "=", or if it would be best to just use something like AutoHotKey?

0
Comment actions Permalink

You can do that using macros as described here: https://www.jetbrains.com/help/idea/using-macros-in-the-editor.html#bind. Record a macro inserting ':=' and bind it to the shortcut you want. I'd also suggest using post-fix completion. With it you can type 'foo().var', invoke completion, and it will create a variable for you.

0

Please sign in to leave a comment.