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:

System Requirements

Installing KawigiEdit

How to install KawigiEdit in 10 easy steps:

  1. 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.
  2. Start the TopCoder Arena applet.
  3. From the Options menu, choose "Editor". The Editor Preferences dialog should come up.
  4. Click on the "Add" button. A dialog titled "Enter Plugin Information" should pop up.
  5. For "Name", enter "KawigiEdit" (or whatever you want it to be called, it doesn't really matter)
  6. For "EntryPoint", enter "kawigi.KawigiEdit" (this one does matter).
  7. For "ClassPath", hit "Browse" and find the KawigiEdit jar.
  8. Once those are entered in correctly, click OK.
  9. If you want KawigiEdit to be used as your default editor, check the "Default" box next to the new entry in the table.
  10. 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:

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:

  1. Set up your java compiler in the Languages settings
  2. 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:

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:

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):

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:

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.

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:

  1. Have some way of "looking" into classes that doesn't involve illegally reverse-engineering them
  2. 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.