blob: 1d2717c3b1925bc12ef7824aa707bdc4aca02a78 [file] [log] [blame]
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00001" Test for checking the source code style.
2
Bram Moolenaarabc81302023-06-04 16:55:27 +01003def s:ReportError(fname: string, lnum: number, msg: string)
4 if lnum > 0
5 assert_report(fname .. ' line ' .. lnum .. ': ' .. msg)
6 endif
7enddef
8
Christian Brabandtb147d312023-09-01 17:58:35 +01009def s:PerformCheck(fname: string, pattern: string, msg: string, skip: string)
John Marriott78d742a2024-04-02 20:26:01 +020010 var prev_lnum = 1
Christian Brabandtb147d312023-09-01 17:58:35 +010011 var lnum = 1
12 while (lnum > 0)
13 cursor(lnum, 1)
14 lnum = search(pattern, 'W', 0, 0, skip)
John Marriott78d742a2024-04-02 20:26:01 +020015 if (prev_lnum == lnum)
16 break
17 endif
18 prev_lnum = lnum
Christian Brabandtb147d312023-09-01 17:58:35 +010019 if (lnum > 0)
John Marriott78d742a2024-04-02 20:26:01 +020020 ReportError(fname, lnum, msg)
Christian Brabandtb147d312023-09-01 17:58:35 +010021 endif
22 endwhile
23enddef
24
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000025def Test_source_files()
Christian Brabandtb147d312023-09-01 17:58:35 +010026 for fname in glob('../*.[ch]', 0, 1) + ['../xxd/xxd.c']
Bram Moolenaarbf630112023-05-19 21:41:02 +010027 bwipe!
Bram Moolenaarabc81302023-06-04 16:55:27 +010028 g:ignoreSwapExists = 'e'
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000029 exe 'edit ' .. fname
30
Ken Takatac8a582a2024-07-20 11:55:13 +020031 # Some files are generated files and may contain space errors.
32 if fname =~ 'dlldata.c'
33 || fname =~ 'if_ole.h'
34 || fname =~ 'iid_ole.c'
35 continue
36 endif
37
Christian Brabandtb147d312023-09-01 17:58:35 +010038 PerformCheck(fname, ' \t', 'space before Tab', '')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000039
Christian Brabandtb147d312023-09-01 17:58:35 +010040 PerformCheck(fname, '\s$', 'trailing white space', '')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000041
42 # some files don't stick to the Vim style rules
43 if fname =~ 'iscygpty.c'
44 continue
45 endif
46
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000047 var skip = 'getline(".") =~ "condition) {" || getline(".") =~ "vimglob_func" || getline(".") =~ "{\"" || getline(".") =~ "{\\d" || getline(".") =~ "{{{"'
Christian Brabandtb147d312023-09-01 17:58:35 +010048 PerformCheck(fname, ')\s*{', 'curly after closing paren', skip)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000049
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000050 # Examples in comments use double quotes.
51 skip = "getline('.') =~ '\"'"
Bram Moolenaarbf630112023-05-19 21:41:02 +010052
Christian Brabandtb147d312023-09-01 17:58:35 +010053 PerformCheck(fname, '}\s*else', 'curly before "else"', skip)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000054
Christian Brabandtb147d312023-09-01 17:58:35 +010055 PerformCheck(fname, 'else\s*{', 'curly after "else"', skip)
Bram Moolenaarc9471b12023-05-09 15:00:00 +010056
Christian Brabandtb147d312023-09-01 17:58:35 +010057 PerformCheck(fname, '\<\(if\|while\|for\)(', 'missing white space after "if"/"while"/"for"', skip)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000058 endfor
59
60 bwipe!
61enddef
62
Bram Moolenaar94722c52023-01-28 19:19:03 +000063def Test_test_files()
64 for fname in glob('*.vim', 0, 1)
Bram Moolenaarabc81302023-06-04 16:55:27 +010065 g:ignoreSwapExists = 'e'
Bram Moolenaar94722c52023-01-28 19:19:03 +000066 exe 'edit ' .. fname
67
68 # some files intentionally have misplaced white space
69 if fname =~ 'test_cindent.vim' || fname =~ 'test_join.vim'
70 continue
71 endif
72
73 # skip files that are known to have a space before a tab
74 if fname !~ 'test_comments.vim'
75 && fname !~ 'test_listchars.vim'
76 && fname !~ 'test_visual.vim'
77 cursor(1, 1)
Christian Brabandt22105fd2024-07-15 20:51:11 +020078 var skip = 'getline(".") =~ "codestyle: ignore"'
79 var lnum = search(fname =~ "test_regexp_latin" ? '[^รก] \t' : ' \t', 'W', 0, 0, skip)
Bram Moolenaarabc81302023-06-04 16:55:27 +010080 ReportError('testdir/' .. fname, lnum, 'space before Tab')
Bram Moolenaar94722c52023-01-28 19:19:03 +000081 endif
82
83 # skip files that are known to have trailing white space
84 if fname !~ 'test_cmdline.vim'
85 && fname !~ 'test_let.vim'
86 && fname !~ 'test_tagjump.vim'
87 && fname !~ 'test_vim9_cmd.vim'
Doug Kearnsdbe39ed2025-01-04 17:12:24 +010088 && fname !~ 'test_vim9_enum.vim'
Bram Moolenaar94722c52023-01-28 19:19:03 +000089 cursor(1, 1)
90 var lnum = search(
91 fname =~ 'test_vim9_assign.vim' ? '[^=]\s$'
92 : fname =~ 'test_vim9_class.vim' ? '[^)]\s$'
93 : fname =~ 'test_vim9_script.vim' ? '[^,:3]\s$'
94 : fname =~ 'test_visual.vim' ? '[^/]\s$'
95 : '[^\\]\s$')
Bram Moolenaarabc81302023-06-04 16:55:27 +010096 ReportError('testdir/' .. fname, lnum, 'trailing white space')
Bram Moolenaar94722c52023-01-28 19:19:03 +000097 endif
98 endfor
99
100 bwipe!
101enddef
102
h-eastd9509842023-02-21 13:33:17 +0000103def Test_help_files()
104 var lnum: number
105 set nowrapscan
106
107 for fpath in glob('../../runtime/doc/*.txt', 0, 1)
Bram Moolenaarabc81302023-06-04 16:55:27 +0100108 g:ignoreSwapExists = 'e'
h-eastd9509842023-02-21 13:33:17 +0000109 exe 'edit ' .. fpath
110
111 var fname = fnamemodify(fpath, ":t")
112
113 # todo.txt is for developers, it's not need a strictly check
114 # version*.txt is a history and large size, so it's not checked
115 if fname == 'todo.txt' || fname =~ 'version.*\.txt'
116 continue
117 endif
118
119 # Check for mixed tabs and spaces
120 cursor(1, 1)
121 while 1
122 lnum = search('[^/] \t')
123 if fname == 'visual.txt' && getline(lnum) =~ "STRING \tjkl"
124 || fname == 'usr_27.txt' && getline(lnum) =~ "\[^\? \t\]"
125 continue
126 endif
Bram Moolenaarabc81302023-06-04 16:55:27 +0100127 ReportError(fpath, lnum, 'space before tab')
h-eastd9509842023-02-21 13:33:17 +0000128 if lnum == 0
129 break
130 endif
131 endwhile
132
133 # Check for unnecessary whitespace at the end of a line
134 cursor(1, 1)
135 while 1
136 lnum = search('[^/~\\]\s$')
137 # skip line that are known to have trailing white space
138 if fname == 'map.txt' && getline(lnum) =~ "unmap @@ $"
139 || fname == 'usr_12.txt' && getline(lnum) =~ "^\t/ \t$"
140 || fname == 'usr_41.txt' && getline(lnum) =~ "map <F4> o#include $"
141 || fname == 'change.txt' && getline(lnum) =~ "foobar bla $"
142 continue
143 endif
Bram Moolenaarabc81302023-06-04 16:55:27 +0100144 ReportError('testdir' .. fpath, lnum, 'trailing white space')
h-eastd9509842023-02-21 13:33:17 +0000145 if lnum == 0
146 break
147 endif
148 endwhile
149
Bram Moolenaarabc81302023-06-04 16:55:27 +0100150# # TODO: Check for line over 80 columns
h-eastd9509842023-02-21 13:33:17 +0000151# cursor(1, 1)
152# while 1
153# lnum = search('\%>80v.*$')
Bram Moolenaarabc81302023-06-04 16:55:27 +0100154# ReportError(fpath, lnum, 'line over 80 columns')
h-eastd9509842023-02-21 13:33:17 +0000155# if lnum == 0
156# break
157# endif
158# endwhile
159
160 endfor
161
162 set wrapscan&vim
163 bwipe!
164enddef
165
Naruhiko Nishinoc2a90002025-05-04 20:05:47 +0200166def Test_indent_of_source_files()
167 for fname in glob('../*.[ch]', 0, 1) + ['../xxd/xxd.c']
168 execute 'tabnew ' .. fname
169 for lnum in range(1, line('$'))
170 var name: string = synIDattr(synID(lnum, 1, 0), 'name')
171 if -1 == index(['cComment', 'cCommentStart'], name)
172 var line: string = getline(lnum)
173 var indent: string = matchstr(line, '^\s*')
174 var tailing: string = matchstr(line, '\s*$')
175 if !empty(indent)
176 if indent !~# '^\t* \{0,7\}$'
177 ReportError('testdir/' .. fname, lnum, 'invalid indent')
178 endif
179 endif
180 if !empty(tailing)
181 ReportError('testdir/' .. fname, lnum, 'tailing spaces')
182 endif
183 endif
184 endfor
185 close
186 endfor
187enddef
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000188
Christian Brabandt22105fd2024-07-15 20:51:11 +0200189" vim: shiftwidth=2 sts=2 expandtab nofoldenable