locked
How to change EnvDte.CodeFunction.Type (function returning type)? RRS feed

  • Question

  • Hello, i write a VS plugin (using C#), that parse opened solution (C++) and can make some changes there.

    I need to change some variable and functions types.

    I read this thread http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/82f7fab5-a638-4932-aa75-7e32297048e1/ and for variables its usefull, but when i try to change type of function using EditPoint.ReplaceText - it doesn't change function prototype.

    for example i have this code:

    class MyClass
    {
    public: void get(int p);
    }
    
    void MyClass::get(int p)
    {
    }
    

    And i try to change it:

    try
    {
    EditPoint2 startPoint = (EditPoint2)elem.GetStartPoint(vsCMPart.vsCMPartAttributes).CreateEditPoint();
    
    TextPoint endPoint = elem.GetEndPoint(vsCMPart.vsCMPartBody);
    String prototype = startPoint.GetText(endPoint);
    
    //... Some actions with textBody, returning String newPrototype
    //newPrototype = "int MyClass::get(int p)\r\n{\r\n\tint a;\r\n\tint b;\r\n\tint c;\r\n";
    
    startPoint.ReplaceText(endPoint, newPrototype, (int)vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
    }
    
    These changes do not affect the function prototype in the class.

    Thursday, April 21, 2011 6:48 AM

All replies

  • Hi Byakuya_ ,

     

    Thanks for your post.

    I checked the link you pasted above, but I don't think Carlos means

    startPoint.ReplaceText can help change function type.

    I check the MSDN library

    http://msdn.microsoft.com/en-us/library/y1cksdak(v=VS.80).aspx

    but cannot find any information on changing function prototype.

    If I misunderstand your issue, be free to let me know.


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 25, 2011 8:51 AM
  • startPoint.ReplaceText can help to change the whole line (or selected part). I know that its better to use code model functions, for example i use this code to change function access mode to public:

    ((CodeFunction2) elem).Access = vsCMAccess.vsCMAccessPublic;
    

    But i can't do something like this:

    ((CodeFunction2) elem).Type = NEW_TYPE;
    

    because NEW_TYPE must be CodeTypeRef object, but CodeTypeRef is an interface and i cant just create new object.

    For class variables its the same, but if i need to change expression like this:

    int a;
    

    to this:

    double a;
    

    i can replace whole string using ReplaceText method.


    The main problem that if we have code like this:

    class A{
    void foo();
    }
    
    void A::foo(){}
    
    we can't see function prototype using code model, whats why we can't change its type;

    Tuesday, April 26, 2011 6:51 AM
  • Hi again,

    Thanks for your feedback.

    You can use vsCMTypeRef Enumeration to set function type,

    and you can check the good sample in the following post

    http://stackoverflow.com/questions/2976363/how-do-i-generate-a-constructor-from-class-fields-using-visual-studio-and-or-res/2976514

    Hope my reply helps resolve your issue.


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 27, 2011 8:38 AM
  • It's not usefull for me, because when i try to do something like this:

    ((CodeFunction2) elem).Type = vsCMTypeRef.vsCMTypeRefLong;
    
    It cause error: Cannot implicitly convert type 'EnvDTE.vsCMTypeRef' to 'EnvDTE.CodeTypeRef'

    Thursday, April 28, 2011 12:29 PM
  • hello, I meet the same problem. could i know your last resolution?
    Wednesday, June 18, 2014 9:56 AM