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

Extract setting for preference of newline placement for computation expressions #2746

Merged
merged 19 commits into from
Feb 2, 2023

Conversation

josh-degraw
Copy link
Contributor

I'm very open to bike-shedding the name of the setting here, I just went with what made the most sense in my head at the time but I'm open to alternatives if something makes more sense.

Closes #2276

Copy link
Contributor

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @josh-degraw, sorry for the slow response. I've been travelling last week.

From afar this PR looks good. I would explore the option to port some of the Oak code to Context.

let isStroustrup (config: FormatConfig) (e: Expr) =
    let isMultiRecord = config.MultilineBracketStyle = ExperimentalStroustrup

    match e with
    | Expr.Record node when isMultiRecord ->
        match node.Extra with
        | RecordNodeExtra.Inherit _
        | RecordNodeExtra.With _ -> false
        | RecordNodeExtra.None -> true
    | Expr.AnonRecord node when isMultiRecord ->
        match node.CopyInfo with
        | Some _ -> false
        | None -> true
    | Expr.ArrayOrList _ when isMultiRecord -> true
    | Expr.NamedComputation _ when config.PreferComputationExpressionNameOnSameLine -> true
    | _ -> false

I would however like to have another name for this.
The Prefer in PreferComputationExpressionNameOnSameLine really rubs me the wrong way. The word prefer is a bit excessive, given that it is a setting, it already implies a preference. On the top of my head, I would go in the direction of StroustrupComputationExpression. I'm not sure that exact name is what we want though, but I think Stroustrup is just so well known on the streets that I would somehow incorporate it.

If I understand the code right now, you only need to enable the setting to do something.
Having MultilineBracketStyle = Stroustrup is not required to enable the code. If that is the case, I do like that.

Lastly, this would need documentation. Perhaps we need to update the configuration.fsx script to use a local version of Fantomas.Core instead of a version on NuGet. Otherwise, we always need to play catch-up.


if isStroustrup then
if isNamedComputationAndPreferSameLine e ctx || isStroustrup then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is TriviaBefore in the NamedComputation that should be captured as well.
Example:

[<Test>]
let ``prefer computation expression name on same line, with trivia`` () =
    formatSourceString
        false
        """
let t =
    //
    task {
        let! thing = otherThing ()
        return 5
    }
"""
        config
    |> prepend newline
    |> should
        equal
        """
let t =
    //
    task {
        let! thing = otherThing ()
        return 5
    }
"""

@@ -1738,10 +1738,6 @@ type Expr =
match node.CopyInfo with
| Some _ -> false
| None -> true
| Expr.NamedComputation node ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought but I think this member should be extracted into a function in Context.
If you have that, you can pass the FormatConfig into it and just add an extra when expression for the named computation expression.

I think this makes sense as it further removes the coupling between the Oak model and any coding style.

By refactoring this, I think the current flow could be preserved slightly easier.

@josh-degraw
Copy link
Contributor Author

If I understand the code right now, you only need to enable the setting to do something. Having MultilineBracketStyle = Stroustrup is not required to enable the code. If that is the case, I do like that.

Yep, that was the goal, this setting should be able to be enabled whether or not the user has enabled Stroustrup globally.

I would however like to have another name for this. The Prefer in PreferComputationExpressionNameOnSameLine really rubs me the wrong way. The word prefer is a bit excessive, given that it is a setting, it already implies a preference.

I agree the name is pretty chunky, I think removing Prefer makes total sense.

On the top of my head, I would go in the direction of StroustrupComputationExpression. I'm not sure that exact name is what we want though, but I think Stroustrup is just so well known on the streets that I would somehow incorporate it.

I see what you mean, the only hesitation I have to that is that like you mentioned this is intended to be fully orthogonal to the Stroustrup setting, and I worry that it could cause confusion to have the name on this setting. I think ComputationExpressionNameSameLine or SameLineComputationExpressionName or ShortComputationExpressions or CompressComputationExpressions might work, but I'm not in love with either of them either ¯\(ツ)/¯.

@nojaf
Copy link
Contributor

nojaf commented Jan 31, 2023

I have to that is that like you mentioned this is intended to be fully orthogonal to the Stroustrup setting, and I worry that it could cause confusion to have the name on this setting.

Yeah, I can see why you don't want to attach it to the Stroustrup brand. It is less obvious to think about computation expressions in that way.

I'm not overly crazy about the SameLine wording. That feels a bit clunky still. However, no clue how to improve it. ComputationExpressionStartOnCurrentLine, I think Start is more general instead of Name (technically it could be an expression and not only a name). I'm not sure CurrentLine is any better than SameLine ¯(ツ)/¯.

@josh-degraw
Copy link
Contributor Author

