Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions src/components/MonacoEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ const MonacoEditor = () => {
.filter((segment: string) => segment !== "")
.map((segment: string) => decodePointerSegment(segment));

const buildPathCandidates = (segments: string[]) => {
return segments.reduce<Array<Array<string | number>>>(
const buildPathCandidates = (segments: string[]) =>
segments.reduce<Array<Array<string | number>>>(
(candidates, segment) => {
if (!/^\d+$/.test(segment)) {
return candidates.map((candidate) => [...candidate, segment]);
Expand All @@ -188,34 +188,14 @@ const MonacoEditor = () => {
},
[[]]
);
const getTypedPath = (segments: string[], parsedRoot: unknown) => {
const typedSegments: Array<string | number> = [];
let current: any = parsedRoot;

for (const segment of segments) {
const isArrayIndex = Array.isArray(current) && /^\d+$/.test(segment);
const pathSegment: string | number = isArrayIndex
? parseInt(segment, 10)
: segment;

typedSegments.push(pathSegment);
current = current?.[pathSegment as keyof typeof current];
}

return typedSegments;
};

let startPos, endPos;

try {
const pathCandidates = buildPathCandidates(rawPath);
const parsedRoot =
schemaFormat === "yaml" ? YAML.load(text) : JSON.parse(text);
const typedPath = getTypedPath(rawPath, parsedRoot);

if (schemaFormat === "yaml") {
const doc = parseDocument(text);
// If path is empty, we are at root, otherwise fetch the node.
const node = (rawPath.length === 0
? doc.contents
: pathCandidates
Expand All @@ -226,9 +206,6 @@ const MonacoEditor = () => {
typeof candidateNode === "object" &&
"range" in candidateNode
)) as any;
const node = (typedPath.length === 0
? doc.contents
: doc.getIn(typedPath, true)) as any;

if (!node || !node.range) return;

Expand All @@ -242,7 +219,6 @@ const MonacoEditor = () => {
const node = pathCandidates
.map((candidatePath) => findNodeAtLocation(tree, candidatePath))
.find((candidateNode) => !!candidateNode);
const node = findNodeAtLocation(tree, typedPath);
if (!node) return;

startPos = model.getPositionAt(node.offset);
Expand Down
Loading