fsprojects / zarchive-fsharpbinding

Archive of F# Language Bindings for Open Editors
Other
307 stars 106 forks source link

Integrate source code formatting feature #287

Closed dungpa closed 10 years ago

dungpa commented 10 years ago

It has been discussed for a while (see https://github.com/dungpa/fantomas/issues/5), but nothing has been done yet.

Recently we fixed a few major bugs and got Fantomas working on Mono. I would like to get it integrated into F# Binding.

Here are the supported features:

After playing with Xamarin Studio, I can see that they have entry points for formatting document and formatting selection. I'm not sure where I could integrate formatting around cursor.

Here is my rough plan:

Non-goals:

I've got minimal version working at https://github.com/dungpa/fsharpbinding/tree/code_formatting before hitting a few issues.

As I don't have any prior experience with Xamarin Studio, I will ask questions as much as I can.

dungpa commented 10 years ago

First questions:

  1. How do you trace F# Binding code? I use Debug.WriteLine and devenv /debugexe "c:\Program Files (x86)\Xamarin Studio\bin\XamarinStudio.exe" in order to debug XS using Visual Studio. However, there is no output printed in Visual Studio's Output window.
  2. Where can I query F# file types (fsi vs. fs and fsx)? F# formatting is different for signature files and implementation files. The formatting entry point only provides input string (see https://github.com/mono/monodevelop/blob/master/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.CodeFormatting/ICodeFormatter.cs).
7sharp9 commented 10 years ago

Whats the exception?

  1. I debug XS with XS, I just set the output action to run an instance of XS
  2. To get the rule type use: Document.Editor.MimeType
rneatherway commented 10 years ago

I normally run XS from the command line with --no-redirect and add a ConsoleTraceListener so it comes out on standard out.

System.Diagnostics.Trace.Listeners.Add(
    new System.Diagnostics.TextWriterTraceListener(Console.Out));

On Thu, Jan 9, 2014 at 3:39 PM, Anh-Dung Phan notifications@github.comwrote:

First questions:

1.

How do you trace F# Binding code? I use Debug.WriteLine and devenv /debugexe "c:\Program Files (x86)\Xamarin Studio\bin\XamarinStudio.exe"in order to debug XS using Visual Studio. However, there is no output printed in Visual Studio's Output window. 2.

Where can I query F# file types (fsi vs. fs and fsx). F# formatting is different for signature files and implementation files.

— Reply to this email directly or view it on GitHubhttps://github.com/fsharp/fsharpbinding/issues/287#issuecomment-31943657 .

7sharp9 commented 10 years ago

@dungpa Maybe mimeTypeChain.First () would get you the current mime type, you would have to try it...

I never use console output, this is my CustomCommand for Execute:

    <CustomCommands>
      <CustomCommands>
        <Command type="Execute" command="&quot;/Applications/Xamarin Studio.app/Contents/MacOS/lib/monodevelop/bin/XamarinStudio.exe&quot;" workingdir="/Applications/Xamarin Studio.app/Contents/MacOS/lib/monodevelop/bin" />
      </CustomCommands>
    </CustomCommands>

Remember if you want to hit a breakpoint you need to make sure to debug symbols are present in the fsharpbindings addin directory.

dungpa commented 10 years ago

I tried Robin's technique; it doesn't seem to work on Windows. Might have to switch platforms to debug it further. Sigh.

7sharp9 commented 10 years ago

If you give me a few hours I might be able to look at the exception later on tonight …

On 9 Jan 2014, at 16:27, Anh-Dung Phan notifications@github.com wrote:

I tried Robin's technique; it doesn't seem to work on Windows. Might have to switch platforms to debug it further. Sigh.

— Reply to this email directly or view it on GitHub.

rneatherway commented 10 years ago

Where did you try adding the TraceListener? I normally put it in the constructor of LanguageService. Not particularly elegant I know...

On Thu, Jan 9, 2014 at 4:27 PM, Anh-Dung Phan notifications@github.comwrote:

I tried Robin's technique; it doesn't seem to work on Windows. Might have to switch platforms to debug it further. Sigh.

— Reply to this email directly or view it on GitHubhttps://github.com/fsharp/fsharpbinding/issues/287#issuecomment-31949202 .

7sharp9 commented 10 years ago

@dungpa You can also debug XS from Visual Studio, I used to do that before I got debugging working, just open the source file in VS and add a break point to the command and step in.

dungpa commented 10 years ago

I tried to compile FSB on Mac, and hit this bug https://github.com/fsharp/fsharpbinding/issues/257. Where shall I manually copy dlls in order that XS picks them up?

7sharp9 commented 10 years ago

probably something like /Users/xxx/Library/Application Support/XamarinStudio-4.0/LocalInstall/Addins/MonoDevelop.FSharpBinding.3.2.22

7sharp9 commented 10 years ago

@dungpa @rneatherway I wonder what causes the pack bug, never seen it myself?

ghost commented 10 years ago

Nor me, built fine for me after a configure.sh

rneatherway commented 10 years ago

The only time I have seen it was when I ran configure.sh then make pack before having ever run monodevelop on a clean VM. I verified that the mdtool command that failed was the same as usual and tried running it manually on the command line where it also failed. I then launched and closed monodevelop and reran the mdtool command, it then succeeded.

Without another way to reproduce it, I think it is a monodevelop/mdtool bug and we should close it here.

On Thu, Jan 9, 2014 at 11:22 PM, Don Syme notifications@github.com wrote:

Nor me, built fine for me after a configure.sh

— Reply to this email directly or view it on GitHubhttps://github.com/fsharp/fsharpbinding/issues/287#issuecomment-31988200 .

dungpa commented 10 years ago

@7sharp9 MimeType doesn't help. It's always text/x-fsharp for both fs and fsi files. Would it be better to separate fsi files to use another mime type? Or do you have an easier way to recognize fsi files?

7sharp9 commented 10 years ago

Some of the MimeTypes is in MonoDevelop and some in the F# binding, the reason icons dont work for our mime types is they use the icon definition in MonoDevelop ... which is empty.

Only the following mime types are registered in MonoDevelop:

    <MimeType id="text/x-fsharp" _description="f# files" isText="true">
        <File pattern="*.fs" />
        <File pattern="*.fsx" />
    </MimeType>

They are needed in MonoDevelop because the syntax highlighting is in MonoDevelop too, which has a dependency on registed mime types, we are kind of in a half merged state ...

7sharp9 commented 10 years ago

@dungpa Can you use DesktopService.GetMimeTypeDescription(...) on the mime types to get a description? What mime types are in the sequence?

7sharp9 commented 10 years ago

@dungpa I get errors when I format selection: It uses a selection different to what I selected and corrupts the output, It also moves the view off screen.

7sharp9 commented 10 years ago

@dungpa Format document doesn't adjust the text editors state to modified so the file is not showing the edited icon. If you press Cmd S after to save it isn't persisted.

dungpa commented 10 years ago

Can you use DesktopService.GetMimeTypeDescription(...) on the mime types to get a description?

It doesn't work either. I get two descriptions: f# files and Text file for both fs and fsi files.

Why doesn't it return our registered mime types at https://github.com/fsharp/fsharpbinding/blob/master/monodevelop/MonoDevelop.FSharpBinding/FSharpBinding.addin.xml.orig#L112? I would expect them to override MonoDevelop mime types.

My suggestion is to create text/x-fsharpsig mime type for signature files and pull request upstreams to update these on MonoDevelop.

7sharp9 commented 10 years ago

In addition, the code that is selected for formatting should be selected after formatting too.

7sharp9 commented 10 years ago

@dungpa You would have to debug monodevelop to see why the mime types are not there, and raise a bugzilla bug if there is an issue with monodevelop.

7sharp9 commented 10 years ago

The following selected code:

screen shot 2014-01-10 at 20 59 19

is formatted to:

screen shot 2014-01-10 at 21 00 16

7sharp9 commented 10 years ago

@dungpa I would inherit from the abstract AdvancedCodeFormatter and return false for the features we are not supporting at the moment. just override the FormatTextmethod

dungpa commented 10 years ago

I migrated to AbstractAdvancedFormatter and fixed the bug in formatting selection at https://github.com/dungpa/fsharpbinding/commit/74da3f0e5ce3137ec3bd48c026e2ffc016969c12.

@7sharp9 Could you take a look at saving formatted document? I spent a couple of hours on it, but wasn't able to solve it.

7sharp9 commented 10 years ago

I can take a look when I get a chance, the best advice it to follow what's done in the C# formatter class.

Sent from my iPhone

On 11 Jan 2014, at 08:04, Anh-Dung Phan notifications@github.com wrote:

I migrated to AbstractAdvancedFormatter and fixed the bug in formatting selection at dungpa@74da3f0.

@7sharp9 Could you take a look at saving formatted document? I spent a couple of hours on it, but wasn't able to solve it.

— Reply to this email directly or view it on GitHub.

7sharp9 commented 10 years ago

Closing this as #291 has the latest details.