Skip to content

Commit 196301c

Browse files
fathybCompuIves
authored andcommitted
Improve Monaco syntax highlighting (codesandbox#834)
* Improve tokenization * Add two syntax highlight rule
1 parent 1c388c5 commit 196301c

File tree

3 files changed

+76
-52
lines changed

3 files changed

+76
-52
lines changed

packages/app/src/app/components/CodeEditor/Monaco/elements.js

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,52 +62,32 @@ export const CodeContainer = styled.div`
6262
.mtk5 {
6363
color: #99c794 !important;
6464
}
65-
.mtk12.PropertyAssignment {
66-
color: #99c794;
67-
}
68-
.mtk12.PropertyAssignment.PropertyAccessExpression {
69-
color: #fac863;
70-
}
71-
.mtk12.Identifier.JsxOpeningElement {
72-
color: #ec5f67;
73-
}
74-
.mtk12.Identifier.JsxExpression.JsxClosingElement {
75-
color: #ec5f67;
76-
}
77-
.mtk12.Identifier.JsxClosingElement {
78-
color: #ec5f67 !important;
79-
}
80-
.mtk12.Identifier.JsxSelfClosingElement {
81-
color: #ec5f67;
82-
}
83-
.mtk12.Identifier.VariableStatement.JsxClosingElement {
84-
color: #ec5f67 !important;
65+
66+
.JsxText,
67+
.JsxSelfClosingElement,
68+
.JsxClosingElement {
69+
color: #e0e0e0;
8570
}
86-
.mtk12.VariableStatement.JsxSelfClosingElement.Identifier {
71+
72+
.tagName-of-JsxOpeningElement,
73+
.tagName-of-JsxClosingElement,
74+
.tagName-of-JsxSelfClosingElement {
8775
color: #ec5f67;
8876
}
89-
.mtk12.Identifier.JsxAttribute.VariableDeclaration {
77+
78+
.name-of-JsxAttribute {
9079
color: #aa759f;
9180
}
92-
.mtk12.JsxExpression.VariableStatement {
93-
color: #fac863;
94-
}
95-
.mtk12.VariableStatement.JsxSelfClosingElement {
96-
color: #e0e0e0;
97-
}
98-
.mtk12.VariableStatement.JsxClosingElement {
99-
color: #e0e0e0;
100-
}
101-
.mtk12.JsxElement.JsxText {
102-
color: #e0e0e0;
81+
82+
.name-of-PropertyAssignment {
83+
color: #99c794;
10384
}
104-
.JsxText {
105-
color: #e0e0e0;
85+
86+
.typeName-of-TypeReference {
87+
color: #5faeec;
10688
}
10789
108-
.Identifier.CallExpression
109-
+ .OpenParenToken.CallExpression
110-
+ .Identifier.CallExpression {
111-
color: #fac863 !important;
90+
.name-of-PropertyAccessExpression {
91+
color: #83bdc9;
11292
}
11393
`;

packages/app/src/app/components/CodeEditor/Monaco/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,11 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
10411041
classification.end
10421042
),
10431043
options: {
1044-
inlineClassName: classification.kind,
1044+
inlineClassName: classification.type
1045+
? `${classification.kind} ${classification.type}-of-${
1046+
classification.parentKind
1047+
}`
1048+
: classification.kind,
10451049
},
10461050
}));
10471051

packages/app/src/app/components/CodeEditor/Monaco/workers/syntax-highlighter.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,60 @@ function nodeToRange(node) {
2828
return [0, 0];
2929
}
3030

31+
function getNodeType(parent, node) {
32+
return Object.keys(parent).find(key => parent[key] === node);
33+
}
34+
35+
function getParentRanges(node) {
36+
const ranges = [];
37+
const [start, end] = nodeToRange(node);
38+
let lastEnd = start;
39+
40+
self.ts.forEachChild(node, child => {
41+
const [start, end] = nodeToRange(child);
42+
43+
ranges.push({
44+
start: lastEnd,
45+
end: start,
46+
});
47+
lastEnd = end;
48+
});
49+
50+
if (lastEnd !== end) {
51+
ranges.push({
52+
start: lastEnd,
53+
end,
54+
});
55+
}
56+
57+
return ranges;
58+
}
59+
3160
function addChildNodes(node, lines, classifications) {
61+
const parentKind = ts.SyntaxKind[node.kind];
62+
3263
self.ts.forEachChild(node, id => {
33-
const [start, end] = nodeToRange(id);
34-
35-
const { offset, line: startLine } = getLineNumberAndOffset(start, lines);
36-
const { line: endLine } = getLineNumberAndOffset(end, lines);
37-
classifications.push({
38-
start: id.getStart() + 1 - offset,
39-
end: id.getEnd() + 1 - offset,
40-
kind: self.ts.SyntaxKind[id.kind],
41-
node: id,
42-
startLine,
43-
endLine,
44-
});
64+
const type = getNodeType(node, id);
65+
66+
classifications.push(
67+
...getParentRanges(id).map(({ start, end }) => {
68+
const { offset, line: startLine } = getLineNumberAndOffset(
69+
start,
70+
lines
71+
);
72+
const { line: endLine } = getLineNumberAndOffset(end, lines);
73+
74+
return {
75+
start: start + 1 - offset,
76+
end: end + 1 - offset,
77+
kind: ts.SyntaxKind[id.kind],
78+
parentKind,
79+
type,
80+
startLine,
81+
endLine,
82+
};
83+
})
84+
);
4585

4686
addChildNodes(id, lines, classifications);
4787
});

0 commit comments

Comments
 (0)