Skip to content

Commit

Permalink
give filesystem completions on string literals in build files (#1668)
Browse files Browse the repository at this point in the history
Many string literals in `build.zig` files refer to file system paths so
we can optimistically provide completions there. If the string literal is
not a valid file path then no completions will appear.
  • Loading branch information
Techatrix authored Dec 18, 2023
1 parent 7c27ca5 commit 46de13c
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,11 @@ fn completeDot(document_store: *DocumentStore, analyser: *Analyser, arena: std.m
return enum_completions;
}

/// asserts that `pos_context` is one of the following:
/// - `.import_string_literal`
/// - `.cinclude_string_literal`
/// - `.embedfile_string_literal`
/// - `.string_literal`
fn completeFileSystemStringLiteral(
arena: std.mem.Allocator,
store: *DocumentStore,
Expand All @@ -817,7 +822,15 @@ fn completeFileSystemStringLiteral(
) ![]types.CompletionItem {
var completions: DocumentScope.CompletionSet = .{};

const loc = pos_context.loc().?;
const loc = switch (pos_context) {
.import_string_literal,
.cinclude_string_literal,
.embedfile_string_literal,
.string_literal,
=> |loc| loc,
else => unreachable,
};

var completing = handle.tree.source[loc.start + 1 .. loc.end - 1];

var separator_index = completing.len;
Expand Down Expand Up @@ -852,6 +865,7 @@ fn completeFileSystemStringLiteral(
.import_string_literal => ".zig",
.cinclude_string_literal => ".h",
.embedfile_string_literal => null,
.string_literal => null,
else => unreachable,
};
switch (entry.kind) {
Expand Down Expand Up @@ -950,9 +964,13 @@ pub fn completionAtIndex(server: *Server, analyser: *Analyser, arena: std.mem.Al
.import_string_literal,
.cinclude_string_literal,
.embedfile_string_literal,
=> completeFileSystemStringLiteral(arena, &server.document_store, handle.*, pos_context) catch |err| {
log.err("failed to get file system completions: {}", .{err});
return null;
.string_literal,
=> blk: {
if (pos_context == .string_literal and !DocumentStore.isBuildFile(handle.uri)) break :blk null;
break :blk completeFileSystemStringLiteral(arena, &server.document_store, handle.*, pos_context) catch |err| {
log.err("failed to get file system completions: {}", .{err});
return null;
};
},
else => null,
};
Expand All @@ -968,6 +986,7 @@ pub fn completionAtIndex(server: *Server, analyser: *Analyser, arena: std.mem.Al
pos_context != .import_string_literal and
pos_context != .cinclude_string_literal and
pos_context != .embedfile_string_literal and
pos_context != .string_literal and
pos_context.loc() != null and
lookahead_context.loc() != null and
pos_context.loc().?.end != lookahead_context.loc().?.end)
Expand Down

0 comments on commit 46de13c

Please sign in to comment.