f42ce5490db0970e1841816b357c42e8a5334fc0
[stack/conf/vim.git] / addons / TagHighlight / autoload / TagHighlight / LoadDataFile.vim
1 " Tag Highlighter:
2 "   Author:  A. S. Budden <abudden _at_ gmail _dot_ com>
3 " Copyright: Copyright (C) 2009-2011 A. S. Budden
4 "            Permission is hereby granted to use and distribute this code,
5 "            with or without modifications, provided that this copyright
6 "            notice is copied with it. Like anything else that's free,
7 "            the TagHighlight plugin is provided *as is* and comes with no
8 "            warranty of any kind, either expressed or implied. By using
9 "            this plugin, you agree that in no event will the copyright
10 "            holder be liable for any damages resulting from the use
11 "            of this software.
12
13 " ---------------------------------------------------------------------
14 try
15         if &cp || v:version < 700 || (exists('g:loaded_TagHLLoadDataFile') && (g:plugin_development_mode != 1))
16                 throw "Already loaded"
17         endif
18 catch
19         finish
20 endtry
21 let g:loaded_TagHLLoadDataFile = 1
22
23 function! TagHighlight#LoadDataFile#LoadDataFile(filename)
24         let filename = g:TagHighlightPrivate['PluginPath'] . '/data/' . a:filename
25         return TagHighlight#LoadDataFile#LoadFile(filename)
26 endfunction
27
28 function! TagHighlight#LoadDataFile#LoadFile(filename)
29         let result = {}
30         let entries = readfile(a:filename)
31         
32         let top_key = ''
33         for entry in entries
34                 if entry[0] == '#'
35                 elseif entry[0] =~ '\k'
36                         " Keyword character, so not sub entry or comment
37                         if entry[len(entry)-1:] == ":"
38                                 " Beginning of a field, but we don't know whether
39                                 " it's a list or a dict yet
40                                 let top_key = entry[:len(entry)-2]
41                         elseif stridx(entry, ':') != -1
42                                 " This is key:value, so it's a simple dictionary entry
43                                 let parts = split(entry, ':')
44                                 " Rather coarse replacement of split(x,y,n)
45                                 if len(parts) > 2
46                                         let parts[1] = join(parts[1:], ':')
47                                 endif
48                                 if stridx(parts[1], ',') != -1
49                                         " This entry is a list
50                                         let result[parts[0]] = split(parts[1], ',')
51                                 else
52                                         let result[parts[0]] = parts[1]
53                                 endif
54                                 " Clear the top key as this isn't a multi-line entry
55                                 let top_key = ''
56                         else
57                                 call TagHLDebug("  Unhandled line: '" . entry . "'", "Error")
58                         endif
59                 elseif entry[0] == "\t" && top_key != ''
60                         " This is a continuation of a top level key
61                         if stridx(entry, ':') != -1
62                                 " The key is a dictionary, check for mismatch:
63                                 if has_key(result, top_key)
64                                         if type(result[top_key]) != type({})
65                                                 call TagHLDebug("Type mismatch on line '".entry."': expected key:value", "Error")
66                                         endif
67                                 else
68                                         let result[top_key] = {}
69                                 endif
70                                 " Handle the entry (without the preceding tab)
71                                 let parts = split(entry[1:], ':')
72                                 " Rather coarse replacement of split(x,y,n)
73                                 if len(parts) > 2
74                                         let parts[1] = join(parts[1:], ':')
75                                 endif
76                                 if stridx(parts[1], ',') != -1
77                                         " This entry is a list
78                                         let result[top_key][parts[0]] = split(parts[1], ',')
79                                 else
80                                         let result[top_key][parts[0]] = parts[1]
81                                 endif
82                         else
83                                 " This is a list of strings, check for mismatch
84                                 if has_key(result, top_key)
85                                         if type(result[top_key]) != type([])
86                                                 call TagHLDebug("Type mismatch on line '".entry."': didn't expect key:value", "Error")
87                                         endif
88                                 else
89                                         let result[top_key] = []
90                                 endif
91                                 " Add to the list (without the preceding tag)
92                                 let result[top_key] += [entry[1:]]
93                         endif
94                 else
95                         " Probably a comment or blank line
96                 endif
97         endfor
98         return result
99 endfunction