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

Anonymous function in struct breaks indentation #432

Closed
andy-hanson opened this issue Feb 16, 2019 · 2 comments
Closed

Anonymous function in struct breaks indentation #432

andy-hanson opened this issue Feb 16, 2019 · 2 comments

Comments

@andy-hanson
Copy link

andy-hanson commented Feb 16, 2019

Input:

import std.stdio : writeln;

struct S {
	ulong x;
	ulong y;
	ulong function(ulong) f;
}

immutable S s = {
	1111111111111111111,
	1111111111111111111,
	(x) {
		return x + 1111;
	},
};

void main() {
	writeln(s.f(1));
}

Output:

import std.stdio : writeln;

struct S
{
    ulong x;
    ulong y;
    ulong function(ulong) f;
}

immutable S s = {
    1111111111111111111, 1111111111111111111, (x) { return x + 1111; },};

    void main()
    {
        writeln(s.f(1));
    }
@andy-hanson andy-hanson changed the title Anonymous function in struct breaks indentation Long struct initializer breaks indentation Feb 16, 2019
@andy-hanson andy-hanson changed the title Long struct initializer breaks indentation Anonymous function in struct breaks indentation Feb 16, 2019
@andy-hanson
Copy link
Author

It looks like an anonymous function and a long struct initializer are needed, but there doesn't need to be nesting:

import std.stdio : writeln;

struct S {
	ulong x;
	ulong y;
	ulong z;
	ulong w;
}

immutable int function(int) f = (x) {
	return x + 1111;
};

immutable S s = {
	1111111111111111111,
	1111111111111111111,
	1111111111111111111,
	1111111111111111111,
};

void main() {
}

becomes:

struct S
{
    ulong x;
    ulong y;
    ulong z;
    ulong w;
}

immutable int function(int) f = (x) { return x + 1111; };

immutable S s = {
    1111111111111111111, 1111111111111111111, 1111111111111111111, 1111111111111111111,};

    void main()
    {
    }

WebFreak001 pushed a commit that referenced this issue Oct 18, 2023
Overview:
Array astInformation.structInitEndLocations is used to find index to use
in astInformation.indentInfoSortedByEndLocation.
Both are sorted, first is used to find the array index, second is
accessed at that index to get brace indentation information.

Problem:
structInitEndLocations is generated from struct initializers
exclusively while the brace information array also contains entries
for function literal initializers. Thus when function literal init(s)
are used, we get accumulating off by one errors in the second array:

match value in structInitEndLocations and take that index:
[3, 50]
    ^--> index 1

take brace indent information from that index:
[3, 15, 50]
    |   ^--> the one we want
    ^------> the one we get (function literal init)

Solution:
This guarantees that searching forward works.
While better search strategies than linear are possible, this should be
enough for any sane and most of the insane code files.
@WebFreak001
Copy link
Member

fixed in #590

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants