IDEA Live template $SELECTION$ doesn't work when not used with surround template - intellij-idea

I'm creating some live templates in IntellIJ IDEA 14 and I need to do something similar to sout live template where I can write
something.sout
and become
System.out.println(something);
The sout live template is
System.out.println($END$);
So I tried using $END$ (which, according to Docs, should just change the cursor position after the change) but my template instead of taking the value before the dot adds itself at the end without text inside
Assert.assertFalse($END$);
so something.af is replaced with something.Assert.assertFalse();.
Then I tried using $SELECTION$ and it worked if used with COMMAND+ALT+J (Surround generator) but I get the same effect if I use the normal syntax with something.af

It's not a Live Template you want in this case, you want to configure a new Postfix Completion. The sout case that you were looking at was the live template version, which is, confusingly, not the same as the postfix version.
I don't think you can create your own postfix completion templates yet, it looks like you can only enable or disable existing ones (Preferences -> Editor -> General -> Postfix Completion).
You might want to vote for or comment on this suggestion to allow creation of custom templates.

Related

'fold up' sections of code - like when you close a control structure

I remember that there was a tag which made it possible to fold multiple lines.
e.g. like if you would fold down a for loop:
to
Is there a tag which makes this possible? Or is this an IDE specific tag?
Depends on the language and ide/editor.
In C# there are #region's that can be used for this. In some editors you can enable folding on all scopes (brackets). In some editors you can teach the editor to enable folding on comments with brackets in them ("//{" "//}").
In most cases this is an editor option that has to be enabled and configured.
What editor are you using? (and what language is this, JavaScript?)
For netbeans checkout the following: (You don't need to go past the first one)
http://wiki.netbeans.org/FaqCustomCodeFolds - Manual method
http://wiki.netbeans.org/SurroundWithCodeFolding - Code Template
https://ui.netbeans.org/docs/ui/code_folding/cf_uispec.html#custom - Talks about the how they work.
Example from first article:
// <editor-fold>
Your code goes here...
// </editor-fold>

How to fold custom region in CLion through comments?

I read this:
https://www.jetbrains.com/clion/help/folding-and-expanding-custom-blocks.html
and tried following this link:
https://www.jetbrains.com/clion/help/Folding_Custom_Regions_with_Line_Comments.html
but it appears to be broken, since it redirects to "Meet CLion" page.
How do I define custom folding regions in CLion using comments?
CLion is based on IntelliJ IDEA, and thus supports multiple ways, including:
//region Description
//endregion
and
//<editor-fold desc="Description">
//</editor-fold>
These can be easily accessed by selecting the code you wish to wrap in a region, and hitting Ctrl+Alt+T. You can alternatively access it by clicking Code > Surround with... in the menu bar.

switching between languages in same file

I recently attended a user group meeting where the IntelliJ representative was demonstrating version 13.
He demonstrated how to switch the code completion view of a file. I do not exactly remember what the file extension of this particular file was, probably java.
The concept was that if the file is html with embedded javascript he could then switch the code completion between html and javascript with a shortcut. If he says treat the file as html then all code in file was treated for code completion purposes as html, and vice versa for javascript.
Does anybody know what shortcut he might have been using to enable the language switch?
Sounds like you may be referring to the IntelliLang feature. IntelliJ IDEA can be aware of other languages embedded within a file.
A simple example is in an HTML file that has CSS and JavaScript.
Notice when I am inside the HTML markup:
or inside an HTML element:
The code complete shows HTML completion options. However, when I am inside the style attribute, I get CSS code completion:
I also get CSS code completion if I am inside a <style> element. So even though I am in an HTML file, I see CSS code completion because of my location.
Same case with JavaScript. When I invoke code completion inside a <script> element, I get JavaScript completion, even though I am in an HTML file.
Anytime IntelliJ IDEA can determine that another embedded language is present, it provides, via IntelliLang, the appropriate syntax highlighting, error highlighting, and code completion. The same holds true for Java. Notice here that IDEA knows the method I am competing takes an SQL statement and therefore highlights the String value using SQL highlighting, and provides SQL code completion:
So even though I am in a .java file, I get SQL code completion. The reason is that IntelliLang comes pre-configured knowing the embedded language of some methods. You modify them, or add more, in File > Settings > [Project Settings] > Language Injections.
In addition, you can use an annotation to tell IntelliJ IDEA (as well as developers looking at the code) that a String must be valid in a particular language. For example, I can annotate a String field, variable, or parameter, to indicate it must be valid HTML:
Notice I get HTML syntax highlighting, HTML code completions, and the CSS color shows in the left gutter. If I annotate a method parameter, then any time I call the method, I get the appropriate syntax highlighting, code completion, and error/warning highlighting:
The #Language annotation is inside the annotations.jar that is contained in the redist directory inside the IntelliJ IDEA installation directory. It is also available in maven central, or IDEA will offer to attach it as a Library if you use the annotation without it being attached.
IntelliLang and the #Language annotation supports a large number of languages. Just use code Completion inside the quotes after typing #Language("") to see a list. (Inline search works in the list as well.) One of the most useful is Regexp. For example, if you have a method that expects the string passed in to be a valid Regular Expression, annotating it as such will give anyone that calls it Regex code completion and error highlighting if they are passing in an invalid Regex pattern. Even for developers using other IDEs it is useful as a form of documentation.
As for a shortcut to change the the language on the fly for code completion, the only thing I can think that you might be referring to is the "Inject Language" intention. If I am entering a String value, and I bring up the quick-fix/intention menu via Alt+Enter, I am given an option to inject a language:
If I select that, IntelliJ IDEA will ask me what language I want to use:
After making my selection, IntelliJ IDEA will give me temporary language injection (including code completion) for the selected language.
It also gives me an option to add the #Language annotation for permanent injection.
To the best of my knowledge (as a 10 year IntelliJ IDEA user) that is the only way to switch code completion language types. So hopefully that is what you are looking for. To me, IntelliLang is one of the coolest features in IntelliJ. (It actually started as a third party plug-in and JetBrains then absorbed it into the product.)

Automatic generation of HAML from HTML in SublimeText 2?

I would like to have SublimeText show the HTML result of parsing my HAML in another window, live, as I type the HAML.
Does anyone have an idea of how to make this happen? In, e.g., WebStorm there are file watchers that do this, but is there something like that built into ST2?
I did something similar to this with Typescript, only I only updated the Javascript output when I saved the file. First, I would say to take a look at SublimeBuildOnSave. That'll show you how to hook into the save function and see what file is being saved.
When you've got the general idea, you can take a look here and mess around with opening another pane (or using the existing pane) to show the compiled output.
It's pretty simple to make a simple version of this. If you want more advanced features like live compiling, it'll take a bit more effort, but it's not impossible. But the above scenario worked just fine for Typescript for me. (If I find where I left that code, I'll post it.)

IntelliJ wrapping comments in Javascript

Is it possible to wrap long comment lines in IntelliJ in javascript files? I have turned on line wrapping as shown in the settings below, but when I do a code reformat, it doesn't wrap the line.
It's not supported yet on code reformatting, added a new feature request, please vote.
I'm using IntelliJ 14 on a Mac, which has a Fill Paragraph command. Access it via the awesome universal Command-Shift-A action search feature. Works like a charm!