MCPcopy Create free account
hub / github.com/WJX20/claude-code / highlightLine

Function highlightLine

packages/color-diff-napi/src/index.ts:511–545  ·  view source on GitHub ↗
(
  state: { lang: string | null; stack: unknown },
  line: string,
  theme: Theme,
)

Source from the content-addressed store, hash-verified

509let loggedEmitterShapeError = false
510
511function highlightLine(
512 state: { lang: string | null; stack: unknown },
513 line: string,
514 theme: Theme,
515): Block[] {
516 // syntect-parity: feed a trailing \n so line comments terminate, then strip
517 const code = line + '\n'
518 if (!state.lang) {
519 return [[defaultStyle(theme), code]]
520 }
521 let result
522 try {
523 result = hljs().highlight(code, {
524 language: state.lang,
525 ignoreIllegals: true,
526 })
527 } catch {
528 // hljs throws on unknown language despite ignoreIllegals
529 return [[defaultStyle(theme), code]]
530 }
531 if (!hasRootNode(result.emitter)) {
532 if (!loggedEmitterShapeError) {
533 loggedEmitterShapeError = true
534 logError(
535 new Error(
536 `color-diff: hljs emitter shape mismatch (keys: ${Object.keys(result.emitter).join(',')}). Syntax highlighting disabled.`,
537 ),
538 )
539 }
540 return [[defaultStyle(theme), code]]
541 }
542 const blocks: Block[] = []
543 flattenHljs(result.emitter.rootNode, theme, undefined, blocks)
544 return blocks
545}
546
547// ---------------------------------------------------------------------------
548// Word diff

Callers 2

renderMethod · 0.85
renderMethod · 0.85

Calls 6

hljsFunction · 0.85
hasRootNodeFunction · 0.85
flattenHljsFunction · 0.85
keysMethod · 0.80
defaultStyleFunction · 0.70
logErrorFunction · 0.70

Tested by

no test coverage detected