Exemplo n.º 1
0
        /// <summary>
        /// Adds all import statements from list
        /// </summary>
        private static void AddUsedNamespacesToList(CodeElements elements, NamespacesList list)
        {
            if (elements == null)
            {
                return;
            }

            foreach (CodeElement2 element in elements)
            {
                if (element.Kind == vsCMElement.vsCMElementImportStmt)
                {
                    CodeImport codeImport = (CodeImport)element;
                    list.Add(codeImport.Namespace, codeImport.Alias, true);
                }
            }
        }
Exemplo n.º 2
0
        private static void GetUsedNamespacesInternal(CodeNamespace codeNamespace, ProjectItem item, NamespacesList list)
        {
            if (codeNamespace != null)   // add itself, its children and its parents
            {
                list.Add(codeNamespace.FullName, null, false);

                AddUsedNamespacesToList(codeNamespace.Children, list);

                CodeNamespace parent = GetNamespace(codeNamespace as CodeElement);
                GetUsedNamespacesInternal(parent, item, list);
            }
            else     // top level namespace - only add top level import statements
            {
                bool          fileOpened;
                FileCodeModel model = item.GetCodeModel(false, false, out fileOpened);
                if (model != null)
                {
                    AddUsedNamespacesToList(model.CodeElements, list);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called after page directive &lt;%@ %&gt;
        /// </summary>
        public void OnPageDirective(DirectiveContext context)
        {
            // add new imported namespace
            if (context.DirectiveName == "Import" && context.Attributes.Exists((info) => { return(info.Name == "Namespace"); }))
            {
                declaredNamespaces.Add(new UsedNamespaceItem(context.Attributes.Find((info) => { return(info.Name == "Namespace"); }).Value, null, true));
            }
            if (context.DirectiveName == "Page" || context.DirectiveName == "Control")
            {
                string lang = null; // value of Language attribute
                string ext  = null; // extension of the code-behind file
                foreach (var info in context.Attributes)
                {
                    if (info.Name == "Language")
                    {
                        lang = info.Value;         // get file language
                    }
                    if (info.Name == "CodeFile")   // get code-behind file
                    {
                        int index = info.Value.LastIndexOf('.');
                        if (index != -1 && index + 1 < info.Value.Length)
                        {
                            ext = info.Value.Substring(index + 1);
                        }
                    }
                }
                if (string.IsNullOrEmpty(lang))
                {
                    if (!string.IsNullOrEmpty(ext))   // infer file language from the extension
                    {
                        fileLanguage = StringConstants.CsExtensions.Contains(ext.ToLower()) ? FILETYPE.CSHARP : FILETYPE.VB;
                    }
                }
                else
                {
                    fileLanguage = lang == "C#" ? FILETYPE.CSHARP : FILETYPE.VB;
                }
            }
            if (context.DirectiveName == "Register")
            {
                string assembly = null, nmspc = null, src = null, tagName = null, tagPrefix = null;
                foreach (AttributeInfo info in context.Attributes)
                {
                    if (info.Name == "Assembly")
                    {
                        assembly = info.Value;
                    }
                    if (info.Name == "Namespace")
                    {
                        nmspc = info.Value;
                    }
                    if (info.Name == "Src")
                    {
                        src = info.Value;
                    }
                    if (info.Name == "TagName")
                    {
                        tagName = info.Value;
                    }
                    if (info.Name == "TagPrefix")
                    {
                        tagPrefix = info.Value;
                    }
                }
                // add definitions of elements for future type resolution
                if (!string.IsNullOrEmpty(tagPrefix))
                {
                    if (!string.IsNullOrEmpty(assembly) && !string.IsNullOrEmpty(nmspc))
                    {
                        if (webConfig != null)
                        {
                            webConfig.AddTagPrefixDefinition(new TagPrefixAssemblyDefinition(assembly, nmspc, tagPrefix));
                        }
                    }
                    else if (!string.IsNullOrEmpty(tagName) && !string.IsNullOrEmpty(src))
                    {
                        if (webConfig != null)
                        {
                            webConfig.AddTagPrefixDefinition(new TagPrefixSourceDefinition(projectItem,
                                                                                           VisualLocalizerPackage.Instance.DTE.Solution, tagName, src, tagPrefix));
                        }
                    }
                }
            }

            if (parentCommand is BatchMoveCommand)   // no resource references can be directly in the attributes, safe to look only for string literals
            {
                foreach (AttributeInfo info in context.Attributes)
                {
                    if (info.ContainsAspTags)
                    {
                        continue;                       // attribute's value contains &lt;%= - like tags - localization is not desirable
                    }
                    if (info.Name.ToLower() == "language")
                    {
                        continue;
                    }

                    AspNetStringResultItem item = AddResult(info, null, context.DirectiveName, context.WithinClientSideComment, false, false, true);
                    if (item != null)
                    {
                        item.ComesFromDirective = true;
                        item.AttributeName      = info.Name;
                    }
                }
            }
        }