forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.tsx
149 lines (133 loc) · 4.67 KB
/
context.tsx
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { size } from 'lodash';
import { tint } from 'polished';
import React from 'react';
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
import javascript from 'react-syntax-highlighter/dist/cjs/languages/hljs/javascript';
import python from 'react-syntax-highlighter/dist/cjs/languages/hljs/python';
import ruby from 'react-syntax-highlighter/dist/cjs/languages/hljs/ruby';
import xcode from 'react-syntax-highlighter/dist/cjs/styles/hljs/xcode';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import { StackframeWithLineContext } from '../../../../typings/es_schemas/raw/fields/stackframe';
SyntaxHighlighter.registerLanguage('javascript', javascript);
SyntaxHighlighter.registerLanguage('python', python);
SyntaxHighlighter.registerLanguage('ruby', ruby);
const ContextContainer = euiStyled.div`
position: relative;
border-radius: ${({ theme }) => theme.eui.euiBorderRadiusSmall};
`;
const LINE_HEIGHT = 18;
const LineHighlight = euiStyled.div<{ lineNumber: number }>`
position: absolute;
width: 100%;
height: ${LINE_HEIGHT}px;
top: ${(props) => props.lineNumber * LINE_HEIGHT}px;
pointer-events: none;
background-color: ${({ theme }) => tint(0.9, theme.eui.euiColorWarning)};
`;
const LineNumberContainer = euiStyled.div<{ isLibraryFrame: boolean }>`
position: absolute;
top: 0;
left: 0;
border-radius: ${({ theme }) => theme.eui.euiBorderRadiusSmall};
background: ${({ isLibraryFrame, theme }) =>
isLibraryFrame
? theme.eui.euiColorEmptyShade
: theme.eui.euiColorLightestShade};
`;
const LineNumber = euiStyled.div<{ highlight: boolean }>`
position: relative;
min-width: 42px;
padding-left: ${({ theme }) => theme.eui.euiSizeS};
padding-right: ${({ theme }) => theme.eui.euiSizeXS};
color: ${({ theme }) => theme.eui.euiColorMediumShade};
line-height: ${LINE_HEIGHT}px;
text-align: right;
border-right: 1px solid ${({ theme }) => theme.eui.euiColorLightShade};
background-color: ${({ highlight, theme }) =>
highlight ? tint(0.9, theme.eui.euiColorWarning) : null};
&:last-of-type {
border-radius: 0 0 0 ${({ theme }) => theme.eui.euiBorderRadiusSmall};
}
`;
const LineContainer = euiStyled.div`
overflow: auto;
margin: 0 0 0 42px;
padding: 0;
background-color: ${({ theme }) => theme.eui.euiColorEmptyShade};
&:last-of-type {
border-radius: 0 0 ${({ theme }) => theme.eui.euiBorderRadiusSmall} 0;
}
`;
const Line = euiStyled.pre`
// Override all styles
margin: 0;
color: inherit;
background: inherit;
border: 0;
border-radius: 0;
overflow: initial;
padding: 0 ${LINE_HEIGHT}px;
line-height: ${LINE_HEIGHT}px;
`;
const Code = euiStyled.code`
position: relative;
padding: 0;
margin: 0;
white-space: pre;
z-index: 2;
`;
function getStackframeLines(stackframe: StackframeWithLineContext) {
const line = stackframe.line.context;
const preLines = stackframe.context?.pre || [];
const postLines = stackframe.context?.post || [];
return [...preLines, line, ...postLines].map(
(x) => (x.endsWith('\n') ? x.slice(0, -1) : x) || ' '
);
}
function getStartLineNumber(stackframe: StackframeWithLineContext) {
const preLines = size(stackframe.context?.pre || []);
return stackframe.line.number - preLines;
}
interface Props {
stackframe: StackframeWithLineContext;
codeLanguage?: string;
isLibraryFrame: boolean;
}
export function Context({ stackframe, codeLanguage, isLibraryFrame }: Props) {
const lines = getStackframeLines(stackframe);
const startLineNumber = getStartLineNumber(stackframe);
const highlightedLineIndex = size(stackframe.context?.pre || []);
const language = codeLanguage || 'javascript'; // TODO: Add support for more languages
return (
<ContextContainer>
<LineHighlight lineNumber={highlightedLineIndex} />
<LineNumberContainer isLibraryFrame={isLibraryFrame}>
{lines.map((line, i) => (
<LineNumber key={line + i} highlight={highlightedLineIndex === i}>
{i + startLineNumber}.
</LineNumber>
))}
</LineNumberContainer>
<LineContainer>
{lines.map((line, i) => (
<SyntaxHighlighter
key={line + i}
language={language}
style={xcode}
PreTag={Line}
CodeTag={Code}
customStyle={{ padding: undefined, overflowX: undefined }}
>
{line}
</SyntaxHighlighter>
))}
</LineContainer>
</ContextContainer>
);
}