Eclipse Shortcut to Split Long Strings - eclipse

I swear I've seen someone do this, but I can't find it in the various lists of shortcuts.
Given:
String s = "A very long ............................ String";
Is there an Eclipse shortcut to turn it into:
String s = "A very long ............................ "
+ "String";

Yup - just hit return when your cursor is in the middle of the string.
Admittedly that puts the + at the end of the first line instead of the start of the second, which is irritating if your style guide demands the latter, but if you're not fussy it's great :)

All the formatting templates in Eclipse will put the plus on the next row (which I find really annoying), so you can simply apply the code formatter and the plus will end up on the next row.

There may be a Quick Fix (Ctrl + 1) for this as well.
I was amazed in 3.4 to discover that there are Quick Fixes to transform +-based string concats into uses of StringBuilder or MessageFormat. Brilliant!

Also you can format code using regular expression. Select expression, press Ctrl+F and use:
Find: "\s*?\+\s*?\R(\s*?)"
Replace with: "\R$1\+ "
☑ Regular expressions

Related

How to tranform css hex to uppercase in vscode?

I'm just using vscode prettier, and I was told that hex value should be uppercase as company's standard. I've been through a search and I found their github-issue
that states,
" Personally I prefer uppercase too, but I was asked to do lowercase... But changing this would cause a lot of unnecessary churn for all Prettier users... this decision has been made and is not going to change. An option won't be added." - Sep. 29,2018
So basically, prettier transformed hex value to lowercase, and does not provide an option to change it to uppercase.
I want to ask, have they change their decision now and made an option to transform css hex value to uppercase? And if theres no really option for this in prettier, is there any alternatives to achieve this?
My basic alternative that I used is the idea of #rioV8, however I would prefer to use replace field instead of Transform to UpperCase, I think that would be more quick.
I would like to provide this as answer because it's at least working.
In vscode find #(?:[0-9a-fA-F]{3}){1,2} using regular expression, and replace it with \U$0.
Updated: how to auto transform after save
Follow this link: How to automatically run a "find and replace" after save?
Since \U$0 did not work well, we looked for alternatives.
Search by regular expression
Open a search window with cmd + shift + f, click .*, and execute a regular expression search with #(?:[0-9a-fA-F]{3}){1,2}.
Select the target string
In the search results window, use cmd + a to select all and cmd + shift + l to select the target string.
Open the command palette
Open the command palette with the cmd + shift + p.
Convert to upper case
Type upperor lower on the command palette and execute.

Netbeans Find and replace all strings

