F# code formatting: adjust spaces

1.2k views Asked by At

I use VS2015 to write code in F#. I usually used R# to format C# code and now I try to find something similiar but for F#.

The main goal is to adjust spaces. For example

MethodName (var1,var2  )|> SomeMethod    |> SomeMethod

should be converted to :

MethodName ( var1, var2 ) |> SomeMethod |> SomeMethod

What tool can format my code in such manner?
How to do it?

Update1

I have tried Visual F# Power Tools and it doesn't work for me.

Thats why i asked here - to receive community opinion about different tools that can format code in F#

2

There are 2 answers

4
Guy Coder On BEST ANSWER

I thought this was a duplicate question but could not find it.

The problem with finding the answer by searching such as with Google is that you run into F# Formatting but that is not what you want.

The key is knowing that the project is named fantomas.

How to install Fantomas

It can be installed as a stand alone extension but is more commonly installed as part of the F# Power Tools.

Using Visual Studio

From the VS menu
Tools -> Extensions and Updates...

enter image description here

enter image description here

In the left index click Online

enter image description here

In the search box in the upper right enter F# Power Tools

enter image description here

In the list in the middle select Visual F# Power Tools

enter image description here

Click Download

enter image description here

enter image description here

Click Install

enter image description here

Click Restart Now

Switch to the source code tab

Quick demo of using Fantomas

Note: For this example I had to use different code than provided in the question because this only works with valid F# code and the code in the question was not valid F# code.

enter image description here

let somefunction x = x

let functionName   var1    var2 = var1   |> somefunction       |> somefunction

Ctrl-K, D (Press Ctrl-K, then Ctrl-D)

enter image description here

let somefunction x = x

let functionName var1 var2 = 
    var1
    |> somefunction
    |> somefunction

Setting options for Fantomas

From Visual Studio menu

Tools -> Options...

enter image description here

enter image description here

In left index scroll down and select F# Power Tools

enter image description here

Select Formatting

enter image description here

0
marklam On

You could try Chris McGrath's Code Alignment extension, that's the one I use. It provides toolbar buttons which allow you to align the text on the selected lines by predefined or custom character strings.