How to find extra newlines with RubyMine? - rubymine

I am trying to detect extra newlines between two methods in my code.
How can I detect all of them with RubyMine? For example:
def some_method
end
# two new lines
def other_method
end

Edit -> Find -> Find in Path
Set "Text to find" to
end\n\n\n
Check "Regular expression"
click Find
There might be spaces before any of those newlines, so you might find more matches with
end *\n *\n *\n
You could also consider removing extra newlines automatically by reformatting your code:
make sure that Preferences -> Project settings -> Code Style -> Ruby -> Other -> "Blank lines around methods" is set the way you want (and all the other Ruby code style settings are the way you want)
in the project page, select the parent directory of all of the files you want to reformat
Code -> Reformat Code...
hit Run
Of course you would only do this if you were prepared to reformat everything.

Related

Use BBedit to replace CR with LF

I have a text file where I would like to change all the Carriage Return to Line Feed. I am working on a Mac, and it seems like BBedit should be able to easily do this. However, in the search function it does not appear to differentiate between CR (\r) and LF (\n). Searching for either character gets the same hits, and a search for "\r" to replace with "\n" does not work.
Is there some other way to represent CR and LF, so that BBedit can differentiate between them?
A possibly simpler way to do this is to make sure you enable "Line Break Types" in BBedit's Preferences -> Appearance.
This will add a Line Types popup menu at the bottom of the window.
Not only does it show you what the current format is, but it makes it trivial to switch between them.
(I also turn on "Text Encoding" as well, in case I need to switch between UTF8 and UFT16 and others.)
For a single file:
Text -> Apply Text Transform
Select Change Line Endings
Click the Configure button and choose your desired line endings
For multiple files:
File -> New -> Text Factory
Select Change Line Endings
Click the Options button and choose your desired line endings
Save the Text Factory and apply it to the files you want changed through the Choose and Options buttons

Formatting code in Netbeans to printable format

I am writing Java class file using NetBeans and I use ALT+SHIFT+F for auto formatting.However, This is not wrapping the code to be printable in single page.
How to change customize Formatting settings so that when i do auto formatting the code gets formatted as such it can be printable in single page?
There are a couple of different ways to handle this:
If you go to Tools -> Options , Select Editor at the top and the Formatting tab, select Java in the Language drop-down list and Wrapping in the Category drop-down list. There are many options that make the auto-formatting break more lines by switching (Never to either Always or If Long ) which would keep line lengths shorter but still not guarantee that a really long line doesn't extend off the page. It would probably have to break the syntax of Java in order to always be able to break those lines.
Another option would be to leave auto-formatting alone and just adjust the Print options. Select the text file. Go To File -> Print .... On the first dialog click Print Options. On that dialog click the Wrap Lines checkbox and/or change the zoom to print in fewer pages.

How to add tab space infront of mutiple lines of code

I am using eclipse IDE for development I have written some code in if-else block and now I want to tab indent every block, Is there any way or command or key sequence so that I can select the lines of code and simply add tab space in front of every line selected.
Use the formatter for this. Right click your file and select Source -> Format. You can also execute this for a single code block. Just select the block and right click -> Source -> Format. You can also format Javadoc this way.
If you don't like the way your code gets formatted you can change the formatter template in Windows -> Preferences -> Java -> Code -> Formatter.
Select the block(or set of statements) and just press TAB to indent. The whole selected block(or set of statements) will be moved.

Automatic EOL conversion in Eclipse

Need to keep EOL format consistent in all resources under Eclipse workspace.
I know about Eclipse preference that sets new line style for newly created files, but I would like to have automatic conversion for already existing files. Is there some settings/plugins?
I want just setup once and be sure that all line endings are in the same format.
In addition to the Window > Preferences > General > Workspace setting for new files that you already know about, there is a File > Convert Line Delimiters To option. I don't know of any existing plugin/tool that will do this automatically when you save, but you could certainly write one or make converting the line ending part of your process.
To make it easier on yourself, you can bind keyboard shortcuts to the conversion commands by going to Window > Preferences > General > Keys and filtering using "delimiter":
In Eclipse, to convert the line endings for existing files:
Go to the file browser view, and click on the project/folder/file that you wish to convert.
From the menu bar, select File > Convert Line Delimiters To > Windows / Unix / MacOS 9.
You can Search your resources with the Search-Dialog and go to the tab File Search. There you can enter a Regular expression. Enter \r\n or whatever line ending you want to change.
Then hit the Replace .. Button instead of Search.
I want just setup once and be sure that all line endings are in the same format.
... ok, my answer does not consider this.
You might get usefull results with Eclipse save actions: If the eclipse formatter also converts the EOL style, you could use it to modify EOL style only for the files you are modifying.
Unfortunately I don't have eclipse here, so I can't test if this actually works. Worth a try, however.

How to auto-remove trailing whitespace in Eclipse?

