Surround multiple lines with quotes - intellij-idea

I'm trying to build a live template that will work like the 'wrap in comment' live template (that puts a // before each selected line). For example, select multiple lines of text, click Code > Surround With > Single quotes (custom live template) and quotes will appear around each line.
E.g. from:
text1
text2
text3
to:
'text1'
'text2'
'text3'
Unfortunately the template I define:
'$SELECTION$'
produces:
'text1
text2
text3'
which makes sense.
Is there any way to define a Live Template that will work on each line of my selection?

"Wrap in comment" is not a live template, but an action that is implemented in Java. In the same way, you can't accomplish what you need using a live template, but you can write a small plugin in Java to implement that feature. Please refer to the Editor Basics tutorial to get started with writing the plugin.

Related

Intellij shortcut to create code snippet for testing

Is there any shortcut/setting/plugin available in intellij where I can create test data structure with some default values ?
e.g.
Map<String,String> stringMap = new HashMap();
stringMap.put("1","A")
stringMap.put("2","B")
stringMap.put("3","C")
I dont want to type all of the above as I want to test very quick with any values.
You can use the 'Live Templates' functionality, for instance. A guide on how to create such a template can be found here.
Use live templates to insert common constructs into your code, such as
loops, conditions, various declarations, or print statements.
To expand a code snippet, type the corresponding template abbreviation
and press Tab
In the screenshot below, I created a custom template called smap in the 'other' template group, added your code to it and selected the language (Java) that this template can be applied to.
Once in the editor, I can type smap and a pop-up will appear suggesting me to use the existing template to replace the abbreviation with code. Hitting Tab or Enter will perform the replacement.

logd shortcut doesn't work in Intellij with Kotlin

Logging Java in Intellij is easy with shortcuts such as 'logt', 'logd', 'loge'... and so on. But I moved to Kotlin, I noticed that those shortcuts doesn't work anymore. I don't know if it has something to do with my configuration, but if not, how can I fix this?
You should create separate templates to make them work correctly.
Here is the step-by-step guide:
Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:
1. logd (and others)
Select the logd item and press "Edit variables"
Change expression to: kotlinFunctionName()
Also, remove ; from the end of the template, as you don't need it in Kotlin.
Now your method name will be shown correctly
logt
This one is a bit trickier.
Solution 1 TAG = class name.
Template text :
private val TAG = "$className$"
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName())
Solution 2 TAG = file name (can be used inside Companion)
Template text :
private const val TAG = "$className$
or:
companion object {
private const val TAG = "$className$"
}
Edit variables -> Expression:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
Edit 19.02.19
Also, it might be useful for someone.
In order to avoid creating the TAG variable, you can use the class name as a variable, for instance:
Log.d("BaseActivity", "onCreate: ")
Where BaseActivity is the class name.
The template will look now as:
android.util.Log.d("$CLASS_NAME$", "$METHOD_NAME$: $content$")
Where CLASS_NAME variable is:
groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension())
These are provided in IntelliJ as a Live Template configuration for AndroidLog (found in Preferences -> Editor -> Live Templates), and are applicable specifically to Java code:
There isn't anything broken in your configuration, but if you want to make these Live Templates available for Kotlin you will need to add new Live Template for AndroidLog and make them applicable to Kotlin code.
https://www.jetbrains.com/help/idea/2017.1/creating-and-editing-live-templates.html
There's an open feature request to have them added as defaults here: https://youtrack.jetbrains.com/issue/KT-10464
Change the scope of the template in the applicable option.
In Android Studio 4.0 there's new AndroidLogKotlin block. You can implement #LeoDroidcoder's solution there.

IntelliJ plugin autocomplete formatting

I add an autocomplete fonctionnality to my IntelliJ plugin like this :
String completionString = "myFunction(err, data){if(err){}else{}}";
return LookupElementBuilder
.create(completionString)
.withBoldness(true)
.withIcon(SailsJSIcons.SailsJS)
.withPresentableText("myFunction()")
.withCaseSensitivity(true)
.withTypeText(type)
.withTailText(" (" + item + ")", true)
.withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE);
How can I format the new method I will put on editor ? I don't want a custom formatting, just want the default formatting of IntelliJ
You can add an InsertHandler to your LookupElement and perform CodeStyleManager.reformat(psiElement) in its handleInsert method.
However, I don't see why you're making this available through a completion contributor at all. This looks like a live template, and your plugin can contribute live templates with far less effort than custom completion. Live templates are automatically shown in completion lists and support automatic reformatting.

InteliJ - How to create shortcuts for code samples

I am using InteliJ and really love using it. One of the questions I have is this:
Is there a way to create code short cuts?
For instance, while bug testing, I am forever writing:
<?php die(var_dump($var)); ?>
and figured it would be great to have a shortcut key to automate this. i.e.
"Cmd Option D"
or something similar to dump the pre-defined statement into my code...
Any thoughts on this?
You can use Live Templates:
To define a template go to Settings/Live templates, then select group or create new group of templates and hit the green plus button and select Live Template.
In the abbreviation field type for example vd which will be the trigger for your snippet, define context, which represents the languages this template will be available for and put this in the Template Text field:
<?php die(var_dump($SELECTION$)); ?>
The $SELECTION$ part is a variable which represents current selection.
Now when you are in editor, you can just type vd and hit Tab. That will expand your snippet and put your cursor inside var_dump().
You can event type the variable name you want to dump, select it, hit CTRL+ALT+T, which will show you a Surround with dialog, where you can choose your template. After you select it your variable name will be surrounded with the var_dump snippet.
Another way to invoke a live template is to hit CTRL+J which will show you autocomplete popup with the available templates.

haml editing on gEdit, any way to auto indent?

I'm using gEdit to edit haml files and the auto indenting feature seems to work when i'm writing one line after another, but when I go and clear out a exisiting element (a div or a class) the rest of the code below doesn't adjust itself. Suppose this is my code
.rightside
.container
%ul
%li hello
%li world
Now is there any way (using some plugin) through which when I delete .rightside, the rest of the code moves to left by two spaces? I now have to manually erase the space on every line. Thanks for your input.
You can select the block underneath the tag in question and use Shift-Tab or Ctrl-Shift-T to un-indent the text, assuming you have gedit set to use 2 spaces for tabs.
I have also installed the multi-edit plugin. This gives me multiple carets at which points any edits i make will be duplicated. In this case, I can just position the cursor at the top of that block, use the Ctrl-PgDown key shortcut to select the whole block, and then hit delete a couple times.