An alternative could be to do something like NewlineBeforeComputationExpression that defaults to true? I like the name better but would it be confusing at all to have a setting that defaults to true? I originally thought so but now I'm not sure.

@nojaf
Copy link
Contributor

nojaf commented Jan 31, 2023

Oh, I like that one. Maybe NewlineBeforeMultilineComputationExpression to remove all ambiguity.
(Although it becomes long again that way).

@josh-degraw
Copy link
Contributor Author

josh-degraw commented Jan 31, 2023

Yeah that's longer but I think I'm fine with that. It's long but it still doesn't feel quite as clunky as my original suggested name, IMO. I'm fine with that if you are.

src/Fantomas.Core/Context.fs Outdated Show resolved Hide resolved
@nojaf
Copy link
Contributor

nojaf commented Feb 1, 2023

This looks good! The only thing I'm really missing is a documentation entry.
I've changed the script to use a local Fantomas.Core instead of grabbing it from NuGet.
Could you add a section for the new setting, please?

Copy link
Contributor

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! I'm very happy you pulled this off with minimal changes.

@nojaf nojaf merged commit 5828265 into fsprojects:v6.0 Feb 2, 2023
nojaf added a commit that referenced this pull request Feb 3, 2023
…xpressions (#2746)

* Add new setting

* Add test

* Implement new setting

* Add setting-specific tests to its own file

* Fix an issue with applying to lambdas

* Fix issue when applying to LetBang and AndBang

* Fix all failing tests, some cleanup

* Remove invalid tests, some more cleanup

* Rename some helpers

* temp

* Cleanup, add test

* Rename setting

* Use local Fantomas.Core reference for Configuration.fsx.

* Rename some things and fix assertion

* Update docs

* Simplify isStroustrupStyleExpr

* Rename test file to match setting name.

* Add additional test to highlight that settings don't affect each other.

---------

Co-authored-by: nojaf <[email protected]>
nojaf added a commit that referenced this pull request Feb 4, 2023
…xpressions (#2746)

* Add new setting

* Add test

* Implement new setting

* Add setting-specific tests to its own file

* Fix an issue with applying to lambdas

* Fix issue when applying to LetBang and AndBang

* Fix all failing tests, some cleanup

* Remove invalid tests, some more cleanup

* Rename some helpers

* temp

* Cleanup, add test

* Rename setting

* Use local Fantomas.Core reference for Configuration.fsx.

* Rename some things and fix assertion

* Update docs

* Simplify isStroustrupStyleExpr

* Rename test file to match setting name.

* Add additional test to highlight that settings don't affect each other.

---------

Co-authored-by: nojaf <[email protected]>
nojaf added a commit that referenced this pull request Feb 22, 2023
…xpressions (#2746)

* Add new setting

* Add test

* Implement new setting

* Add setting-specific tests to its own file

* Fix an issue with applying to lambdas

* Fix issue when applying to LetBang and AndBang

* Fix all failing tests, some cleanup

* Remove invalid tests, some more cleanup

* Rename some helpers

* temp

* Cleanup, add test

* Rename setting

* Use local Fantomas.Core reference for Configuration.fsx.

* Rename some things and fix assertion

* Update docs

* Simplify isStroustrupStyleExpr

* Rename test file to match setting name.

* Add additional test to highlight that settings don't affect each other.

---------

Co-authored-by: nojaf <[email protected]>
nojaf added a commit that referenced this pull request Mar 17, 2023
…xpressions (#2746)

* Add new setting

* Add test

* Implement new setting

* Add setting-specific tests to its own file

* Fix an issue with applying to lambdas

* Fix issue when applying to LetBang and AndBang

* Fix all failing tests, some cleanup

* Remove invalid tests, some more cleanup

* Rename some helpers

* temp

* Cleanup, add test

* Rename setting

* Use local Fantomas.Core reference for Configuration.fsx.

* Rename some things and fix assertion

* Update docs

* Simplify isStroustrupStyleExpr

* Rename test file to match setting name.

* Add additional test to highlight that settings don't affect each other.

---------

Co-authored-by: nojaf <[email protected]>
nojaf added a commit that referenced this pull request Mar 27, 2023
…xpressions (#2746)

* Add new setting

* Add test

* Implement new setting

* Add setting-specific tests to its own file

* Fix an issue with applying to lambdas

* Fix issue when applying to LetBang and AndBang

* Fix all failing tests, some cleanup

* Remove invalid tests, some more cleanup

* Rename some helpers

* temp

* Cleanup, add test

* Rename setting

* Use local Fantomas.Core reference for Configuration.fsx.

* Rename some things and fix assertion

* Update docs

* Simplify isStroustrupStyleExpr

* Rename test file to match setting name.

* Add additional test to highlight that settings don't affect each other.

---------

Co-authored-by: nojaf <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants