Skip to content
Closed
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
38 changes: 37 additions & 1 deletion src/line_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,13 @@ export class LineCursor extends Marker {
* @param node The current position in the AST.
* @param isValid A function true/false depending on whether the given node
* should be traversed.
* @param loop A boolean for whether to try to loop back to the top block.
* @returns The next node in the traversal.
*/
getNextNode(
node: ASTNode | null,
isValid: (p1: ASTNode | null) => boolean,
loop = true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a loop param to getPrevNode

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI Grace won't be around until tomorrow so please feel free to take this and run with it.

We may need should pass loop to the recursive calls too.

): ASTNode | null {
if (!node) {
return null;
Expand All @@ -333,6 +335,13 @@ export class LineCursor extends Marker {
} else if (siblingOrParent) {
return this.getNextNode(siblingOrParent, isValid);
}
if (loop) {
// Loop back to first block if it exists.
const topBlocks = this.workspace.getTopBlocks(true);
return topBlocks.length > 0
? Blockly.ASTNode.createTopNode(topBlocks[0])
: null;
}
return null;
}

Expand Down Expand Up @@ -366,7 +375,34 @@ export class LineCursor extends Marker {
} else if (newNode) {
return this.getPreviousNode(newNode, isValid);
}
return null;
// Loop back to last block if it exists.
return this.getLastNode();
}

/**
* Get the very last valid AST node.
*
* @returns The last AST node or null.
*/
private getLastNode(): ASTNode | null {
const topBlocks = this.workspace.getTopBlocks(true);
if (topBlocks.length === 0) {
return null;
}
const lastTopBlockNode = Blockly.ASTNode.createTopNode(
topBlocks[topBlocks.length - 1],
);
let prevNode = lastTopBlockNode;
let nextNode: ASTNode | null = lastTopBlockNode;
while (nextNode) {
prevNode = nextNode;
nextNode = this.getNextNode(
prevNode,
this.validLineNode.bind(this),
false,
);
}
return prevNode;
}

/**
Expand Down