I would like to automatically convert between tabs and spaces for indentation when I commit/update code to/from our repository. I have found the AnyEdit plugin for eclipse, which can convert directories of files. Not bad for a start, but does anybody have more expierience on how to handle this? Or maybe know of an Ant script or something else?
Make sure you have your editor set to use spaces instead of tabs.
Select all text and hit CMD + I on mac or CTRL + I on windows.
Why not just use the code formatter and/or cleanup function? It has settings that take care of that stuff for you. You can even have it run automatically on save.
Edit: As Peter Perháč points out in the comments, this only answers half the question. I don't have any practical experience, but you could try the Maven Eclipse Format Plugin to format from a Maven build.
Unfortunately, that's Maven only, and I know of no light-weight command line formatter. But if you happen to use Maven, you can bind the format goal to the proper phase, and if you set Eclipse to auto-build, it would format on update.
Depending on the SCM tool (git, svn, etc), you could also create a hook that runs the build (but it might be a bit too heavy-weight for that).
I use the AnyEdit plugin to auto-convert tabs to spaces on the save of a file. I also configure the base text editor (from which pretty much all the others derive) to insert spaces instead of tabs. This sounds redundant, but what it does is ensure that I don't insert any tabs, and any file that I edit that already has tabs will be converted as soon as I save it.
Tabs have no place in source code. If someone else looks at the file with their tab-stops set to a different value, they lose most alignment/formatting anyway.
(Of course, if you have Makefiles that you edit directly, you'll want to make sure their tabs are retained. But in my projects, if make is used at all the Makefile is derived from a different source, such as a Makefile.PL in Perl.)
A bit overkill, and only something to attempt with certain repository products that can handle it, but a hook script to call indent or astyle could do the trick. It'll format everyone's code the same way for every file, depending how you write the hook script, and it'd have to be pre-commit of course.
You may lose alignment/formatting by using tabs instead of spaces if and only if the tabs are not at the beginning of the line. Never use tabs insides lines, always use tabs at the front of lines. This allows you to use your editor to adjust to your desired indent level without impacting your co-workers view of the file. Challenge: Find an example where tabs at the front of the line loses alignment.
I use Kedit for just this thing. It also natively converts text files from Macintosh, UNIX and MS-Dos. Since it's an older editor, I use one of it's scripts to handle unicode files.
You might also want to look at some of the other smart editors.
I use Eclipse for Java EE developer 4.6.0 Neon. I use http://marketplace.eclipse.org/content/anyedit-tools
Related
it is kinda hard to explain what I am talking about
easier is have a look at this video at minute 18:50 until 19:00
sublime's nifty text block select and change
he selects a text block, then selects other text blocks with it, and changes them all together at once
is there something like this in intellij or eclipse?
What you want is called Multiple Selections, and it is not yet implemented in official IntelliJ, there is just my prototype which works pretty much like Sublime, and if you want, you can build it and give it a try :).
Edit: It is now implemented in some form in official IntelliJ
There might be an option in Eclipse to Rename In File through Quick Assist (Ctrl+1), but it usually applies to variables and not simply every occurrence of a string in the file.
I need to edit this HTML page and re-upload it.
The problem is that somehow all of the newlines have been eliminated from it so that its just one long line. I'm using Notepad++ now, although I don't have to be.
What is a good way to view and edit this HTML/PHP/Javascript source in a way that a reasonable human being can read it?
Thanks!
You can tidy this up (as #AR suggested) with notepad++. Go into Notepad++, go to the plugin manager and select
textFX and install that.
among other things, it will install a new menu item in the toolbar called "textFX"
select it, then select Text Fx HTML Tidy. This will give you a selection of formatting objects and ought to pretty it up for you.
once again:
TextFx >> TextFx HTML Tidy
I use Dreamweaver at work and NetBeans IDE at home, but in both cases project is stored at github. My problem is that NetBeans seems to be constructing code indenting out of spaces and when opened in dreamweaver or exported to github, indenting here and there breaks (moves unexpectedly further then it was intended or vice versa), although when opened again in NetBeans, goes back to normal. It has been pain for some time already. Is there any resolution to this?
I should say that we at work (and me including) prefer tab indenting.
It depends on the IDE's definition of a TAB. There are usually options within the IDE to indent using a TAB but turn the TAB into a given number of SPACES. I usually indent using TAB but have the IDE turn that into 3 SPACES. This means that it doesn't matter what I (or anyone else) use to read my code later the indets are always the same.
In NetBeans you need to look in the Code Editor options but I have no idea where to find the settings in Dreamweaver.
Its been known that one should use double space rather than tab, because that fact that different IDEs define how the tab button works DIFFERENTLY.
For example, emberjs team require contributor to use double-space rather than tab for formatting the code.
It make sense that to use something recognized in common without an IDE, so that the code structure will not be a problem in a highly collaborated coding team environment.
HOWEVER, if customization of how a tab works in an IDE is enabled, tab will be a great time saver in this case-- Press twice the button cost 200% of the time to press one obviously :)
Hope that helps!
I'm developing a small project with some friends and we're facing some indentation issues. Each of us is using a different editor (we all have different favorites :) and we also are on different operating systems.
What is the best solution to be able to all develop together and set our programming editors so indentation and encoding is the same? What settings do you recommend and why?
You should have your version control system handle the line ending problems. In SVN, the "svn:eol-style" property can be set to "native" to handle auto-converting the line endings. I assume your code is ASCII or UTF8 so you don't have problems with character encoding.
As far as formatting goes, you'll have to choose roughly some style, such as where to put the braces, but there's no reason you have to have the same indent style, provided you use the same number of indents. That's a confusing statement, so allow me to explain: nearly every IDE ever has the ability to set tab widths to any value you want. If one team member likes to indent 2 character widths, another 4, that's fine. One tab character stored in the file can display either way based on IDE settings. Just configure your IDE to indent with tabs and then set the tab widths per your desires.
(at the risk of starting a flame war, this is why I am solidly in the camp of tabs in the tabs vs spaces war. strangely, the spaces folks seem to consider this very feature the reason to use spaces... I will never understand them).
If you are all insistent to have your own indentation settings, then you will need to use exclusively the tab character to indent the code. (Provided your various editors support this).
This approach is counter to the general consensus of using spaces and only spaces for indenting purposes.
Whatever you do, I recommend converting tabs to a common number of spaces. I know Eclipse can do this on the fly, but I'm not sure if edits like gvim can. In any case, with all spaces it is not up to the individual editors/IDEs to interpret how many spaces make up a tab. Also, if anyone's using a Windows editor/IDE, set it to use Unix-style newlines, so that you don't get the annoying ^M at the end of every line when you edit in a different OS.
In NetBeans the GUI Builder generates code that is off limits to you from the editor.
I know you can switch to Design Mode and specify custom code and then enter edit in indirectly that way, but it's a pain to not be able to edit text that's 2 lines from your cursor.
Can I allow editing within those regions... I promise I'll be careful :)
Thanks.
Note: I don't want to lose code generation
If you open it in an external editor, there are two possibilities:
there is no guarantee that the changes you make will work (and not break anything) in the Netbeans UI Editor
The next time you edit the file in Netbeans it will be regenerated.
Either way, probably not what you want. The best thing to do would probably be to make the whole design up in the gui editor and when you are happy with it, delete the comments and continue on.
[sarcasm]
With the drag and drop gui editor, what could you possibly want to change by hand anyway?
[/sarcasm] ;)
If you open the .java files in Notepad or WordPad, you'll find pairs of comments that say
// GEN-BEGIN: <name of section>
...
// GEN-END: <name of section>
Delete those comments, and two things will happen.
You'll be able to edit anywhere in the file in NetBeans.
NetBeans will not be able to regenerate the file from the GUI editor.
So it can be done, but don't do it unless you really mean it.
EDIT: "Note: I don't want to lose code generation." Ok, let's see here.
You want to change what is generated.
You don't want to lose code generation.
You don't want to use the facilities NetBeans provides for adding custom code.
It sounds like you're wishing yourself trouble. :D
I had a same problem. I extended JPanel with my own panel class and I was not able to pack it with the rest of UI. First I searched the web and I found your question. I was not able to find elegant solution to this problem.
Therefore, I decided to create another java class and copy NetBeans created code into it. It worked like a charm.
I agree, it would be nice if we can modify existing code like in .NET.
I copy the initcomponents() method, create my own method newInitComponents() paste the generated code, and override what needs to be overridden before i distribute the code. That way when a change is made in the editor, you can still copy paste the code into your new method. You keep the functionality without costing you the ability to override where necessary. Just call newInitComponents() in your constructor rather than the original. This is not the most efficient way of doing things, but it does what you want.
You can also try moving the .form file in
(project name)\src\(name of JFrame/JPanel).form
NetBeans will now see it as a .java file.
If you did not edit anything necessary for the GUI editor, you can move the .form file back into the src folder.
Can you subclass the class and redefine the method where this code is? Of course then you'll have to maintain the method over time as you make changes to the GUI.
You can maybe also look into AspectJ. It will modify the byte-code at build time and make the changes you define. Not sure if this gives you enough control over what code to change.
If you use APT you can rewrite source code at compile time. This way you can overwrite the code that's there at the moment. You could keep the new code in the same Java file along with comments to explain what is going on. This tutorial makes it pretty easy to get started. Copy that code, get it working, then tweak it to your liking. To find out where in the code the annotated method is call getPosition() on the Declaration.
Go to design page and select any one of the fields. Right click on any selected field and choose Customizer Code , In Customizer Code change all default code to custom creation . Now you can edit your design code as you like...
Very simple... the crappy UTF-8 did not let me compile one JasperReport (everything done by GUI). So, I needed to change Project to iso-8859-1. Result? The project is messed up and some of the things to correct are in the blocked parts of the code. So, I really NEED to edit those automatically generated parts. When they are regenerated, they will be fine because the project is now iso-8859-1.
Step # 1 Close the project in NetBeans
Step # 2 Delete the .form file from the source directory of your project
Reopen the project and it will be editable now!