Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions assets/parsers/diff/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
(comment) @comment @spell

[
(addition)
(new_file)
] @diff.plus

[
(deletion)
(old_file)
] @diff.minus

(change) @diff.delta

(commit) @constant

(location) @attribute

(command
"diff" @function
(argument) @variable.parameter)

(filename) @string.special.path

(special) @string.special

"\\" @punctuation.special

(mode) @number

; Line markers (+ - < > !) carry no capture of their own, so they inherit the
; line's diff.plus/diff.minus color. OpenTUI ignores `priority` and would let a
; punctuation capture override the line color.
".." @punctuation.special

[
(binary_change)
(similarity)
(dissimilarity)
(file_change)
] @label

(index
"index" @keyword)

(similarity
(score) @number
"%" @number)

(dissimilarity
(score) @number
"%" @number)

(binary_patch
[
"GIT"
"binary"
"patch"
] @label)

(binary_hunk
[
"literal"
"delta"
] @keyword
(size) @number)

forward: (binary_hunk
(payload) @diff.plus)

reverse: (binary_hunk
(payload) @diff.minus)
Binary file added assets/parsers/diff/tree-sitter-diff.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions parsers-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
"https://raw.githubusercontent.com/nvim-treesitter/nvim-treesitter/master/queries/css/highlights.scm"
]
}
},
{
"filetype": "diff",
"aliases": ["patch"],
"wasm": "https://github.com/tree-sitter-grammars/tree-sitter-diff/releases/download/v0.2.0/tree-sitter-diff.wasm",
"queries": {
"highlights": ["./assets/parsers/diff/highlights.scm"]
}
}
]
}
15 changes: 15 additions & 0 deletions src/app/lib/renderAnsi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { dlopen, suffix } from 'bun:ffi'
import { getTreeSitterClient } from '@opentui/core'
import { buildTree } from './ast'
import { renderAnsi } from './renderAnsi'
import { theme } from '../styles/theme'

const stripAnsi = (s: string): string => s.replace(/\x1b\[[0-9;]*m/g, '')
const fg = (hex: string): string => {
const [r, g, b] = [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16))
return `38;2;${r};${g};${b}m`
}

describe('renderAnsi', () => {
test('renders heading and paragraph text', async () => {
Expand Down Expand Up @@ -77,6 +82,16 @@ describe('renderAnsi', () => {
expect(escCount(highlighted)).toBeGreaterThan(escCount(plain))
})

test.each(['diff', 'patch'])('colors %s fence lines, markers included', async lang => {
const out = await renderAnsi({
nodes: buildTree(`\`\`\`${lang}\n@@ -1 +1 @@\n-old\n+new\n\`\`\`\n`).nodes,
width: 80,
maxHeight: 200,
})
expect(out).toContain(`${fg(theme.red)}-old`)
expect(out).toContain(`${fg(theme.green)}+new`)
})

test.skipIf(process.platform === 'win32')(
'leaves stdout blocking after render (OpenTUI sets O_NONBLOCK on fd 1)',
async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/app/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import htmlWasm from '../../assets/parsers/html/tree-sitter-html.wasm' with { ty
import htmlHighlights from '../../assets/parsers/html/highlights.scm' with { type: 'file' }
import cssWasm from '../../assets/parsers/css/tree-sitter-css.wasm' with { type: 'file' }
import cssHighlights from '../../assets/parsers/css/highlights.scm' with { type: 'file' }
import diffWasm from '../../assets/parsers/diff/tree-sitter-diff.wasm' with { type: 'file' }
import diffHighlights from '../../assets/parsers/diff/highlights.scm' with { type: 'file' }

export const extraParsers: FiletypeParserOptions[] = [
parser('bash', bashWasm, bashHighlights, ['sh', 'shell', 'zsh']),
Expand All @@ -31,6 +33,7 @@ export const extraParsers: FiletypeParserOptions[] = [
parser('toml', tomlWasm, tomlHighlights),
parser('html', htmlWasm, htmlHighlights, ['htm']),
parser('css', cssWasm, cssHighlights),
parser('diff', diffWasm, diffHighlights, ['patch']),
]

function parser(
Expand Down
4 changes: 4 additions & 0 deletions src/app/styles/syntax-style.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SyntaxStyle } from '@opentui/core'
import { theme } from './theme'

// VS Code Dark+ inspired token colors keyed by tree-sitter scope names.
export const syntaxStyle = SyntaxStyle.fromStyles({
Expand Down Expand Up @@ -30,4 +31,7 @@ export const syntaxStyle = SyntaxStyle.fromStyles({
attribute: { fg: '#9cdcfe' },
escape: { fg: '#d7ba7d' },
label: { fg: '#c8c8c8' },
'diff.plus': { fg: theme.green },
'diff.minus': { fg: theme.red },
'diff.delta': { fg: theme.yellow },
})
14 changes: 14 additions & 0 deletions test/lang-parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ opentui = "*"
}
```

## Diff

```diff
diff --git a/src/greet.ts b/src/greet.ts
index 3350e91..a9f836e 100644
--- a/src/greet.ts
+++ b/src/greet.ts
@@ -1,3 +1,3 @@ export function greet()
export function greet(name: string) {
- return 'Hello, ' + name
+ return `Hello, ${name}!`
}
```

## Zig

```zig
Expand Down
Loading