Delete Blocks of Whitespace in Emacs - emacs

I often end up with blocks of code like this:
public class CustomFile {
public String path;
public String name;
public CustomFile (String pathToFile, String dbName) {
path = pathToFile;
name = dbName;
}
}
I want to be able to put my cursor on the line above public CustomFile and be able to delete all of the whitespace up to but not including public String name;. Is there a command or macro that will allow me to do this?

This looks like what you want:
C-x C-o runs the command delete-blank-lines, which is an interactive
compiled Lisp function in `simple.el'.
It is bound to C-x C-o.
(delete-blank-lines)
On blank line, delete all surrounding blank lines, leaving just one.
On isolated blank line, delete that one.
On nonblank line, delete any immediately following blank lines.

Related

Eclipse formatter: How to move comment between method name and open brace to always have open brace on same line as method declaration

I have code that looks like this, that I'm trying to format
Original code:
public int doThing(int a) // -incredibly useful comment here
{
int ab = a+1;
return ab;
}
I want it to look like this
public int doThing() { // -incredibly useful comment here
int ab = a+1;
return ab;
}
If I try to turn on the Brace position -> Method Declaration -> Same line option and run the formatter, any code with a comment in the position "breaks" the formatter, and I get an output for my example that looks the same as the original code, but methods without a comment have the correct formatting (meaning the results are inconsistent).
Is it possible with the eclipse formatter to get the style I want? I'm trying to run it against a large amount of code, and would prefer not to have to fix these all manually to get a consistent brace position.
The problem here is that is not formatting but rewriting. Using File Search + regular expression + Replace could do that in bulk.
Try this regex
^(\s*(?:public|private|protected)\s+[^(]+\([^)]*\))(\s*\/\/[^/]+)\R\s*\{
On File Search ( Ctrl + H)
Hit Replace and use $1 { $2\n as replacement
Code should compile after the refactoring.
UPDATE:
Fixed regex part that represents function arguments
\([^)]*\)
Full Regex matches these cases
public int doSmthg() // coment here
{
return 1;
}
private String doSmthgElse(String arg) // coment here
{
return arg;
}

How can I return a formatted string from a REST call?

How can I create a Restful web service that will display as a multi line result in a Windows console.
I have tried the following but it displays the \r\n as string characters instead of line feeds. Also tried just \n, 0x0A, nothing seems to work.
#RequestMapping(value="/fetchMultiLine.do", method = RequestMethod.GET)
public String fetchMultiLine(HttpServletRequest request) {
return "Lines:\r\nfirst\r\nsecond\r\netc"
}
It always interprets the new line character as string characters and displays:
Lines:\r\nfirst\r\nsecond\r\netc
When I really want
Lines:
first
second
etc
Any hope?

Can Eclipse add method description on {+enter

How can I have Eclipse to automatically add method description block on method definition?
If I write: public int test(int hello){
And then press "Enter".
The Eclipse will add the end bracket two lines below, so the code ends up like this:
public int test(int hello){
}
If I then press ctrl+shift+j while the cursor is in the method name, the code ends up like this:
/**
* #param hello
* #return
*/
public int test(int hello){
}
Now my question is: Is it possible to let Eclipse add the method description (what is added on ctrl+shift+j) automatically, when I create the method? At the same time as it creates the end bracket.
What you have mentioned as method description is technically known as Javadoc comment. In Eclipse - Windows -> Preferences -> General -> Keys has the list of command and the keys binding to those commands. You can change the keys as per your comfort.

Return brackets as string with shortcut

in my autohotkey script i have
:*:eclick:: .addEventListener("click",function(){});
but {} is always ignore, is it possible to return {} as text or is it protected ?!
Thanks,gui
When sending special characters, it's best to use raw mode in order to ensure that the keys won't be misinterpreted:
#Hotstring r
:*:eclick:: .addEventListener("click",function(){});
Alternatively, you could use SendRaw:
:*:eclick::
SendRaw, %A_SPACE%.addEventListener("click",function(){});
return

Is it possible with Eclipse to only find results in string literals?

Lets assume I have the following Java code:
public String foo()
{
// returns foo()
String log = "foo() : Logging something!"
return log;
}
Can I search in Eclipse for foo() occurring only in a String literal, but not anywhere else in the code? So in the example here Eclipse should only find the third occurrance of foo(), not the first one, which is a function name and not the second one, which is a comment.
Edit: Simple Regular Expressions won't work, because they will find foo() in a line like
String temp = "literal" + foo() + "another literal"
But here foo() is a function name and not a String literal.
You can try it like this:
"[^"\n]*foo\\(\\)[^"\n]*"
You have to escape brackets, plus this regex do not match new lines or additional quotes, which prevent wrong matches.
Maybe you should use regex to find any occurence of foo() between two " ?