Friend Function GeneratedCodeNamespace()

in vsintegration/src/FSharp.ProjectSystem.PropertyPages/Common/Utils.vb [1142:1246]


        Friend Function GeneratedCodeNamespace(ByVal Hierarchy As IVsHierarchy, ByVal ItemId As UInteger, ByVal IncludeRootNamespace As Boolean, ByVal SupportCustomToolNamespace As Boolean) As String
            
            Dim RootNamespace As String = ""
            If IsVbProject(Hierarchy) Then
                Dim Project As EnvDTE.Project = DTEUtils.EnvDTEProject(Hierarchy)
                If Project IsNot Nothing Then
                    Dim RootNamespaceProperty As EnvDTE.Property = Nothing
                    Try
                        RootNamespaceProperty = Project.Properties().Item("RootNamespace")
                    Catch ex As ArgumentException
                        
                    End Try
                    If RootNamespaceProperty IsNot Nothing Then
                        RootNamespace = TryCast(RootNamespaceProperty.Value, String)
                    End If
                End If
            End If

            Debug.Assert(RootNamespace = "" OrElse IsVbProject(Hierarchy), "Only VB projects should have a root namespace property!")


            Dim CustomToolNamespace As String = ""
            Dim objDefaultNamespace As Object = Nothing
            Dim DefaultNamespace As String = ""
            Try
                If ItemId = VSConstants.VSITEMID_ROOT Then
                    
                    VSErrorHandler.ThrowOnFailure(Hierarchy.GetProperty(ItemId, __VSHPROPID.VSHPROPID_DefaultNamespace, objDefaultNamespace))
                    If TypeOf objDefaultNamespace Is String Then
                        DefaultNamespace = DesignerFramework.DesignUtil.GenerateValidLanguageIndependentNamespace(DirectCast(objDefaultNamespace, String))
                    End If
                Else
                    Dim item As EnvDTE.ProjectItem = Common.DTEUtils.ProjectItemFromItemId(Hierarchy, ItemId)
                    Debug.Assert(item IsNot Nothing, "Failed to get EnvDTE.ProjectItem from given hierarchy/itemid")
                    If item IsNot Nothing AndAlso item.Properties IsNot Nothing Then
                        Dim prop As EnvDTE.Property = Nothing
                        If SupportCustomToolNamespace Then
                            Try
                                prop = item.Properties.Item("CustomToolNamespace")
                            Catch ex As ArgumentException
                                
                            End Try
                        End If

                        If prop IsNot Nothing AndAlso CStr(prop.Value) <> "" Then
                            CustomToolNamespace = DesignerFramework.DesignUtil.GenerateValidLanguageIndependentNamespace(CStr(prop.Value))
                        Else
                            VSErrorHandler.ThrowOnFailure(Hierarchy.GetProperty(ItemId, __VSHPROPID.VSHPROPID_DefaultNamespace, objDefaultNamespace))
                            If TypeOf objDefaultNamespace Is String Then
                                DefaultNamespace = DesignerFramework.DesignUtil.GenerateValidLanguageIndependentNamespace(DirectCast(objDefaultNamespace, String))
                            End If
                        End If
                    End If
                End If
            Catch ex As System.ArgumentException
                
            Catch ex As Exception When Not Common.IsUnrecoverable(ex)
                Debug.Fail(String.Format("Failed to get item.Properties('CustomToolNamespace') {0}", ex))
            End Try

            
            
            If CustomToolNamespace <> "" Then
                If RootNamespace <> "" AndAlso IncludeRootNamespace Then
                    Return String.Concat(RootNamespace, ".", CustomToolNamespace)
                Else
                    Return CustomToolNamespace
                End If
            End If

            
            Debug.Assert(CustomToolNamespace = "", "We should have used the CustomToolNamespace if set!")

            
            If Not IncludeRootNamespace Then
                
                
                
                Try
                    If RootNamespace <> "" AndAlso DefaultNamespace.StartsWith(RootNamespace, StringComparison.Ordinal) Then
                        If DefaultNamespace.Length > RootNamespace.Length Then
                            
                            
                            
                            
                            DefaultNamespace = DefaultNamespace.Substring(RootNamespace.Length() + 1)
                        Else
                            
                            
                            
                            DefaultNamespace = ""
                        End If
                    End If
                Catch ex As System.ArgumentException
                    Debug.Fail("Why did we get a System.ArgumentException when trying to get the root namespace?")
                Catch ex As Exception
                    Debug.Fail(String.Format("Why did we get a {0} when trying to get the root namespace?", ex))
                End Try
            End If
            Try
                Return DesignerFramework.DesignUtil.GenerateValidLanguageIndependentNamespace(DefaultNamespace)
            Catch ex As ArgumentException
                Return ""
            End Try
        End Function