Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 40

Thread: eclipse setup, tricks, etc

  1. #1
    Developer roippi's Avatar
    Join Date
    Aug 2010
    Posts
    2,663

    Default eclipse setup, tricks, etc

    Doing this thread as a more tutorial-oriented thing than this thread, which may also be useful to nascent mafia devs. The other one was more along the lines of me querying other devs as to their tools; this is a thread on how to use a specific set of tools. Specifically, eclipse.

    Motivation

    If you've never worked on a collaborative project before, mafia's codebase seems impenetrable. How on earth do the devs ever find that one line of code that they're looking for?! There's thousands of files!?! AHH

    Okay, calm down, I'd first say. Then I'd say: sometimes we know exactly where to look because we've been doing this for years. But often, we're unfamiliar with the particular subsystem that we're trying to troubleshoot. That's when we rely on tools. Use a global search to get yourself in the neighborhood, then IDE tools to navigate up and down the code tree. But I'm getting ahead of myself.

    Sir Not Appearing in This Tutorial

    Vim, Emacs, Linux CLI utilities. External SVN tools. There are myriad options out there for people who want to use them. Not in this tutorial! We're going to be installing Eclipse with the Subclipse SVN addon. The reasons for the addon are 1) because it's awesome and 2) so everything you need to get started is self-contained in this tutorial. There's no "check out mafia's root directory using an external SVN program" step. On to the good stuff.

    Eclipse with Subclipse

    First thing's first
    Before you start, you need to get a JDK (Java Development Kit) on your system. Download one from here. As of this writing, I'd recommend using the older (but stable) JDK6 rather than the still-buggy JDK7. That recommendation is likely to change eventually, we'll see.

    Installing Subclipse
    • Download and install Eclipse first. Duh.
    • Fire up Eclipse. Ignore all the stuff in the main interface for now. Help > Install New Software. Copy/paste this url into the box: http://subclipse.tigris.org/update_1.8.x
    • Check all the boxes. Accept all the license agreements. Restart when prompted. Subclipse installed!

    Check out Mafia

    Now that you've got a SVN client hooked up to your IDE, this couldn't be simpler.
    • File > Import...
    • SVN > Checkout project from SVN...
    • Create a new repository. Give it this url: https://svn.code.sf.net/p/kolmafia/code/
    • Choose the root directory, hit finish. It will probably pop up a warning about checking out the root directory. Yes, that's okay.
    • You'll probably need to tell it where to create the folder, what to call the project, etc. You can probably handle that stuff. That's it!

    So, that wasn't hard. You have a dev environment. Now, for how to (properly) use it.
    Last edited by roippi; 05-29-2013 at 11:41 PM. Reason: new sourceforge svn url

  2. #2
    Developer roippi's Avatar
    Join Date
    Aug 2010
    Posts
    2,663

    Default

    What am I looking at?!

    The first thing that you need to know about IDEs, if you're new to them, is that I'm pretty sure that they never say "no" to any feature requests. If a dev somewhere wanted it, they added it.

    Okay, that's a bit of an exaggeration, but seriously: there are more features here than you'll ever need. Close most of those side-tabs. I code with the main editor window open and the bottom window on whatever "utility" tab I care about at the time. When I need to browse to another file, I open up the package explorer, which collapses after I click away from it.

    overview1-1.jpg

    In the bottom "utility" area, there are some useful tabs: problems, search, console, history. I'll cover the more useful ones in the Tips section.

    Already you may notice something added by the subclipse plugin in the above image - the amber-colored information after each .java file is info provided by SVN about the various files. Time/date of last commit, and who did it. Try right clicking on a file and doing Team > History... Pretty cool, huh?

    Running the project

    You spent all that time getting all this set up, let's do something with it. In package explorer, right-click on the project folder and choose Run As... > Java Application. Tell it that the main class is KoLMafia.java. It should fire up the familiar mafia interface quickly. In the future, you can just hit the green "play" button in the top bar and it should instantly fire up mafia with all the changes you've made to it (provided there are no errors).

    Under the hood, what it's doing here is quite advanced: it is running an "incremental" builder. If you've ever built mafia using ant before, you know that it takes something like 10-15 seconds to build your program. Instead of doing a clean build every time, javac is just on-the-fly rebuilding the .class files that you make edits to - making the whole thing essentially instantaneous. Pretty ridiculous if you ask me. This makes debugging things SO MUCH faster and fun - type a line of code, see if it works, fiddle with it, repeat. Those 10-15 seconds add up when you're doing it 50 times in a row.

    Navigating the code

    Time for the good stuff. Note the case-sensitivity of these commands; h is different from H!

    ctrl-h: workspace search

    A good brute-force standby. There are several tabs here. "File search" is probably what you'd think of when you think of grep or the like - just look through all the files and find the string I want. Java search is more refined, if you know what you're looking for - a method, a class, etc. The other tabs I never use, I'd turn them off by clicking the Customize... button.

    ctrl-f: file search

    You probably know what ctrl-f does. Make sure to tick "incremental." If you want a lighter-weight version with no dialog box, use ctrl-j.

    ctrl-G: find references

    Such an awesome command. Say I see a method doStuffToString( String blah), and I want to know where in mafia that method is used. Select it, ctrl-G. You'll get an answer in the search tab down below.

    search1.JPG

    The constructor HiddenCityRequest( int ) is called there, and there. Double click to go there.

    F3: find declaration

    Equally awesome. I'm browsing through the code and it makes a new DoStuffRequest( blah1, blah2 ). What does that do? Select it and hit f3 - it will take you to that constructor. You can use this on practically any object - fields, variables, etc. Think of it like clicking on a hyperlink. In fact, if that makes more sense to you, you can use ctrl+click, which does the same thing. Once you get how this and ctrl-G interact, you shouldn't have any problems navigating the code.

    Coding

    Oh, you wanted to use the IDE to code? Okay, I guess...

    ctrl-space: content assist

    The autocomplete menu actually will pop up by default, after you add a decimal (.) that indicates you're calling a method. If you lose it for whatever reason - clicking away, etc - ctrl-space gets it back; it will show you matching methods in the current type, or fill it in if there's only one matching method. Think tab in Linux. Absurdly useful. You can try ctrl-shift-space for a slightly different flavor.

    logger1.jpg

    alt+R: rename

    Ever renamed a variable and had to go through your code and fix it 37 times elsewhere? A find/replace will sometimes do it, but what if it's a field that is referenced by multiple types? Alt-R. This is probably the most basic of the "refactor" utilities that eclipse provides; see the right-click menu for lots more. I use rename and move a lot, with a good amount of Extract Method.

    ctrl-F: invoke formatter

    I am not the tidiest coder. I forget spaces, indent things wrong, etc all the time. What to do?!

    Use the formatter to clean up your code. Mafia provides an .xml file to teach eclipse how to do this, actually. To set this up, first go to properties... > Java Code Style > Formatter. Hit import... and browse to util/formatter.xml. Do the same for Clean up and cleanup.xml. Now, when you select something and hit ctrl-F, it will fix all the whitespace, indenting, brackets, etc. It will even organize imports and fix some bad code style things (static references using this, etc. don't worry about it for now.) Cool.

    ctrl-shift-/ and ctrl-shift-\: block comment/uncomment

    Quick way to throw some /* */ tags around some code. Plain ctrl-/ works too, but is ugly.
    Last edited by roippi; 06-27-2012 at 07:19 PM.

  3. #3
    Developer roippi's Avatar
    Join Date
    Aug 2010
    Posts
    2,663

    Default

    Contrived examples

    Theoretical ridiculous example 1: I want to debug something that's going wrong with the "!" CLI command.

    -> Okay, first I have to find it. Let's say I know nothing about mafia's type hierarchy whatsoever. I can't global search "!", that's ridiculous. But, it's a CLI command, so let's search for a nearby one - ashref will do nicely. I hit ctrl-h, click the file search tab, and (for the purposes of the exercise) tick case sensitive.

    ctrlh.JPG

    Okay, one result. Double-clicking that gets me into KoLmafiaCLI.java.

    Looking down a bit ( or doing a ctrl-f on "!" ) quickly finds this line:

    Code:
    		new BangPotionsCommand().register( "!" ).register( "bang" ).register( "vials" );
    Cool, that's what I need. I select BangPotionsCommand and hit F3. Now I'm where I need to be! Time to debug.

    Quickly browsing the code, I might suspect that some indices are being miscalculated. I'd insert this line on line 73:
    Code:
    			RequestLogger.printLine( "left chop: " + chopl +"; right chop: " + chopr);
    (then run the formatter with ctrl-F to clean up my ugly formatting, haha)

    Note that if RequestLogger were not already imported (it is, by chance), Eclipse would tell me about that. Hovering over the red underline would provide possible solutions, the top one being import RequestLogger.

    Inserting RequestLogger print statements is perhaps the only tool you need to debug 96% of problems. You can use System.out.print too, but I favor having things displayed in the mafia gCLI rather than console output.



    Theoretical contrived example 2: Some jerk broke something with a commit, and I want to fix it and post a .patch

    I just updated to r12345 and my favorite widget has stopped widgeting. I know that my widget was working in r12300.

    Using the SVN tools, pin down exactly which revision caused the problem. Right click on the kolmafia package > team > update to version. Use a highly-advanced search algorithm! Pick the halfway point of r12322, see if it's broken. Then iterate upwards if it's not broken yet, downwards if it is.

    Now that you know the particular revision that caused it, do team > history and look at the files affected by the change. Zero in on the problem.

    Fix the problem.

    Whew, okay, you fixed the problem. That sure was easy. Time to post your fix on kolmafia.us. For this, you're going to generate a .patch file. Again right click on kolmafia package > team > Create Patch... You'll need to exclude all of the extraneous files other than the ones you've altered/added/deleted specifically for this fix. We probably don't need to commit your personal preferences file.
    Last edited by roippi; 06-16-2012 at 06:56 AM.

  4. #4

    Default

    Thank you for posting this! Also thanks for linking to the previous one, I must have missed it.

  5. #5
    Developer roippi's Avatar
    Join Date
    Aug 2010
    Posts
    2,663

    Default

    Thank you for posting this! Also thanks for linking to the previous one, I must have missed it.
    Originally Posted by Chish View Post
    Sure thing. A couple things in that older thread are a bit dated, I edited in some comments appropriately.

    I'll provide a few more examples when I get around to it.

  6. #6

    Default

    Needs a wiki! Also, I should read this one of these days... I may well be the individual most involved with KoLmafia who's never even attempted to contribute to the code.
    Before you ask, please check the FAQ.
    Veracity: My "humble opinion" trumps your "humble opinion". IMHO.

  7. #7

    Default

    I picked up a few things that carry over to NetBeans from this, so thanks (mostly because I barely know where things are unless there's no practical way to get by without them).

  8. #8
    Developer roippi's Avatar
    Join Date
    Aug 2010
    Posts
    2,663

    Default

    On netbeans: I actually adore netbeans. Its native SVN support is awesome. (Eclipse only natively supports CVS, hence the needed subclipse addon) Eclipse just has too many features I've come to depend on.

    Ah, that reminds me of an example I should add.

  9. #9
    Developer roippi's Avatar
    Join Date
    Aug 2010
    Posts
    2,663

    Default

    Advanced-ish Stuff

    Subclipse-powered quick diff

    I'd recommend doing the following. Right click that area to the right of the vertical scrollbar (the "overview ruler") and choose preferences. Go to the quick diff tab and enable quick diff (I like showing it in the overview ruler too). Change the dropdown to be against Pristine SVN Copy (restart may be required). Now you can quickly see exactly what you've changed in your local version versus the SVN version.

    Back on the annotations tab, you can tweak how eclipse displays errors, warnings, and the like. I prefer to turn off warnings showing up in the overview ruler, and a couple other tweaks.

    Advanced-ish Commands

    These are commands that are not strictly necessary, just fun to use once you are more comfortable with the basic ones.

    alt+uparrow/downarrow

    My new favorite thing, basically. Move a line or lines of code up and down.

    ctrl+shift+uparrow/downarrow

    Go to previous/next method in a file. You can fly through a big file this way.

    ctrl+t

    Show type hierarchy. This is like F4, but pops up in a nice context menu. You can toggle super/sub hierarchy by hitting ctrl+t again. Useful when dealing with lots of inheritance, or checking out exactly what something overrides.

    ctrl+T

    Open type. You know you want to open up UseItemRequest, you're tired of opening package explorer and browsing to it. There ya go.

    ctrl+(lowercase L)

    Go to line. Somebody posted a bug report, and you want to go to that line in the stack trace? Bingo.

    ctrl+o

    Method search. A nicer way of going to that particular get.. method that you can't quite remember the name of. Bonus: hitting ctrl+o again shows inherited stuff. Useful if your top-level type is shallow with just a couple of methods, but extends something with a boatload of methods.

    ctrl+1

    Quick fix. Potentially the most powerful command of any I've listed, but be careful with it. Essentially equivalent to the options provided when there is a warning/error and you hover over it. It is rather smart with providing useful recommendations, but not all are going to be sane. Make sure you know what it did if you invoke it.

  10. #10
    Senior Member
    Join Date
    Jul 2007
    Posts
    1,193

    Default

    Decent "short" tutorial you've written, Roippi

    Ctrl+Left/Right is a good one too, useful after you have pressed F3 to jump to a declaration, you can press Ctrl+Left to jump back. Maybe I should try Eclipse again...
    Procrastination
    Leaves many things unfinished

    Originally Posted by /haiku

Similar Threads

  1. New Content - Implemented Manuel got some new tricks.
    By Erich in forum Bug Reports
    Replies: 38
    Last Post: 05-07-2013, 09:34 AM
  2. KoLmafia code formatting rules file for Eclipse request
    By wrldwzrd89 in forum Community Support
    Replies: 1
    Last Post: 09-14-2012, 07:17 PM
  3. 9102: Ignore Eclipse project-specific settings
    By RSS Bot in forum Latest SVN Changes
    Replies: 0
    Last Post: 02-24-2011, 09:02 AM
  4. Mood Setup?
    By tyilo in forum Scripting Discussion
    Replies: 5
    Last Post: 02-27-2008, 07:38 PM
  5. eclipse
    By bollwyvl in forum Scripting Discussion
    Replies: 4
    Last Post: 04-21-2006, 05:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •