Skip to content

WIP: Adds support for attributes on modules, types and members - #467

Merged
matthid merged 1 commit into
fsprojects:masterfrom
kblohm:supportForAttributes
May 6, 2018
Merged

matthid merged 1 commit into
fsprojects:masterfrom
kblohm:supportForAttributes

Conversation

@kblohm

@kblohm kblohm commented May 5, 2018

Copy link
Copy Markdown
Contributor

Hi,
i have added support for attributes declared on types, members and modules.
There are also some functions to support the System.ObsoleteAttribute so it is easier to mark stuff as obsolete in documentation.

There is some stuff i am not quite sure about:

  • As of now i do not add attributes to function parameters etc. That would probably not be too hard in general, but MemberOrValue.TypeArguments is just a string list right now, so its not really compatible.
  • I have also not modified any templates yet. There are functions to format and an attribute/ multiple attributes, but i did not want to break all the existing templates. I am not sure whats the right approach here.
  • If there are any plans to add attributes into the types list of a namespace/module, there might be some naming conflicts later on. But i am not sure if that is too important.
  • I added the attributes for members right on the Member type and not on MemberOrValue. I am not sure what u prefer here.

Some side notes:

  • the editorconfig enforces 4 spaces but alot of the files use 2. That makes it kinda hard to work with ;)
  • is it just me or does the intellisense not work in vscode? I had to use vs. Not sure what i am doing wrong here :(

@matthid

matthid commented May 5, 2018

Copy link
Copy Markdown
Member

Thanks for taking care of this. It will help a lot!

As of now i do not add attributes to function parameters etc. That would probably not be too hard in general, but MemberOrValue.TypeArguments is just a string list right now, so its not really compatible.

I guess you mean

type MemberOrValue =
{ Usage : int -> string
Modifiers : string list
TypeArguments : string list
Signature : string
SourceLocation : string option
CompiledName : string option }
member x.FormatUsage(maxLength) = x.Usage(maxLength)
member x.FormatTypeArguments = String.concat ", " x.TypeArguments
member x.FormatModifiers = String.concat " " x.Modifiers
member x.FormatSourceLocation = defaultArg x.SourceLocation ""
member x.FormatCompiledName = defaultArg x.CompiledName ""
static member Create(usage, mods, typars, sign, location, compiledName) =
{ Usage = usage; Modifiers = mods; TypeArguments = typars;
Signature = sign; SourceLocation = location; CompiledName = compiledName }
type MemberKind =
// In a module
| ValueOrFunction = 0
| TypeExtension = 1
| ActivePattern = 2
// In a class
| Constructor = 3
| InstanceMember = 4
| StaticMember = 5
// In a class, F# special members
| UnionCase = 100
| RecordField = 101
| StaticParameter = 102
type Member =
{ Name : string
Category : string
Kind : MemberKind
Details : MemberOrValue
Comment : Comment }
static member Create(name, kind, cat, details, comment) =
{ Member.Name = name; Kind = kind;
Category = cat; Details = details; Comment = comment }

If we really want this we would just add a "properly" typed member and either leave the existing one in place or add a forwarding property. We only really care about source code compat and not really about binary compat (as razor templates are re-compiled every time anyway). But I feel like we can leave it as-is as we currently only care about the obsolete information anyway.

I have also not modified any templates yet. There are functions to format and an attribute/ multiple attributes, but i did not want to break all the existing templates. I am not sure whats the right approach here.

Not sure what you mean there? How will changing the templates break stuff? Or do you mean using the templates with older versions of the library? I guess we don't really need to be compatible in that way, because people usually copy (and modify) the templates or they use the ones bundled with the nuget package (but again it will contain a compatible version).

If there are any plans to add attributes into the types list of a namespace/module, there might be some naming conflicts later on. But i am not sure if that is too important.

Again I'm not sure I understand.

I added the attributes for members right on the Member type and not on MemberOrValue. I am not sure what u prefer here.

Honestly I'm not sure about that split either, maybe @tpetricek can enlighten us both? But if not I guess it will be fine ;) Probably only some implementation reason to make stuff easier.

the editorconfig enforces 4 spaces but alot of the files use 2. That makes it kinda hard to work with ;)

Feed free to change to 2 I guess ;)

