|
| 1 | +# printer.nvim |
| 2 | + |
| 3 | +Neovim plugin that inserts a `console.log()` statement for the identifier under the cursor at a syntactically valid position. Uses tree-sitter to walk up the AST and find the right insertion point — handles cases where the cursor is inside an array, object, JSX, return statement, etc. |
| 4 | + |
| 5 | +Scoped to JavaScript/TypeScript only. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +Single module: `lua/printer.lua`. No configuration system, no setup function — just one exported function `M.add_console_log()`. |
| 10 | + |
| 11 | +**Flow:** |
| 12 | +1. Validate cursor is on an `identifier` or `shorthand_property_identifier_pattern` node |
| 13 | +2. Walk up the AST looking for "restricted" parent nodes (object, array, jsx_element, formal_parameters, etc.) |
| 14 | +3. If inside a restricted node, insert the print statement above or below that node (above for return_statement/parenthesized_expression, below for everything else) |
| 15 | +4. If not restricted, insert below the current line |
| 16 | +5. Auto-indent the inserted line with `==` |
| 17 | + |
| 18 | +**Dependency:** `nvim-treesitter` (uses `nvim-treesitter.ts_utils`) |
| 19 | + |
| 20 | +## Tests |
| 21 | + |
| 22 | +Tests use Plenary (`PlenaryBustedFile`). Each test case is a pair of files in `tests/`: |
| 23 | +- `<name>.js` — input fixture (the buffer content) |
| 24 | +- `<name>.lua` — test spec that positions cursor, calls `add_console_log()`, and asserts buffer content |
| 25 | + |
| 26 | +Run from the `tests/` directory: |
| 27 | +``` |
| 28 | +./run_tests.sh |
| 29 | +``` |
| 30 | + |
| 31 | +**Known issues:** |
| 32 | +- `arrow_function` test is a known failing test (commit `0e24a06`) — cursor on `index` in `[1,2,3].map((n, index) => {})` should insert inside the function body |
| 33 | +- Array/object tests have TODOs for printing the variable on the left side of the declaration (e.g., `arr` in `const arr = [...]`) |
| 34 | + |
| 35 | +## Print statement format |
| 36 | + |
| 37 | +``` |
| 38 | +console.log({ variableName }) |
| 39 | +``` |
| 40 | + |
| 41 | +Uses JS object shorthand so the logged output shows both the name and value. |
0 commit comments