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

Generate interface implementation members #129

Merged
merged 26 commits into from
Oct 8, 2020

Conversation

mcon
Copy link
Contributor

@mcon mcon commented May 28, 2020

Summary

Able to generate placeholder implementations of interfaces for a particular class. The implementation does the bare bones, and is at present only able to handle the interface implementation being completely empty.

e.g.

type IPrintable =
   abstract member Print1 : unit -> unit
   abstract member Print2 : string -> unit
type SomeClass1(x: int, y: float) =
   interface IPrintable

to

type IPrintable =
   abstract member Print1 : unit -> unit
   abstract member Print2 : string -> unit
type SomeClass1(x: int, y: float) =
   interface IPrintable with
     member this.Print1 () = failwith "todo"
     member this.Print2 var0 = failwith "todo"

TODO

  • Insertion of the with keyword if it's missing from the IInterfaceImplementation
  • More testing to add around FSharpElementFactory additions
  • Testing of generics
  • Expansion of tupled members
  • Make action a quickfix

@vasily-kirichenko
Copy link
Contributor

I recommend to look at https://github.com/fsprojects/Archive-VisualFSharpPowerTools/blob/master/src/FSharp.Editing/CodeGeneration/InterfaceStubGenerator.fs for ideas etc.

@auduchinok
Copy link
Member

auduchinok commented Jun 2, 2020

Please make it a quick fix instead of a context action. Here's an example QF working on the same/similar errors you're after: b28b01d.

@mcon
Copy link
Contributor Author

mcon commented Jun 5, 2020

Hi @auduchinok, this one's pretty much good to go, but can't get the quick fix for an IInterfaceImplementation to be recognized - any ideas why that might be?

BTW I realise this doesn't cover object expressions, I'm fairly sure I can generalise this in future.

@mcon mcon marked this pull request as ready for review June 11, 2020 20:55
@dbarbashov
Copy link

Great feature! Any progress on this?

@mcon
Copy link
Contributor Author

mcon commented Aug 3, 2020

Hi @auduchinok,

I've merged in net202, and worked around the changes in member grammar - looks to me like a small tweak was required to the parameters, and I've added a parser testcase for that too.

Taking a look at making this one a quick fix, it seems that I'd be looking to match a parse error (https://github.com/fsharp/FSharp.Compiler.Service/blob/1d173b6e753199f4598f6283ff5ecac811cad18e/src/fsharp/FSStrings.resx#L486), which gets mapped to SyntaxError (https://github.com/fsharp/FSharp.Compiler.Service/blob/9.0.0/src/fsharp/CompileOps.fs#L258), which would then get triggered for every syntax error in a file - I could update the PR to work that way, but I don't know what would be best for performance. If you could let me know I'll update it however you'd like.

Cheers,
Matt

@auduchinok auduchinok changed the base branch from net202 to net203 September 28, 2020 13:31
@auduchinok
Copy link
Member

auduchinok commented Sep 28, 2020

Hi @mcon,

I'm sorry for the late response here. I've merged in net203, made it a quick fix instead of a context action (or an intention, in IntelliJ terms), and did some cleanup.

and worked around the changes in member grammar - looks to me like a small tweak was required to the parameters, and I've added a parser testcase for that too.

It was almost correct: there actually may be no parameters pattern, and that would mean the member declaration is a property declaration.

Taking a look at making this one a quick fix, it seems that I'd be looking to match a parse error

Yes, if there's a with keyword but no actual members, it fails to parse the interface implementation. It should be handled better in the FCS parser by adding better error recovery, so we could use the interface implementation node here. The quick fix currently will work only when there's no with keyword or if there're any members.

What else is needed to be done here (I'll add more if anything comes to my mind):

  • convert action to quick fix
  • generate properties
  • handle escaped names
  • handle partially implemented interfaces
  • inherited interfaces
  • handle method overloads (check signatures instead of member names)
  • print types where needed for overloads (including for already implemented members)
  • handle type parameter substitution when printing types
  • generate events
  • separate getters/setters (and other accessors?) (including already implemented parts)
  • add code style setting for (not) always generating parens
  • enable for object expressions
  • optional: generate missing parameter names based on types

@auduchinok
Copy link
Member

auduchinok commented Oct 8, 2020

I've implemented things needed for what it seems the most common cases (the todo list has became rather long here) and am going to handle the missing parts out of scope of this PR.

@auduchinok auduchinok merged commit b9878f5 into JetBrains:net203 Oct 8, 2020
@auduchinok
Copy link
Member

auduchinok commented Oct 8, 2020

@mcon Thanks for your contribution! It worked as a good starting point, and we're almost there now, I think it'll work great once the remaining pieces are done.

@mcon mcon deleted the add-interface-implementation branch October 9, 2020 16:52
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.

4 participants