Is there any way to find all Strings in my source code regardless of their content. In other words I want to find everything that starts and ends with " . What I want to do afterwards is replace all found strings with someMethod(string).
Edit:
I think I have not expressed myself clear. I want to find all strings no matter what the content between the " is and the "encapsulate" them. In your example syso("test") should become syso(somethod("test"))
You can use the Find In Project functionality using the Basic Wildcards options.
Then use the * wild card and search for syso(*) . This will return all occurrences of this method. Then manually you can replace the text you want.
CTRL+F - search for " and replace manually:
search for : the thing you want to replace(e.g. ")
replace with: the thing you want to past instead (e.g.someMethod())
-> But look out for errors! think about replacing bevor you try it- because if you replace all " with somemethod, you have a lot of errors! Look at this example:
syso("test");
//---replace actoin: someMethod() instead of "
syso(someMethod()testsomeMethod())
-> best way is to make this kind of manipulations (in my oppinion): manually!
Perhaps there are some Refactoring-Tools in Netbeans which could help you-but i don´t think that there is a "simple and easy" solution.
If you are searching for a solution for the whole project you are working on, go into the Edit menu-y there ou should find a item for "Replace in Project";
I hope i understood you right & could help a little!

How to make Eclipse break String literal with + operator in next line?

(Edited) NOTE: this question is NOT about how Eclipse's code formatter wrap long String literals. It is the behavior of MANUALLY breaking/wrapping String literal
The default behavior when I break a String literal by hitting enter inside the literal, is Eclipse will append " + at the place I hit enter, and start the rest of my line in next line, with " prefixed.
// v ENTER HERE
String longString = "abcdefghij|klmno";
String longString = "abcdefghij" +
"klmno";
However, normal coding style practice suggests line wrap before operator. Is there any way that I can tell Eclipse to break my String like this?
String longString = "abcdefhij"
+ "klmno";
In 4.3 you can control this setting with the Wrap before operator checkbox for Binary expressions.
Finally I found this is one missing feature of Eclipse.
Currently this issue is tracked under Eclipse's issue tracker :
https://bugs.eclipse.org/bugs/show_bug.cgi?id=48433
Update: I am now using Eclipse 4.3, and found that this feature is now available. Position of + operator when you are manually breaking a long String will now follows your code formatter setting.

Parentheses over selected words in Eclipse

A few days back I felt this question to be dumb and dint post it here, but after even after searching a lot I dint find a proper solution.
For those of you who used TextEdit (on Mac), they will perfectly know what I am talking about.
While coding I just want to put quotes or parentheses over a word or a line.To do this I'll have to move back to the starting of the word, open the quote and then go to the ending of the word and close it.
Is there a plugin or so in eclipse where I can just surround the current selection with quotes or parenthesis ?? I am not talking about quick fix (Ctrl + 1). It can be used for much complex templates.
You can do that with a custom template, so for example if you want to create this template for java, you can do :
Preferences ---> Java ---> Templates
Create a new template and call it quote, then type this as pattern :
"${word_selection}"${cursor}
Save it and Apply.
After that you can use that template selecting the text you want to quote then press CTRL + SPACE and then chose quote.
Same thing for parenthesis :
(${word_selection})${cursor}

How to search and replace 2 lines (together) in Eclipse?

I would like to search multiple files via eclipse for the following 2 lines:
#Length(max = L_255)
private String description;
and replace them with these two:
#Length(max = L_255, message="{validator.description.len}")
private String description;
Another tip on how to get the regex for a selected block.
Open one of the files that contains the multiple lines (multiline) to search or replace.
Click Ctrl+F and select "Regular expression". Close the Find/Replace window.
Select the block you need and click again Ctrl+F to open the Find/Replace window.
Now in the Find text box you have the regular expression that exactly matches your selection block.
(I discovered this, only after creating manually a regexp for very long block :)
Search are multi-line by default in Eclipse when you are using regex:
(\#Length\(max = L_255)\)([\r\n\s]+private)
I would like to add "private String description;"
(\#Length\(max = L_255)\)([\r\n\s]+private\s+?String\s+description\s*?;)
replaced by:
\1, message="{validator.description.len}")\2
It works perfectly in a File Search triggered by a CTRL-H.
As mentioned in Tika's answer, you can directly copy the two lines selected in the "Containing Text" field: those lines will be converted as a regexp for you by Eclipse.
CTRL+H does take two lines if you use regexp (and you don't have to write the regexp by yourself, eclipse does that for you).
Select your lines.
Click CTRL+H. The search dialog opens up.
If "Regular expression" is already checked, eclipse will have converted the two lines you search for into regexp for you, click Search.
If "Regular expression" if not already checked", check it and click Cancel (eclipse remembers your choice).
Select your lines again.
Click CTRL+H. The search dialog opens up. This time "Regular expression" is already selected. eclipse will have converted the two lines you search for into regexp for you, click Search.
A quick tip for including multiple lines as part of a manually constructed regular expression:
Where you would normally use .* to match any character zero or more times, instead consider using something like (?:.|\r?\n)*. Or put an extra ? at the end to make it non-greedy.
Explanation: . doesn't match new lines so need to do an "either-or": The parentheses match either the . before the pipe or the new line after it. The ? after \r makes the carriage return before the line feed optional to allow Windows or Unix new lines. The ?: excludes the whole thing as a capturing group (which helps to avoid a stack overflow).
Click Ctrl + F and select "Regular Expression" and then search the lines. In case to perform the same on multiple files, click Ctrl + H, click on 'File Search' and perform the same.
Select the folder that contains all your files and press Ctrl+H.