The KawigiEdit Plugin for TopCoder
KawigiEdit is a full-blown TopCoder Editor plugin with all the features I decided I wanted to have in it.
Among the most useful features in this plugin are:
- Syntax hilighting and line numbering in the editor and auto-indent.
- Undo/Redo support
- Search and Replace for variables or on regular expressions, either on selected text or on the whole document.
- Generates testing code from the example test cases in the problem statement.
- Times the execution of each test case in the test code (currently a Java-only feature).
- Saves your code locally, compiles it, and runs the test cases all at the click of a button.
- Provides quick access to past code saved by KawigiEdit
- Saves a database of code snippets for general use.
- Configurable to your preferences of syntax colors, fonts, etc, as well as filenames, and compile and run commands.
- Complete support (except for test timing, as mentioned above) for C++, Java, C# and Visual Basic (suggestions welcome on improving this)!
- Quick stats lookups on TopCoder handles so you can see who might be good to challenge.
Installing KawigiEdit
How to install KawigiEdit in 10 easy steps:
- Download the KawigiEdit jar to your computer (and remember where you saved it).
- Start the TopCoder Arena applet.
- From the Options menu, choose "Editor". The Editor Preferences dialog should come up.
- Click on the "Add" button. A dialog titled "Enter Plugin Information" should pop up.
- For "Name", enter "KawigiEdit" (or whatever you want it to be called, it doesn't really matter)
- For "EntryPoint", enter "kawigi.KawigiEdit" (this one does matter).
- For "ClassPath", hit "Browse" and find the KawigiEdit jar.
- Once those are entered in correctly, click OK.
- If you want KawigiEdit to be used as your default editor, check the "Default" box next to the new entry in the table.
- Click "Save".
Configuring KawigiEdit
Assuming everything worked without any error messages, KawigiEdit is now installed and ready to use. However, it is advisable to do a few more things, to make sure it is all configured correctly for your system (and to your liking).
To configure KawigiEdit, simply select it on the "Editor Preferences" dialog and hit the "Configure" button. A dialog will come up with three tabs for "General/Testing", "Editor" and "Languages".
General Settings
One setting you are likely to want to customize is where on your computer KawigiEdit will save your code. By default, it makes a directory called "testprograms" whereever the applet's current working directory is (in Windows this appears to be your desktop, in Linux it uses your home directory). Use the Browse button or directly enter the name of the folder you want to use.
The other general setting is "Use more standard UI". When writing this, I made UI design decisions based on the speed I could access features. As such, some of my UI might be "weird" to use, and may even not work in some versions of the JVM. This is now only partially implemented, so I don't recommend checking it at the moment. Eventually what it will do is use dialog boxes for the file chooser to view local code, for search and replace functions, and for the panel to add a snippet, rather than having the functionality right on the menu.
Testing Settings
Here, you can change the font and colors of the testing console panel. This is where the output to your programs will be displayed when you use the local compile and test features.
Editor Settings
Here, you can change the font and colors used in the editor and local code display. This includes syntax-related coloring, like:
- Keywords : if, class, public, etc.
- Types : float, int, boolean, decimal, etc.
- Operators : +, -, *, /, sizeof, instanceof, typeof, etc.
- Strings/Characters : "In this format, or like" 'x'
- Comments: /*like this */ //or like this 'or in VB, like this!
- Compiler Directives: #include, #define, #region, etc.
Language Settings
On this tab, you configure how to save, compile and run your programs locally. Note that $PROBLEM$ will substitute the problem class name in, and $CWD$ will substitute the current working directory for the run command only. The CWD is not available for the filename and compile command (You may assume that you are in the current working directory).
The default settings should work for most people whose system is configured exactly like mine. Fat chance, eh?
The default Java settings should work beautifully if you have Sun's Java SDK installed, and Java's bin directory is in your default system path.
The default C++ settings should be correct if you use g++, and it is in the path, for either Windows or *nix.
The default C# and VB settings should be correct if you use Windows and have version 1.1 of the Microsoft .NET Framework, even if the compilers are not in the default path.
The rule of thumb on these settings is that you should try them out, and if they work, don' mess with them.
Using KawigiEdit - A Walkthrough
Here is a quick example of how to use KawigiEdit in the Arena.
- Log on to the applet and go to practice room 152 (SRM 191 Division 1).
- Skeleton and testing code should have been automatically generated in your language of choice by KawigiEdit. If you would like to use a different language, choose it at the top of the window and then press "Generate code".
- This problem involves parsing comma-delimited lists of integers from the given strings. Write a function called "splitInts" that returns an array/vector of integers and takes a string argument and tokenizes the string into the numbers. An example of a Java implementation might be:
int[] splitInts(String s)
{
String[] split = s.split("[ ,]+");
int[] ret = new int[split.length];
for (int i=0; i<ret.length; i++)
ret[i] = Integer.parseInt(split[i]);
return ret;
}
- Now select the method you wrote. You can do this by dragging in the code pane, or by clicking and dragging on the line numbers.
- Right click on the code pane with the method selected, and go to "Add Snippet". A panel should appear with two text fields and a button. The method name ("splitInts") should have appeard as the suggested Snippet name.
- Enter a category (Such as "string" or "string/parse" - use a '/' to denote "folders" in your categories) and hit enter or click on the "Record Snippet" button.
- Deselect your method. If you successfully added that method as a snippet, you should be able to right click on the code pane now and find it in the "Snippet" submenu. Try clicking on it. You'll notice that it pastes that code wherever your cursor was. These snippets are saved on your hard drive, so that they will be available to you later when you don't have time to type nice functions during the SRM.
- Select your method again. You just decided that the name you gave your return value was stupid. Right click on the code pane and go to "Change var name". A panel should come up with a few text fields and a "Change" button. Put the old name of your return variable in the "Change" field, and some other valid variable name in the "To" field, and press enter or hit the "Change" button. All of the variable names within your selection should have changed. Note that other variables of the same name elsewhere in the file are not effected (unless you don't select anything, in which case it does the search and replace on the whole document), and other substrings that look like your old return variable's name should have been replaced, either.
- Try playing with the "normal" search and replace, as well. It works like the "Change var name" option, except that it's a full regular expression search/replace. Again, it will do the search and replace on the whole document if you don't have anything selected, or only on the selection if you do have something selected. The regular expression syntax is the one used by Java. For more information, go to the API documentation at http://java.sun.com/ and read the specification on java.util.regex.Pattern. Backreferences may be used in the replacement (and maybe even in the pattern?) using $1, $2, etc.
- Type a few things, and then hit ctrl+z a few times. It should undo your edits one thing at a time. Then try pushing ctrl+y - this should redo any undone edits.
- Now that you've messed around with it a bit, write the code in the correctVolume method. My Java version looks like this:
public int correctVolume(String[] queries, int numberOfBoxes, int ithBox)
{
int ret = 0;
for (int i=0; i<queries.length; i++)
{
int[] info = splitInts(queries[i]);
if (info[0] == ithBox || info[1] == ithBox)
ret = Math.max(ret, info[2]);
}
return ret;
}
- Once you're done typing it in, click on "Run Tests", or hit Alt+R. This should save the code to the local directory you have configured, compile it and run it. It will also automatically switch control over to the "Test Results" tab. If the Test Results displays something like My error: java.io.IOException: CreateProcess: <some kind of command> error=2, then either your run or compile command is not configured correctly. It could mean that your compiler isn't in the path or isn't in the place that it's configured to be. Go back to the configure dialog and see if you can figure it out. You may have also gotten compiler errors into the test results console. Fix them! Otherwise, you got a series of test results. If it tells you you're a stud, and you think you don't have any other hidden bugs, go ahead and click compile and submit on the applet (I made it a habit to click on TopCoder's "compile" and my "Run Tests" at the same time so I don't forget to do it).
- Since we've taken so long on this match already, it's time to get ready for the challenge phase. Click on the "Challenge tools" button. A Window should come up with the title, "Challenge Tools". You can close the editor, and this window should stay open.
- Type your handle into the text field at the bottom of the window and hit enter or click on Get Info. A few numbers from your division 1 submission history should come up (sorry, I sort of hard-coded that in). A tab with your handle should also have come up behind that panel. If you click on it, you sould see a few tables with information from your profile page on TopCoder. Ever wish you could have these stats quickly during the challenge phase? Me too. Try entering in other handles that you know. You can do them one at a time, or put a few in delimited by commas. After a short wait, you should see other peoples' stats pop up in tabs on the Challenge Tools window.
My Reflector Utility
I also included a simple Java Reflection Utility I wrote that helped me figure out how to use undocumented classes referenced in the TopCoder plugin API. I figured it would be useful to other potential plugin authors if they could:
- Have some way of "looking" into classes that doesn't involve illegally reverse-engineering them
- Have a functional open-source plugin to look at for examples.
To use the Reflector on the contest applet jar, first download that jar (or copy it from your internet cache or whatever). It's called ContestApplet.jar, and its location on TopCoder.com can be derived from the applet tag in the window that launches the arena.
Put the contest applet jar in the same folder as the KawigiEdit jar, and browse there in a console window.
Now type:
java -classpath ContestApplet.jar;KawigiEdit.jar kawigi.Reflector ContestApplet.jar
Note: On some operating systems, you will need to use a ':' instead of a ';'
A window should pop up with a JTree widget that can be expanded to see the packages, classes, and public/protected methods/members within the jar.
Eventually, I may write a bit on the useful undocumented features that I've slowly and painfully learned to use, also to help out future plugin authors.
Editing KawigiEdit
I included the source to KawigiEdit for a reason - I think that it's a good place to learn from (even if I did some slightly hackish things in some places), and I don't think it's perfect for everyone. If you are familiar with Java and want to customize KawigiEdit further than can be done through my configure dialog, you can unpack it and start hacking at it. If you find ways to improve it that you think would be useful to others, too, please let me know what you did and how/why you did it.
The standard way to unpack the jar would be to use Sun's jar utility (part of the Java SDK) like this:
jar -xf KawigiEdit.jar
You can also likely open it and extract it using WinZip or another similar program.
To make the applet use the extracted plugin instead of the jarred one, set the directory you extracted it into as the classpath (rather than the jar). This directory should have a directory called "kawigi" in it (but it shouldn't be the directory called "kawigi").
You'll also notice that this html file, a folder for the manifest, and a folder called "docs" popped out of the jar. In the docs folder, you'll find full documentation of the classes included in the jar (sorry if it's not quite accurate). This may help you to know where to start in changing stuff.
Other Notes
As for things that I should mention that didn't fit anywhere else, the most notable thing is main methods - on testing, it seems to be ok to put a main method in your source file to use for test cases. It compiles and runs fine locally and compiles and runs fine on TopCoder, with the exception of using C++. Putting a main method in your source file in C++ causes a linker error on TopCoder. As such, I don't put it in there. However, don't be deceived - I still generate a main method for C++ programs for local testing, I just do it when you save the file. So, at the end of the saved file (if you were to open it), you would see something like this:
int main() {
int time;
int errors = 0;
time = test0();
if (time < 0)
errors++;
time = test1();
if (time < 0)
errors++;
...
if (errors == 0)
cout <<"You're a stud (at least on the example cases)!" <<endl;
else
cout <<"Some of the test cases had errors." <<endl;
}
This allows you to test and also compile successfully on TopCoder.
Contact Info
Anyone is welcome to contact me about features they hacked in, features they think I should add, problems, suggestions, etc. Try to keep the "uR pLu61n suX0rZ, j00z sh0uLD uZ3 P0p53di7!" emails to a minimum.
- Email: kawigi at yajags dot com
- AIM : kawigi
- MSN IM : kawigi@hotmail.com (note: I won't get emails sent to this address)
- Y! : kawigi
- ICQ : #6944879