zig icon indicating copy to clipboard operation
zig copied to clipboard

better handle indentation when rendering source code

Open andrewrk opened this issue 1 year ago • 0 comments

Extracted from #19249.

Example

pub fn count(self: Self) usize {
            return self.entries.len;
        }

Instead it should render like this:

pub fn count(self: Self) usize {
    return self.entries.len;
}

This happens because it is rendering only the function declaration directly from the file, which in fact is indented two levels. The rendering function needs to detect this and chop off the indentation levels.

Some Examples

Case 1

Input:

    fn add(a: u32, b: u32) u32 {
        return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
    return a + b;
}

Case 2

Input:

    const a = 1; fn add(a: u32, b: u32) u32 {
        return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
    return a + b;
}

Case 3

Input:

    fn add(a: u32, b: u32) u32 {
return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
return a + b;
}

Case 4

Input:

    const a = 1; fn add(a: u32, b: u32) u32 {
return a + b;
    }

Expected Output:

fn add(a: u32, b: u32) u32 {
return a + b;
}

Note that those last two cases are planned to become syntax errors:

  • #35

andrewrk avatar Mar 13 '24 23:03 andrewrk