blob: ea12b6c66dbd702af06aa42a47849a017813d9c6 [file] [log] [blame]
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001" Test for checking the source code style.
2
Christian Brabandt681f1c92025-05-21 20:50:11 +02003let s:list_of_c_files = []
4
Bram Moolenaarabc81302023-06-04 16:55:27 +01005def s:ReportError(fname: string, lnum: number, msg: string)
6 if lnum > 0
7 assert_report(fname .. ' line ' .. lnum .. ': ' .. msg)
8 endif
9enddef
10
Christian Brabandtb147d312023-09-01 17:58:35 +010011def s:PerformCheck(fname: string, pattern: string, msg: string, skip: string)
John Marriott78d742a2024-04-02 20:26:01 +020012 var prev_lnum = 1
Christian Brabandtb147d312023-09-01 17:58:35 +010013 var lnum = 1
14 while (lnum > 0)
15 cursor(lnum, 1)
16 lnum = search(pattern, 'W', 0, 0, skip)
John Marriott78d742a2024-04-02 20:26:01 +020017 if (prev_lnum == lnum)
18 break
19 endif
20 prev_lnum = lnum
Christian Brabandtb147d312023-09-01 17:58:35 +010021 if (lnum > 0)
John Marriott78d742a2024-04-02 20:26:01 +020022 ReportError(fname, lnum, msg)
Christian Brabandtb147d312023-09-01 17:58:35 +010023 endif
24 endwhile
25enddef
26
Christian Brabandt681f1c92025-05-21 20:50:11 +020027def s:Get_C_source_files(): list<string>
28 if empty(list_of_c_files)
29 var list = glob('../*.[ch]', 0, 1) + ['../xxd/xxd.c']
30 # Some files are auto-generated and may contain space errors, so skip those
31 list_of_c_files = filter(list, (i, v) => v !~ 'dlldata.c\|if_ole.h\|iid_ole.c')
32 endif
33 return list_of_c_files
34enddef
35
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000036def Test_source_files()
Christian Brabandt681f1c92025-05-21 20:50:11 +020037 for fname in Get_C_source_files()
Bram Moolenaarbf630112023-05-19 21:41:02 +010038 bwipe!
Bram Moolenaarabc81302023-06-04 16:55:27 +010039 g:ignoreSwapExists = 'e'
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000040 exe 'edit ' .. fname
41
Christian Brabandtb147d312023-09-01 17:58:35 +010042 PerformCheck(fname, ' \t', 'space before Tab', '')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000043
Christian Brabandtb147d312023-09-01 17:58:35 +010044 PerformCheck(fname, '\s$', 'trailing white space', '')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000045
46 # some files don't stick to the Vim style rules
47 if fname =~ 'iscygpty.c'
48 continue
49 endif
50
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000051 var skip = 'getline(".") =~ "condition) {" || getline(".") =~ "vimglob_func" || getline(".") =~ "{\"" || getline(".") =~ "{\\d" || getline(".") =~ "{{{"'
Christian Brabandtb147d312023-09-01 17:58:35 +010052 PerformCheck(fname, ')\s*{', 'curly after closing paren', skip)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000053
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000054 # Examples in comments use double quotes.
55 skip = "getline('.') =~ '\"'"
Bram Moolenaarbf630112023-05-19 21:41:02 +010056
Christian Brabandtb147d312023-09-01 17:58:35 +010057 PerformCheck(fname, '}\s*else', 'curly before "else"', skip)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000058
Christian Brabandtb147d312023-09-01 17:58:35 +010059 PerformCheck(fname, 'else\s*{', 'curly after "else"', skip)
Bram Moolenaarc9471b12023-05-09 15:00:00 +010060
Christian Brabandtb147d312023-09-01 17:58:35 +010061 PerformCheck(fname, '\<\(if\|while\|for\)(', 'missing white space after "if"/"while"/"for"', skip)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000062 endfor
63
64 bwipe!
65enddef
66
Bram Moolenaar94722c52023-01-28 19:19:03 +000067def Test_test_files()
68 for fname in glob('*.vim', 0, 1)
Bram Moolenaarabc81302023-06-04 16:55:27 +010069 g:ignoreSwapExists = 'e'
Bram Moolenaar94722c52023-01-28 19:19:03 +000070 exe 'edit ' .. fname
71
72 # some files intentionally have misplaced white space
73 if fname =~ 'test_cindent.vim' || fname =~ 'test_join.vim'
74 continue
75 endif
76
77 # skip files that are known to have a space before a tab
78 if fname !~ 'test_comments.vim'
79 && fname !~ 'test_listchars.vim'
80 && fname !~ 'test_visual.vim'
81 cursor(1, 1)
Christian Brabandt22105fd2024-07-15 20:51:11 +020082 var skip = 'getline(".") =~ "codestyle: ignore"'
83 var lnum = search(fname =~ "test_regexp_latin" ? '[^รก] \t' : ' \t', 'W', 0, 0, skip)
Bram Moolenaarabc81302023-06-04 16:55:27 +010084 ReportError('testdir/' .. fname, lnum, 'space before Tab')
Bram Moolenaar94722c52023-01-28 19:19:03 +000085 endif
86
87 # skip files that are known to have trailing white space
88 if fname !~ 'test_cmdline.vim'
89 && fname !~ 'test_let.vim'
90 && fname !~ 'test_tagjump.vim'
91 && fname !~ 'test_vim9_cmd.vim'
Doug Kearnsdbe39ed2025-01-04 17:12:24 +010092 && fname !~ 'test_vim9_enum.vim'
Bram Moolenaar94722c52023-01-28 19:19:03 +000093 cursor(1, 1)
94 var lnum = search(
95 fname =~ 'test_vim9_assign.vim' ? '[^=]\s$'
96 : fname =~ 'test_vim9_class.vim' ? '[^)]\s$'
97 : fname =~ 'test_vim9_script.vim' ? '[^,:3]\s$'
98 : fname =~ 'test_visual.vim' ? '[^/]\s$'
99 : '[^\\]\s$')
Bram Moolenaarabc81302023-06-04 16:55:27 +0100100 ReportError('testdir/' .. fname, lnum, 'trailing white space')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000101 endif
102 endfor
103
104 bwipe!
105enddef
106
h-eastd9509842023-02-21 13:33:17 +0000107def Test_help_files()
108 var lnum: number
109 set nowrapscan
110
111 for fpath in glob('../../runtime/doc/*.txt', 0, 1)
Bram Moolenaarabc81302023-06-04 16:55:27 +0100112 g:ignoreSwapExists = 'e'
h-eastd9509842023-02-21 13:33:17 +0000113 exe 'edit ' .. fpath
114
115 var fname = fnamemodify(fpath, ":t")
116
117 # todo.txt is for developers, it's not need a strictly check
118 # version*.txt is a history and large size, so it's not checked
119 if fname == 'todo.txt' || fname =~ 'version.*\.txt'
120 continue
121 endif
122
123 # Check for mixed tabs and spaces
124 cursor(1, 1)
125 while 1
126 lnum = search('[^/] \t')
127 if fname == 'visual.txt' && getline(lnum) =~ "STRING \tjkl"
128 || fname == 'usr_27.txt' && getline(lnum) =~ "\[^\? \t\]"
129 continue
130 endif
Bram Moolenaarabc81302023-06-04 16:55:27 +0100131 ReportError(fpath, lnum, 'space before tab')
h-eastd9509842023-02-21 13:33:17 +0000132 if lnum == 0
133 break
134 endif
135 endwhile
136
137 # Check for unnecessary whitespace at the end of a line
138 cursor(1, 1)
139 while 1
140 lnum = search('[^/~\\]\s$')
141 # skip line that are known to have trailing white space
142 if fname == 'map.txt' && getline(lnum) =~ "unmap @@ $"
143 || fname == 'usr_12.txt' && getline(lnum) =~ "^\t/ \t$"
144 || fname == 'usr_41.txt' && getline(lnum) =~ "map <F4> o#include $"
145 || fname == 'change.txt' && getline(lnum) =~ "foobar bla $"
146 continue
147 endif
Bram Moolenaarabc81302023-06-04 16:55:27 +0100148 ReportError('testdir' .. fpath, lnum, 'trailing white space')
h-eastd9509842023-02-21 13:33:17 +0000149 if lnum == 0
150 break
151 endif
152 endwhile
153
Bram Moolenaarabc81302023-06-04 16:55:27 +0100154# # TODO: Check for line over 80 columns
h-eastd9509842023-02-21 13:33:17 +0000155# cursor(1, 1)
156# while 1
157# lnum = search('\%>80v.*$')
Bram Moolenaarabc81302023-06-04 16:55:27 +0100158# ReportError(fpath, lnum, 'line over 80 columns')
h-eastd9509842023-02-21 13:33:17 +0000159# if lnum == 0
160# break
161# endif
162# endwhile
163
164 endfor
165
166 set wrapscan&vim
167 bwipe!
168enddef
169
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200170def Test_indent_of_source_files()
Christian Brabandt681f1c92025-05-21 20:50:11 +0200171 for fname in Get_C_source_files()
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200172 execute 'tabnew ' .. fname
Hirohito Higashi38972d82025-05-06 18:13:29 +0200173 if &expandtab
174 continue
175 endif
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200176 for lnum in range(1, line('$'))
177 var name: string = synIDattr(synID(lnum, 1, 0), 'name')
178 if -1 == index(['cComment', 'cCommentStart'], name)
179 var line: string = getline(lnum)
180 var indent: string = matchstr(line, '^\s*')
181 var tailing: string = matchstr(line, '\s*$')
182 if !empty(indent)
Hirohito Higashi38972d82025-05-06 18:13:29 +0200183 if indent !~# '^\t* \{0,7}$'
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200184 ReportError('testdir/' .. fname, lnum, 'invalid indent')
185 endif
186 endif
187 if !empty(tailing)
188 ReportError('testdir/' .. fname, lnum, 'tailing spaces')
189 endif
190 endif
191 endfor
192 close
193 endfor
194enddef
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000195
Christian Brabandt22105fd2024-07-15 20:51:11 +0200196" vim: shiftwidth=2 sts=2 expandtab nofoldenable