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.
- 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, compile and run commands, tabwidth, etc.
- Complete support for C++, Java, C# and Visual Basic (suggestions welcome on improving this)!
- Block matching - for language constructs that have a logical beginning and ending, if your cursor is over one end of it, the opposite end is highlighted
- In addition to the normal block matching, a graphical bracket next to the line numbers shows the span of curly braces that you are currently in.
- Mouse wheel scrolling (this seems to be extremely important to some people :-p).
- Drag and drop code, if you enjoy doing that.
- Quick stats lookups on TopCoder handles so you can see who might be good to challenge.
- Configurable tab width.
- Powerful template system (you can actually insert code to write your template code!)
- Can save your code to disk or load it back up from disk. Trust me, if your computer ever crashes during a match, this will make you happy.
- Several standard editing features
System Requirements
- Java 1.4 Runtime Environment! Quite a bit of my code depends on Java 1.4 features, and it's been out there for more than 2 years, so get it for heck's sake.
- I recommend having at least about a 400-500 Mhz Intel-type processor, or something reasonable (like a G3 maybe) in Mac terms. It will run on a slower system, as well. For computers at about that level, it will take awhile to load the first time, but will run fine after that (so do one practice problem to get it warmed up before an SRM).
- I recommend having at least 64 MB of RAM, but I've never actually tested it with less than 256 MB of RAM. I don't suspect KawigiEdit is likely to use nearly that much, let me know if it ever has more than 40 MB, because that's where I think it's getting absurd, even with a graphical Java application.
- If you want to use the testing features, you must have a compiler (and possibly runtime) installed for your language(s).
- If you want to use the Template system, you must have a Java compiler. I recommend Sun's javac. It's part of the Java 2 Standard Development Kit, which can be downloaded from the Java website. You'll have to set it up as if you were going to use it for testing. The template system has not been tested with jikes.
Installing KawigiEdit
How to install KawigiEdit in 10 easy steps:
- Download the KawigiEdit jar to your computer (and remember where you saved it). Note: Sometimes the version on the TopCoder website is not up to date. You may want to download the version from my website.
- 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".
Now KawigiEdit will be ready to use! Of course, to get the most out of KawigiEdit, you will probably want to tweak the configuration.
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.
Problem Timer
You can use this section to customize how often the problem points timer updates, what colors it uses, and even turn the darn thing off. Rest assured that if you turn it off (even if you turn it off with the editor running), it will be removed and the thread it was running on will be killed, so you don't have to worry about it consuming any CPU time. If you like it but are worried about CPU time, just lengthen the update delay.
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.
- Standard library classes: vector (C++), StringTokenizer (java), etc.
- Template tags: specifically for when you're editing your template in my template editor.
You can also turn on or off parentheses (, etc.) matching and specify what color the highlights should be (I recommend a color that is not too much different from your background color) and set your tab width.
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).
You will also want to set up the java compiler to work if you want to use the KawigiEdit template engine, even if you don't program in Java.
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.
Template Editor
This is the place to edit your code templates (the program that creates your skeleton and test code when you open a problem). Before doing this, make sure you have:
- Set up your java compiler in the Languages settings
- Decided for sure where your local test directory is
You probably also need to save your settings and re-open the config dialog if you changed either of these from their default settings.
The next thing to do before really starting work on your template is to click the "Install Templates" button on the bottom right of this panel. It writes a few files and downloads the TopCoder contest applet from the TopCoder website (if this is ever moved, it will tell you about the error and you'll have to find it and download it yourself). Assuming this is successful, you are ready to start hacking the template.
Here is a list of new files you should see in your test directory:
- ContestApplet.jar - I download it here so I can compile code with dependencies on it.
- Four .ktt files - one for each language. These are binary files needed to override the templates in each language. You probably shouldn't frob these.
- Four .ket files - these are the actual default template scripts. You edit them in the Template Editor, where you are now.
- Four .tlb files - these are libraries for use in the template scripts. You'll see them being referenced in the .ket files. You can change them or make new ones in the Tag Library Editor, or even in notepad (they are ascii and the syntax isn't impossible to figure out).
To get started, you might want to open the template (.ket file) for your preferred language. Let's take the C++ template for an example (the others are reasonably similar, insomuch as the languages themselves are similar). It should look something like this:
<%!include kawigi.language.ExtendedCPPLanguage %>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class <%:class-name%> {
public:
<%:return-type%> <%:method-name%>(<%:param-type-list%>);
}
<%:return-type%> <%:method-name%>(<%:param-list%>) {
<%:set-caret%>
}
<%:testing-code%>
//Powered by [KawigiEdit]
Some of this you'll probably recognize as C++ code. Some of it looks completely weird, unless you're familiar with a few web scripting engines that inspired it. These weird parts are called "tags", and they're specifically recognizable by the fact they start with <% and end with %>. There are several kinds of them recognized by KawigiEdit:
- Raw code tags - just <% some java code in here %>
- Directive tags - starts with <%! and ends with %> :
- include directive - if you want to include the tag library in kawigi.language.ExtendedCPPLanguage.tlb, you'd put in <%!include kawigi.language.ExtendedCPPLanguage %>.
- insert directive - you can insert one template into another one. For instance if you wrote a seperate test code generating template called MyTestTemplate.ket that you wanted to duplicate in the current template, you'd put in <%!insert MyTestTemplate %>
- Quick insertion tags - <%=comp.getClassName()%> is a shorter way of saying <%code += comp.getClassName() %>.
- Tag library tags - usually look like <%:method-or-action-name%> or <%method-or-action-name(param1, param2, param3)%>. These call into basic functionality provided by tag libraries. The tag library containing the tags you call must be included using the insert directive tag.
Take a look at the tags used in the template. Most of them should be self-explanitory by their name, even though they don't really show how they do it (that's the beauty of this whole thing, really).
Now change things around. You could add a few macros you like, change the list of libraries included (for C++ you'll want to leave string and vector for TC stuff, and ctime and iostream if you use the default testing code). Change the formatting if you want. Move things around. Move the caret. Make it look the way you want. You could change the way your class is defined or make it a struct instead of a class.
A more interesting thing you can do is write code to generate code. The code-generation code *has to be in java*. A few things you should know (these apply to making templates for any language):
- code is a String and it's the value returned as the template code.
- comp is a ProblemComponent and lang is a Language, as defined in the TopCoder plugin docs. They are parameters to the code-generating function, so you can use them.
- No standard java libraries are imported, so you'll have to use the fully qualified names of classes that aren't in the java.lang package.
- Anything you put in <% and %> tags is your code and might give you compiler errors if it's less than perfect. Just a heads-up.
- All of this stuff is eventually compiled into java code which is executed when you open the problem. That's why any java is pretty much legal inside the raw code tags.
- If you want to write your own methods to go in the code along with this primary method, use tag libraries (see the next section).
Once you are done working on your template, save it and try compiling it. The first time you save it (unless you clicked on "Change Template" to tell it ahead of time), the Template Editor will tell you to pick a template template for your template. That's a confusing way to say "tell me what language this is for". Pick the ktt for your language and click "Save", and KawigiEdit will attempt to translate your template code into java code and compile it on your computer.
Sometimes a dialog will come up and tell you about a template syntax error. Other times it will come up and tell you that there was a compiler problem. You can open up the java console with your browser and likely get some idea what the problem is. When looking at compiler errors in templates, remember that your template is being translated into java code, and that line numbers won't correspond exactly to what you're looking at. If the dialog comes up saying that your template compiled, click "yes" to start using your template for that language. Clicking "no" will leave whatever was previously the template for that language (or the default template if nothing has been set).
If you have questions about using it, contact me (or other KawigiEdit users if you know them), or get on the KawigiEdit wiki.
Tag Library Editor
If you want to make a quick library of simple functions you can call from a template, the way to do it is with tag libraries. You can use the Tag Library Editor to change existing tag libraries or write your own.
Each tag has basically three pieces of data:
- A name - this is how it is accessed from the template, i.e. <%:name%> or <%:name(param, param)%>
- Method code - this can be blank, or it can be a method declaration (or several) or variable declaration or all kinds of things. These methods will go into the code in the same class as the template that uses this method. Most of the tag libraries that come with KawigiEdit only call into methods inherited from the ExtendedLanguage classes, so nothing is defined here.
- Calling code - this is the code that gets inserted in the main template method wherever someone calls this tag. Most of these look like code += methodName(parameters);. If the user should have put parameters into this tag, they are referred by $1, $2, $3, etc. That means that you could have the calling code be something like code += method($1, $2); and if someone put <%:method(foo, bar)%> in their template, then code += method(foo, bar) goes into their code.
Hopefully the Save, Save As... and Open... buttons are fairly self-explanitory. You can select a tag to edit in the "Tag Name" field. Delete removes the current tag from the tag library, and Add adds it (if it has the same name as one that's already in the tag library, it doesn't work. Normally you'll do this after selecting and then changing the name of it, but you could also just change the name of another tag if what you were doing was similar, then click Add to copy it to the new name instead of just changing the name of the old tag permanently.
Remember when making tag libraries that no libraries are imported in the final java source, so you will have to use the fully qualified names of library types outside of the java.lang package. For instance, use java.util.ArrayList instead of just ArrayList.
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
- KawigiEdit Discussion Website: http://topcoder.yajags.com/mywiki.cgi