( lines: readonly string[], cursor: CursorPosition, tabSize: number = DEFAULT_TAB_SIZE, )
| 207 | * @returns Indent string to prepend to new line |
| 208 | */ |
| 209 | export function computeAutoIndent( |
| 210 | lines: readonly string[], |
| 211 | cursor: CursorPosition, |
| 212 | tabSize: number = DEFAULT_TAB_SIZE, |
| 213 | ): string { |
| 214 | const currentLine = lines[cursor.line] ?? ""; |
| 215 | |
| 216 | // Match leading whitespace of current line |
| 217 | const match = currentLine.match(/^(\s*)/); |
| 218 | let indent = match ? (match[1] ?? "") : ""; |
| 219 | |
| 220 | // Check if line ends with opening brace/bracket |
| 221 | const trimmedBeforeCursor = currentLine.slice(0, cursor.column).trimEnd(); |
| 222 | if ( |
| 223 | trimmedBeforeCursor.endsWith("{") || |
| 224 | trimmedBeforeCursor.endsWith("[") || |
| 225 | trimmedBeforeCursor.endsWith("(") || |
| 226 | trimmedBeforeCursor.endsWith(":") |
| 227 | ) { |
| 228 | indent += " ".repeat(tabSize); |
| 229 | } |
| 230 | |
| 231 | return indent; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Indent lines in a range. |
no outgoing calls
no test coverage detected