locked
How can I remove all errors code elements from my CSharp project by EnvDTE RRS feed

  • Question

  • Hi,

    I got a project containing too many errors. I want to search all code elements whose name property containing sub string "AAA" and remove them. How can I implement it by EnvDTE features. I have known how to create and config a VSPackage project, here I just need a code sample.

    Thanks.

    Tuesday, February 16, 2016 3:20 PM

Answers

  • I have made my code work, following code is correct one:

    Dim sel As TextSelection = _
                    CType(dte.ActiveDocument.Selection, TextSelection)
                Dim fun As CodeFunction = _
                    CType(sel.ActivePoint.CodeElement( _
                    vsCMElement.vsCMElementFunction), CodeFunction)
    
                If MsgBox("Remove " & fun.Name & "?", MsgBoxStyle.YesNo) = _
                    MsgBoxResult.Yes Then
                    ' Remove the function.
                    fun.Parent.ProjectItem.FileCodeModel.Remove(fun)   'IMPORTANT!!
                End If

    Thanks to all

    • Marked as answer by DotNet Wang Monday, March 7, 2016 2:29 AM
    Friday, February 19, 2016 9:28 AM

All replies

  • Hi sincos,

    Based on your description, your case more related to Visual Studio Integrate, I will move your case to that forum for better support.

    Best regards,

    Kristin  


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 17, 2016 2:45 AM
  • Hi sincos2007,

    >>How can I remove all errors code elements from my CSharp project by EnvDTE

    Could you provide more information about your requirement?  Below are things that Visual Studio Extension could do.

    • Add commands, buttons, menus, and other UI elements to the IDE

    • Add tool windows for new functionality

    • Extend IntelliSense for a given language, or provide IntelliSense for new programming languages

    • Use light bulbs to provide hints and suggestions that help developers write better code

    • Enable support for a new language

    • Add a custom project type

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 18, 2016 3:33 AM
  • Hi,

    To get the code elements see:

    HOWTO: Navigate the code elements of a file from a Visual Studio .NET macro or add-in.
    http://www.visualstudioextensibility.com/articles/add-ins/

    To remove them:

    - Call EnvDTE.TextPoint startTextPoint = codeElement.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes)

    - Call EnvDTE.TextPoint endTextPoint = codeElement.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes)

    - Call EnvDTE.EditPoint startEditPoint = startTextPoint.CreateEditPoint()

    - Call startEditPoint.Delete(endTextPoint)


    My portal and blog about VSX: http://www.visualstudioextensibility.com
    Twitter: https://twitter.com/VSExtensibility
    MZ-Tools productivity extension for Visual Studio: http://www.mztools.com

    Thursday, February 18, 2016 9:41 AM
  • Hi Carlos,

    I viewed your code which use startEditPoint.Delete method to remove code element from source code file. I'm confuse why I can not use FileCodeModel.Remove to remove code element. following is my code which reports an error to me:

    Dim sel As TextSelection = _
                    CType(dte.ActiveDocument.Selection, TextSelection)
                Dim fun As CodeFunction = _
                    CType(sel.ActivePoint.CodeElement( _
                    vsCMElement.vsCMElementFunction), CodeFunction)
    
                If MsgBox("Remove " & fun.Name & "?", MsgBoxStyle.YesNo) = _
                    MsgBoxResult.Yes Then
                    ' Remove the function.
                    fun.ProjectItem.FileCodeModel.Remove(fun)
                End If


    Before I run this code, I have put insert point in a function in a source code.

    Thanks.

    • Edited by sincos2007 Thursday, February 18, 2016 5:51 PM
    Thursday, February 18, 2016 5:45 PM
  • I have made my code work, following code is correct one:

    Dim sel As TextSelection = _
                    CType(dte.ActiveDocument.Selection, TextSelection)
                Dim fun As CodeFunction = _
                    CType(sel.ActivePoint.CodeElement( _
                    vsCMElement.vsCMElementFunction), CodeFunction)
    
                If MsgBox("Remove " & fun.Name & "?", MsgBoxStyle.YesNo) = _
                    MsgBoxResult.Yes Then
                    ' Remove the function.
                    fun.Parent.ProjectItem.FileCodeModel.Remove(fun)   'IMPORTANT!!
                End If

    Thanks to all

    • Marked as answer by DotNet Wang Monday, March 7, 2016 2:29 AM
    Friday, February 19, 2016 9:28 AM