Skip to content

Commit

Permalink
Merge branch 'mini' into browser
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Nov 20, 2024
2 parents 8ad8069 + 0ac374b commit 4028506
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ export abstract class AstNode implements AstNodeBase {
return parentNode ?? this;
}

/**
* 将行列号转换为字符位置
* @param top 行号
* @param left 列号
*/
indexFromPos(top: number, left: number): number | undefined {
const lines = String(this).split('\n');
return top >= 0 && left >= 0 && top <= lines.length - 1 && left <= lines[top]!.length
? lines.slice(0, top).reduce((acc, cur) => acc + cur.length + 1, 0) + left
: undefined;
}

/**
* 将字符位置转换为行列号
* @param index 字符位置
Expand Down

0 comments on commit 4028506

Please sign in to comment.