IDEA: How to switch between quotes in current cursor position - intellij-idea

I want to have an easy one command way to switch between quotes style in IDEA.
"Sample test" => 'Sample test'
'Sample test' => "Sample test"
I know I can select all the text with quotes and type " to change from single quotes, for example.
I can use CMD-Up few times to expand selection and after that change surrounding.
But It is too many actions to do for such small change.
Maybe Idea have a better way to change the current quote style?
Or any plugins for that?

Without plugins you can:
Use Alt-Enter context action with your cursor in the string and select "Replace single-quoted string with double-quoted string" (or vice versa). This will only apply to the string where your cursor is.
Update your code style settings in whatever language you are using to be the type of quotes you want and then apply your code style with Cmd-Alt-L. This has the side-effect of potentially applying code style not related to quote type if other code doesn't match your configured style.

Related

Extracting tokens out of ES6 template literals with Ragel

JavaScript contains the following syntax:
`hello ${name}`
I'm wondering how a Ragel machine would split the syntax above. The way I see it, the type of the closing curly brace depends on the parsing state. For example, in the code below the curly brace is instead part of the string token, since the ${ token isn't there:
`hello name}`
Finally, it becomes more tricky when you consider that the right curly can also be found within the variable expression itself, ie:
`hello ${() => { return name }()}`
How would a similar context-dependent grammar be implemented with Ragel?
The syntax inside of `` is not normally something you would handle with your lexical analyzer. Better to send it to your parser as a sequence of literal text and/or tokens. So you'd send "`" as opening, "hello " as some literal text, then the tokens "(", ")" etc. To know when to stop and go back to literal text you either need some feedback from your parser to to your scanner, or inside the scanner you need to balance the parens.
Note I've never actually made a parser for javascript, just going on what you provided above.

intellij wrapping brackets around if-else

Coming from a language like python, I often forget to wrap parenthesis around if conditions when writing Java.
Is there a parenthesis completer built into IntelliJ? If not, how do I best work around this issue? (short of manually wrapping with parenthesis myself -:))
When you will start typing if code completion will ocurs. Push enter - your carret will land inside bracket.
Use surrounding blocks of code. You can first write boolean condition then press shortcut for Surround With option (on windows/linux by default it is Ctrl + Alt + T) and choose if(expr).
Or if you want to only surround with paranthesis, then use (Surround With) and then choose (Curly Braces).
You can use predefined live template with ifn which will initialize if block with comparison to null. You can also configure your own template.
Also you might want to check if this option is selected in your IDE:
Settings(Preferences) | Editor | General | Smart Keys, Surround selection on typing quote or brace

How to use IntelliJ's structural replacement for "all matches, except..."?

Lets say I want to replace print(x) with print(wrapper(x)). I can match
print($Argument$)
and replace it with
print(wrapper($Argument$))
However, if I already have a call to print(wrapper(x)), I don't want to replace that with print(wrapper(wrapper(x))). How can I avoid that? In other words, how do I say "do the replacement, unless the arguments match some pattern"?
You would:
Input search template (for example): System.out.println($args$)
Click Edit Variables
Choose the $args$ variable
Under Text constraints -> Text/regexp enter ^wrapper\(.*\)$ and tick Invert condition
Obviously you can tweak that regex to whatever you want. Invert condition means that the search will skip all instances where condition is met. Basically you write a regex to match what you don’t want to see and Invert condition is a NOT operator.
On my test text:
System.out.println( ex.getMessage() );
System.out.println( wrapper( ex.getMessage() ) );
The second instance was not in the search results.
Another option is to replace
print($Argument$)
with
print(wrapper($Argument$))
and then get rid of any double wrappers by replacing
print(wrapper(wrapper($Argument$)))
with
print(wrapper($Argument$))

How to set default value of variable in live template in Intellij IDEA?

