dsherret / dsherret/ts-morph

Get node at (line, col)

Open
#891 4 comments 3 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Is your feature request related to a problem? Please describe.**
I get line & column coordinates from an external source, I want to get the node(s) at this coordinate.

**Describe the solution you'd like**
The obvious solution would be `sourceFile.getChildAtPos`, but this takes a 1D coordinate. I can convert from "pos" to line+col using `sourceFile.getLineAndColumnAtPos` (#801) but I can't find anything for the reverse.

**Describe alternatives you've considered**
I bodged together this:
```ts
for (const node of source.getDescendants()) {
if (line === node.getStartLineNumber() && col === node.getStart() - node.getStartLinePos()) {
// We have our node!
}
}
```
But that doesn't seem like an efficient solution.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.