Skip to content

Commit

Permalink
optimize rangeForDefun slowing repl printout #942
Browse files Browse the repository at this point in the history
  • Loading branch information
brdloush committed Jul 9, 2021
1 parent 2fd8ef9 commit 7af1f7c
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/cursor-doc/token-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,25 +612,46 @@ export class LispTokenCursor extends TokenCursor {
* @param commentIsTopLevel? Controls
*/
rangeForDefun(offset: number, depth = 0, commentCreatesTopLevel = true): [number, number] {
const cursor = this.clone();
while (cursor.forwardSexp()) {
if (cursor.offsetEnd >= offset) {
if (depth < 1 && cursor.getPrevToken().raw === ')') {
const commentCursor = cursor.clone();
commentCursor.previous();
if (commentCursor.getFunctionName() === 'comment' && commentCreatesTopLevel) {
commentCursor.backwardList();
commentCursor.forwardWhitespace();
commentCursor.forwardSexp();
return commentCursor.rangeForDefun(offset, depth + 1);
}
const cursor = this.doc.getTokenCursor(offset);
if (cursor.getPrevToken().raw == ')') {
cursor.backwardSexp();
}
if (cursor.getToken().type === 'eol' || cursor.getToken().type === 'ws') {
cursor.forwardWhitespace();
}
if (cursor.getToken().raw == '(') {
cursor.next();
}
if (cursor.getToken().raw === 'comment' && commentCreatesTopLevel) {
cursor.next();
cursor.forwardWhitespace();
}
if (cursor.getToken().raw == '(') {
cursor.next();
}
do {
let rowColBefore = cursor.rowCol;
const peekForwardUpPrevCursor = cursor.clone();
peekForwardUpPrevCursor.forwardList();
peekForwardUpPrevCursor.upList();
peekForwardUpPrevCursor.previous();
if (peekForwardUpPrevCursor.getFunctionName() === 'comment' && commentCreatesTopLevel) {
break;
}
cursor.forwardList();
if (!cursor.upList()) {
if (cursor.getPrevToken().type === 'eol' && cursor.getToken().type === 'eol') {
return [offset, offset]
} else {
return cursor.rangeForCurrentForm(0);
}
const end = cursor.offsetStart;
cursor.backwardSexp();
return [cursor.offsetStart, end];
}
}
return [offset, offset]
let rowColAfter = cursor.rowCol;
if (rowColAfter[0] == rowColBefore[0] && rowColAfter[1] == rowColBefore[1]) {
break;
}
} while (cursor.withinValidList());
return cursor.rangeForCurrentForm(0);
}

rangesForTopLevelForms(): [number, number][] {
Expand Down

0 comments on commit 7af1f7c

Please sign in to comment.