Archive

Archive for July, 2014

Vim: highlight repeated properties of property file

While refactoring some localization property files I found that many messages and properties were repeated. This might be due to the lack of a naming convention for the strings or maybe because the properties were not sorted alphabetically.

For example, in one of the files the string Upload was repeated in three different places:

BUTTON_UPLOAD=Upload
UPLOAD=Upload
VIDEO_UPLOAD=Upload...

After normalizing the file I ended up with:

UPLOAD=Upload
UPLOAD=Upload
UPLOAD=Upload...

Before deleting the duplicated properties I wanted to check if the string they had was the same in all occurrences. With that in mind, I decided to make a little vim function that would highlight all the duplicated property names. For that you have to write the following in your .vimrc file:

function! HighlightRepeatedProps() range
let propCounts = {}
let lineNum = a:firstline
while lineNum <= a:lastline
let lineText = getline(lineNum)
if lineText != ""
let propName = matchstr(lineText, "^[^=]*")
let propCounts[propName] = (has_key(propCounts, propName) ?
propCounts[propName] : 0) + 1
endif
let lineNum = lineNum + 1
endwhile
exe 'syn clear Repeat'
for propName in keys(propCounts)
if propCounts[propName] >= 2
exe 'syn match Repeat
"^' . escape(propName, '".\^$*[]') . '=.*$"'
endif
endfor
endfunction

command! -range=% HighlightRepeatedProps <line1>,<line2>call HighlightRepeatedProps()

After saving your changes on .vimrc open your .properties file and type :HighlightRepeatedProps, if you have any repeated property names vim will highlight them in a different colour.

Useful, right?

Categories: Uncategorized