Vim Tips Wiki
No edit summary
(minor updates in view of talk page)
Line 1: Line 1:
 
Here is the beginning of a script that helps indenting [[wikipedia:JavaFX|JavaFX]] source files. JavaFX is a new GUI-based scripting language developed by Sun Microsystems to compete with Adobe Flash and Microsoft Silverlight.
 
Here is the beginning of a script that helps indenting [[wikipedia:JavaFX|JavaFX]] source files. JavaFX is a new GUI-based scripting language developed by Sun Microsystems to compete with Adobe Flash and Microsoft Silverlight.
   
This script reuses the Java indent file function <tt>SkipJavaBlanksAndComments</tt> because I don't know how to properly source it in this script. Please help to improve the code so that it can be included as a plugin at [http://www.vim.org/scripts/ Vim scripts].
+
Please help to improve the code so that it can be included as a plugin at [http://www.vim.org/scripts/ Vim scripts].
   
 
<pre>
 
<pre>
Line 17: Line 17:
 
" endif
 
" endif
   
  +
" From indent/java.vim
 
function! SkipJavaBlanksAndComments(startline)
 
function! SkipJavaBlanksAndComments(startline)
 
let lnum = a:startline
 
let lnum = a:startline
Line 104: Line 105:
   
 
==Comments==
 
==Comments==
This tip arose from a [http://groups.google.com/group/vim_dev/browse_thread/thread/701e01b871760def discussion] on the vim_dev mailing list. The aim of posting the script here is to seek feedback to improve the code. [[User:Black panda|Black panda]] may decide to upload the plugin to vim.org/scripts, and in three months or so, we can evaluate whether to keep this script here. Please make any fixes to the code, or add comments below. [[User:JohnBeckett|JohnBeckett]] 06:55, October 6, 2009 (UTC)
+
This tip arose from a [http://groups.google.com/group/vim_dev/browse_thread/thread/701e01b871760def discussion] on the vim_dev mailing list. The aim of posting the script here is to seek feedback to improve the code. [[User:Black panda|Black panda]] may decide to upload the plugin to vim.org/scripts, and in three months or so, we can evaluate whether to keep this script here. Please make any fixes to the code, or add comments on the [[Talk:JavaFX indent plugin|talk page]]. [[User:JohnBeckett|JohnBeckett]] 04:24, October 8, 2009 (UTC)
 
[[Category:Plugin]]
 
[[Category:Plugin]]

Revision as of 04:24, 8 October 2009

Here is the beginning of a script that helps indenting JavaFX source files. JavaFX is a new GUI-based scripting language developed by Sun Microsystems to compete with Adobe Flash and Microsoft Silverlight.

Please help to improve the code so that it can be included as a plugin at Vim scripts.

set debug=msg,throw
if exists("b:did_indent")
  finish
endif

let b:did_indent = 1
setlocal indentkeys=0{,0},!^F,o,O,e

" Only define the function once.
" if exists("*GetFxIndent")
"     finish
" endif

" From indent/java.vim
function! SkipJavaBlanksAndComments(startline)
  let lnum = a:startline
  while lnum > 1
    let lnum = prevnonblank(lnum)
    if getline(lnum) =~ '\*/\s*$'
      while getline(lnum) !~ '/\*' && lnum > 1
        let lnum = lnum - 1
      endwhile
      if getline(lnum) =~ '^\s*/\*'
        let lnum = lnum - 1
      else
        break
      endif
    elseif getline(lnum) =~ '^\s*//'
      let lnum = lnum - 1
    else
      break
    endif
  endwhile
  return lnum
endfunction

function! GetFxIndent()
  if v:lnum == 1
    return indent(v:lnum)
  endif
  let theCIndent = cindent(v:lnum)

  " If we're in the middle of a comment or at the end of a java-like
  " statement or block, use cindent
  if getline(v:lnum) =~ '^\s*[\*/;}]'
    "call confirm('hey!!!', 'ok')
    return theCIndent
  endif

  " find start of previous line, in case it was a continuation line
  let lnum = SkipJavaBlanksAndComments(v:lnum - 1)
  let prev = lnum
  while prev > 1
    let next_prev = SkipJavaBlanksAndComments(prev - 1)
    if getline(next_prev) !~ ',\s*$'
      break
    endif
    let prev = next_prev
  endwhile

  " take care of property-type format x: y
  let captures = matchlist(getline(prev), '^\([^:]*\):\(.*\)$')
  if ! empty(captures)
    if captures[1] =~ '{' || captures[2] =~ '{'
      if captures[2] =~ '}'
        " this may be a complete block enclosure on one line
        return indent(prev)
      else
        " a block is starting in the property.
        return indent(prev) + &sw
      endif
    elseif captures[0] =~ '^\s*\%(\<var\>\|\<def\>\)'
      " what we thought was a property-type format was really a
      " declaration of a variable.
      return theCIndent
    elseif getline(v:lnum) =~ '^\s*}'
      " the end of the property block
      return indent(prev) - &sw
    else
      return indent(prev)
    endif
    " end of property-type format x: y check
  elseif getline(prev) =~ '{'
    " maybe it's a completely enclosed block.
    " if so, we can keep the previus indent
    if getline(prev) =~ '{[^}]\+}'
      return indent(prev)
    else
      return indent(prev) + &sw
    endif
  elseif getline(prev) =~ '^\s*}'
    return indent(prev)
  endif
  return theCIndent
endfunction

" Set the function to do the work.
setlocal indentexpr=GetFxIndent()

Comments

This tip arose from a discussion on the vim_dev mailing list. The aim of posting the script here is to seek feedback to improve the code. Black panda may decide to upload the plugin to vim.org/scripts, and in three months or so, we can evaluate whether to keep this script here. Please make any fixes to the code, or add comments on the talk page. JohnBeckett 04:24, October 8, 2009 (UTC)