blob: 3ef54c08cfe1b43182b1a3010cf228711199e208 [file] [log] [blame]
Misha Brukman831ad842005-04-24 17:05:04 +00001" LLVM coding guidelines conformance for VIM
Dan Gohman1b9dd242010-02-26 21:38:04 +00002" $Revision$
Misha Brukmanac7b4562009-01-02 16:26:14 +00003"
4" Maintainer: The LLVM Team, http://llvm.org
Misha Brukman831ad842005-04-24 17:05:04 +00005" WARNING: Read before you source in all these commands and macros! Some
Misha Brukmanf1ed8ed2005-05-12 21:41:48 +00006" of them may change VIM behavior that you depend on.
Misha Brukmanac7b4562009-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 Brukman831ad842005-04-24 17:05:04 +000013
Misha Brukman831ad842005-04-24 17:05:04 +000014" A tab produces a 2-space indentation
Dan Gohman2dca8282009-01-04 00:05:43 +000015set softtabstop=2
Misha Brukman831ad842005-04-24 17:05:04 +000016set shiftwidth=2
17set expandtab
18
Dan Gohman94fa92b2010-02-26 21:24:46 +000019" Highlight trailing whitespace and lines longer than 80 columns.
20highlight LongLine ctermbg=DarkYellow guibg=DarkYellow
Misha Brukmanac7b4562009-01-02 16:26:14 +000021highlight WhitespaceEOL ctermbg=DarkYellow guibg=DarkYellow
Dan Gohman94fa92b2010-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 Gohmanb53daba2010-02-26 21:45:37 +000027 " whitespace that has just been typed.
Dan Gohman94fa92b2010-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 Brukmanac7b4562009-01-02 16:26:14 +000038
Dan Gohman455be5a2009-01-21 21:30:25 +000039" Enable filetype detection
40filetype on
41
Misha Brukmanf1ed8ed2005-05-12 21:41:48 +000042" Optional
43" C/C++ programming helpers
Dan Gohman455be5a2009-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 Gohmanbe631a32009-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 Gohmand5ae1362010-01-09 17:15:21 +000052set cinoptions=:0,g0,(0,Ws,l1
Misha Brukmanf1ed8ed2005-05-12 21:41:48 +000053" Add and delete spaces in increments of `shiftwidth' for tabs
54set smarttab
55
Misha Brukmanac7b4562009-01-02 16:26:14 +000056" Highlight syntax in programming languages
57syntax on
58
Misha Brukman831ad842005-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 Brukmanac7b4562009-01-02 16:26:14 +000062 au! BufRead,BufNewFile *Makefile* set filetype=make
Misha Brukman831ad842005-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 Brukman93bd1ca2009-01-08 02:17:30 +000071command! DeleteTrailingWs :%s/\s\+$//
Misha Brukman831ad842005-04-24 17:05:04 +000072
73" Convert all tab characters to two spaces
Misha Brukmanac7b4562009-01-02 16:26:14 +000074command! Untab :%s/\t/ /g
Dan Gohman31555332009-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 Gohman94fa92b2010-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 Gohmanf34728a2010-08-26 18:12:22 +000094
Dan Gohman5faac392010-10-26 23:24:54 +000095" Clang code-completion support. This is somewhat experimental!
Dan Gohmanf34728a2010-08-26 18:12:22 +000096
Dan Gohman7e64c982010-08-27 15:15:31 +000097" A path to a clang executable.
98let g:clang_path = "clang++"
Dan Gohmanf34728a2010-08-26 18:12:22 +000099
100" A list of options to add to the clang commandline, for example to add
101" include paths, predefined macros, and language options.
102let g:clang_opts = [
103 \ "-x","c++",
104 \ "-D__STDC_LIMIT_MACROS=1","-D__STDC_CONSTANT_MACROS=1",
105 \ "-Iinclude" ]
106
107function! ClangComplete(findstart, base)
108 if a:findstart == 1
109 " In findstart mode, look for the beginning of the current identifier.
110 let l:line = getline('.')
111 let l:start = col('.') - 1
112 while l:start > 0 && l:line[l:start - 1] =~ '\i'
113 let l:start -= 1
114 endwhile
115 return l:start
116 endif
117
118 " Get the current line and column numbers.
119 let l:l = line('.')
120 let l:c = col('.')
121
122 " Build a clang commandline to do code completion on stdin.
123 let l:the_command = shellescape(g:clang_path) .
124 \ " -cc1 -code-completion-at=-:" . l:l . ":" . l:c
125 for l:opt in g:clang_opts
126 let l:the_command .= " " . shellescape(l:opt)
127 endfor
128
129 " Copy the contents of the current buffer into a string for stdin.
130 " TODO: The extra space at the end is for working around clang's
131 " apparent inability to do code completion at the very end of the
132 " input.
133 " TODO: Is it better to feed clang the entire file instead of truncating
134 " it at the current line?
135 let l:process_input = join(getline(1, l:l), "\n") . " "
136
137 " Run it!
138 let l:input_lines = split(system(l:the_command, l:process_input), "\n")
139
140 " Parse the output.
141 for l:input_line in l:input_lines
142 " Vim's substring operator is annoyingly inconsistent with python's.
143 if l:input_line[:11] == 'COMPLETION: '
144 let l:value = l:input_line[12:]
145
146 " Chop off anything after " : ", if present, and move it to the menu.
147 let l:menu = ""
148 let l:spacecolonspace = stridx(l:value, " : ")
149 if l:spacecolonspace != -1
150 let l:menu = l:value[l:spacecolonspace+3:]
151 let l:value = l:value[:l:spacecolonspace-1]
152 endif
153
Dan Gohmanfa4a4702010-08-27 15:16:09 +0000154 " Chop off " (Hidden)", if present, and move it to the menu.
155 let l:hidden = stridx(l:value, " (Hidden)")
156 if l:hidden != -1
157 let l:menu .= " (Hidden)"
158 let l:value = l:value[:l:hidden-1]
159 endif
160
Dan Gohmanca158412010-08-27 15:16:40 +0000161 " Handle "Pattern". TODO: Make clang less weird.
Dan Gohmanf34728a2010-08-26 18:12:22 +0000162 if l:value == "Pattern"
163 let l:value = l:menu
164 let l:pound = stridx(l:value, "#")
165 " Truncate the at the first [#, <#, or {#.
166 if l:pound != -1
167 let l:value = l:value[:l:pound-2]
168 endif
169 endif
170
171 " Filter out results which don't match the base string.
172 if a:base != ""
173 if l:value[:strlen(a:base)-1] != a:base
174 continue
175 end
176 endif
177
178 " TODO: Don't dump the raw input into info, though it's nice for now.
179 " TODO: The kind string?
180 let l:item = {
181 \ "word": l:value,
182 \ "menu": l:menu,
183 \ "info": l:input_line,
184 \ "dup": 1 }
185
186 " Report a result.
187 if complete_add(l:item) == 0
188 return []
189 endif
190 if complete_check()
191 return []
192 endif
193
194 elseif l:input_line[:9] == "OVERLOAD: "
195 " An overload candidate. Use a crazy hack to get vim to
196 " display the results. TODO: Make this better.
197 let l:value = l:input_line[10:]
198 let l:item = {
199 \ "word": " ",
200 \ "menu": l:value,
201 \ "info": l:input_line,
202 \ "dup": 1}
203
204 " Report a result.
205 if complete_add(l:item) == 0
206 return []
207 endif
208 if complete_check()
209 return []
210 endif
211
212 endif
213 endfor
214
215
216 return []
217endfunction ClangComplete
218
Dan Gohman5faac392010-10-26 23:24:54 +0000219" This to enables the somewhat-experimental clang-based
220" autocompletion support.
221set omnifunc=ClangComplete