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