androidvimlayout-optimization

What vim errorformat to use for Android's layoutopt tool?


What vim errorformat do you use when running Android's layoutopt tool? (So you can load the results in Vim's quickfix window.)

Sample layoutopt output:

res/layout/main.xml
    31:31 Use an android:layout_height of 0dip instead of fill_parent for better performance

Solution

  • I think 31:31 means from line 31 to line 31. So we can ignore the second number (since there's no ranges in quickfix).

    Put the following in ~/.vim/after/ftplugin/android-layout.vim

    " Set the error format. Output is on multiple lines, so we use %P to push the
    " filename onto the stack and %Q to pop it off. There are two kinds of errors
    " I've seen: regular ones (begin with \t) and fatal ones.
    "
    " efm=Read the filename
    "   ,regular errors
    "   ,fatal errors
    "   ,forget the filename
    setlocal efm=%-P%f
        \,\ %l:%*[0-9]\ %m
        \,[Fatal\ Error]\ :%l:%*[0-9]:\ %m
        \,%-Q
    
    
    " For some reason, I can't set layoutopt as the makeprg -- it never outputs
    " anything when run from vim, but it works fine from a terminal or from vim's
    " :!
    setlocal makeprg=make\ layoutopt
    

    Here's the corresponding makefile (put this in your project root -- so the LAYOUTS path is valid).

    LAYOUTOPT = $(HOME)/data/code/android/android-sdk-linux_86/tools/layoutopt
    LAYOUTS = res/layout/*.xml
    
    layoutopt:  $(LAYOUTS)
        $(LAYOUTOPT) $(LAYOUTS)
    .PHONY: layoutopt
    

    Sidenote: You can use this to auto-invoke your ftplugin (instead of making a subtype of the xml filetype):

    au BufRead,BufNewFile *.xml if match(expand('%:p'), '/res/layout/', 0) >= 0 | runtime ftplugin/android-layout.vim | endif