@@ -188,11 +188,6 @@ fun DiffView(diff: String, modifier: Modifier = Modifier, saveKey: String? = nul
188188 // `@@` range text (rare but possible) are still distinct toggles. The KDoc promised this
189189 // affordance; it's now implemented.
190190 val collapsedHunks = remember { mutableStateOf(emptySet<Int>()) }
191− // Resolve a syntax for highlighting from the first file header (if any). Memoized so a
192− // streaming re-parse doesn't re-resolve per token.
193− val syntax = remember(lines) {
194− lines.firstOrNull { it is DiffLine.FileHeader }?.let { syntaxForHeader(it.text) }
195− }
196191 val palette = rememberHighlightPalette()
197192
198193 Column(
@@ -229,7 +224,7 @@ fun DiffView(diff: String, modifier: Modifier = Modifier, saveKey: String? = nul
229224 )
230225 }
231226 Column(modifier = Modifier.horizontalScroll(hScrollState)) {
232− rows.forEach { row -> RenderDiffRow(row, syntax, palette, collapsedHunks) }
227+ rows.forEach { row -> RenderDiffRow(row, palette, collapsedHunks) }
233228 }
234229 DiffExpandFooter(lines = lines, expanded = expanded, onToggle = { expanded = !expanded })
235230 }
@@ -255,9 +250,6 @@ fun LazyDiffView(diff: String, modifier: Modifier = Modifier, saveKey: String? =
255250 val hScrollState = rememberScrollState()
256251 var expanded by rememberSaveable(saveKey ?: diff) { mutableStateOf(false) }
257252 val collapsedHunks = remember { mutableStateOf(emptySet<Int>()) }
258− val syntax = remember(lines) {
259− lines.firstOrNull { it is DiffLine.FileHeader }?.let { syntaxForHeader(it.text) }
260− }
261253 val palette = rememberHighlightPalette()
262254 val lazyListState = androidx.compose.foundation.lazy.rememberLazyListState()
263255
@@ -298,7 +290,7 @@ fun LazyDiffView(diff: String, modifier: Modifier = Modifier, saveKey: String? =
298290 rows,
299291 key = { i, row -> "${row.kind::class.simpleName}:$i:${row.text.hashCode()}" },
300292 ) { _, row ->
301− RenderDiffRow(row, syntax, palette, collapsedHunks)
293+ RenderDiffRow(row, palette, collapsedHunks)
302294 }
303295 }
304296 DiffExpandFooter(lines = lines, expanded = expanded, onToggle = { expanded = !expanded })
@@ -406,6 +398,10 @@ private data class DiffRowItem(
406398 val oldLine: Int?,
407399 val newLine: Int?,
408400 val hunkIndex: Int?,
401+ // The syntax resolved from the file header this row belongs to. A multi-file diff switches
402+ // language at each FileHeader; carrying syntax per row (instead of one for the whole diff)
403+ // ensures a later file's hunk is highlighted with its own language, not the first file's.
404+ val syntax: FileSyntax?,
409405)
410406
411407/** Walk [visibleLines] advancing the old/new line counters per hunk, producing a list of
@@ -426,6 +422,9 @@ private fun buildDiffRows(
426422 var oldLine = 0
427423 var newLine = 0
428424 var hunkIndex = -1
425+ // Track the current file's syntax as we walk the list, re-resolving at each FileHeader so
426+ // a multi-file diff highlights each hunk with its own language instead of the first file's.
427+ var currentSyntax: FileSyntax? = null
429428 val hunkRegex = Regex("""@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@""")
430429 return visibleLines.map { line ->
431430 when (line) {
@@ -436,21 +435,24 @@ private fun buildDiffRows(
436435 oldLine = m.groupValues[1].toInt()
437436 newLine = m.groupValues[2].toInt()
438437 }
439− DiffRowItem(line, line.text, Color.Transparent, tertiary, null, null, hunkIndex)
438+ DiffRowItem(line, line.text, Color.Transparent, tertiary, null, null, hunkIndex, currentSyntax)
439+ }
440+ is DiffLine.FileHeader -> {
441+ currentSyntax = syntaxForHeader(line.text)
442+ DiffRowItem(line, line.text, Color.Transparent, onSurface, null, null, null, currentSyntax)
440443 }
441− is DiffLine.FileHeader -> DiffRowItem(line, line.text, Color.Transparent, onSurface, null, null, null)
442− is DiffLine.Meta -> DiffRowItem(line, line.text, Color.Transparent, onSurfaceVariant, null, null, null)
444+ is DiffLine.Meta -> DiffRowItem(line, line.text, Color.Transparent, onSurfaceVariant, null, null, null, currentSyntax)
443445 is DiffLine.Add -> {
444446 val n = newLine++
445− DiffRowItem(line, line.text, addColor, addText, null, n, hunkIndex)
447+ DiffRowItem(line, line.text, addColor, addText, null, n, hunkIndex, currentSyntax)
446448 }
447449 is DiffLine.Remove -> {
448450 val o = oldLine++
449− DiffRowItem(line, line.text, removeColor, removeText, o, null, hunkIndex)
451+ DiffRowItem(line, line.text, removeColor, removeText, o, null, hunkIndex, currentSyntax)
450452 }
451453 is DiffLine.Context -> {
452454 val o = oldLine++; val n = newLine++
453− DiffRowItem(line, line.text, Color.Transparent, onSurface, o, n, hunkIndex)
455+ DiffRowItem(line, line.text, Color.Transparent, onSurface, o, n, hunkIndex, currentSyntax)
454456 }
455457 }
456458 }
@@ -462,7 +464,6 @@ private fun buildDiffRows(
462464@Composable
463465private fun RenderDiffRow(
464466 row: DiffRowItem,
465− syntax: FileSyntax?,
466467 palette: HighlightPalette,
467468 collapsedHunks: androidx.compose.runtime.MutableState<Set<Int>>,
468469) {
@@ -528,9 +529,9 @@ private fun RenderDiffRow(
528529 color = MaterialTheme.colorScheme.onSurfaceVariant,
529530 modifier = Modifier.padding(vertical = 1.dp),
530531 )
531− is DiffLine.Add -> DiffRow(row.text, "+", row.bg, row.textColor, oldLine = row.oldLine, newLine = row.newLine, syntax = syntax, palette = palette)
532− is DiffLine.Remove -> DiffRow(row.text, "-", row.bg, row.textColor, oldLine = row.oldLine, newLine = row.newLine, syntax = syntax, palette = palette)
533− is DiffLine.Context -> DiffRow(row.text, " ", row.bg, row.textColor, oldLine = row.oldLine, newLine = row.newLine, syntax = syntax, palette = palette)
532+ is DiffLine.Add -> DiffRow(row.text, "+", row.bg, row.textColor, oldLine = row.oldLine, newLine = row.newLine, syntax = row.syntax, palette = palette)
533+ is DiffLine.Remove -> DiffRow(row.text, "-", row.bg, row.textColor, oldLine = row.oldLine, newLine = row.newLine, syntax = row.syntax, palette = palette)
534+ is DiffLine.Context -> DiffRow(row.text, " ", row.bg, row.textColor, oldLine = row.oldLine, newLine = row.newLine, syntax = row.syntax, palette = palette)
534535 }
535536}
536537