I’d like to format the document: give it indentation and stuff like that.
Ctrl K + Ctrl D/F in Visual Studio doesn’t work for F#, does anybody have any workaround for that? It does work for C# though…
EDIT: What I’d like to do is to copy-paste some code from an external source. I usually use Ctrl K + Ctrl D to format it, and it works for C#. However, in an .fs file, it doesn’t seem to work. On top of that, indentation seems to be pretty much a must-have for F#…
F# is indentation sensitive, so if you copy valid code from one location to another, the only thing that you might need to do is to make sure it has the right offset from the left side. For example, say you have:
Now, if you wanted to copy the line
(*)and use it instead of a call totest, just Copy & Paste would turn your code into the following:This has a different meaning though! It repeates both of the
printfnlines 10 times. So, instead what you would want to get is this:The way to do Copy & Paste in Visual Studio to keep the same meaning of code is to paste the copied code as usual (Ctrl+V) and then, while the code is still selected, correct the indentation. To indent the code further use Tab and to indent it less far use Shift+Tab.
This way, you can use Copy and Paste for F# code just fine. You do not need to reformat the entire block, because valid F# code copied to another place will automatically be well formatted. You just need to fix the indentation.
Regarding the
#lightmode mentioned in comments – in earlier versions of F#, it was possible to use additional keywords and semicolons instead of indentation-sensitive mode. The modern indentation-sensitive style was called#light, but it is now default and you do not need to worry about the legacy style.