There could be a little misunterstanding in live templates in Intellij IDEA. I mean default values for variables in templates.
Suppose we have this live template
What I expect here, that when calling this template (type jqon and press TAB) I will see default values already typed which I can change or leave as it is. Like this
But no. I have empty strings instead of default values
Why?
I was wrong about Default value field. I don't need this in my case. I need to fill Expression field.
If I want just paste some string as default value I should put this string in quote in Expression. So now my variable settings look this way
And everything works how I want!
If you want a hardcoded string as the default value field (in the edit variables dialog), it needs to be in double quotes ("ii"). Putting a string there with no quotes (ii) does not result in an error, but also does not work.

How to output ${expression} in Freemarker without it being interpreted?

I'm trying to use Freemarker in conjunction with jQuery Templates.
Both frameworks use dollar sign/curly brackets to identify expressions for substitution (or as they're called in freemarker, "interpolations") , e.g. ${person.name} .
So when I define a jQuery Template with expressions in that syntax, Freemarker tries to interpret them (and fails).
I've tried various combinations of escaping the ${ sequence to pass it through Freemarker to no avail - \${, \$\{, $\{, etc.
Inserting a freemarker comment in between the dollar and the curly (e.g. $<#-- -->{expression}) DOES work - but I'm looking for a more concise and elegant solution.
Is there a simpler way to get a Freemarker template to output the character sequence ${?
This should print ${person.name}:
${r"${person.name}"}
From the freemarker docs
A special kind of string literals is the raw string literals. In raw string literals, backslash and ${ have no special meaning, they are considered as plain characters. To indicate that a string literal is a raw string literal, you have to put an r directly before the opening quotation mark or apostrophe-quote
For longer sections without FreeMarker markup, use <#noparse>...</#noparse>.
Starting with FreeMarker 2.3.28, configure FreeMarker to use square bracket syntax ([=exp]) instead of brace syntax (${exp}) by setting the interpolation_syntax configuration option to square_bracket.
Note that unlike the tag syntax, the interpolation syntax cannot be specified inside the template. Changing the interpolation syntax requires calling the Java API:
Configuration cfg;
// ...
cfg.setInterpolationSyntax(SQUARE_BRACKET_INTERPOLATION_SYNTAX);
Then FreeMarker will consider ${exp} to be static text.
Do not confuse interpolation syntax with tag syntax, which also can have square_bracket value, but is independent of the interpolation syntax.
When using FreeMarker-based file PreProcessor (FMPP), either configure the setting via config.fmpp or on the command-line, such as:
fmpp --verbose --interpolation-syntax squareBracket ...
This will call the appropriate Java API prior to processing the file.
See also:
https://freemarker.apache.org/docs/dgui_misc_alternativesyntax.html
http://fmpp.sourceforge.net/settings.html#templateSyntax
Another option is to use #include with parse=false option. That is, put your jQuery Templates into the separate include page and use parse=false so that freemarker doesn't try and parse it.
This would be a good option when the templates are larger and contain double quotes.
I had to spent some time to figure out the following scenarios to escape ${expression} -
In Freemarker assignment:
<#assign var = r"${expression}">
In html attribute:
Some link
In Freemarker concatenation:
<#assign x = "something&"+r"${expression}"/>
If ${ is your only problem, then you could use the alternate syntax in the jQuery Templates plugin like this: {{= person.name}}
Maybe a little cleaner than escaping it.
Did you try $$?
I found from the Freemarker manual that ${r"${person.name}"} will print out ${person.name} without attempting to render it.
Perhaps you should also take a look at Freemarker escaping freemarker
I can confirm that the
${r"${item.id}"}
is the correct way as an example.
So I kinda full example will look like
<span> Remove </span>
and the output will be :
<span> Remove </span>
In the case when you want to use non-raw strings so that you can escape double quotes, apostrophes, etc, you can do the following:
Imagine that you want to use the string ${Hello}-"My friend's friend" inside of a string. You cannot do that with raw strings. What I have used that works is:
${"\x0024{Hello}-\"My friend's friend\""}
I have not escaped the apostrophe since I used double quotes.