diff --git a/assets/parsers/diff/highlights.scm b/assets/parsers/diff/highlights.scm new file mode 100644 index 0000000..40464db --- /dev/null +++ b/assets/parsers/diff/highlights.scm @@ -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) diff --git a/assets/parsers/diff/tree-sitter-diff.wasm b/assets/parsers/diff/tree-sitter-diff.wasm new file mode 100644 index 0000000..6f2db8f Binary files /dev/null and b/assets/parsers/diff/tree-sitter-diff.wasm differ diff --git a/parsers-config.json b/parsers-config.json index 582659c..ea19f70 100644 --- a/parsers-config.json +++ b/parsers-config.json @@ -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"] + } } ] } diff --git a/src/app/lib/renderAnsi.test.ts b/src/app/lib/renderAnsi.test.ts index c512616..4f50ce5 100644 --- a/src/app/lib/renderAnsi.test.ts +++ b/src/app/lib/renderAnsi.test.ts @@ -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 () => { @@ -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 () => { diff --git a/src/app/parsers.ts b/src/app/parsers.ts index 4bba8fd..b5af038 100644 --- a/src/app/parsers.ts +++ b/src/app/parsers.ts @@ -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']), @@ -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( diff --git a/src/app/styles/syntax-style.ts b/src/app/styles/syntax-style.ts index 16ec98b..0db1ec7 100644 --- a/src/app/styles/syntax-style.ts +++ b/src/app/styles/syntax-style.ts @@ -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({ @@ -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 }, }) diff --git a/test/lang-parsers.md b/test/lang-parsers.md index 3646e07..8d867d1 100644 --- a/test/lang-parsers.md +++ b/test/lang-parsers.md @@ -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