Skip to content

Commit cdff95e

Browse files
committed
wip resumable
1 parent afebe4b commit cdff95e

File tree

307 files changed

+2232
-2363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

307 files changed

+2232
-2363
lines changed

src/fmt/fmt2.zig

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const Formatter = struct {
2323
ast: *const AST2,
2424
source: []const u8,
2525
ident_store: *const Ident.Store,
26-
tokens: std.ArrayList(Token),
27-
token_regions: std.ArrayList(base.Region), // Region for each token
26+
tokens: []const Token, // Now uses pre-collected tokens from parser
2827
output: std.ArrayList(u8),
2928

3029
// Formatting state - matches original formatter
@@ -33,28 +32,21 @@ const Formatter = struct {
3332
last_formatted_pos: usize = 0, // Current position in source being formatted
3433
last_comment_end: usize = 0, // Track last comment position to avoid duplicates
3534

36-
fn init(allocator: std.mem.Allocator, ast: *const AST2, source: []const u8, ident_store: *const Ident.Store) !Formatter {
37-
var formatter = Formatter{
35+
fn init(allocator: std.mem.Allocator, ast: *const AST2, source: []const u8, ident_store: *const Ident.Store, tokens: []const Token) !Formatter {
36+
return Formatter{
3837
.allocator = allocator,
3938
.ast = ast,
4039
.source = source,
4140
.ident_store = ident_store,
42-
.tokens = std.ArrayList(Token).init(allocator),
43-
.token_regions = std.ArrayList(base.Region).init(allocator),
41+
.tokens = tokens, // Use pre-collected tokens
4442
.output = std.ArrayList(u8).init(allocator),
4543
.last_formatted_pos = 0,
4644
.last_comment_end = 0,
4745
};
48-
49-
// Tokenize once at the beginning
50-
try formatter.tokenizeSource();
51-
52-
return formatter;
5346
}
5447

5548
fn deinit(self: *Formatter) void {
56-
self.tokens.deinit();
57-
self.token_regions.deinit();
49+
// tokens are now owned externally, don't deinit them
5850
self.output.deinit();
5951
}
6052

@@ -79,7 +71,7 @@ const Formatter = struct {
7971
}
8072

8173
// Flush any trailing comments using token-based approach
82-
if (self.token_regions.items.len > 0) {
74+
if (self.tokens.len > 0) {
8375
_ = try self.flushTrailingComments();
8476
}
8577
}
@@ -124,8 +116,8 @@ const Formatter = struct {
124116

125117
/// Find the token index that contains or is after the given position
126118
fn findTokenAtPosition(self: *const Formatter, pos: Position) ?usize {
127-
for (self.token_regions.items, 0..) |region, i| {
128-
if (region.start.offset >= pos.offset) {
119+
for (self.tokens, 0..) |token, i| {
120+
if (token.region.start.offset >= pos.offset) {
129121
return i;
130122
}
131123
}
@@ -135,8 +127,8 @@ const Formatter = struct {
135127
/// Find the token index that is before the given position
136128
fn findTokenBeforePosition(self: *const Formatter, pos: Position) ?usize {
137129
var result: ?usize = null;
138-
for (self.token_regions.items, 0..) |region, i| {
139-
if (region.end.offset <= pos.offset) {
130+
for (self.tokens, 0..) |token, i| {
131+
if (token.region.end.offset <= pos.offset) {
140132
result = i;
141133
} else {
142134
break;
@@ -2928,7 +2920,7 @@ const Formatter = struct {
29282920
if (token_idx == 0) return false;
29292921

29302922
// Get the region between previous token and this position
2931-
const prev_token_end = self.token_regions.items[token_idx - 1].end.offset;
2923+
const prev_token_end = self.tokens[token_idx - 1].region.end.offset;
29322924
const end_offset = pos.offset;
29332925

29342926
if (end_offset <= prev_token_end) return false;
@@ -2940,11 +2932,11 @@ const Formatter = struct {
29402932
fn flushCommentsAfterToken(self: *Formatter, pos: Position) !bool {
29412933
// Find the token before this position
29422934
const token_idx = self.findTokenBeforePosition(pos) orelse return false;
2943-
if (token_idx >= self.token_regions.items.len - 1) return false;
2935+
if (token_idx >= self.tokens.len - 1) return false;
29442936

29452937
// Get the region between this position and next token
29462938
const start_offset = pos.offset;
2947-
const next_token_start = self.token_regions.items[token_idx + 1].start.offset;
2939+
const next_token_start = self.tokens[token_idx + 1].region.start.offset;
29482940

29492941
if (next_token_start <= start_offset) return false;
29502942

@@ -3052,9 +3044,9 @@ const Formatter = struct {
30523044
}
30533045

30543046
fn flushTrailingComments(self: *Formatter) !bool {
3055-
if (self.token_regions.items.len == 0) return false;
3047+
if (self.tokens.len == 0) return false;
30563048

3057-
const last_token_end = self.token_regions.items[self.token_regions.items.len - 1].end.offset;
3049+
const last_token_end = self.tokens[self.tokens.len - 1].region.end.offset;
30583050
if (last_token_end >= self.source.len) return false;
30593051

30603052
const remaining = self.source[last_token_end..];
@@ -3063,8 +3055,39 @@ const Formatter = struct {
30633055
};
30643056

30653057
/// Formats AST2 with source code
3066-
pub fn formatAst(allocator: std.mem.Allocator, ast: *const AST2, source: []const u8, ident_store: *const Ident.Store, root_node: ?Node.Idx) ![]u8 {
3067-
var formatter = try Formatter.init(allocator, ast, source, ident_store);
3058+
pub fn formatAst(
3059+
allocator: std.mem.Allocator,
3060+
ast: *const AST2,
3061+
source: []const u8,
3062+
ident_store: *const Ident.Store,
3063+
root_node: ?Node.Idx,
3064+
) ![]u8 {
3065+
// Parse and collect tokens using the new token-fed architecture
3066+
var env = try CommonEnv.init(allocator, source);
3067+
defer env.deinit(allocator);
3068+
3069+
var byte_slices = collections.ByteSlices{ .entries = .{} };
3070+
defer byte_slices.entries.deinit(allocator);
3071+
3072+
var messages: [256]tokenize_iter.Diagnostic = undefined;
3073+
3074+
// Create a temporary AST for parsing (we'll use the provided one for formatting)
3075+
var parse_ast = try AST2.initCapacity(allocator, 1024);
3076+
defer parse_ast.deinit(allocator);
3077+
3078+
// Parse and collect tokens
3079+
const parse_result = try Parser2.parseAndCollectTokens(
3080+
&env,
3081+
allocator,
3082+
source,
3083+
&messages,
3084+
&parse_ast,
3085+
&byte_slices,
3086+
);
3087+
var tokens = parse_result.tokens;
3088+
defer tokens.deinit();
3089+
3090+
var formatter = try Formatter.init(allocator, ast, source, ident_store, tokens.items);
30683091
defer formatter.deinit();
30693092

30703093
try formatter.format(root_node);
@@ -3075,4 +3098,5 @@ pub fn formatAst(allocator: std.mem.Allocator, ast: *const AST2, source: []const
30753098
return output;
30763099
}
30773100

3101+
30783102
// Keep the old interface for compatibility

0 commit comments

Comments
 (0)