Skip to content

Commit 4438fc8

Browse files
authored
add TextIndexExtensions.zero() utility (#1440)
add `TextIndexExtensions.zero()` utility to create an index at offset zero, which is useful for creating cursors from child nodes where parent offset is not needed. The API already exists in Rust, so this just exposes it to TypeScript. See the discussion in #1439 for more information.
1 parent ba4fc11 commit 4438fc8

File tree

8 files changed

+71
-5
lines changed

8 files changed

+71
-5
lines changed

.changeset/many-rivers-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/slang": minor
3+
---
4+
5+
add `TextIndexExtensions.zero()` utility to create an index at offset zero, which is useful for creating cursors from child nodes where parent offset is not needed.

crates/metaslang/cst/src/text_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct TextIndex {
2828
}
2929

3030
impl TextIndex {
31-
/// Shorthand for `TextIndex { utf8: 0, utf16: 0, line: 0, char: 0 }`.
31+
/// Creates a text index with zero offsets.
3232
pub const ZERO: TextIndex = TextIndex {
3333
utf8: 0,
3434
utf16: 0,

crates/solidity/outputs/cargo/wasm/src/wasm_crate/interface/cst.generated.wit

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/solidity/outputs/cargo/wasm/src/wasm_crate/interface/cst.wit.jinja2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ interface cst {
278278
column: u32,
279279
}
280280

281+
//// Useful extension methods for working with text indices.
282+
resource text-index-extensions {
283+
/// Creates a text index with zero offsets.
284+
zero: static func() -> text-index;
285+
}
286+
281287
/// Represents a range in the source text.
282288
record text-range {
283289
/// Starting (inclusive) position of the range.

crates/solidity/outputs/cargo/wasm/src/wasm_crate/wrappers/cst/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ mod ffi {
99
AncestorsIterator, AncestorsIteratorBorrow, Cursor, CursorBorrow, CursorIterator,
1010
CursorIteratorBorrow, Edge, EdgeBorrow, EdgeLabel, Guest, GuestAncestorsIterator,
1111
GuestCursor, GuestCursorIterator, GuestEdge, GuestNonterminalNode, GuestQuery,
12-
GuestQueryMatchIterator, GuestTerminalKindExtensions, GuestTerminalNode, Node,
13-
NonterminalKind, NonterminalNode, NonterminalNodeBorrow, Query, QueryBorrow, QueryError,
14-
QueryMatch, QueryMatchIterator, QueryMatchIteratorBorrow, TerminalKind, TerminalNode,
15-
TerminalNodeBorrow, TextIndex, TextRange,
12+
GuestQueryMatchIterator, GuestTerminalKindExtensions, GuestTerminalNode,
13+
GuestTextIndexExtensions, Node, NonterminalKind, NonterminalNode, NonterminalNodeBorrow,
14+
Query, QueryBorrow, QueryError, QueryMatch, QueryMatchIterator, QueryMatchIteratorBorrow,
15+
TerminalKind, TerminalNode, TerminalNodeBorrow, TextIndex, TextRange,
1616
};
1717
}
1818

@@ -38,6 +38,8 @@ impl ffi::Guest for crate::wasm_crate::World {
3838

3939
type Query = QueryWrapper;
4040
type QueryMatchIterator = QueryMatchIteratorWrapper;
41+
42+
type TextIndexExtensions = TextIndexExtensionsWrapper;
4143
}
4244

4345
//================================================
@@ -478,6 +480,20 @@ impl FromFFI<rust::TextIndex> for &ffi::TextIndex {
478480
}
479481
}
480482

483+
//================================================
484+
//
485+
// resource text-index-extensions
486+
//
487+
//================================================
488+
489+
pub struct TextIndexExtensionsWrapper;
490+
491+
impl ffi::GuestTextIndexExtensions for TextIndexExtensionsWrapper {
492+
fn zero() -> ffi::TextIndex {
493+
crate::rust_crate::cst::TextIndex::ZERO._into_ffi()
494+
}
495+
}
496+
481497
//================================================
482498
//
483499
// record text-range

crates/solidity/outputs/npm/package/src/cst/index.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,10 @@ export type QueryMatchIterator = wasm.cst.QueryMatchIterator;
8181
/** {@inheritDoc wasm.cst.TextIndex} */
8282
export type TextIndex = wasm.cst.TextIndex;
8383

84+
/** {@inheritDoc wasm.cst.TextIndexExtensions} */
85+
export const TextIndexExtensions = wasm.cst.TextIndexExtensions;
86+
/** {@inheritDoc wasm.cst.TextIndexExtensions} */
87+
export type TextIndexExtensions = wasm.cst.TextIndexExtensions;
88+
8489
/** {@inheritDoc wasm.cst.TextRange} */
8590
export type TextRange = wasm.cst.TextRange;

crates/solidity/outputs/npm/package/wasm/generated/interfaces/nomic-foundation-slang-cst.d.ts

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TextIndexExtensions } from "@nomicfoundation/slang/cst";
2+
3+
it("zero()", () => {
4+
const zero = TextIndexExtensions.zero();
5+
6+
expect(zero).toEqual({
7+
utf8: 0,
8+
utf16: 0,
9+
line: 0,
10+
column: 0,
11+
});
12+
});

0 commit comments

Comments
 (0)