Skip to content

Commit

Permalink
swap id strategey to jsonc (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnuttandrew authored Jul 3, 2023
1 parent bf9c56f commit 932511c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 99 deletions.
6 changes: 4 additions & 2 deletions src/lib/projections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function widgetBuilder(
) {
const widgets: Range<Decoration>[] = [];
const { schemaTypings, codeUpdateHook } = state.field(cmStatePlugin);
// for (const { from, to } of view.visibleRanges) {
const logger = new Set();
syntaxTree(state).iterate({
from: 0,
Expand All @@ -97,7 +96,7 @@ function widgetBuilder(
});
},
});
// }

try {
const result = Decoration.set(widgets.sort((a, b) => a.from - b.from));
return result;
Expand Down Expand Up @@ -221,6 +220,9 @@ function identifyProjectionLocationsPreCache(state: EditorState) {
) as ProjectionInline[];
syntaxTree(state).iterate({
enter: ({ from, to, node }) => {
if (node.node.type.name === "⚠") {
return;
}
const baseRange = state.selection.ranges[0];
let blockChildren = false;
inlineProjections.forEach((projection) => {
Expand Down
105 changes: 9 additions & 96 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,83 +223,6 @@ export function syntaxNodeToAbsPath(node: SyntaxNode): AbsPathItem[] {
return (parent ? syntaxNodeToAbsPath(parent) : []).concat(add);
}

/**
* Transform an absolute path item list into our key path notation
* @param absPath
* @param root
* @returns
*/
function absPathToKeyPath(
absPath: AbsPathItem[],
root: any
): (string | number)[] {
const keyPath: (string | number)[] = [];
const pointerLog = [];
let idx = 1;
while (idx < absPath.length) {
const item = absPath[idx];
const pointer = getNestedVal(keyPath, root);
pointerLog.push(pointer);
switch (item.nodeType) {
case "Array":
if (absPath.at(idx + 1)) {
const arrayKey = absPath[idx + 1].index;
keyPath.push(arrayKey);
} else if (absPath[idx - 1].nodeType === "Property") {
// this may lead to bug watch out
const terminal = keyPath.at(-1);
keyPath.push(`${terminal}___value`);
}
break;
case "Object":
if (absPath.at(idx + 1)) {
const nextItem = absPath[idx + 1]; // a property node
const targetIndex = nextItem.index;
// TODO there's a bug here, not sure what
// console.log("bugger", targetIndex, pointer);
const objKey = Object.keys(pointer)[targetIndex];
keyPath.push(objKey);
}
break;
case "Property":
break;
case "PropertyName":
const PropertyNameTerminal = keyPath.pop();
keyPath.push(`${PropertyNameTerminal}___key`);
break;
case "Number":
case "String":
case "Boolean":
const prevItem = absPath[idx - 1];
if (prevItem.nodeType === "Property") {
const terminal = keyPath.at(-1);
keyPath.push(`${terminal}___value`);
}
break;
default:
break;
}

idx++;
}
return keyPath;
}

const getNestedVal = (props: (string | number)[], obj: any) =>
props.reduce((a, prop) => a[prop], obj);

let prevCode: string | undefined = undefined;
let prevVal: any = {};
const parseWithCache = (code: string): any => {
if (prevCode && code === prevCode) {
return prevVal;
}
const result = Json.parse(code);
prevVal = result;
prevCode = code;
return result;
};

let codeKey = "";
let pathCache: Record<string, (string | number)[]> = {};
export function syntaxNodeToKeyPath(
Expand All @@ -313,26 +236,16 @@ export function syntaxNodeToKeyPath(
if (pathCache[cacheKey]) {
return pathCache[cacheKey];
}
const absPath = syntaxNodeToAbsPath(node);
let parsedRoot = {};
try {
parsedRoot = parseWithCache(fullCode);
} catch (e) {
console.error(e);
console.error("ERROR CREATING PATH", fullCode);
return [];
}

try {
const resultPath = absPathToKeyPath(absPath, parsedRoot);
pathCache[cacheKey] = resultPath;
codeKey = fullCode;
return pathCache[cacheKey];
} catch (e) {
console.error(e);
console.error("ERROR CREATING PATH", absPath, parsedRoot);
return [];
const loc = Json.getLocation(fullCode, node.from);
const path = loc.path;
if (node.type.name === "PropertyName") {
path[path.length - 1] = `${path.at(-1)}___key`;
} else if (node.parent?.type.name === "Property") {
path.push(`${path.at(-1)}___value`);
}
pathCache[cacheKey] = loc.path;
codeKey = fullCode;
return loc.path;
}

function findParseTargetWidth(
Expand Down
2 changes: 1 addition & 1 deletion src/projections/standard-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const bundle = {
ClickTarget,
ColorChip,
ConvertHex: HexConversionProject,
Debugger,
// Debugger,
// HeuristicJSONFixes,
InsertRandomWord,
NumberSlider,
Expand Down

0 comments on commit 932511c

Please sign in to comment.