is it just me or does the intellisense not work in vscode? I had to use vs. Not sure what i am doing wrong here :(

I hadn't opened this solution for a while now.
/cc @Krzysztof-Cieslak

@kblohm

kblohm commented May 5, 2018

Copy link
Copy Markdown
Contributor Author

Not sure what you mean there? How will changing the templates break stuff? Or do you mean using the templates with older versions of the library? I guess we don't really need to be compatible in that way, because people usually copy (and modify) the templates or they use the ones bundled with the nuget package (but again it will contain a compatible version).

It would not really break stuff but someone who uses the default template might be surprised. So the general question i should have asked is probably: Should a signature with attributes (not just obsolete) be the default in a template? Should it be optional? If none of the two we can leave it as is. Otherwise i can try to think of a reasonable default in a template.

Again I'm not sure I understand.

After reading that again, i did not really understand it myself anymore ;P.
So let me rephase that:
If i define an attribute in my API, is it currently listed under any of the types so i can generate documentation for it? If not, is this planned? Because if it is planned, it might be confusing what attribute property is for the attributes my types consume (for example the obsolete one) and what are the attributes i declared myself and want to have documented. Does that make sense?

Feed free to change to 2 I guess ;)

I can also change the file to use 4 if u dont mind. I like 4;)

@matthid

matthid commented May 5, 2018

Copy link
Copy Markdown
Member

It would not really break stuff but someone who uses the default template might be surprised.

If people do not copy but reference our template, they should expect it to change and they decided to opt-in into new features (at least that's my standpoint on this)

If i define an attribute in my API, is it currently listed under any of the types so i can generate documentation for it?

Yes it is a regular type?

Because if it is planned, it might be confusing what attribute property is for the attributes my types consume (for example the obsolete one) and what are the attributes i declared myself and want to have documented.

In the razor-template you can filter out and write custom code to handle that if it is useful for someone.

I can also change the file to use 4 if u dont mind. I like 4;)

In general yes, but please in a separate PR for this. remembering how some modules are written I expect some modules to go "quite a bit to the right" and might need some additional refactoring if you really want to start this "mess" ;)

@Krzysztof-Cieslak

Copy link
Copy Markdown
Member

Seems to be working for me, but initialization/loading of projects at startup is bit slow.

screenshot from 2018-05-05 20-13-37

@kblohm

kblohm commented May 6, 2018 •

Copy link
Copy Markdown
Contributor Author

@Krzysztof-Cieslak thanks for checking, apparently i am doing something wrong :(.

Yes it is a regular type?

of course. Sorry, i did not really make clear what i meant. As far as i can see u just cant tell if a type is a module or a class or an attribute right now. So if there were support for that the naming might be confusing.

I will try to come up with something for razor that looks OK. I do like how DocFx generated pages handle attributes etc. Example
or how Microsoft F# docs do it here.. Both add the whole signature including attributes. I find that much easier to see everythink at once. I dont think that is really possible right now though and maybe not even wanted. (Or just something that could be added later)

@matthid do u know how i can get proper intellisense for the razor templates? Or is that just me again and it is working for you. VS does not recognize the Model.

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

As far as i can see u just cant tell if a type is a module or a class or an attribute right now.

I think basically the logic is: If some base class in the hierarchy is System.Attribute...

do u know how i can get proper intellisense for the razor templates?

Don't waste too much time on this. It worked with a hack on older Visual Studio versions but honestly I'd like to throw Razor away eventually. My current thinking is to replace (= mark obsolete) Razor short/mid-term with https://mangelmaxime.github.io/Fulma/#fulma
In my current thinking templates would be specially written .fsx files compiled and executed by fsharp.formatting

@matthid matthid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you update the RELEASE_NOTES.md file - it will be released on the appveyor feed automatically after merging...

Feel free to let me know when this is ready (we can do the templates in a separate PR if we want)

@Krzysztof-Cieslak

Copy link
Copy Markdown
Member

Don't waste too much time on this. It worked with a hack on older Visual Studio versions but honestly I'd like to throw Razor away eventually.

Maybe move Razor to separate NuGet package so people that want it can continue using it? It should be fairly simple since all Razor code is in its own project? While I love the idea of removing it I think it may be bit too big change for many people.

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

@Krzysztof-Cieslak

My current thinking is to replace (= mark obsolete) Razor

Yes it will still be there but it won't be the first choice anymore

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

@Krzysztof-Cieslak Even more important would be to get rid of the requirement that we have to provide all dependencies to FCS. This would remove quite some churn from the ecosystem

@kblohm

kblohm commented May 6, 2018

Copy link
Copy Markdown
Contributor Author

I think basically the logic is: If some base class in the hierarchy is System.Attribute...

yes, but that is probably not something u want to check in razor/whatever generates html.

Fulma templates sound fun :). Not sure if that is relevant in any form, but DocFx is currently building a new version. I am hoping they add proper F# support. As far as i can see they are using the asp.net core razor templates as an engine. Maybe there is some interest in cooperating with them (if that is even possible)
https://github.com/dotnet/docfx/tree/v3/src/docfx

Can you update the RELEASE_NOTES.md file - it will be released on the appveyor feed automatically after merging...
Feel free to let me know when this is ready (we can do the templates in a separate PR if we want)

