You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/plugin-kit/README.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -205,6 +205,8 @@ The `TextSourceCodeBase` class is intended to be a base class that has several o
205
205
206
206
-`lines` - an array of text lines that is created automatically when the constructor is called.
207
207
-`getLoc(node)` - gets the location of a node. Works for nodes that have the ESLint-style `loc` property and nodes that have the Unist-style [`position` property](https://github.com/syntax-tree/unist?tab=readme-ov-file#position). If you're using an AST with a different location format, you'll still need to implement this method yourself.
208
+
-`getLocFromIndex(index)` - Converts a source text index into a `{ line: number, column: number }` pair. (For this method to work, the root node should always cover the entire source code text, and the `getLoc()` method needs to be implemented correctly.)
209
+
-`getIndexFromLoc(loc)` - Converts a `{ line: number, column: number }` pair into a source text index. (For this method to work, the root node should always cover the entire source code text, and the `getLoc()` method needs to be implemented correctly.)
208
210
-`getRange(node)` - gets the range of a node within the source text. Works for nodes that have the ESLint-style `range` property and nodes that have the Unist-style [`position` property](https://github.com/syntax-tree/unist?tab=readme-ov-file#position). If you're using an AST with a different range format, you'll still need to implement this method yourself.
209
211
-`getText(nodeOrToken, charsBefore, charsAfter)` - gets the source text for the given node or token that has range information attached. Optionally, can return additional characters before and after the given node or token. As long as `getRange()` is properly implemented, this method will just work.
210
212
-`getAncestors(node)` - returns the ancestry of the node. In order for this to work, you must implement the `getParent()` method yourself.
// Continue parsing until no more matches are found or we have enough lines.
381
+
additionalLinesNeeded-=1;
382
+
}
252
383
}
253
384
254
385
/**
@@ -271,6 +402,135 @@ export class TextSourceCodeBase {
271
402
);
272
403
}
273
404
405
+
/**
406
+
* Converts a source text index into a `{ line: number, column: number }` pair.
407
+
* @param {number} index The index of a character in a file.
408
+
* @throws {TypeError|RangeError} If non-numeric index or index out of range.
409
+
* @returns {{line: number, column: number}} A `{ line: number, column: number }` location object with 0 or 1-indexed line and 0 or 1-indexed column based on language.
410
+
* @public
411
+
*/
412
+
getLocFromIndex(index){
413
+
if(typeofindex!=="number"){
414
+
thrownewTypeError("Expected `index` to be a number.");
415
+
}
416
+
417
+
if(index<0||index>this.text.length){
418
+
thrownewRangeError(
419
+
`Index out of range (requested index ${index}, but source text has length ${this.text.length}).`,
420
+
);
421
+
}
422
+
423
+
const{
424
+
start: {line: lineStart,column: columnStart},
425
+
end: {line: lineEnd,column: columnEnd},
426
+
}=this.getLoc(this.ast);
427
+
428
+
// If the index is at the start, return the start location of the root node.
429
+
if(index===0){
430
+
return{
431
+
line: lineStart,
432
+
column: columnStart,
433
+
};
434
+
}
435
+
436
+
// If the index is `this.text.length`, return the location one "spot" past the last character of the file.
437
+
if(index===this.text.length){
438
+
return{
439
+
line: lineEnd,
440
+
column: columnEnd,
441
+
};
442
+
}
443
+
444
+
// Ensure `#lineStartIndices` are lazily calculated.
445
+
this.#ensureLineStartIndicesFromIndex(index);
446
+
447
+
/*
448
+
* To figure out which line `index` is on, determine the last place at which index could
449
+
* be inserted into `#lineStartIndices` to keep the list sorted.
`Column number out of range (column ${loc.column} requested). Valid range for line ${loc.line}: ${columnStart}-${lineEndIndex-lineStartIndex+columnStart+(isLastLine ? 0 : -1)}`,
528
+
);
529
+
}
530
+
531
+
returnpositionIndex;
532
+
}
533
+
274
534
/**
275
535
* Returns the range information for the given node or token.
276
536
* @param {Options['SyntaxElementWithLoc']} nodeOrToken The node or token to get the range information for.
@@ -356,6 +616,8 @@ export class TextSourceCodeBase {
356
616
* @public
357
617
*/
358
618
getlines(){
619
+
this.#ensureLines();// Ensure `#lines` is lazily calculated.
0 commit comments