amir9480 / vscode-cpp-helper

vscode extension to create implementation for c++ function prototypes.
https://marketplace.visualstudio.com/items?itemName=amiralizadeh9480.cpp-helper
MIT License
368 stars 34 forks source link

getter/setter helper #32

Open jhnyang opened 3 years ago

jhnyang commented 3 years ago

thank you for convenient tool!

it would be great if it supports getter/setter implementation as well. thank you

Ares9323 commented 3 years ago

Hi, I'm commenting here because I read the "help wanted" tag. What do you exactly mean by "getter/setter implementation"?

I actually use C++ only for unreal engine and they have a particular syntax but I think you can solve your problem with a simple snippet like this (removing FORCEINLINE stuff if you don't need it):

"Unreal GetSet": {
    "prefix": "GetSet Variable",
    "body": [
        "//GetSet ${1:Variable_Name}",
        "FORCEINLINE ${2:Variable_Type} Get${1:Variable_Name}() {return ${1:Variable_Name};}",
        "FORCEINLINE void Set${1:Variable_Name}(${2:Variable_Type} In${1:Variable_Name}){${1:Variable_Name} = In${1:Variable_Name};}",
    ],
    "description": "Create getter/setter functions"
},

To write a getter/setter for a variable you just need to type "getset" and autocomplete will suggest you the snippet, then you have to enter the variable name, press tab and enter the variable type, you will have this result:

//GetSet Counter
FORCEINLINE int32 GetCounter() {return Counter;}
FORCEINLINE void SetCounter(int32 InCounter){Counter = InCounter;}

You can also add a string to the snippet after the comment to declare at the same time the variable and the getter/setter: "${2:Variable_Type} ${1:Variable_Name};",

(If you want to type the Variable type before the variable name just invert the numbers 1 and 2)

It will be really cool to right click the variable and create this syntax but for me this is good enough at the moment!