blob: 074468e7776db8079eba26e7154d61a838f5a149 [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
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00009def Test_source_files()
10 for fname in glob('../*.[ch]', 0, 1)
Bram Moolenaarbf630112023-05-19 21:41:02 +010011 bwipe!
Bram Moolenaarabc81302023-06-04 16:55:27 +010012 g:ignoreSwapExists = 'e'
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000013 exe 'edit ' .. fname
14
15 cursor(1, 1)
16 var lnum = search(' \t')
Bram Moolenaarabc81302023-06-04 16:55:27 +010017 ReportError(fname, lnum, 'space before Tab')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000018
19 cursor(1, 1)
20 lnum = search('\s$')
Bram Moolenaarabc81302023-06-04 16:55:27 +010021 ReportError(fname, lnum, 'trailing white space')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000022
23 # some files don't stick to the Vim style rules
24 if fname =~ 'iscygpty.c'
25 continue
26 endif
27
28 # Examples in comments use "condition) {", skip them.
29 # Skip if a double quote or digit comes after the "{".
30 # Skip specific string used in os_unix.c.
31 # Also skip fold markers.
32 var skip = 'getline(".") =~ "condition) {" || getline(".") =~ "vimglob_func" || getline(".") =~ "{\"" || getline(".") =~ "{\\d" || getline(".") =~ "{{{"'
33 cursor(1, 1)
34 lnum = search(')\s*{', '', 0, 0, skip)
Bram Moolenaarabc81302023-06-04 16:55:27 +010035 ReportError(fname, lnum, 'curly after closing paren')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000036
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000037 # Examples in comments use double quotes.
38 skip = "getline('.') =~ '\"'"
Bram Moolenaarbf630112023-05-19 21:41:02 +010039
40 cursor(1, 1)
41 lnum = search('}\s*else', '', 0, 0, skip)
Bram Moolenaarabc81302023-06-04 16:55:27 +010042 ReportError(fname, lnum, 'curly before "else"')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000043
44 cursor(1, 1)
45 lnum = search('else\s*{', '', 0, 0, skip)
Bram Moolenaarabc81302023-06-04 16:55:27 +010046 ReportError(fname, lnum, 'curly after "else"')
Bram Moolenaarc9471b12023-05-09 15:00:00 +010047
48 cursor(1, 1)
49 lnum = search('\<\(if\|while\|for\)(', '', 0, 0, skip)
Bram Moolenaarabc81302023-06-04 16:55:27 +010050 ReportError(fname, lnum, 'missing white space after "if"/"while"/"for"')
Bram Moolenaarebfec1c2023-01-22 21:14:53 +000051 endfor
52
53 bwipe!
54enddef
55
Bram Moolenaar94722c52023-01-28 19:19:03 +000056def Test_test_files()
57 for fname in glob('*.vim', 0, 1)
Bram Moolenaarabc81302023-06-04 16:55:27 +010058 g:ignoreSwapExists = 'e'
Bram Moolenaar94722c52023-01-28 19:19:03 +000059 exe 'edit ' .. fname
60
61 # some files intentionally have misplaced white space
62 if fname =~ 'test_cindent.vim' || fname =~ 'test_join.vim'
63 continue
64 endif
65
66 # skip files that are known to have a space before a tab
67 if fname !~ 'test_comments.vim'
68 && fname !~ 'test_listchars.vim'
69 && fname !~ 'test_visual.vim'
70 cursor(1, 1)
71 var lnum = search(fname =~ "test_regexp_latin" ? '[^รก] \t' : ' \t')
Bram Moolenaarabc81302023-06-04 16:55:27 +010072 ReportError('testdir/' .. fname, lnum, 'space before Tab')
Bram Moolenaar94722c52023-01-28 19:19:03 +000073 endif
74
75 # skip files that are known to have trailing white space
76 if fname !~ 'test_cmdline.vim'
77 && fname !~ 'test_let.vim'
78 && fname !~ 'test_tagjump.vim'
79 && fname !~ 'test_vim9_cmd.vim'
80 cursor(1, 1)
81 var lnum = search(
82 fname =~ 'test_vim9_assign.vim' ? '[^=]\s$'
83 : fname =~ 'test_vim9_class.vim' ? '[^)]\s$'
84 : fname =~ 'test_vim9_script.vim' ? '[^,:3]\s$'
85 : fname =~ 'test_visual.vim' ? '[^/]\s$'
86 : '[^\\]\s$')
Bram Moolenaarabc81302023-06-04 16:55:27 +010087 ReportError('testdir/' .. fname, lnum, 'trailing white space')
Bram Moolenaar94722c52023-01-28 19:19:03 +000088 endif
89 endfor
90
91 bwipe!
92enddef
93
h-eastd9509842023-02-21 13:33:17 +000094def Test_help_files()
95 var lnum: number
96 set nowrapscan
97
98 for fpath in glob('../../runtime/doc/*.txt', 0, 1)
Bram Moolenaarabc81302023-06-04 16:55:27 +010099 g:ignoreSwapExists = 'e'
h-eastd9509842023-02-21 13:33:17 +0000100 exe 'edit ' .. fpath
101
102 var fname = fnamemodify(fpath, ":t")
103
104 # todo.txt is for developers, it's not need a strictly check
105 # version*.txt is a history and large size, so it's not checked
106 if fname == 'todo.txt' || fname =~ 'version.*\.txt'
107 continue
108 endif
109
110 # Check for mixed tabs and spaces
111 cursor(1, 1)
112 while 1
113 lnum = search('[^/] \t')
114 if fname == 'visual.txt' && getline(lnum) =~ "STRING \tjkl"
115 || fname == 'usr_27.txt' && getline(lnum) =~ "\[^\? \t\]"
116 continue
117 endif
Bram Moolenaarabc81302023-06-04 16:55:27 +0100118 ReportError(fpath, lnum, 'space before tab')
h-eastd9509842023-02-21 13:33:17 +0000119 if lnum == 0
120 break
121 endif
122 endwhile
123
124 # Check for unnecessary whitespace at the end of a line
125 cursor(1, 1)
126 while 1
127 lnum = search('[^/~\\]\s$')
128 # skip line that are known to have trailing white space
129 if fname == 'map.txt' && getline(lnum) =~ "unmap @@ $"
130 || fname == 'usr_12.txt' && getline(lnum) =~ "^\t/ \t$"
131 || fname == 'usr_41.txt' && getline(lnum) =~ "map <F4> o#include $"
132 || fname == 'change.txt' && getline(lnum) =~ "foobar bla $"
133 continue
134 endif
Bram Moolenaarabc81302023-06-04 16:55:27 +0100135 ReportError('testdir' .. fpath, lnum, 'trailing white space')
h-eastd9509842023-02-21 13:33:17 +0000136 if lnum == 0
137 break
138 endif
139 endwhile
140
Bram Moolenaarabc81302023-06-04 16:55:27 +0100141# # TODO: Check for line over 80 columns
h-eastd9509842023-02-21 13:33:17 +0000142# cursor(1, 1)
143# while 1
144# lnum = search('\%>80v.*$')
Bram Moolenaarabc81302023-06-04 16:55:27 +0100145# ReportError(fpath, lnum, 'line over 80 columns')
h-eastd9509842023-02-21 13:33:17 +0000146# if lnum == 0
147# break
148# endif
149# endwhile
150
151 endfor
152
153 set wrapscan&vim
154 bwipe!
155enddef
156
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000157
158" vim: shiftwidth=2 sts=2 expandtab