The question has two parts, one of which I already have the answer for.
How to auto-remove trailing whitespace from the entire file being edited? -> Answer: use the AnyEdit plugin, which can be set to do that on any save to the file.
How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.
I assume your questions is with regards to Java code. If that's the case, you don't actually need any extra plugins to accomplish 1). You can just go to Preferences -> Java -> Editor -> Save Actions and configure it to remove trailing whitespace.
By the sounds of it you also want to make this a team-wide setting, right? To make life easier and avoid having to remember setting it up every time you have a new workspace you can set the save action as a project specific preference that gets stored into your SCM along with the code.
In order to do that right-click on your project and go to Properties -> Java Editor -> Save Actions. From there you can enable project specific settings and configure it to remove trailing whitespace (among other useful things).
NB: This option has been removed in Eclipse Kepler (4.3) and following releases.
NB #2: The option seems to be back in Eclipse Luna - Luna Service Release 1a (4.4.1)
Removing whitespace from the entire file being edited:
Preferences -> Java -> Editors -> Save Actions -> check Perform the selected actions on save -> check Additional actions -> click Configure.. -> go to Code organizing tab -> check Remove trailing whitespace -> select All lines.
Removing whitespace only from the lines I changed:
Preferences -> Java -> Editor -> Save Actions -> check "Perform the selected actions on save -> check Format source code -> select Format edited lines.
Note it is not necessary to click Configure the formatter settings on the Formatter page - all the code styles automatically include removing trailing whitespace. At least I couldn't find a setting for this in Formatter configuration, and it worked out of the box for built-in Java Conventions, Eclipse, Eclipse 2.1 styles, as well as GoogleStyle.
When using this set-up, you obviously need to also turn off the solution to part 1 of the question.
Eclipse version checked: 4.5.2, 4.11
You don't need any plugin to do so. For instance, if you code JAVA, you can erase trailing whitespaces configuring save actions:
Eclipse 3.6
Preferences -> Java -> Editors -> Save Actions -> Check Perform the selected actions on save -> Check Additional actions -> Click the Configure.. button.
In the Code organizing tab, check Remove trailing whitespace
PyDev can do it by either Ctrl+Shift+F if you have code formatter option set to do it, or by during saving:
Eclipse -> Window -> Preferences -> PyDev -> Editor -> Code Style -> Code Formatter:
I use at least these:
Auto format before saving
Right trim lines?
Add new line at end of file
Do following:
Preferences -> Java -> Editor -> Save Actions
You can map a key in Eclipse to manually remove trailing whitespaces in the whole file, but only on request instead of automatically at save. (Preference/Keys and then map a set of keys to File/Remove Trailing Whitespace) This can be useful if you want to sanitize all new files, but keep legacy code untouched.
Another strategy is to activate visual display of whitespace, so at least you'll know when you're adding some trailing whitespace. As far as I know, there's no way to display only trailing whitespace though, but I'll be glad to be proved wrong.
In a pinch, for those editors that don't support removal of trailing whitespace at all (e.g. the XML editor), you can remove it from all lines by doing a find and replace, enabling regular expressions, then finding "[\t ]+$" and replacing it with "" (blank). There's probably a better regex to do that but it works for me without needing to install AnyEdit.
I would say AnyEdit too. It does not provide this specific functionalities. However, if you and your team use the AnyEdit features at each save actions, then when you open a file, it must not have any trailing whitespace.
So, if you modify this file, and if you add new trailing spaces, then during the save operation, AnyEdit will remove only these new spaces, as they are the only trailing spaces in this file.
If, for some reasons, you need to keep the trailing spaces on the lines that were not modified by you, then I have no answer for you, and I am not sure this kind of feature exists in any Eclipse plugin...
It is impossible to do it in Eclipse in generic way right now, but it can be changed given with basic Java knowledge and some free time to add basic support for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=180349
The dependent issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=311173
For php there is also an option:
There is a really easy way to do this with sed, the Unix command line tool. You could probably create a macro in Eclipse to run this:
sed -i 's/[[:space:]]*$//' <filename>
As #Malvineous said, It's not professional but a work-around to use the Find/Replace method to remove trailing space (below including tab U+0009 and whitespace U+0020).
Just press Ctrl + F (or command + F)
Find [\t ][\t ]*$
Replace with blank string
Use Regular expressions
Replace All
extra:
For removing leading space, find ^[\t ][\t ]* instead of [\t ][\t ]*$
For removing blank lines, find ^\s*$\r?\n
I am not aware of any solution for the second part of your question. The reason is that it is not clear how to define I changed. Changed when? Just between 2 saves or between commits... Basically - forget it.
I assume you would like to stick to some guideline, but do not touch the rest of the code. But the guideline should be used overall, and not for bites and pieces. So my suggestion is - change all the code to the guideline: it is once-off operation, but make sure that all your developers have the same plugin (AnyEdit) with the same settings for the project.
I used this command for git: git config --global core.whitespace cr-at-eol
It removes ^M characters that are trailing.