zls icon indicating copy to clipboard operation
zls copied to clipboard

Add autocompletion for things that are checked at comptime.

Open echoptic opened this issue 2 years ago • 1 comments

Here I have a simple example that checks whether T contains the field x at comptime. This of course doesnt have to be limited to fields, but everything in std.meta.trait. It would be great if zls could detect what is checked in the comptime block and know that self.inner will 100% contain the field x so it will have autocompletion when you access self.inner.x.

const std = @import("std");
const assert = std.debug.assert;

pub fn main() !void {
    var sth: Something(struct { x: u32 }) = .{
        .inner = .{ .x = 21 },
    };
    sth.printX();
}

fn Something(comptime T: type) type {
    comptime {
        assert(std.meta.trait.hasField("x")(T));
    }
    return struct {
        inner: T,

        fn printX(self: @This()) void {
            std.debug.print("{}\n", .{self.inner.x});
        }
    };
}

echoptic avatar May 25 '23 20:05 echoptic

#1105 is trying to solve a similar problem with a different approach. Compared to that Issue, this one appears to be much harder to implemented because how do handle functions in std.meta.trait, hard code their behavior or infer it somehow? What if someone has their own trait function and ZLS can't handle that one?

Techatrix avatar Oct 11 '23 03:10 Techatrix