-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(hooks): add table-formatter hook for proper column alignment #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
- Add experimental.text.complete hook to format markdown tables - Use string-width for Unicode-aware column width calculation - Handle CJK characters, emoji, and alignment markers correctly - Register hook in disabled_hooks config schema
|
All contributors have signed the CLA. Thank you! ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
Greptile SummaryThis PR adds a Key changes:
Implementation quality:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant LLM as LLM Response
participant Plugin as OhMyOpenCode Plugin
participant Hook as TableFormatterHook
participant Parser as Parser
participant Formatter as Formatter
participant TUI as OpenCode TUI
LLM->>Plugin: Generate markdown table text
Plugin->>Hook: experimental.text.complete(output)
Hook->>Parser: parseMarkdownTables(text)
Parser->>Parser: Identify table rows with TABLE_ROW_REGEX
Parser->>Parser: Find separator rows (---:)
Parser->>Parser: Extract headers, alignments, rows
Parser->>Parser: Calculate startIndex/endIndex
Parser-->>Hook: ParsedTable[]
alt Tables found
Hook->>Formatter: formatTablesInText(text, tables)
loop For each table (reverse order)
Formatter->>Formatter: Calculate column widths with stringWidth()
Formatter->>Formatter: Pad cells (left/center/right alignment)
Formatter->>Formatter: Create separator with alignment markers
Formatter->>Formatter: Replace original table with formatted
end
Formatter-->>Hook: Formatted text
Hook->>Plugin: Mutate output.text
else No tables
Hook->>Plugin: Return unchanged
end
Plugin->>TUI: Display formatted table
TUI->>TUI: Render with proper CJK/emoji alignment
|
Greptile found no issues!From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section. This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 issues found across 14 files
Confidence score: 3/5
- Missing try/catch in
src/hooks/table-formatter/index.tsleaves parsing and formatting errors uncaught, so malformed markdown can crash the hook instead of failing gracefully. src/hooks/table-formatter/formatter.tsonly maps existing cells, so rows with fewer cells than headers will misalign columns despite width computation accounting for all columns.- Pay close attention to
src/hooks/table-formatter/index.ts,src/hooks/table-formatter/formatter.ts- add error handling and ensure rows pad out to header width to avoid crashes and misaligned output.
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="src/hooks/table-formatter/formatter.test.ts">
<violation number="1" location="src/hooks/table-formatter/formatter.test.ts:88">
P2: Test only verifies separator row format but doesn't check that data rows are actually centered. Unlike the right alignment test which verifies data row alignment (`| 100 |`), this test doesn't verify that 'OK' and 'ERROR' are centered in the output. Consider adding assertions for data row centering.</violation>
</file>
<file name="src/hooks/table-formatter/index.test.ts">
<violation number="1" location="src/hooks/table-formatter/index.test.ts:66">
P2: Test assertions don't verify formatting occurred. These assertions check for strings that already exist unchanged in the input, so they would pass even if the hook did nothing. Consider asserting on padded values (e.g., in test 4, check that `| KR |` appears since `KR` should be padded to width 3 to match `KR1`/`KR5`).</violation>
</file>
<file name="src/hooks/table-formatter/formatter.ts">
<violation number="1" location="src/hooks/table-formatter/formatter.ts:74">
P2: Row formatting uses `row.map()` which only iterates over actual cells in the row. If a row has fewer cells than headers, the output will have misaligned columns. The width calculation already handles this with `row[col] ?? ""`, but the formatting should iterate over `columnCount` instead.</violation>
</file>
<file name="src/hooks/table-formatter/parser.test.ts">
<violation number="1" location="src/hooks/table-formatter/parser.test.ts:134">
P2: Incomplete test coverage: the second row with the ZWJ emoji (`👨👩👧`) is not verified. ZWJ emoji sequences are more complex than simple emojis and are a common edge case for Unicode width calculation. Consider adding an assertion for `rows[1]` to ensure the parser handles ZWJ emojis correctly.</violation>
</file>
<file name="src/hooks/table-formatter/index.ts">
<violation number="1" location="src/hooks/table-formatter/index.ts:8">
P1: Missing try/catch wrapper around parsing/formatting operations. According to the hooks AGENTS.md anti-patterns, hooks should have error handling to avoid crashing the session. The `parseMarkdownTables` and `formatTablesInText` calls could throw on malformed input.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
- Add try/catch to prevent session crash on malformed input (P1) - Fix row.map() to iterate over columnCount for proper alignment when rows have fewer cells than headers (P2)
- Add center alignment data row verification (formatter.test.ts) - Verify actual padding in multiple tables test (index.test.ts) - Verify padded CJK and emoji handling (index.test.ts) - Add ZWJ emoji row verification (parser.test.ts) - Add test for rows with fewer cells than headers
Summary
table-formatterhook to fix markdown table column misalignment in OpenCode TUIstring-widthlibrary for Unicode-aware column width calculation (CJK, emoji support)experimental.text.completeto format tables after LLM response, before renderingProblems
LLM-generated markdown tables display with misaligned columns in OpenCode TUI:
(I captured below images during work, so some columns are skipped due to security issue)
Changes
src/hooks/table-formatter/with parser, formatter, and testsstring-widthdependency for Unicode width calculationsrc/index.tsand config schema:) correctly in separator rowsAddressed Review Comments (c936856, 291adaf)
columnCountinstead ofrow.map()for proper alignment when rows have fewer cells than headers (P2)Known Issues
Testing
bun run typecheckpassesbun run buildsucceedsChecklist
bun run typecheckpassesbun run buildsucceedspackage.json