Vim Tips Wiki
Register
Line 61: Line 61:
 
function run(args: String[]) {
 
function run(args: String[]) {
 
def c=Capitalization {};
 
def c=Capitalization {};
println(c.capitalize("test"));
+
println(c.capitalize("test"));
 
}
 
}
   

Revision as of 17:36, 30 November 2009

Reusing function

One of my first questions is the usage of SkipJavaBlanksAndComments in this indent file. This function is defined in the Java indent file, but it is useful in the JavaFX indent file, since the comment types are identical. I didn't want to reinvent the wheel, so I tried to use it and ended up making a copy of the function in my own indent file. Is there a way to simply reuse the existing version of the function without copying it?

Black panda 14:22, October 6, 2009 (UTC)

I do not have an authoritative answer, but I imagine in practice it would be better to duplicate the function as you have done. The cost of duplication is not high (although I fully understand the mistakes of repeating code), whereas having a JavaFX script dependent on a Java script would be awkward. Vim cannot use a function unless the script defining that function has been sourced. You could have code that sources indent/java.vim (while catching errors), and you could then use the function because it is global, but that's ugly. If you start Vim with no Java file loaded (start editing a .txt file), you can type :function Skip then press Tab to verify that no Skip* function is defined. If you now edit something.java and repeat, you will find that the function exists (and :scriptnames would show that indent/java.vim has been loaded). JohnBeckett 06:30, October 7, 2009 (UTC)
I agree with JohnBeckett, duplication here is the way to go. To avoid the duplication (and do it right), you would have to collaborate with the author of indent/java.vim and extract the function to a separate autoload script, which then would have to be maintained jointly. Not worth it for such a small function.
You could make the SkipJavaBlanksAndComments() script-local by prefixing it with s: to better indicate that this is a private function. This is recommended for all internal script functions. -- Inkarkat 11:05, October 7, 2009 (UTC)
I decided to rename the function s:SkipJavaFxBlanksAndComments. This breaks the association with the Java version, in case that one changes. I also updated the indent script to deal with lines with closing brackets on their own and other minor idiosyncrasies. Hopefully people will continue to test it, as I am doing with various JavaFX files. By the way, is there a way to make the markup for displayed code have a larger font size? I can barely read the stuff between the tt and pre tags. Black panda 15:19, October 8, 2009 (UTC)
I think you just need to change your font size in your browser for monospace fonts. It looks fine for me. For some reason a lot of browsers default to something ridiculously small for monospace. --Fritzophrenic 16:17, October 8, 2009 (UTC)
Groan. I remember that I once had that problem where code on various web sites (not wikis) was absurdly small. I have a vague feeling that Internet Explorer used some personal css settings that could be customised, but there is too much garbage on the net now to find anything useful, and I have totally forgotten. I have not seen the problem since switching to Firefox some years ago, and I don't think I have tweaked anything much in Firefox. We really should try to find some info on this. Perhaps we can fix the problem by some code in Mediawiki:Common.css, because it will affect other users. JohnBeckett 00:46, October 9, 2009 (UTC)

Two errors in the script

Using MacVim and two lines cause error messages in my setup.

First, function! s:SkipJavaFxBlanksAndComments(startline) After I removed "s:" no more error messages for this line. Apparently, the syntax doesn't like "s:" before the function name.

Second, if getline(earlier_prev) !~ '[\w\s],\s*$' And this one I can't fix.

Because of these errors, the script is unusable for me. Could you correct these.

Thanks for the script!

What version of "MacVim" are you using? I don't have a Mac, so I can't speak to this, so I'll ask for help. Inkarkat, why doesn't the "s:" prefix work for "MacVim"? Also, according to a comment on the main article, !~ does not work, but != does work. I assume that person also used "MacVim", or was the !~ syntax introduced in a later version? How would I future proof the script for an outdated version of Vim? --Black panda 19:59, November 29, 2009 (UTC)
Black panda, you've only applied the "s:" prefix to the function definition, not to the calls. I've fixed that for you. Regarding the !~, this is the correct syntax, and it has been in Vim since version 6. -- Inkarkat 20:44, November 29, 2009 (UTC)

To the OP who reported the error Please reply here letting us know the result. Can you use the script as it appears in the tip now? As Inkarkat says, the second problem you mentioned is valid syntax and should work (see :help expr4). If you still have a problem, please say exactly what happens. Is there an error (entering command :messages in Vim might help)? What is the error exactly? JohnBeckett 09:25, November 30, 2009 (UTC)

Working now!

Black panda and Inkarkat,

I am the poster of both of the posts. Magically your single correction fixed both errors. I don't know how it happen, perhaps my removal of s: caused the second error. Unfortunately, I am too new to vim and javafx to fully understand the cause. Anyhow, it works now. Many thanks indeed for your effort!

On a different subject, do you also maintain the syntax script for javafx? The closest I could get is here: http://www.vim.org/scripts/script.php?script_id=1943 But, this script has some issues, too. E.g., it doesn't highlight "def" keyword, some well know types etc.



Another bug?

Here's me again, still testing the script and seemingly stumbled upon another bug. Please see below code after "gg=G".

class Capitalization {

  function capitalize(s: String) {
     "{s.substring(0,1).toUpperCase()}{s.substring(1, s.length()).toLowerCase()}";
     }
  }
  function run(args: String[]) {
     def c=Capitalization {};
        println(c.capitalize("test"));
  }

--Wikia preserved indentation but split code into pieces, don't know why.-- As you can see on the 4th line the closing bracket is at the same level as the previous line while it should be at the same level as "function". The fifth line should be at the same level as "class".

The run function block is top level and so should start at the leftmost column.

And lastly, println line got indented though should not be.

Just guessing (please don't shoot me), it looks like indentation logic breaks after lines which contain opening and closing curly braces on the same line, i.e. 3rd and 7th.

Just in case, I am running MacVim 7.2 snapshot 51 (64bit Cocoa on Snow Leopard). And by the way, the code compiles OK.

Thanks for looking into this!