logd shortcut doesn't work in Intellij with Kotlin - intellij-idea

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.

Related

what is the shortcut key of kotlin for loop in Intellij?

when we working with java in Intellij we have fori key for auto complete a for loop but i could not find the same shortcut key for that when working with Kotlin in Intellij .
actually i have tried and found iter shortcut but it is for foreach i want auto complete of some thing like for (i in 0 until 10 step 3) .
can you help me find the correct key for that
I think iter is the closest you get out of the box; it is is defined in the live templates as:
for ($VAR$ in $ITERABLE$) {
$END$
}
Go to preferences then search for live, this will show you all the live templates. If you then open the Kotlin section in the tree on the right
you can make a copy of iter (using the copy icon) then you can edit it in the pane at the bottom. Call the copied template iteru and amend it to something like
for ($VAR$ in $START$ until $ENDV$ step $STEP$) {
$END$
}
If iter is displaying foreach it might be that intellij thinks the source file type is something other than Kotlin.

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.

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.

How to make jedit file-dropdown to display absolute path (not filename followed by directory)?

All is in the title.
If a have opened the three files:
/some/relatively/long/path/dir1/file_a
/some/relatively/long/path/dir1/file_b
/some/relatively/long/path/dir2/file_a
The file dropdown contains:
file_a (/some/relatively/long/path/dir1)
file_a (/some/relatively/long/path/dir2)
file_b (/some/relatively/long/path/dir1)
And that bother me because I have to look on the right to differentiate the two file_a, and on the left for the others. This happens a lot to me mostly because I code in python, and thus I often have several __init__.py files opened.
How do I get jedit to display
/some/relatively/long/path/dir1/file_a
/some/relatively/long/path/dir1/file_b
/some/relatively/long/path/dir2/file_a
config:
jedit 5.1.0
java 1.6.0_26
mac osx 10.6
Unfortunately this is not easily possible currently, I just had a look at the source and this is not configurable.
You can:
Submit a Feature Request to make this configurable (good idea in any case)
Create or let create a startup macro that
registers an EBComponent with the EditBus that listens for new EditPanes getting created
retrieve the BufferSwitcher from the EditPane
retrieve the ListCellRenderer from the BufferSwitcher
set a new ListCellRenderer to the BufferSwitcher that first calls the retrieved ListCellRenderer and then additionally sets the text to value.getPath()
Try the Buffer List plugin as to whether it maybe suits your needs
Now follows code that implements the work-part of option two, runnable as BeanShell code which does this manipulation for the current edit pane. The third line is not necessary when done in an EBComponent, this is just that the on-the-fly manipulation is shown immediately.
r = editPane.getBufferSwitcher().getRenderer();
editPane.getBufferSwitcher().setRenderer(
new ListCellRenderer() {
public Component getListCellRendererComponent(list, value, index, isSelected, cellHasFocus) {
rc = r.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
rc.setText(value.getPath());
return rc;
}
});
editPane.repaint();