Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e2aa43e

Browse files
committedAug 8, 2024·
added yaml editor
1 parent 9426ff3 commit e2aa43e

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
 

‎docs/configure-coderabbit.md

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ sidebar_position: 3
1212
import SchemaViewer from "@site/src/components/SchemaViewer";
1313
```
1414

15+
```mdx-code-block
16+
import YamlEditor from "/src/components/YamlEditor/YamlEditor";
17+
```
18+
1519
CodeRabbit offers various configuration options to tailor the reviews to your
1620
requirements. Configuration can be made using one of the below options, in order
1721
of precedence:
@@ -56,6 +60,12 @@ chat:
5660
auto_reply: true
5761
```
5862
63+
Write your configuration file in the below editor to validate:
64+
65+
```mdx-code-block
66+
<YamlEditor />
67+
```
68+
5969
The configuration file can be used to set the following options:
6070

6171
```mdx-code-block

‎package-lock.json

+42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"postcss": "^8.4.32",
2828
"prism-react-renderer": "^2.3.0",
2929
"react": "^18.0.0",
30+
"react-ace": "^12.0.0",
3031
"react-dom": "^18.0.0",
3132
"tailwindcss": "^3.4.0"
3233
},
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { React, useState } from "react";
2+
3+
import AceEditor from "react-ace";
4+
import "ace-builds/src-noconflict/theme-github";
5+
import "ace-builds/src-noconflict/ext-language_tools";
6+
7+
import "ace-builds/webpack-resolver";
8+
import "ace-builds/src-noconflict/mode-yaml";
9+
10+
import jsYaml from "js-yaml";
11+
12+
import Ajv from "ajv";
13+
const ajv = new Ajv({ allErrors: true });
14+
15+
import Schema from "../../../static/schema/schema.v2.json";
16+
17+
export default function YamlEditor() {
18+
const [annotations, setAnnotations] = useState([]);
19+
const [value, setValue] = useState(
20+
"# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json\n"
21+
);
22+
const validate = ajv.compile(Schema.definitions.schema);
23+
function getRowFromPath(path) {
24+
// Convert path to row number (0-based)
25+
return path.split("/").length - 1;
26+
}
27+
function getLineNumber(yaml, path) {
28+
const lines = yaml.split("\n");
29+
const pathParts = path.split("/").filter(Boolean);
30+
let currentObj = jsYaml.load(yaml);
31+
let lineNumber = 0;
32+
33+
for (const part of pathParts) {
34+
for (let i = lineNumber; i < lines.length; i++) {
35+
if (lines[i].trim().startsWith(part + ":")) {
36+
lineNumber = i;
37+
break;
38+
}
39+
}
40+
currentObj = currentObj[part];
41+
}
42+
43+
return lineNumber;
44+
}
45+
function onChange(newValue) {
46+
setValue(newValue);
47+
try {
48+
const doc = jsYaml.load(newValue, { strict: true });
49+
const valid = validate(doc);
50+
console.log("Validation result:", valid);
51+
console.log("Validation errors:", validate.errors);
52+
53+
if (!valid && validate.errors) {
54+
setAnnotations(
55+
validate.errors.map((err) => ({
56+
row: err.instancePath
57+
? getLineNumber(newValue, err.instancePath)
58+
: 0,
59+
column: 0,
60+
text: `${err.keyword}: ${err.message} ${
61+
err?.params?.allowedValues
62+
? `Allowed values: ${err.params.allowedValues.join(", ")}`
63+
: ""
64+
}`,
65+
type: "error",
66+
}))
67+
);
68+
} else {
69+
setAnnotations([]);
70+
}
71+
} catch (err) {
72+
console.error("Error:", err);
73+
console.log({
74+
errorMessage: err.reason,
75+
column: err.mark?.column,
76+
line: err.mark?.line,
77+
});
78+
setAnnotations([
79+
{
80+
row: err.instancePath ? getLineNumber(newValue, err.instancePath) : 0,
81+
column: 0,
82+
text:
83+
`${err.keyword}: ${err.message} ${
84+
err?.params?.allowedValues
85+
? `Allowed values: ${err.params.allowedValues.join(", ")}`
86+
: ""
87+
}` || "YAML parsing error",
88+
type: "error",
89+
},
90+
]);
91+
}
92+
}
93+
return (
94+
<AceEditor
95+
mode="yaml"
96+
theme="github"
97+
onChange={onChange}
98+
value={value}
99+
name="UNIQUE_ID_OF_DIV"
100+
editorProps={{ $blockScrolling: true }}
101+
setOptions={{
102+
useWorker: false,
103+
showPrintMargin: false,
104+
showGutter: true,
105+
}}
106+
annotations={annotations}
107+
width="100%"
108+
height="400px"
109+
/>
110+
);
111+
}

0 commit comments

Comments
 (0)
Please sign in to comment.