|
| 1 | +# Introduction to formatting behavior |
| 2 | + |
| 3 | +## Basic Statement |
| 4 | + |
| 5 | +The algorithm itself will use more reasonable principles to do basic code layout optimization: |
| 6 | +* All logos/keywords/literal expressions usually keep a space or relative line spacing with the next element |
| 7 | +* Binocular/monocular operators will keep a space with the operand or keep relative line spacing, there is no space between the comma and the previous element and the next element keeps a space |
| 8 | +* Short statement blocks can (but not necessarily) remain on the same line. This principle is valid for all function/do/while/repeat/if statements |
| 9 | +* The algorithm will not automatically interrupt a single-line statement that is too long. The algorithm seldom actively wraps. The algorithm will try to ensure that the line spacing of all elements is the same as before. |
| 10 | +* The algorithm does not remove any visible elements, such as semicolons. Do not format if there is a syntax error |
| 11 | +* The algorithm will not typeset the content of the comment, for example, all the parts of the long comment that are considered to be comments will not increase the indentation |
| 12 | + |
| 13 | +```lua |
| 14 | +local t=123 --local |
| 15 | +aaa,bbbb = 1123, 2342 |
| 16 | +local f =function() end |
| 17 | +local d = { |
| 18 | + -- comment |
| 19 | + aa = 213, --comment |
| 20 | +}; |
| 21 | +local e = a+b+c+d |
| 22 | +function fff(a,b,c) end |
| 23 | +function ff2() |
| 24 | +--[[ |
| 25 | + long comment |
| 26 | +]] |
| 27 | +end |
| 28 | +``` |
| 29 | + |
| 30 | +Will be formatted as: |
| 31 | + |
| 32 | +```lua |
| 33 | +local t = 123 --local |
| 34 | +aaa, bbbb = 1123, 2342 |
| 35 | +local f = function() end |
| 36 | +local d = { |
| 37 | + -- comment |
| 38 | + aa = 213, --comment |
| 39 | +}; |
| 40 | +local e = a + b + c + d |
| 41 | +function fff(a, b, c) end |
| 42 | + |
| 43 | +function ff2() |
| 44 | + --[[ |
| 45 | + long comment |
| 46 | +]] |
| 47 | +end |
| 48 | +``` |
| 49 | + |
| 50 | +## Continuous assignment statement |
| 51 | + |
| 52 | +For consecutive assignments or local statements, when the algorithm recognizes that the code tries to align to the equal sign, it will typeset in the manner of aligning to the equal sign. The basic principle of identifying alignment is: |
| 53 | + |
| 54 | +* For consecutive assignment/local/comment statements, when the equal sign of the first assignment/local statement is more than 1 space away from the element to its left, the consecutive assignment/local statement will be aligned to the equal sign. |
| 55 | +* The definition of continuous alignment here is: no more than 2 lines between statements |
| 56 | +* Alignment will be aligned to the furthest equal sign |
| 57 | + |
| 58 | +```lua |
| 59 | +local ttt = 123 --first |
| 60 | +cd = 345 --second |
| 61 | +``` |
| 62 | + |
| 63 | +格式化后: |
| 64 | + |
| 65 | +```lua |
| 66 | +local ttt = 123 --first |
| 67 | +cd = 345 --second |
| 68 | +``` |
| 69 | + |
| 70 | +## Table layout |
| 71 | + |
| 72 | +For common table expressions, the default formatting method is: |
| 73 | +* The field in the table will be spaced a blank from the braces of the table or keep relative line spacing |
| 74 | +* Different fields in the table will keep their relative positions unchanged |
| 75 | +* If all the fields are in different rows and are in the form of key = value, when different fields try to align to the equal sign, the algorithm will align to the equal sign |
| 76 | +* The basic principle of trying to align to the equal sign is key = value/comment field. When the distance between the equal sign of the first line of field and the element to its left is greater than 1 space, the key = value field in the table will be aligned to the equal sign |
| 77 | + |
| 78 | +```lua |
| 79 | +local t = {1,2,3} |
| 80 | +local c = { |
| 81 | + aaa, bbb, eee |
| 82 | +} |
| 83 | +local d = { |
| 84 | + aa =123, |
| 85 | + bbbb = 4353, |
| 86 | + eee = 131231, |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +格式化后: |
| 91 | + |
| 92 | +```lua |
| 93 | +local t = { 1, 2, 3 } |
| 94 | +local c = { |
| 95 | + aaa, bbb, eee |
| 96 | +} |
| 97 | +local d = { |
| 98 | + aa = 123, |
| 99 | + bbbb = 4353, |
| 100 | + eee = 131231, |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Long expression list |
| 105 | + |
| 106 | +If the long expression list and the long expression have more than one line, except for the first line, the remaining lines will be indented by one continuation line: |
| 107 | + |
| 108 | +For example: |
| 109 | + |
| 110 | +```lua |
| 111 | +local aaa = 13131, bbbb, |
| 112 | + ccc, 123131, ddddd, |
| 113 | + eeee, fff |
| 114 | + |
| 115 | +return aaa, bbb, dddd |
| 116 | + or cccc |
| 117 | + |
| 118 | +if aaa and bbb |
| 119 | + or ccc() then |
| 120 | + |
| 121 | +end |
| 122 | + |
| 123 | +``` |
| 124 | + |
| 125 | +## Statements containing statement blocks |
| 126 | + |
| 127 | +For if statement, repeat statement, while statement, do statement, for statement and function definition statement they all have the following formatting characteristics: |
| 128 | + |
| 129 | +* When the statement containing the statement block is only one line, the algorithm will keep them in one line |
| 130 | +* For statements that usually contain statement blocks, there will be one more indentation in the statement block |
| 131 | +* The statement containing the statement block and the next statement are usually separated by at least 1 line |
| 132 | + |
| 133 | +For example: |
| 134 | + |
| 135 | +```lua |
| 136 | +do return end |
| 137 | +function f(x,y) return x<y end |
| 138 | +function fff() |
| 139 | +local t =123 |
| 140 | +end |
| 141 | +``` |
| 142 | + |
| 143 | +Will be formatted as: |
| 144 | + |
| 145 | +```lua |
| 146 | +do return end |
| 147 | + |
| 148 | +function f(x, y) return x < y end |
| 149 | + |
| 150 | +function fff() |
| 151 | + local t = 123 |
| 152 | +end |
| 153 | + |
| 154 | +``` |
| 155 | + |
| 156 | +## Function definition parameters and function call parameters |
| 157 | + |
| 158 | +The definition parameters of function definition statements have the following principles: |
| 159 | +* By default, if the definition parameter of the function wraps, it will be aligned to the first parameter after the wrap |
| 160 | + |
| 161 | +For example: |
| 162 | + |
| 163 | +```lua |
| 164 | +function ffff(a, b, c, |
| 165 | + callback) |
| 166 | +end |
| 167 | +``` |
| 168 | + |
| 169 | +After formatting: |
| 170 | + |
| 171 | +```lua |
| 172 | +function ffff(a, b, c, |
| 173 | + callback) |
| 174 | +end |
| 175 | + |
| 176 | +``` |
| 177 | + |
| 178 | +The parameters of function call have the following principles: |
| 179 | +* If the function has multiple parameters, if the parameter list itself changes lines, except for the first line, the remaining lines will be indented by one more continuation line |
| 180 | +* If the function call is a single parameter without parentheses, there will be a space between the function identifier and the parameter |
| 181 | + |
| 182 | +For example: |
| 183 | + |
| 184 | +```lua |
| 185 | +print(aaa, bbbb, eeee, ffff, |
| 186 | +ggggg, hhhhh, |
| 187 | +lllll) |
| 188 | +require "code_format" |
| 189 | +``` |
| 190 | +After formatting: |
| 191 | + |
| 192 | +```lua |
| 193 | +print(aaa, bbbb, eeee, ffff, |
| 194 | + ggggg, hhhhh, |
| 195 | + lllll) |
| 196 | +require "code_format" |
| 197 | +``` |
| 198 | + |
| 199 | +## Comment statements and inline comments |
| 200 | + |
| 201 | +Comment statements are divided into long comments and single-line short comments and inline comments |
| 202 | +* When the comment statement is formatted, the short comment will not make any changes, keeping the same line spacing as the next statement, while the large blank text of the long comment is considered part of the comment, so no changes will be made, and the same Same line spacing in the next statement |
| 203 | +* Inline comments, using the principle of keeping a space with the previous text, if the next element is still on the same line, it will also keep a space |
| 204 | + |
| 205 | +```lua |
| 206 | +--[[ |
| 207 | + |
| 208 | +]] |
| 209 | + |
| 210 | +---@param f number |
| 211 | +---@return number |
| 212 | +function ffff(f) |
| 213 | +end |
| 214 | + |
| 215 | +local t = 123 --fffff |
| 216 | +local c = { |
| 217 | + aaa = 123, --[[ffff]] dddd, |
| 218 | + ccc = 456 |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +格式化后: |
| 223 | + |
| 224 | +```lua |
| 225 | +--[[ |
| 226 | + |
| 227 | +]] |
| 228 | + |
| 229 | +---@param f number |
| 230 | +---@return number |
| 231 | +function ffff(f) |
| 232 | +end |
| 233 | + |
| 234 | +local t = 123 --fffff |
| 235 | +local c = { |
| 236 | + aaa = 123, --[[ffff]] dddd, |
| 237 | + ccc = 456 |
| 238 | +} |
| 239 | +``` |
| 240 | + |
0 commit comments