Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 1 | " Test for checking the source code style. |
| 2 | |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 3 | def s:ReportError(fname: string, lnum: number, msg: string) |
| 4 | if lnum > 0 |
| 5 | assert_report(fname .. ' line ' .. lnum .. ': ' .. msg) |
| 6 | endif |
| 7 | enddef |
| 8 | |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 9 | def Test_source_files() |
| 10 | for fname in glob('../*.[ch]', 0, 1) |
Bram Moolenaar | bf63011 | 2023-05-19 21:41:02 +0100 | [diff] [blame] | 11 | bwipe! |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 12 | g:ignoreSwapExists = 'e' |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 13 | exe 'edit ' .. fname |
| 14 | |
| 15 | cursor(1, 1) |
| 16 | var lnum = search(' \t') |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 17 | ReportError(fname, lnum, 'space before Tab') |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 18 | |
| 19 | cursor(1, 1) |
| 20 | lnum = search('\s$') |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 21 | ReportError(fname, lnum, 'trailing white space') |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 22 | |
| 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 Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 35 | ReportError(fname, lnum, 'curly after closing paren') |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 36 | |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 37 | # Examples in comments use double quotes. |
| 38 | skip = "getline('.') =~ '\"'" |
Bram Moolenaar | bf63011 | 2023-05-19 21:41:02 +0100 | [diff] [blame] | 39 | |
| 40 | cursor(1, 1) |
| 41 | lnum = search('}\s*else', '', 0, 0, skip) |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 42 | ReportError(fname, lnum, 'curly before "else"') |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 43 | |
| 44 | cursor(1, 1) |
| 45 | lnum = search('else\s*{', '', 0, 0, skip) |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 46 | ReportError(fname, lnum, 'curly after "else"') |
Bram Moolenaar | c9471b1 | 2023-05-09 15:00:00 +0100 | [diff] [blame] | 47 | |
| 48 | cursor(1, 1) |
| 49 | lnum = search('\<\(if\|while\|for\)(', '', 0, 0, skip) |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 50 | ReportError(fname, lnum, 'missing white space after "if"/"while"/"for"') |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 51 | endfor |
| 52 | |
| 53 | bwipe! |
| 54 | enddef |
| 55 | |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 56 | def Test_test_files() |
| 57 | for fname in glob('*.vim', 0, 1) |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 58 | g:ignoreSwapExists = 'e' |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 59 | 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 Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 72 | ReportError('testdir/' .. fname, lnum, 'space before Tab') |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 73 | 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 Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 87 | ReportError('testdir/' .. fname, lnum, 'trailing white space') |
Bram Moolenaar | 94722c5 | 2023-01-28 19:19:03 +0000 | [diff] [blame] | 88 | endif |
| 89 | endfor |
| 90 | |
| 91 | bwipe! |
| 92 | enddef |
| 93 | |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 94 | def Test_help_files() |
| 95 | var lnum: number |
| 96 | set nowrapscan |
| 97 | |
| 98 | for fpath in glob('../../runtime/doc/*.txt', 0, 1) |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 99 | g:ignoreSwapExists = 'e' |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 100 | 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 Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 118 | ReportError(fpath, lnum, 'space before tab') |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 119 | 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 Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 135 | ReportError('testdir' .. fpath, lnum, 'trailing white space') |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 136 | if lnum == 0 |
| 137 | break |
| 138 | endif |
| 139 | endwhile |
| 140 | |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 141 | # # TODO: Check for line over 80 columns |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 142 | # cursor(1, 1) |
| 143 | # while 1 |
| 144 | # lnum = search('\%>80v.*$') |
Bram Moolenaar | abc8130 | 2023-06-04 16:55:27 +0100 | [diff] [blame] | 145 | # ReportError(fpath, lnum, 'line over 80 columns') |
h-east | d950984 | 2023-02-21 13:33:17 +0000 | [diff] [blame] | 146 | # if lnum == 0 |
| 147 | # break |
| 148 | # endif |
| 149 | # endwhile |
| 150 | |
| 151 | endfor |
| 152 | |
| 153 | set wrapscan&vim |
| 154 | bwipe! |
| 155 | enddef |
| 156 | |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 157 | |
| 158 | " vim: shiftwidth=2 sts=2 expandtab |