Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

give filesystem completions on string literals in build files #1668

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
) ![]types.CompletionItem {
var completions: DocumentScope.CompletionSet = .{};

const loc = pos_context.loc().?;
const loc = switch (pos_context) {

Check warning on line 825 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L825

Added line #L825 was not covered by tests
.import_string_literal,
.cinclude_string_literal,
.embedfile_string_literal,
.string_literal,
=> |loc| loc,
else => unreachable,

Check warning on line 831 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L831

Added line #L831 was not covered by tests
};

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

var separator_index = completing.len;
Expand Down Expand Up @@ -852,6 +865,7 @@
.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 @@
.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;

Check warning on line 972 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L969-L972

Added lines #L969 - L972 were not covered by tests
};
},
else => null,
};
Expand All @@ -968,6 +986,7 @@
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