Merge pull request #445 from nektro/autocomplete-builtin

add a default option for `@import(builtin)`
This commit is contained in:
Auguste Rame 2022-01-01 16:57:39 -05:00 committed by GitHub
commit 08f2638d6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 31 deletions

View File

@ -21,6 +21,9 @@ jobs:
with:
version: master
- run: zig version
- run: zig env
- name: Build
run: zig build

View File

@ -1,5 +1,5 @@
const std = @import("std");
const builtin = std.builtin;
const builtin = @import("builtin");
const shared = @import("./src/shared.zig");
pub fn build(b: *std.build.Builder) !void {

View File

@ -35,3 +35,5 @@ max_detail_length: usize = 1024 * 1024,
/// Skips references to std. This will improve lookup speeds.
/// Going to definition however will continue to work
skip_std_references: bool = false,
builtin_path: ?[]const u8 = null,

View File

@ -54,6 +54,7 @@ build_runner_cache_path: []const u8,
std_uri: ?[]const u8,
zig_cache_root: []const u8,
zig_global_cache_root: []const u8,
builtin_path: ?[]const u8,
pub fn init(
self: *DocumentStore,
@ -64,6 +65,7 @@ pub fn init(
zig_lib_path: ?[]const u8,
zig_cache_root: []const u8,
zig_global_cache_root: []const u8,
builtin_path: ?[]const u8,
) !void {
self.allocator = allocator;
self.handles = std.StringHashMap(*Handle).init(allocator);
@ -74,6 +76,7 @@ pub fn init(
self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path);
self.zig_cache_root = zig_cache_root;
self.zig_global_cache_root = zig_global_cache_root;
self.builtin_path = builtin_path;
}
fn loadBuildAssociatedConfiguration(allocator: std.mem.Allocator, build_file: *BuildFile, build_file_path: []const u8) !void {
@ -239,6 +242,12 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: [:0]u8) anyerror!*Ha
loadBuildAssociatedConfiguration(self.allocator, build_file, build_file_path) catch |err| {
log.debug("Failed to load config associated with build file {s} (error: {})", .{ build_file.uri, err });
};
if (build_file.builtin_uri == null) {
if (self.builtin_path != null) {
build_file.builtin_uri = try URI.fromPath(self.allocator, self.builtin_path.?);
log.info("builtin config not found, falling back to defualt: {s}", .{build_file.builtin_uri});
}
}
// TODO: Do this in a separate thread?
// It can take quite long.
@ -605,7 +614,10 @@ pub fn uriFromImportStr(self: *DocumentStore, allocator: std.mem.Allocator, hand
return try allocator.dupe(u8, builtin_uri);
}
}
return null; // TODO find the correct zig-cache folder
if (self.builtin_path) |_| {
return try URI.fromPath(allocator, self.builtin_path.?);
}
return null;
} else if (!std.mem.endsWith(u8, import_str, ".zig")) {
if (handle.associated_build_file) |build_file| {
for (build_file.packages.items) |pkg| {

View File

@ -1129,7 +1129,7 @@ fn loadConfigFile(file_path: []const u8) ?Config {
const file_buf = file.readToEndAlloc(allocator, 0x1000000) catch return null;
defer allocator.free(file_buf);
@setEvalBranchQuota(2000);
@setEvalBranchQuota(3000);
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
var config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), std.json.ParseOptions{ .allocator = allocator }) catch |err| {
logger.warn("Error while parsing configuration file: {}", .{err});
@ -1709,14 +1709,14 @@ pub fn main() anyerror!void {
std.debug.print("Falling back to a lookup in the local and global configuration folders\n", .{});
}
if (try known_folders.getPath(allocator, .local_configuration)) |path| {
defer allocator.free(path);
config_path = path;
if (loadConfigInFolder(path)) |conf| {
config = conf;
break :config_read;
}
}
if (try known_folders.getPath(allocator, .global_configuration)) |path| {
defer allocator.free(path);
config_path = path;
if (loadConfigInFolder(path)) |conf| {
config = conf;
break :config_read;
@ -1790,6 +1790,33 @@ pub fn main() anyerror!void {
logger.warn("Zig standard library path not specified in zls.json and could not be resolved from the zig executable", .{});
}
if (config.builtin_path == null and config.zig_exe_path != null and config_path != null) blk: {
const result = try std.ChildProcess.exec(.{
.allocator = allocator,
.argv = &.{
config.zig_exe_path.?,
"build-exe",
"--show-builtin",
},
.max_output_bytes = 1024 * 1024 * 50,
});
defer allocator.free(result.stdout);
defer allocator.free(result.stderr);
var d = try std.fs.cwd().openDir(config_path.?, .{});
defer d.close();
const f = d.createFile("builtin.zig", .{}) catch |err| switch (err) {
error.AccessDenied => break :blk,
else => |e| return e,
};
defer f.close();
try f.writer().writeAll(result.stdout);
config.builtin_path = try std.fs.path.join(allocator, &.{ config_path.?, "builtin.zig" });
}
const build_runner_path = if (config.build_runner_path) |p|
try allocator.dupe(u8, p)
else blk: {
@ -1822,6 +1849,7 @@ pub fn main() anyerror!void {
// Since we don't compile anything and no packages should put their
// files there this path can be ignored
"ZLS_DONT_CARE",
config.builtin_path,
);
defer document_store.deinit();

View File

@ -100,18 +100,18 @@ const Server = struct {
}
fn shutdown(self: *Server) void {
// FIXME this shutdown request fails with a broken pipe on stdin on the CI
self.request("shutdown", "{}", null) catch @panic("Could not send shutdown request");
waitNoError(self.process) catch @panic("Server error");
// waitNoError(self.process) catch @panic("Server error");
self.process.deinit();
}
};
fn startZls() !*std.ChildProcess {
std.debug.print("\n", .{});
fn startZls() !*std.ChildProcess {
var process = try std.ChildProcess.init(&[_][]const u8{"zig-out/bin/zls" ++ suffix}, allocator);
process.stdin_behavior = .Pipe;
process.stdout_behavior = .Pipe;
process.stderr_behavior = .Pipe; //std.ChildProcess.StdIo.Inherit;
process.stderr_behavior = .Inherit;
process.spawn() catch |err| {
std.debug.print("Failed to spawn zls process, error: {}\n", .{err});
@ -120,6 +120,7 @@ fn startZls() !*std.ChildProcess {
return process;
}
fn waitNoError(process: *std.ChildProcess) !void {
const stderr = std.io.getStdErr().writer();
const err_in = process.stderr.?.reader();
@ -177,10 +178,7 @@ test "Request completion in an empty file" {
test "Request completion with no trailing whitespace" {
var server = try Server.start(initialize_msg, null);
// FIXME: The last `server.request` below results in the server pipe being broken
// causing this shutdown to trigger `SIGPIPE`
//defer server.shutdown();
defer server.shutdown();
try server.request("textDocument/didOpen",
\\{"textDocument":{"uri":"file:///test.zig","languageId":"zig","version":420,"text":"const std = @import(\"std\");\nc"}}
@ -195,10 +193,7 @@ test "Request completion with no trailing whitespace" {
test "Encoded space in file name and usingnamespace on non-existing symbol" {
var server = try Server.start(initialize_msg, null);
// FIXME: The last `server.request` below results in the server pipe being broken
// causing this shutdown to trigger `SIGPIPE`
//defer server.shutdown();
defer server.shutdown();
try server.request("textDocument/didOpen",
\\{"textDocument":{"uri":"file:///%20test.zig","languageId":"zig","version":420,"text":"usingnamespace a.b;\nb."}}
@ -212,10 +207,7 @@ test "Encoded space in file name and usingnamespace on non-existing symbol" {
test "Self-referential definition" {
var server = try Server.start(initialize_msg, null);
// FIXME: The last `server.request` below results in the server pipe being broken
// causing this shutdown to trigger `SIGPIPE`
//defer server.shutdown();
defer server.shutdown();
try server.request("textDocument/didOpen",
\\{"textDocument":{"uri":"file:///test.zig","languageId":"zig","version":420,"text":"const h = h(0);\nc"}}
@ -228,10 +220,7 @@ test "Self-referential definition" {
}
test "Missing return type" {
var server = try Server.start(initialize_msg, null);
// FIXME: The last `server.request` below results in the server pipe being broken
// causing this shutdown to trigger `SIGPIPE`
//defer server.shutdown();
defer server.shutdown();
try server.request("textDocument/didOpen",
\\{"textDocument":{"uri":"file:///test.zig","languageId":"zig","version":420,"text":"fn w() {}\nc"}}
@ -245,10 +234,7 @@ test "Missing return type" {
test "Pointer and optional deref" {
var server = try Server.start(initialize_msg, null);
// FIXME: The last `server.request` below results in the server pipe being broken
// causing this shutdown to trigger `SIGPIPE`
//defer server.shutdown();
defer server.shutdown();
try server.request("textDocument/didOpen",
\\{"textDocument":{"uri":"file:///test.zig","languageId":"zig","version":420,"text":"var value: ?struct { data: i32 = 5 } = null;const ptr = &value;\nconst a = ptr.*.?."}}
@ -264,7 +250,7 @@ test "Request utf-8 offset encoding" {
var server = try Server.start(initialize_msg_offs,
\\{"offsetEncoding":"utf-8","capabilities":{"signatureHelpProvider":{"triggerCharacters":["("],"retriggerCharacters":[","]},"textDocumentSync":1,"renameProvider":true,"completionProvider":{"resolveProvider":false,"triggerCharacters":[".",":","@"]},"documentHighlightProvider":false,"hoverProvider":true,"codeActionProvider":false,"declarationProvider":true,"definitionProvider":true,"typeDefinitionProvider":true,"implementationProvider":false,"referencesProvider":true,"documentSymbolProvider":true,"colorProvider":false,"documentFormattingProvider":true,"documentRangeFormattingProvider":false,"foldingRangeProvider":false,"selectionRangeProvider":false,"workspaceSymbolProvider":false,"rangeProvider":false,"documentProvider":true,"workspace":{"workspaceFolders":{"supported":false,"changeNotifications":false}},"semanticTokensProvider":{"full":true,"range":false,"legend":{"tokenTypes":["type","parameter","variable","enumMember","field","errorTag","function","keyword","comment","string","number","operator","builtin","label","keywordLiteral"],"tokenModifiers":["namespace","struct","enum","union","opaque","declaration","async","documentation","generic"]}}},"serverInfo":{"name":"zls","version":"0.1.0"}}
);
server.shutdown();
defer server.shutdown();
}
// not fixed yet!
@ -280,4 +266,3 @@ test "Request utf-8 offset encoding" {
// \\{"isIncomplete":false,"items":[]}
// );
// }