Skip to content

Commit

Permalink
build_runners: Attempt to make GeneratedFile steps (#1606)
Browse files Browse the repository at this point in the history
  • Loading branch information
llogick authored Nov 23, 2023
1 parent d433a58 commit 38a1998
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ fn autofix(server: *Server, arena: std.mem.Allocator, handle: *DocumentStore.Han
try diagnostics_gen.getAstCheckDiagnostics(server, arena, handle, &diagnostics);
if (diagnostics.items.len == 0) return .{};

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

var builder = code_actions.Builder{
Expand Down Expand Up @@ -1231,7 +1231,7 @@ fn semanticTokensFullHandler(server: *Server, arena: std.mem.Allocator, request:

const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

return try semantic_tokens.writeSemanticTokens(
Expand All @@ -1250,7 +1250,7 @@ fn semanticTokensRangeHandler(server: *Server, arena: std.mem.Allocator, request
const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;
const loc = offsets.rangeToLoc(handle.tree.source, request.range, server.offset_encoding);

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

return try semantic_tokens.writeSemanticTokens(
Expand All @@ -1268,7 +1268,7 @@ fn completionHandler(server: *Server, arena: std.mem.Allocator, request: types.C

const source_index = offsets.positionToIndex(handle.tree.source, request.position, server.offset_encoding);

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

return .{
Expand All @@ -1283,7 +1283,7 @@ fn signatureHelpHandler(server: *Server, arena: std.mem.Allocator, request: type

const source_index = offsets.positionToIndex(handle.tree.source, request.position, server.offset_encoding);

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

const signature_info = (try signature_help.getSignatureInfo(
Expand Down Expand Up @@ -1322,7 +1322,7 @@ fn gotoHandler(
const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;
const source_index = offsets.positionToIndex(handle.tree.source, request.position, server.offset_encoding);

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

const response = try goto.goto(&analyser, &server.document_store, arena, handle, source_index, kind, server.offset_encoding) orelse return null;
Expand Down Expand Up @@ -1389,7 +1389,7 @@ fn hoverHandler(server: *Server, arena: std.mem.Allocator, request: types.HoverP

const markup_kind: types.MarkupKind = if (server.client_capabilities.hover_supports_md) .markdown else .plaintext;

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

const response = hover_handler.hover(&analyser, arena, handle, source_index, markup_kind, server.offset_encoding);
Expand Down Expand Up @@ -1482,7 +1482,7 @@ fn generalReferencesHandler(server: *Server, arena: std.mem.Allocator, request:
const name = offsets.locToSlice(handle.tree.source, name_loc);
const pos_context = try Analyser.getPositionContext(server.allocator, handle.tree.source, source_index, true);

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

// TODO: Make this work with branching types
Expand Down Expand Up @@ -1566,7 +1566,7 @@ fn inlayHintHandler(server: *Server, arena: std.mem.Allocator, request: types.In
const hover_kind: types.MarkupKind = if (server.client_capabilities.hover_supports_md) .markdown else .plaintext;
const loc = offsets.rangeToLoc(handle.tree.source, request.range, server.offset_encoding);

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

return try inlay_hints.writeRangeInlayHint(
Expand All @@ -1583,7 +1583,7 @@ fn inlayHintHandler(server: *Server, arena: std.mem.Allocator, request: types.In
fn codeActionHandler(server: *Server, arena: std.mem.Allocator, request: types.CodeActionParams) Error!ResultType("textDocument/codeAction") {
const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;

var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip);
var analyser = Analyser.init(server.allocator, &server.document_store, &server.ip, handle);
defer analyser.deinit();

var builder = code_actions.Builder{
Expand Down
15 changes: 13 additions & 2 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ resolved_callsites: std.AutoHashMapUnmanaged(Declaration.Param, ?TypeWithHandle)
resolved_nodes: std.HashMapUnmanaged(NodeWithUri, ?TypeWithHandle, NodeWithUri.Context, std.hash_map.default_max_load_percentage) = .{},
/// used to detect recursion
use_trail: NodeSet = .{},
/// handle of the doc where the request originated
root_handle: ?*DocumentStore.Handle = undefined,

const NodeSet = std.HashMapUnmanaged(NodeWithUri, void, NodeWithUri.Context, std.hash_map.default_max_load_percentage);

pub fn init(gpa: std.mem.Allocator, store: *DocumentStore, ip: *InternPool) Analyser {
pub fn init(gpa: std.mem.Allocator, store: *DocumentStore, ip: *InternPool, root_handle: ?*DocumentStore.Handle) Analyser {
return .{
.gpa = gpa,
.arena = std.heap.ArenaAllocator.init(gpa),
.store = store,
.ip = ip,
.root_handle = root_handle,
};
}

Expand Down Expand Up @@ -1449,7 +1452,15 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
if (node_tags[import_param] != .string_literal) return null;

const import_str = tree.tokenSlice(main_tokens[import_param]);
const import_uri = (try analyser.store.uriFromImportStr(analyser.arena.allocator(), handle.*, import_str[1 .. import_str.len - 1])) orelse return null;
const import_uri = (try analyser.store.uriFromImportStr(
analyser.arena.allocator(),
handle.*,
import_str[1 .. import_str.len - 1],
)) orelse (try analyser.store.uriFromImportStr(
analyser.arena.allocator(),
if (analyser.root_handle) |root_handle| root_handle.* else return null,
import_str[1 .. import_str.len - 1],
)) orelse return null;

const new_handle = analyser.store.getOrLoadHandle(import_uri) orelse return null;

Expand Down
24 changes: 23 additions & 1 deletion src/build_runner/0.11.0.zig
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,36 @@ fn getPkgConfigIncludes(
} else |err| return err;
}

fn reify(step: *Build.Step) anyerror!void {
var progress: std.Progress = .{};
const main_progress_node = progress.start("", 0);
defer main_progress_node.end();

for (step.dependencies.items) |unknown_step| {
try reify(unknown_step);
}

try step.make(main_progress_node);
}

// TODO: Having a copy of this is not very nice
const copied_from_zig = struct {
/// Copied from `std.Build.LazyPath.getPath2` and massaged a bit.
fn getPath(path: std.Build.LazyPath, builder: *Build) ?[]const u8 {
switch (path) {
.path => |p| return builder.pathFromRoot(p),
.cwd_relative => |p| return pathFromCwd(builder, p),
.generated => |gen| return builder.pathFromRoot(gen.path orelse return null),
.generated => |gen| {
if (gen.path) |gen_path|
return builder.pathFromRoot(gen_path)
else {
reify(gen.step) catch return null;
if (gen.path) |gen_path|
return builder.pathFromRoot(gen_path)
else
return null;
}
},
}
}

Expand Down
24 changes: 23 additions & 1 deletion src/build_runner/master.zig
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,36 @@ fn getPkgConfigIncludes(
} else |err| return err;
}

fn reify(step: *Build.Step) anyerror!void {
var progress: std.Progress = .{};
const main_progress_node = progress.start("", 0);
defer main_progress_node.end();

for (step.dependencies.items) |unknown_step| {
try reify(unknown_step);
}

try step.make(main_progress_node);
}

// TODO: Having a copy of this is not very nice
const copied_from_zig = struct {
/// Copied from `std.Build.LazyPath.getPath2` and massaged a bit.
fn getPath(path: std.Build.LazyPath, builder: *Build) ?[]const u8 {
switch (path) {
.path => |p| return builder.pathFromRoot(p),
.cwd_relative => |p| return pathFromCwd(builder, p),
.generated => |gen| return builder.pathFromRoot(gen.path orelse return null),
.generated => |gen| {
if (gen.path) |gen_path|
return builder.pathFromRoot(gen_path)
else {
reify(gen.step) catch return null;
if (gen.path) |gen_path|
return builder.pathFromRoot(gen_path)
else
return null;
}
},
.dependency => |dep| return dep.dependency.builder.pathJoin(&[_][]const u8{
dep.dependency.builder.build_root.path.?,
dep.sub_path,
Expand Down

0 comments on commit 38a1998

Please sign in to comment.