blob: 6f6441b913367fda0cee80d3681279ef0387aae2 [file] [log] [blame]
Misha Brukman3d6eea52005-04-24 17:05:04 +00001" LLVM coding guidelines conformance for VIM
Dan Gohman7b14fc22010-02-26 21:38:04 +00002" $Revision$
Misha Brukmanadf4e4d2009-01-02 16:26:14 +00003"
4" Maintainer: The LLVM Team, http://llvm.org
Misha Brukman3d6eea52005-04-24 17:05:04 +00005" WARNING: Read before you source in all these commands and macros! Some
Misha Brukman2bb55082005-05-12 21:41:48 +00006" of them may change VIM behavior that you depend on.
Misha Brukmanadf4e4d2009-01-02 16:26:14 +00007"
8" You can run VIM with these settings without changing your current setup with:
9" $ vim -u /path/to/llvm/utils/vim/vimrc
10
11" It's VIM, not VI
12set nocompatible
Misha Brukman3d6eea52005-04-24 17:05:04 +000013
Misha Brukman3d6eea52005-04-24 17:05:04 +000014" A tab produces a 2-space indentation
Dan Gohmanf225d2e2009-01-04 00:05:43 +000015set softtabstop=2
Misha Brukman3d6eea52005-04-24 17:05:04 +000016set shiftwidth=2
17set expandtab
18
Dan Gohmand30103d2010-02-26 21:24:46 +000019" Highlight trailing whitespace and lines longer than 80 columns.
20highlight LongLine ctermbg=DarkYellow guibg=DarkYellow
Misha Brukmanadf4e4d2009-01-02 16:26:14 +000021highlight WhitespaceEOL ctermbg=DarkYellow guibg=DarkYellow
Dan Gohmand30103d2010-02-26 21:24:46 +000022if v:version >= 702
23 " Lines longer than 80 columns.
24 au BufWinEnter * let w:m0=matchadd('LongLine', '\%>80v.\+', -1)
25
26 " Whitespace at the end of a line. This little dance suppresses
Dan Gohmanf34ff012010-02-26 21:45:37 +000027 " whitespace that has just been typed.
Dan Gohmand30103d2010-02-26 21:24:46 +000028 au BufWinEnter * let w:m1=matchadd('WhitespaceEOL', '\s\+$', -1)
29 au InsertEnter * call matchdelete(w:m1)
30 au InsertEnter * let w:m2=matchadd('WhitespaceEOL', '\s\+\%#\@<!$', -1)
31 au InsertLeave * call matchdelete(w:m2)
32 au InsertLeave * let w:m1=matchadd('WhitespaceEOL', '\s\+$', -1)
33else
34 au BufRead,BufNewFile * syntax match LongLine /\%>80v.\+/
35 au InsertEnter * syntax match WhitespaceEOL /\s\+\%#\@<!$/
36 au InsertLeave * syntax match WhitespaceEOL /\s\+$/
37endif
Misha Brukmanadf4e4d2009-01-02 16:26:14 +000038
Dan Gohman5e59c272009-01-21 21:30:25 +000039" Enable filetype detection
40filetype on
41
Misha Brukman2bb55082005-05-12 21:41:48 +000042" Optional
43" C/C++ programming helpers
Dan Gohman5e59c272009-01-21 21:30:25 +000044augroup csrc
45 au!
46 autocmd FileType * set nocindent smartindent
47 autocmd FileType c,cpp set cindent
48augroup END
Dan Gohmana0741ba2009-01-04 18:59:55 +000049" Set a few indentation parameters. See the VIM help for cinoptions-values for
50" details. These aren't absolute rules; they're just an approximation of
51" common style in LLVM source.
Dan Gohmanacb75a92010-01-09 17:15:21 +000052set cinoptions=:0,g0,(0,Ws,l1
Misha Brukman2bb55082005-05-12 21:41:48 +000053" Add and delete spaces in increments of `shiftwidth' for tabs
54set smarttab
55
Misha Brukmanadf4e4d2009-01-02 16:26:14 +000056" Highlight syntax in programming languages
57syntax on
58
Misha Brukman3d6eea52005-04-24 17:05:04 +000059" LLVM Makefiles can have names such as Makefile.rules or TEST.nightly.Makefile,
60" so it's important to categorize them as such.
61augroup filetype
Misha Brukmanadf4e4d2009-01-02 16:26:14 +000062 au! BufRead,BufNewFile *Makefile* set filetype=make
Misha Brukman3d6eea52005-04-24 17:05:04 +000063augroup END
64
65" In Makefiles, don't expand tabs to spaces, since we need the actual tabs
66autocmd FileType make set noexpandtab
67
68" Useful macros for cleaning up code to conform to LLVM coding guidelines
69
70" Delete trailing whitespace and tabs at the end of each line
Misha Brukman5539a1e2009-01-08 02:17:30 +000071command! DeleteTrailingWs :%s/\s\+$//
Misha Brukman3d6eea52005-04-24 17:05:04 +000072
73" Convert all tab characters to two spaces
Misha Brukmanadf4e4d2009-01-02 16:26:14 +000074command! Untab :%s/\t/ /g
Dan Gohman789da272009-01-21 21:47:51 +000075
76" Enable syntax highlighting for LLVM files. To use, copy
77" utils/vim/llvm.vim to ~/.vim/syntax .
78augroup filetype
79 au! BufRead,BufNewFile *.ll set filetype=llvm
80augroup END
81
82" Enable syntax highlighting for tablegen files. To use, copy
83" utils/vim/tablegen.vim to ~/.vim/syntax .
84augroup filetype
85 au! BufRead,BufNewFile *.td set filetype=tablegen
86augroup END
Dan Gohmand30103d2010-02-26 21:24:46 +000087
88" Additional vim features to optionally uncomment.
89"set showcmd
90"set showmatch
91"set showmode
92"set incsearch
93"set ruler
Dan Gohman446c6c42010-08-26 18:12:22 +000094
95" Clang code-completion support. This is highly experimental!
96
97" TODO: code-completing on
98" cast_cast<
99" turns up some peculiarities -- "asm("?
100
Dan Gohman34a92b72010-08-27 15:15:31 +0000101" A path to a clang executable.
102let g:clang_path = "clang++"
Dan Gohman446c6c42010-08-26 18:12:22 +0000103
104" A list of options to add to the clang commandline, for example to add
105" include paths, predefined macros, and language options.
106let g:clang_opts = [
107 \ "-x","c++",
108 \ "-D__STDC_LIMIT_MACROS=1","-D__STDC_CONSTANT_MACROS=1",
109 \ "-Iinclude" ]
110
111function! ClangComplete(findstart, base)
112 if a:findstart == 1
113 " In findstart mode, look for the beginning of the current identifier.
114 let l:line = getline('.')
115 let l:start = col('.') - 1
116 while l:start > 0 && l:line[l:start - 1] =~ '\i'
117 let l:start -= 1
118 endwhile
119 return l:start
120 endif
121
122 " Get the current line and column numbers.
123 let l:l = line('.')
124 let l:c = col('.')
125
126 " Build a clang commandline to do code completion on stdin.
127 let l:the_command = shellescape(g:clang_path) .
128 \ " -cc1 -code-completion-at=-:" . l:l . ":" . l:c
129 for l:opt in g:clang_opts
130 let l:the_command .= " " . shellescape(l:opt)
131 endfor
132
133 " Copy the contents of the current buffer into a string for stdin.
134 " TODO: The extra space at the end is for working around clang's
135 " apparent inability to do code completion at the very end of the
136 " input.
137 " TODO: Is it better to feed clang the entire file instead of truncating
138 " it at the current line?
139 let l:process_input = join(getline(1, l:l), "\n") . " "
140
141 " Run it!
142 let l:input_lines = split(system(l:the_command, l:process_input), "\n")
143
144 " Parse the output.
145 for l:input_line in l:input_lines
146 " Vim's substring operator is annoyingly inconsistent with python's.
147 if l:input_line[:11] == 'COMPLETION: '
148 let l:value = l:input_line[12:]
149
150 " Chop off anything after " : ", if present, and move it to the menu.
151 let l:menu = ""
152 let l:spacecolonspace = stridx(l:value, " : ")
153 if l:spacecolonspace != -1
154 let l:menu = l:value[l:spacecolonspace+3:]
155 let l:value = l:value[:l:spacecolonspace-1]
156 endif
157
Dan Gohman88b3a682010-08-27 15:16:09 +0000158 " Chop off " (Hidden)", if present, and move it to the menu.
159 let l:hidden = stridx(l:value, " (Hidden)")
160 if l:hidden != -1
161 let l:menu .= " (Hidden)"
162 let l:value = l:value[:l:hidden-1]
163 endif
164
Dan Gohman446c6c42010-08-26 18:12:22 +0000165 " Handle Pattern. TODO: Make clang less weird.
166 if l:value == "Pattern"
167 let l:value = l:menu
168 let l:pound = stridx(l:value, "#")
169 " Truncate the at the first [#, <#, or {#.
170 if l:pound != -1
171 let l:value = l:value[:l:pound-2]
172 endif
173 endif
174
175 " Filter out results which don't match the base string.
176 if a:base != ""
177 if l:value[:strlen(a:base)-1] != a:base
178 continue
179 end
180 endif
181
182 " TODO: Don't dump the raw input into info, though it's nice for now.
183 " TODO: The kind string?
184 let l:item = {
185 \ "word": l:value,
186 \ "menu": l:menu,
187 \ "info": l:input_line,
188 \ "dup": 1 }
189
190 " Report a result.
191 if complete_add(l:item) == 0
192 return []
193 endif
194 if complete_check()
195 return []
196 endif
197
198 elseif l:input_line[:9] == "OVERLOAD: "
199 " An overload candidate. Use a crazy hack to get vim to
200 " display the results. TODO: Make this better.
201 let l:value = l:input_line[10:]
202 let l:item = {
203 \ "word": " ",
204 \ "menu": l:value,
205 \ "info": l:input_line,
206 \ "dup": 1}
207
208 " Report a result.
209 if complete_add(l:item) == 0
210 return []
211 endif
212 if complete_check()
213 return []
214 endif
215
216 endif
217 endfor
218
219
220 return []
221endfunction ClangComplete
222
223" Uncomment this to enable the highly-broken autocompletion support.
224"set omnifunc=ClangComplete