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

What are the configuration specifics for formatting long piped expression? #641

Open
alarbada opened this issue Aug 1, 2021 · 0 comments
Labels
enhancement New feature or request wrapping Incorrect or undesirable wrapping

Comments

@alarbada
Copy link

alarbada commented Aug 1, 2021

Describe the bug

After reading https://code.haxe.org/category/abstract-types/pipe.html I was about to try the example. However, it doesn't properly format long pipe expressions:

Input file

abstract Pipe<T>(T) to T {
    public inline function new(s:T) {
        this = s;
    }

    @:op(A | B)
    public inline function pipe1<U>(fn:T->U):Pipe<U> {
        return new Pipe(fn(this));
    }

    @:op(A | B)
    public inline function pipe2<A, B>(fn:T->A->B):Pipe<A->B> {
        return new Pipe(fn.bind(this));
    }

    @:op(A | B)
    public inline function pipe3<A, B, C>(fn:T->A->B->C):Pipe<A->B->C> {
        return new Pipe(fn.bind(this));
    }

    @:op(A | B)
    public inline function pipe4<A, B, C, D>(fn:T->A->B->C->D):Pipe<A->B->C->D> {
        return new Pipe(fn.bind(this));
    }
}
inline function addWorld(str:String):String {
    return str + " world!";
}

inline function capitalize(str:String):String {
    return str.toUpperCase();
}

inline function count(str:String):Int {
    return str.length;
}

function main() {
    // function call as argument version
    var nestedHelloWorld = capitalize(addWorld("Hello"));
    trace(nestedHelloWorld); // HELLO WORLD!

    // update variable version
    var hello = "Hello";
    hello = addWorld(hello);
    hello = capitalize(hello);
    trace(hello); // HELLO WORLD!

    // piped version
    var helloWorld:String = new Pipe("Hello")
      | addWorld
      | capitalize;
    trace(helloWorld); // HELLO WORLD!

    // piped version changing type from String to Int
    var helloWorldCount:Int = new Pipe("Hello")
      | addWorld
      | capitalize
      | count;
    trace(helloWorldCount / 2); // 6
}

Broken output

abstract Pipe<T>(T) to T {
	public inline function new(s:T) {
		this = s;
	}

	@:op(A | B)
	public inline function pipe1<U>(fn:T->U):Pipe<U> {
		return new Pipe(fn(this));
	}

	@:op(A | B)
	public inline function pipe2<A, B>(fn:T->A->B):Pipe<A->B> {
		return new Pipe(fn.bind(this));
	}

	@:op(A | B)
	public inline function pipe3<A, B, C>(fn:T->A->B->C):Pipe<A->B->C> {
		return new Pipe(fn.bind(this));
	}

	@:op(A | B)
	public inline function pipe4<A, B, C, D>(fn:T->A->B->C->D):Pipe<A->B->C->D> {
		return new Pipe(fn.bind(this));
	}
}

inline function addWorld(str:String):String {
	return str + " world!";
}

inline function capitalize(str:String):String {
	return str.toUpperCase();
}

inline function count(str:String):Int {
	return str.length;
}

function main() {
	// function call as argument version
	var nestedHelloWorld = capitalize(addWorld("Hello"));
	trace(nestedHelloWorld); // HELLO WORLD!

	// update variable version
	var hello = "Hello";
	hello = addWorld(hello);
	hello = capitalize(hello);
	trace(hello); // HELLO WORLD!

	// piped version
	var helloWorld:String = new Pipe("Hello") | addWorld | capitalize;
	trace(helloWorld); // HELLO WORLD!

	// piped version changing type from String to Int
	var helloWorldCount:Int = new Pipe("Hello") | addWorld | capitalize | count;
	trace(helloWorldCount / 2); // 6
}

Expected output

abstract Pipe<T>(T) to T {
	public inline function new(s:T) {
		this = s;
	}

	@:op(A | B)
	public inline function pipe1<U>(fn:T->U):Pipe<U> {
		return new Pipe(fn(this));
	}

	@:op(A | B)
	public inline function pipe2<A, B>(fn:T->A->B):Pipe<A->B> {
		return new Pipe(fn.bind(this));
	}

	@:op(A | B)
	public inline function pipe3<A, B, C>(fn:T->A->B->C):Pipe<A->B->C> {
		return new Pipe(fn.bind(this));
	}

	@:op(A | B)
	public inline function pipe4<A, B, C, D>(fn:T->A->B->C->D):Pipe<A->B->C->D> {
		return new Pipe(fn.bind(this));
	}
}

inline function addWorld(str:String):String {
	return str + " world!";
}

inline function capitalize(str:String):String {
	return str.toUpperCase();
}

inline function count(str:String):Int {
	return str.length;
}

function main() {
	// function call as argument version
	var nestedHelloWorld = capitalize(addWorld("Hello"));
	trace(nestedHelloWorld); // HELLO WORLD!

	// update variable version
	var hello = "Hello";
	hello = addWorld(hello);
	hello = capitalize(hello);
	trace(hello); // HELLO WORLD!

	// piped version
	var helloWorld:String = new Pipe("Hello")
                 | addWorld
                 | capitalize;
	trace(helloWorld); // HELLO WORLD!

	// piped version changing type from String to Int
	var helloWorldCount:Int = new Pipe("Hello")
                | addWorld 
                | capitalize
                | count;
	trace(helloWorldCount / 2); // 6
}

(Note at the end, the piped versions are correctly indented, just like in the docs example).

Optional: hxformat.json

{} // Not customized
@AlexHaxe AlexHaxe added enhancement New feature or request wrapping Incorrect or undesirable wrapping labels Aug 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request wrapping Incorrect or undesirable wrapping
Projects
None yet
Development

No branches or pull requests

2 participants