File size: 6,198 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
import { useMemo } from 'react'
import { HotlinkedText } from '../hot-linked-text'
import { getFrameSource, type StackFrame } from '../../../shared/stack-frame'
import { useOpenInEditor } from '../../utils/use-open-in-editor'
import { ExternalIcon } from '../../icons/external'
import { FileIcon } from '../../icons/file'
import {
formatCodeFrame,
groupCodeFrameLines,
parseLineNumberFromCodeFrameLine,
} from './parse-code-frame'
export type CodeFrameProps = {
stackFrame: StackFrame
codeFrame: string
}
export function CodeFrame({ stackFrame, codeFrame }: CodeFrameProps) {
const parsedLineStates = useMemo(() => {
const decodedLines = groupCodeFrameLines(formatCodeFrame(codeFrame))
return decodedLines.map((line) => {
return {
line,
parsedLine: parseLineNumberFromCodeFrameLine(line, stackFrame),
}
})
}, [codeFrame, stackFrame])
const open = useOpenInEditor({
file: stackFrame.file,
line1: stackFrame.line1 ?? 1,
column1: stackFrame.column1 ?? 1,
})
const fileExtension = stackFrame?.file?.split('.').pop()
// TODO: make the caret absolute
return (
<div data-nextjs-codeframe>
<div className="code-frame-header">
{/* TODO: This is <div> in `Terminal` component.
Changing now will require multiple test snapshots updates.
Leaving as <div> as is trivial and does not affect the UI.
Change when the new redbox matcher `toDisplayRedbox` is used.
*/}
<p className="code-frame-link">
<span className="code-frame-icon">
<FileIcon lang={fileExtension} />
</span>
<span data-text>
{getFrameSource(stackFrame)} @{' '}
<HotlinkedText text={stackFrame.methodName} />
</span>
<button
aria-label="Open in editor"
data-with-open-in-editor-link-source-file
onClick={open}
>
<span className="code-frame-icon" data-icon="right">
<ExternalIcon width={16} height={16} />
</span>
</button>
</p>
</div>
<pre className="code-frame-pre">
<div className="code-frame-lines">
{parsedLineStates.map(({ line, parsedLine }, lineIndex) => {
const { lineNumber, isErroredLine } = parsedLine
const lineNumberProps: Record<string, string | boolean> = {}
if (lineNumber) {
lineNumberProps['data-nextjs-codeframe-line'] = lineNumber
}
if (isErroredLine) {
lineNumberProps['data-nextjs-codeframe-line--errored'] = true
}
return (
<div key={`line-${lineIndex}`} {...lineNumberProps}>
{line.map((entry, entryIndex) => (
<span
key={`frame-${entryIndex}`}
style={{
color: entry.fg ? `var(--color-${entry.fg})` : undefined,
...(entry.decoration === 'bold'
? // TODO(jiwon): This used to be 800, but the symbols like `─┬─` are
// having longer width than expected on Geist Mono font-weight
// above 600, hence a temporary fix is to use 500 for bold.
{ fontWeight: 500 }
: entry.decoration === 'italic'
? { fontStyle: 'italic' }
: undefined),
}}
>
{entry.content}
</span>
))}
</div>
)
})}
</div>
</pre>
</div>
)
}
export const CODE_FRAME_STYLES = `
[data-nextjs-codeframe] {
--code-frame-padding: 12px;
--code-frame-line-height: var(--size-16);
background-color: var(--color-background-200);
color: var(--color-gray-1000);
text-overflow: ellipsis;
border: 1px solid var(--color-gray-400);
border-radius: 8px;
font-family: var(--font-stack-monospace);
font-size: var(--size-12);
line-height: var(--code-frame-line-height);
margin: 8px 0;
svg {
width: var(--size-16);
height: var(--size-16);
}
}
.code-frame-link,
.code-frame-pre {
padding: var(--code-frame-padding);
}
.code-frame-link svg {
flex-shrink: 0;
}
.code-frame-lines {
min-width: max-content;
}
.code-frame-link [data-text] {
display: inline-flex;
text-align: left;
margin: auto 6px;
}
.code-frame-header {
width: 100%;
transition: background 100ms ease-out;
border-radius: 8px 8px 0 0;
border-bottom: 1px solid var(--color-gray-400);
}
[data-with-open-in-editor-link-source-file] {
padding: 4px;
margin: -4px 0 -4px auto;
border-radius: var(--rounded-full);
margin-left: auto;
&:focus-visible {
outline: var(--focus-ring);
outline-offset: -2px;
}
&:hover {
background: var(--color-gray-100);
}
}
[data-nextjs-codeframe]::selection,
[data-nextjs-codeframe] *::selection {
background-color: var(--color-ansi-selection);
}
[data-nextjs-codeframe] *:not(a) {
color: inherit;
background-color: transparent;
font-family: var(--font-stack-monospace);
}
[data-nextjs-codeframe-line][data-nextjs-codeframe-line--errored="true"] {
position: relative;
isolation: isolate;
> span {
position: relative;
z-index: 1;
}
&::after {
content: "";
width: calc(100% + var(--code-frame-padding) * 2);
height: var(--code-frame-line-height);
left: calc(-1 * var(--code-frame-padding));
background: var(--color-red-200);
box-shadow: 2px 0 0 0 var(--color-red-900) inset;
position: absolute;
}
}
[data-nextjs-codeframe] > * {
margin: 0;
}
.code-frame-link {
display: flex;
margin: 0;
outline: 0;
}
.code-frame-link [data-icon='right'] {
margin-left: auto;
}
[data-nextjs-codeframe] div > pre {
overflow: hidden;
display: inline-block;
}
[data-nextjs-codeframe] svg {
color: var(--color-gray-900);
}
`
|