I can do that. I am working on razor still, so it might take a bit. I have a one more question though:

  • I have added IsObsolete and ObsoleteMessage on every type with attributes. That is along the lines of the rest of the API. Do u prefer that approach or one static method/an extension-method on List that checks for the obsoleteattribute. Same kinda goes for FormatAttributes. They bloat every type that has attributes. But makes it easier to find the functions.

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

Maybe there is some interest in cooperating with them

Maybe, but I'd assume creating the yaml is actually the hardest part, writing the template is not too hard and FSF provides full flexibility. We would probably loose tool tips when hovering over F# code.

I have added IsObsolete and ObsoleteMessage on every type with attributes.

Answering in-line


/// Gets a value indicating whether this attribute the System.ObsoleteAttribute
member x.IsObsoleteAttribute =
x.FullName = "System.ObsoleteAttribute"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with this

x.FullName = "System.ObsoleteAttribute"

/// Returns the obsolete message, when this attribute is the System.ObsoleteAttribute. When its not or no message was specified, an empty string is returned
member x.ObsoleteMessage =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd personally move this up

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What do u mean with move this up?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

up the call hierarchy

Comment thread src/FSharp.MetadataFormat/Main.fs Outdated
static member FormatAttributesFullName(attributes:seq<Attribute>, ?removeAttributeSuffix:bool) =
String.Join("\n", attributes |> Seq.map (fun a -> a.FormatFullName(?removeAttributeSuffix = removeAttributeSuffix)))
/// Tries to find the System.ObsoleteAttribute and return its obsolete message
static member TryGetObsoleteMessage(attributes:seq<Attribute>)=

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again, I'd move that up

/// Gets a value indicating whether this member is obsolete
member x.IsObsolete =
x.Attributes
|> Seq.exists (fun a -> a.IsObsoleteAttribute)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fine by me


/// Returns the obsolete message, when this member is obsolete. When its not or no message was specified, an empty string is returned
member x.ObsoleteMessage =
Attribute.TryGetObsoleteMessage(x.Attributes)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In-line the required code here.
I see that this is not really "clean" but I have no suggestion on how to improve.

Strictly speaking I'm fine with either way. Note that extensions only work when using regular C# extension methods and might lead to problems with the razor template, so I guess leaving this a member is as good as it gets.

@Krzysztof-Cieslak

Copy link
Copy Markdown
Member

Even more important would be to get rid of the requirement that we have to provide all dependencies to FCS. This would remove quite some churn from the ecosystem

One can only dream ;-)

@kblohm
kblohm force-pushed the supportForAttributes branch from a4497f6 to a6abb2d Compare May 6, 2018 19:38
@kblohm

kblohm commented May 6, 2018

Copy link
Copy Markdown
Contributor Author

Ok i think i am done.
I did some cleanup, added new templates and i also added some documentation to most of the types.
If u have a minute, u might want to check the updated templates and the documentation.
One more question: am i supposed to HTML escape comments myself? I did not want to escape the comment.Blurb in the template because i was unsure if thats OK. But right now the docs i added to the Attribute-methods are not properly escaped. For example FormatFullName().

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

One more question: am i supposed to HTML escape comments myself?

The problem is that this are XML-comments if you do not write <summary></summary> it is implicit in F#, but it still is xml. Therefore I think it doesn't like <

{
<div class="alert alert-warning">
<strong>WARNING: </strong> This API is obsolete
</div>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we include the obsolete message (as you have already implemented that)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can. The actual message is also listed under the attributes. So if u hover over a method or click into a type/module u can see it there. But i can also include it here.
For Fake i would definitely include it as there are so many obsolete types. But in most APIS there are not that many.

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

So this is ready?

@kblohm

kblohm commented May 6, 2018

Copy link
Copy Markdown
Contributor Author

1 sec. I will fix the escaping in the comments and add add the message

@kblohm
kblohm force-pushed the supportForAttributes branch from a6abb2d to fc3c431 Compare May 6, 2018 20:30
@kblohm

kblohm commented May 6, 2018

Copy link
Copy Markdown
Contributor Author

Sorry, that should be it.
If i escape it, the HTML is fine but vs-tooltips are not.. that is kind of dumb :p

@matthid

matthid commented May 6, 2018

Copy link
Copy Markdown
Member

Sorry, that should be it.

No need to be sorry I just couldn't wait to merge ;)

If i escape it, the HTML is fine but vs-tooltips are not.. that is kind of dumb :p

Yeah maybe an fsharp.formatting bug ;)

Thanks a lot for this - this is indeed very useful!

@matthid
matthid merged commit 5afe8ff into fsprojects:master May 6, 2018
@kblohm
kblohm deleted the supportForAttributes branch May 6, 2018 20:47
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.

3 participants