Rubberduck: Automating the creation of classes

Created on 9 Sep 2016  路  9Comments  路  Source: rubberduck-vba/Rubberduck

The Parent Class Builder could provide some ideas for automating the creation of Classes in Rubberduck.
It provides a form to define a parent class, a child class and a next level class.
Create parent class
In addition, I'd like Rubberduck also to be able to add template code for fields (whether read/write, read only or write only - if this distinction makes sense) and for methods.

enhancement

Most helpful comment

Funny you mention this. VB6 had a _class builder_ tool that would generate a class module and stubs for all members. Perhaps we could bring that guy back.

Another (related) idea occurred to me as well - I always start my classes with a Private Type, e.g. given class Something:

Option Explicit
Private Type TSomething
    Foo As Long
    Bar As String
End Type
Private this As TSomething

And then proceed to implement the properties:

Public Property Get Foo() As Long
    Foo = this.Foo
End Property

Public Property Let Foo(ByVal value As Long)
    this.Foo = value
End Property

Public Property Get Bar() As String
    Bar = this.Bar
End Property

Public Property Let Bar(ByVal value As String)
    this.Bar = value
End Property

IMO the properties _and_ their implementation could be generated with just a right-click on the this declaration, and then _refactor -> encapsulate UDT members_. I didn't do it because I thought it might be too specific to how I do things.

All 9 comments

Funny you mention this. VB6 had a _class builder_ tool that would generate a class module and stubs for all members. Perhaps we could bring that guy back.

Another (related) idea occurred to me as well - I always start my classes with a Private Type, e.g. given class Something:

Option Explicit
Private Type TSomething
    Foo As Long
    Bar As String
End Type
Private this As TSomething

And then proceed to implement the properties:

Public Property Get Foo() As Long
    Foo = this.Foo
End Property

Public Property Let Foo(ByVal value As Long)
    this.Foo = value
End Property

Public Property Get Bar() As String
    Bar = this.Bar
End Property

Public Property Let Bar(ByVal value As String)
    this.Bar = value
End Property

IMO the properties _and_ their implementation could be generated with just a right-click on the this declaration, and then _refactor -> encapsulate UDT members_. I didn't do it because I thought it might be too specific to how I do things.

There's also a "code snippets" feature idea that's been sitting on a shelf, collecting dust for about two years - I'd like to hack into autocompletion and IntelliSense to make this happen though.

Your _style_ of coding a class makes it simpler to write a code snippet. So, this triggered the idea of writing code snippets and looking for existing class builders.
Imagine RD accepting a code snippet syntax that allows for fields, each RD user could then write his own code snippets with his preferred coding style.
I've factored your class coding style as a code snippet following yasnippet syntax as follows below. I assume that some vba coders are more familiar with the Visual Studio snippet syntax (I don't know this syntax).
I was wondering whether ANTLR or some other approach could parse code snippets and replace the fields with the values chosen by the user.
Generating the properties and their implementation with just a right-click on the _this_ declaration and for all the members of _this_ is a good idea.

# -_\- mode:snippet; require-final-newline: nil -_-
# contributor: eteichm
# name: newclass
# key: newclass
# group: oop
# --
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "${1:ClassName}"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True ' make the class static 
Attribute VB_Exposed = False
Option Explicit

Private Type ${2:TInfo}
    ${3:Field1} As ${4:$$(yas-choose-value '("myType" "String" "Boolean"
    "Byte" "Integer" "Long" "Double" "New Scripting.Dictionary" ))}
    propitem$0
End Type
Private this As $2

Implements I$1

Public Property Get $3() As $4
    $3 = this.$3
End Property

Public Property Let $3(ByVal value As $4)
    this.$3 = value
End Property

Public Property Get Self() As I$1
    Set Self = Me
End Property

Public Function Create(ByVal ${5:dtType} As $4) As I$1
    With New $1
        .$3 = $5
        Set Create = .Self
    End With
End Function

Private Property Get I$1_$3() As $4
    I$1_$3 = this.$3
End Property

Private Function I$1_ToString() As String
    I$1_ToString = CStr(this.$3)
End Function

${1:ClassName} is the 1st field with ClassName as default.
${4:$$(yas-choose-value '("myType" "String" "Boolean" "Byte" "Integer" "Long" "Double" "New Scripting.Dictionary" ))} is the 4th field whose content is to be chosen among different values.

StringTemplate can parse code snippets and replace fields with the values chosen by the user. StringTemplate is a better option than yasnippet.

Looking into StringTemplate
As RubberDuck comes with ANTLR since v1.2, it should be possible to write StringTemplate template files and have Excel generate the corresponding output file.
Can someone give me some hints on how to link the antlr runtime and on the function calls to render the
template files ?

here's a screenshot of Bruce McKinney's Collection Builder:
collectionwizard

Where can I find Bruce McKinney's Collection Builder?

It's on the CD for Hardcore Visual Basic at the Internet Archive https://archive.org/details/hardcore-vb5-second-edition

linking #1314

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SteGriff picture SteGriff  路  3Comments

Hosch250 picture Hosch250  路  3Comments

eteichm picture eteichm  路  4Comments

retailcoder picture retailcoder  路  4Comments

ThunderFrame picture ThunderFrame  路  3Comments