Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 1 | " Test for various indent options |
| 2 | |
| 3 | func Test_preserveindent() |
| 4 | new |
| 5 | " Test for autoindent copying indent from the previous line |
| 6 | setlocal autoindent |
| 7 | call setline(1, [repeat(' ', 16) .. 'line1']) |
| 8 | call feedkeys("A\nline2", 'xt') |
| 9 | call assert_equal("\t\tline2", getline(2)) |
| 10 | setlocal autoindent& |
| 11 | |
| 12 | " Test for using CTRL-T with and without 'preserveindent' |
| 13 | set shiftwidth=4 |
| 14 | call cursor(1, 1) |
| 15 | call setline(1, " \t ") |
| 16 | call feedkeys("Al\<C-T>", 'xt') |
| 17 | call assert_equal("\t\tl", getline(1)) |
| 18 | set preserveindent |
| 19 | call setline(1, " \t ") |
| 20 | call feedkeys("Al\<C-T>", 'xt') |
| 21 | call assert_equal(" \t \tl", getline(1)) |
| 22 | set pi& sw& |
| 23 | |
| 24 | " Test for using CTRL-T with 'expandtab' and 'preserveindent' |
| 25 | call cursor(1, 1) |
| 26 | call setline(1, "\t \t") |
| 27 | set shiftwidth=4 expandtab preserveindent |
| 28 | call feedkeys("Al\<C-T>", 'xt') |
| 29 | call assert_equal("\t \t l", getline(1)) |
| 30 | set sw& et& pi& |
| 31 | |
| 32 | close! |
| 33 | endfunc |
| 34 | |
| 35 | " Test for indent() |
| 36 | func Test_indent_func() |
| 37 | call assert_equal(-1, indent(-1)) |
| 38 | new |
| 39 | call setline(1, "\tabc") |
| 40 | call assert_equal(8, indent(1)) |
| 41 | call setline(1, " abc") |
| 42 | call assert_equal(4, indent(1)) |
| 43 | call setline(1, " \t abc") |
| 44 | call assert_equal(12, indent(1)) |
| 45 | close! |
| 46 | endfunc |
| 47 | |
| 48 | " Test for reindenting a line using the '=' operator |
| 49 | func Test_reindent() |
| 50 | new |
| 51 | call setline(1, 'abc') |
| 52 | set nomodifiable |
| 53 | call assert_fails('normal ==', 'E21:') |
| 54 | set modifiable |
| 55 | |
| 56 | call setline(1, ['foo', 'bar']) |
| 57 | call feedkeys('ggVG=', 'xt') |
| 58 | call assert_equal(['foo', 'bar'], getline(1, 2)) |
| 59 | close! |
| 60 | endfunc |
| 61 | |
| 62 | " Test for shifting a line with a preprocessor directive ('#') |
| 63 | func Test_preproc_indent() |
| 64 | new |
| 65 | set sw=4 |
| 66 | call setline(1, '#define FOO 1') |
| 67 | normal >> |
| 68 | call assert_equal(' #define FOO 1', getline(1)) |
| 69 | |
| 70 | " with 'smartindent' |
| 71 | call setline(1, '#define FOO 1') |
| 72 | set smartindent |
| 73 | normal >> |
| 74 | call assert_equal('#define FOO 1', getline(1)) |
| 75 | set smartindent& |
| 76 | |
| 77 | " with 'cindent' |
| 78 | set cindent |
| 79 | normal >> |
| 80 | call assert_equal('#define FOO 1', getline(1)) |
| 81 | set cindent& |
| 82 | |
| 83 | close! |
| 84 | endfunc |
| 85 | |
| 86 | " Test for 'copyindent' |
| 87 | func Test_copyindent() |
| 88 | new |
| 89 | set shiftwidth=4 autoindent expandtab copyindent |
| 90 | call setline(1, " \t abc") |
| 91 | call feedkeys("ol", 'xt') |
| 92 | call assert_equal(" \t l", getline(2)) |
| 93 | set noexpandtab |
| 94 | call setline(1, " \t abc") |
| 95 | call feedkeys("ol", 'xt') |
| 96 | call assert_equal(" \t l", getline(2)) |
| 97 | set sw& ai& et& ci& |
| 98 | close! |
| 99 | endfunc |
| 100 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 101 | " Test for changing multiple lines with lisp indent |
| 102 | func Test_lisp_indent_change_multiline() |
| 103 | new |
| 104 | setlocal lisp autoindent |
| 105 | call setline(1, ['(if a', ' (if b', ' (return 5)))']) |
| 106 | normal! jc2j(return 4)) |
| 107 | call assert_equal(' (return 4))', getline(2)) |
| 108 | close! |
| 109 | endfunc |
| 110 | |
| 111 | func Test_lisp_indent() |
| 112 | new |
| 113 | setlocal lisp autoindent |
| 114 | call setline(1, ['(if a', ' ;; comment', ' \ abc', '', ' " str1\', ' " st\b', ' (return 5)']) |
| 115 | normal! jo;; comment |
| 116 | normal! jo\ abc |
| 117 | normal! jo;; ret |
| 118 | normal! jostr1" |
| 119 | normal! jostr2" |
| 120 | call assert_equal([' ;; comment', ' ;; comment', ' \ abc', ' \ abc', '', ' ;; ret', ' " str1\', ' str1"', ' " st\b', ' str2"'], getline(2, 11)) |
| 121 | close! |
| 122 | endfunc |
| 123 | |
| 124 | " Test for setting the 'indentexpr' from a modeline |
| 125 | func Test_modeline_indent_expr() |
| 126 | let modeline = &modeline |
| 127 | set modeline |
| 128 | func GetIndent() |
| 129 | return line('.') * 2 |
| 130 | endfunc |
| 131 | call writefile(['# vim: indentexpr=GetIndent()'], 'Xfile.txt') |
| 132 | set modelineexpr |
| 133 | new Xfile.txt |
| 134 | call assert_equal('GetIndent()', &indentexpr) |
| 135 | exe "normal Oa\nb\n" |
| 136 | call assert_equal([' a', ' b'], getline(1, 2)) |
Bram Moolenaar | 95e59a3 | 2020-03-19 20:33:33 +0100 | [diff] [blame] | 137 | |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 138 | set modelineexpr& |
| 139 | delfunc GetIndent |
| 140 | let &modeline = modeline |
| 141 | close! |
Bram Moolenaar | 95e59a3 | 2020-03-19 20:33:33 +0100 | [diff] [blame] | 142 | call delete('Xfile.txt') |
Bram Moolenaar | f5f1e10 | 2020-03-08 05:13:15 +0100 | [diff] [blame] | 143 | endfunc |
| 144 | |
Christian Brabandt | 818ff25 | 2021-11-18 13:56:37 +0000 | [diff] [blame] | 145 | func Test_indent_func_with_gq() |
| 146 | |
| 147 | function GetTeXIndent() |
| 148 | " Sample indent expression for TeX files |
| 149 | let lnum = prevnonblank(v:lnum - 1) |
| 150 | " At the start of the file use zero indent. |
| 151 | if lnum == 0 |
| 152 | return 0 |
| 153 | endif |
| 154 | let line = getline(lnum) |
| 155 | let ind = indent(lnum) |
| 156 | " Add a 'shiftwidth' after beginning of environments. |
| 157 | if line =~ '\\begin{center}' |
| 158 | let ind = ind + shiftwidth() |
| 159 | endif |
| 160 | return ind |
| 161 | endfunction |
| 162 | |
| 163 | new |
| 164 | setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent() |
| 165 | put =[ '\documentclass{article}', '', '\begin{document}', '', |
| 166 | \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non', |
| 167 | \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit', |
| 168 | \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,', |
| 169 | \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.', |
| 170 | \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius', |
| 171 | \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,', |
| 172 | \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '', |
| 173 | \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue', |
| 174 | \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu', |
| 175 | \ 'ipsum congue nisl, quis euismod purus libero in ante. Donec id semper purus.', |
| 176 | \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus', |
| 177 | \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis', |
| 178 | \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl', |
| 179 | \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et', |
| 180 | \ 'ultrices posuere cubilia curae;', '', ''] |
| 181 | 1d_ |
| 182 | call cursor(5, 1) |
| 183 | ka |
| 184 | call cursor(15, 1) |
| 185 | kb |
| 186 | norm! 'agqap |
| 187 | norm! 'bgqap |
| 188 | let expected = [ '\documentclass{article}', '', '\begin{document}', '', |
| 189 | \ 'Lorem ipsum dolor sit amet, consectetur adipiscing', |
| 190 | \ 'elit. Fusce ut enim non libero efficitur aliquet.', |
| 191 | \ 'Maecenas metus justo, facilisis convallis blandit', |
| 192 | \ 'non, semper eu urna. Suspendisse diam diam,', |
| 193 | \ 'iaculis faucibus lorem eu, fringilla condimentum', |
| 194 | \ 'lectus. Quisque euismod diam at convallis', |
| 195 | \ 'vulputate. Pellentesque laoreet tortor sit amet', |
| 196 | \ 'mauris euismod ornare. Sed varius bibendum orci', |
| 197 | \ 'vel vehicula. Pellentesque tempor, ipsum et auctor', |
| 198 | \ 'accumsan, metus lectus ultrices odio, sed', |
| 199 | \ 'elementum mi ante at arcu.', '', '\begin{center}', '', |
| 200 | \ ' Proin nec risus consequat nunc dapibus', |
| 201 | \ ' consectetur. Mauris lacinia est a augue', |
| 202 | \ ' tristique accumsan. Morbi pretium, felis', |
| 203 | \ ' molestie eleifend condimentum, arcu ipsum congue', |
| 204 | \ ' nisl, quis euismod purus libero in ante. Donec', |
| 205 | \ ' id semper purus. Suspendisse eget aliquam nunc.', |
| 206 | \ ' Maecenas fringilla mauris vitae maximus', |
| 207 | \ ' condimentum. Cras a quam in mi dictum eleifend', |
| 208 | \ ' at a lorem. Sed convallis ante a commodo', |
| 209 | \ ' facilisis. Nam suscipit vulputate odio, vel', |
| 210 | \ ' dapibus nisl dignissim facilisis. Vestibulum', |
| 211 | \ ' ante ipsum primis in faucibus orci luctus et', |
| 212 | \ ' ultrices posuere cubilia curae;', '', ''] |
| 213 | call assert_equal(expected, getline(1, '$')) |
| 214 | |
| 215 | bwipe! |
| 216 | delmark ab |
| 217 | delfunction GetTeXIndent |
| 218 | endfu |
| 219 | |
Bram Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 220 | " vim: shiftwidth=2 sts=2 expandtab |