xref: /llvm-project/llvm/utils/vim/vimrc (revision 999a86ba884e952878f76bca188273741222288f)
1831ad84eSMisha Brukman" LLVM coding guidelines conformance for VIM
21b9dd248SDan Gohman" $Revision$
3ac7b456cSMisha Brukman"
4ac7b456cSMisha Brukman" Maintainer: The LLVM Team, http://llvm.org
5831ad84eSMisha Brukman" WARNING:    Read before you source in all these commands and macros!  Some
6f1ed8edeSMisha Brukman"             of them may change VIM behavior that you depend on.
7ac7b456cSMisha Brukman"
8ac7b456cSMisha Brukman" You can run VIM with these settings without changing your current setup with:
9ac7b456cSMisha Brukman" $ vim -u /path/to/llvm/utils/vim/vimrc
10ac7b456cSMisha Brukman
11ac7b456cSMisha Brukman" It's VIM, not VI
12ac7b456cSMisha Brukmanset nocompatible
13831ad84eSMisha Brukman
14831ad84eSMisha Brukman" A tab produces a 2-space indentation
152dca8284SDan Gohmanset softtabstop=2
16831ad84eSMisha Brukmanset shiftwidth=2
17831ad84eSMisha Brukmanset expandtab
18831ad84eSMisha Brukman
1994fa92b4SDan Gohman" Highlight trailing whitespace and lines longer than 80 columns.
2094fa92b4SDan Gohmanhighlight LongLine ctermbg=DarkYellow guibg=DarkYellow
21ac7b456cSMisha Brukmanhighlight WhitespaceEOL ctermbg=DarkYellow guibg=DarkYellow
2294fa92b4SDan Gohmanif v:version >= 702
2394fa92b4SDan Gohman  " Lines longer than 80 columns.
2494fa92b4SDan Gohman  au BufWinEnter * let w:m0=matchadd('LongLine', '\%>80v.\+', -1)
2594fa92b4SDan Gohman
2694fa92b4SDan Gohman  " Whitespace at the end of a line. This little dance suppresses
27b53dababSDan Gohman  " whitespace that has just been typed.
2894fa92b4SDan Gohman  au BufWinEnter * let w:m1=matchadd('WhitespaceEOL', '\s\+$', -1)
2994fa92b4SDan Gohman  au InsertEnter * call matchdelete(w:m1)
3094fa92b4SDan Gohman  au InsertEnter * let w:m2=matchadd('WhitespaceEOL', '\s\+\%#\@<!$', -1)
3194fa92b4SDan Gohman  au InsertLeave * call matchdelete(w:m2)
3294fa92b4SDan Gohman  au InsertLeave * let w:m1=matchadd('WhitespaceEOL', '\s\+$', -1)
3394fa92b4SDan Gohmanelse
3494fa92b4SDan Gohman  au BufRead,BufNewFile * syntax match LongLine /\%>80v.\+/
3594fa92b4SDan Gohman  au InsertEnter * syntax match WhitespaceEOL /\s\+\%#\@<!$/
3694fa92b4SDan Gohman  au InsertLeave * syntax match WhitespaceEOL /\s\+$/
3794fa92b4SDan Gohmanendif
38ac7b456cSMisha Brukman
39455be5a0SDan Gohman" Enable filetype detection
40455be5a0SDan Gohmanfiletype on
41455be5a0SDan Gohman
42f1ed8edeSMisha Brukman" Optional
43f1ed8edeSMisha Brukman" C/C++ programming helpers
44455be5a0SDan Gohmanaugroup csrc
45455be5a0SDan Gohman  au!
46455be5a0SDan Gohman  autocmd FileType *      set nocindent smartindent
47455be5a0SDan Gohman  autocmd FileType c,cpp  set cindent
48455be5a0SDan Gohmanaugroup END
49be631a3cSDan Gohman" Set a few indentation parameters. See the VIM help for cinoptions-values for
50be631a3cSDan Gohman" details.  These aren't absolute rules; they're just an approximation of
51be631a3cSDan Gohman" common style in LLVM source.
52d5ae1369SDan Gohmanset cinoptions=:0,g0,(0,Ws,l1
53f1ed8edeSMisha Brukman" Add and delete spaces in increments of `shiftwidth' for tabs
54f1ed8edeSMisha Brukmanset smarttab
55f1ed8edeSMisha Brukman
56ac7b456cSMisha Brukman" Highlight syntax in programming languages
57ac7b456cSMisha Brukmansyntax on
58ac7b456cSMisha Brukman
59831ad84eSMisha Brukman" LLVM Makefiles can have names such as Makefile.rules or TEST.nightly.Makefile,
60831ad84eSMisha Brukman" so it's important to categorize them as such.
61831ad84eSMisha Brukmanaugroup filetype
62831ad84eSMisha Brukman  au! BufRead,BufNewFile *Makefile* set filetype=make
63831ad84eSMisha Brukmanaugroup END
64831ad84eSMisha Brukman
65831ad84eSMisha Brukman" In Makefiles, don't expand tabs to spaces, since we need the actual tabs
66831ad84eSMisha Brukmanautocmd FileType make set noexpandtab
67831ad84eSMisha Brukman
68831ad84eSMisha Brukman" Useful macros for cleaning up code to conform to LLVM coding guidelines
69831ad84eSMisha Brukman
70831ad84eSMisha Brukman" Delete trailing whitespace and tabs at the end of each line
7193bd1ca5SMisha Brukmancommand! DeleteTrailingWs :%s/\s\+$//
72831ad84eSMisha Brukman
73831ad84eSMisha Brukman" Convert all tab characters to two spaces
74ac7b456cSMisha Brukmancommand! Untab :%s/\t/  /g
7531555330SDan Gohman
7631555330SDan Gohman" Enable syntax highlighting for LLVM files. To use, copy
77*999a86baSDan Gohman" utils/vim/syntax/llvm.vim to ~/.vim/syntax .
7831555330SDan Gohmanaugroup filetype
7931555330SDan Gohman  au! BufRead,BufNewFile *.ll     set filetype=llvm
8031555330SDan Gohmanaugroup END
8131555330SDan Gohman
8231555330SDan Gohman" Enable syntax highlighting for tablegen files. To use, copy
83*999a86baSDan Gohman" utils/vim/syntax/tablegen.vim to ~/.vim/syntax .
8431555330SDan Gohmanaugroup filetype
8531555330SDan Gohman  au! BufRead,BufNewFile *.td     set filetype=tablegen
8631555330SDan Gohmanaugroup END
8794fa92b4SDan Gohman
880615dbe4SBill Wendling" Enable syntax highlighting for reStructuredText files. To use, copy
890615dbe4SBill Wendling" rest.vim (http://www.vim.org/scripts/script.php?script_id=973)
900615dbe4SBill Wendling" to ~/.vim/syntax .
910615dbe4SBill Wendlingaugroup filetype
920615dbe4SBill Wendling au! BufRead,BufNewFile *.rst     set filetype=rest
930615dbe4SBill Wendlingaugroup END
940615dbe4SBill Wendling
9594fa92b4SDan Gohman" Additional vim features to optionally uncomment.
9694fa92b4SDan Gohman"set showcmd
9794fa92b4SDan Gohman"set showmatch
9894fa92b4SDan Gohman"set showmode
9994fa92b4SDan Gohman"set incsearch
10094fa92b4SDan Gohman"set ruler
101f34728a9SDan Gohman
1025faac390SDan Gohman" Clang code-completion support. This is somewhat experimental!
103f34728a9SDan Gohman
1047e64c985SDan Gohman" A path to a clang executable.
1057e64c985SDan Gohmanlet g:clang_path = "clang++"
106f34728a9SDan Gohman
107f34728a9SDan Gohman" A list of options to add to the clang commandline, for example to add
108f34728a9SDan Gohman" include paths, predefined macros, and language options.
109f34728a9SDan Gohmanlet g:clang_opts = [
110f34728a9SDan Gohman  \ "-x","c++",
111f34728a9SDan Gohman  \ "-D__STDC_LIMIT_MACROS=1","-D__STDC_CONSTANT_MACROS=1",
112f34728a9SDan Gohman  \ "-Iinclude" ]
113f34728a9SDan Gohman
114f34728a9SDan Gohmanfunction! ClangComplete(findstart, base)
115f34728a9SDan Gohman   if a:findstart == 1
116f34728a9SDan Gohman      " In findstart mode, look for the beginning of the current identifier.
117f34728a9SDan Gohman      let l:line = getline('.')
118f34728a9SDan Gohman      let l:start = col('.') - 1
119f34728a9SDan Gohman      while l:start > 0 && l:line[l:start - 1] =~ '\i'
120f34728a9SDan Gohman         let l:start -= 1
121f34728a9SDan Gohman      endwhile
122f34728a9SDan Gohman      return l:start
123f34728a9SDan Gohman   endif
124f34728a9SDan Gohman
125f34728a9SDan Gohman   " Get the current line and column numbers.
126f34728a9SDan Gohman   let l:l = line('.')
127f34728a9SDan Gohman   let l:c = col('.')
128f34728a9SDan Gohman
129f34728a9SDan Gohman   " Build a clang commandline to do code completion on stdin.
130f34728a9SDan Gohman   let l:the_command = shellescape(g:clang_path) .
131f34728a9SDan Gohman                     \ " -cc1 -code-completion-at=-:" . l:l . ":" . l:c
132f34728a9SDan Gohman   for l:opt in g:clang_opts
133f34728a9SDan Gohman      let l:the_command .= " " . shellescape(l:opt)
134f34728a9SDan Gohman   endfor
135f34728a9SDan Gohman
136f34728a9SDan Gohman   " Copy the contents of the current buffer into a string for stdin.
137f34728a9SDan Gohman   " TODO: The extra space at the end is for working around clang's
138f34728a9SDan Gohman   " apparent inability to do code completion at the very end of the
139f34728a9SDan Gohman   " input.
140f34728a9SDan Gohman   " TODO: Is it better to feed clang the entire file instead of truncating
141f34728a9SDan Gohman   " it at the current line?
142f34728a9SDan Gohman   let l:process_input = join(getline(1, l:l), "\n") . " "
143f34728a9SDan Gohman
144f34728a9SDan Gohman   " Run it!
145f34728a9SDan Gohman   let l:input_lines = split(system(l:the_command, l:process_input), "\n")
146f34728a9SDan Gohman
147f34728a9SDan Gohman   " Parse the output.
148f34728a9SDan Gohman   for l:input_line in l:input_lines
149f34728a9SDan Gohman      " Vim's substring operator is annoyingly inconsistent with python's.
150f34728a9SDan Gohman      if l:input_line[:11] == 'COMPLETION: '
151f34728a9SDan Gohman         let l:value = l:input_line[12:]
152f34728a9SDan Gohman
153f34728a9SDan Gohman        " Chop off anything after " : ", if present, and move it to the menu.
154f34728a9SDan Gohman        let l:menu = ""
155f34728a9SDan Gohman        let l:spacecolonspace = stridx(l:value, " : ")
156f34728a9SDan Gohman        if l:spacecolonspace != -1
157f34728a9SDan Gohman           let l:menu = l:value[l:spacecolonspace+3:]
158f34728a9SDan Gohman           let l:value = l:value[:l:spacecolonspace-1]
159f34728a9SDan Gohman        endif
160f34728a9SDan Gohman
161fa4a4705SDan Gohman        " Chop off " (Hidden)", if present, and move it to the menu.
162fa4a4705SDan Gohman        let l:hidden = stridx(l:value, " (Hidden)")
163fa4a4705SDan Gohman        if l:hidden != -1
164fa4a4705SDan Gohman           let l:menu .= " (Hidden)"
165fa4a4705SDan Gohman           let l:value = l:value[:l:hidden-1]
166fa4a4705SDan Gohman        endif
167fa4a4705SDan Gohman
168ca158413SDan Gohman        " Handle "Pattern". TODO: Make clang less weird.
169f34728a9SDan Gohman        if l:value == "Pattern"
170f34728a9SDan Gohman           let l:value = l:menu
171f34728a9SDan Gohman           let l:pound = stridx(l:value, "#")
172f34728a9SDan Gohman           " Truncate the at the first [#, <#, or {#.
173f34728a9SDan Gohman           if l:pound != -1
174f34728a9SDan Gohman              let l:value = l:value[:l:pound-2]
175f34728a9SDan Gohman           endif
176f34728a9SDan Gohman        endif
177f34728a9SDan Gohman
178f34728a9SDan Gohman         " Filter out results which don't match the base string.
179f34728a9SDan Gohman         if a:base != ""
180f34728a9SDan Gohman            if l:value[:strlen(a:base)-1] != a:base
181f34728a9SDan Gohman               continue
182f34728a9SDan Gohman            end
183f34728a9SDan Gohman         endif
184f34728a9SDan Gohman
185f34728a9SDan Gohman        " TODO: Don't dump the raw input into info, though it's nice for now.
186f34728a9SDan Gohman        " TODO: The kind string?
187f34728a9SDan Gohman        let l:item = {
188f34728a9SDan Gohman          \ "word": l:value,
189f34728a9SDan Gohman          \ "menu": l:menu,
190f34728a9SDan Gohman          \ "info": l:input_line,
191f34728a9SDan Gohman          \ "dup": 1 }
192f34728a9SDan Gohman
193f34728a9SDan Gohman        " Report a result.
194f34728a9SDan Gohman        if complete_add(l:item) == 0
195f34728a9SDan Gohman           return []
196f34728a9SDan Gohman        endif
197f34728a9SDan Gohman        if complete_check()
198f34728a9SDan Gohman           return []
199f34728a9SDan Gohman        endif
200f34728a9SDan Gohman
201f34728a9SDan Gohman      elseif l:input_line[:9] == "OVERLOAD: "
202f34728a9SDan Gohman         " An overload candidate. Use a crazy hack to get vim to
203f34728a9SDan Gohman         " display the results. TODO: Make this better.
204f34728a9SDan Gohman         let l:value = l:input_line[10:]
205f34728a9SDan Gohman         let l:item = {
206f34728a9SDan Gohman           \ "word": " ",
207f34728a9SDan Gohman           \ "menu": l:value,
208f34728a9SDan Gohman           \ "info": l:input_line,
209f34728a9SDan Gohman           \ "dup": 1}
210f34728a9SDan Gohman
211f34728a9SDan Gohman        " Report a result.
212f34728a9SDan Gohman        if complete_add(l:item) == 0
213f34728a9SDan Gohman           return []
214f34728a9SDan Gohman        endif
215f34728a9SDan Gohman        if complete_check()
216f34728a9SDan Gohman           return []
217f34728a9SDan Gohman        endif
218f34728a9SDan Gohman
219f34728a9SDan Gohman      endif
220f34728a9SDan Gohman   endfor
221f34728a9SDan Gohman
222f34728a9SDan Gohman
223f34728a9SDan Gohman   return []
224f34728a9SDan Gohmanendfunction ClangComplete
225f34728a9SDan Gohman
2265faac390SDan Gohman" This to enables the somewhat-experimental clang-based
2275faac390SDan Gohman" autocompletion support.
2285faac390SDan Gohmanset omnifunc=ClangComplete
229