[TxMt] Fixing the Java class snippet

Sebastian Röder sebroeder at gmail.com
Sat May 30 19:13:16 UTC 2009


The class snippet in the Java bundle (from SVN) is not working as
expected for me. It fails guessing the correct class name from the
file name.

The regex for the class name guessing looks like this in the current snippet:
${TM_FILENAME/(.*?)(\..+)?/(?2:$1:untitled)/}

For a file that is not yet saved I get "class untitled ..." as
expected. However, if I have a Java file TestClass.java the snippet
names the class "untitledTestClass.java" - not what it is ment to be.

In an very old version of the bundle the regex in that snippet was:
${TM_FILENAME/(\w+).*|(.*)/(?1:$1)(?2:ClassName)/}

This one works for me (TM). I hope this is a good pointer to get the
snippet working again in the current bundle.

I also asked myself, whether the regex should be "improved" in such a
way, that it only accepts valid Java identifiers as class names and
otherwise informs the user about it. Nor sure whether this is beyond
the scope of the snippet, so I would like to hear some opinions, befor
I start messing with the regex ;-)

A possible improvement could be like this IMHO:

* A file MyClass.txt results in "class MyClass". As long as the
identifier is valid inside the Java file, the snippet should not care
about inapropriate named files.
* Same goes for file/class names that are against naming conventions
but compile fine anyway: class myclass, class Myclass, etc.
* The regex _should_ complain when file/class name doesn't start with
a Unicode letter, underscore, or currency symbol (dollar, euro, yen
sign). e.g. 1MyClass is not a valid class name (error at compile
time). The snippet could give the user a hint early in the process by
naming the class "class InvalidClassName" or something
* The alphabet for class names _should_ be restricted to Unicode
letters, digets, underscore and currency symbols
* keywords are not allowed as Identifiers but I'd say that's beyond
the scope of the snippet check that too

Last but not least it would be great if the tab triggers were the same
between packages for common snippets like class: python has class-->,
ruby has cla-->, java has cl-->



More information about the textmate mailing list