Example #1
0
        /// <summary>
        /// Check the given file for multiply added import statements
        /// </summary>
        private void CheckForDuplicateUsings(string path)
        {
            ProjectItem projectItem = Agent.GetDTE().Solution.FindProjectItem(path);

            if (projectItem.GetFileType() == FILETYPE.ASPX)
            {
                return;
            }

            HashSet <string> namespaces = new HashSet <string>();
            bool             fileOpened;

            foreach (CodeElement codeElement in projectItem.GetCodeModel(true, true, out fileOpened).CodeElements)
            {
                if (codeElement.Kind != vsCMElement.vsCMElementImportStmt)
                {
                    continue;
                }
                CodeImport import = (CodeImport)codeElement;
                if (namespaces.Contains(import.Namespace))
                {
                    Assert.Fail();
                }

                namespaces.Add(import.Namespace);
            }

            if (fileOpened)
            {
                DocumentViewsManager.CloseFile(path);
            }
        }
Example #2
0
        /// <summary>
        /// Treats given ProjectItem as a VB code file, using VBCodeExplorer to examine the file. LookInVB method is called as a callback,
        /// given plain methods text.
        /// </summary>
        protected override void ProcessVB(ProjectItem projectItem, Predicate <CodeElement> exploreable, bool verbose)
        {
            if (isInitial || editorInstance.UIControl.sourceFilesThatNeedUpdate.Contains(projectItem.GetFullPath().ToLower()))
            {
                base.ProcessVB(projectItem, exploreable, verbose);
            }
            else
            {
                bool           fileOpened;
                FileCodeModel2 codeModel = projectItem.GetCodeModel(false, false, out fileOpened);

                if (codeModel == null && !RDTManager.IsFileOpen(projectItem.GetFullPath()))
                {
                    editorInstance.UIControl.RegisterAsStaticReferenceSource(projectItem);
                    return;
                }

                if (codeModel == null)
                {
                    if (verbose)
                    {
                        VLOutputWindow.VisualLocalizerPane.WriteLine("\tCannot process {0}, file code model does not exist.", projectItem.Name);
                    }
                    return;
                }
                if (verbose)
                {
                    VLOutputWindow.VisualLocalizerPane.WriteLine("\tProcessing {0}", projectItem.Name);
                }

                currentlyProcessedItem = projectItem;

                try {
                    VBCodeExplorer.Instance.Explore(this, exploreable, codeModel);
                } catch (COMException ex) {
                    if (ex.ErrorCode == -2147483638)
                    {
                        VLOutputWindow.VisualLocalizerPane.WriteLine("\tError occured during processing {0} - the file is not yet compiled.", projectItem.Name);
                    }
                    else
                    {
                        throw;
                    }
                }

                currentlyProcessedItem = null;
            }
            editorInstance.UIControl.sourceFilesThatNeedUpdate.Remove(projectItem.GetFullPath().ToLower());
        }
Example #3
0
        /// <summary>
        /// Treats given ProjectItem as a VB code file, using VBCodeExplorer to examine the file. LookInVB method is called as a callback,
        /// given plain methods text.
        /// </summary>
        protected virtual void ProcessVB(ProjectItem projectItem, Predicate <CodeElement> exploreable, bool verbose)
        {
            bool           fileOpened;
            FileCodeModel2 codeModel = projectItem.GetCodeModel(false, true, out fileOpened);

            if (fileOpened)
            {
                VLDocumentViewsManager.AddInvisibleWindow(projectItem.GetFullPath(), invisibleWindowsAuthor);
                VLOutputWindow.VisualLocalizerPane.WriteLine("\tForce opening {0} in background in order to obtain code model", projectItem.Name);
            }

            if (codeModel == null)
            {
                if (verbose)
                {
                    VLOutputWindow.VisualLocalizerPane.WriteLine("\tCannot process {0}, file code model does not exist.", projectItem.Name);
                }
                return;
            }
            if (verbose)
            {
                VLOutputWindow.VisualLocalizerPane.WriteLine("\tProcessing {0}", projectItem.Name);
            }

            currentlyProcessedItem = projectItem;

            try {
                VBCodeExplorer.Instance.Explore(this, exploreable, codeModel);
            } catch (COMException ex) {
                if (ex.ErrorCode == -2147483638)
                {
                    VLOutputWindow.VisualLocalizerPane.WriteLine("\tError occured during processing {0} - the file is not yet compiled.", projectItem.Name);
                }
                else
                {
                    throw;
                }
            }

            currentlyProcessedItem = null;
        }
Example #4
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);
                }
            }
        }
        /// <summary>
        /// Loads types specified for this definition into types cache
        /// </summary>
        public override void Load()
        {
            // get full path of the definition file
            string projPath = projectItem.ContainingProject.FullName;

            if (projPath.EndsWith("\\"))
            {
                projPath = projPath.Substring(0, projPath.Length - 1);
            }
            string sourcePath = Source.Replace("~", projPath);

            if (!File.Exists(sourcePath))
            {
                return;
            }

            // read the source file and stop after first Control directive (code behind class name)
            ControlDirectiveHandler handler = new ControlDirectiveHandler();
            Parser parser = new Parser(File.ReadAllText(sourcePath), handler);

            parser.Process();

            // no code behind class or no Control directive at all
            if (handler.ControlInfo == null || handler.ControlInfo.Inherits == null)
            {
                return;
            }

            // get definition class type
            string codeFile = handler.ControlInfo.CodeFile == null ? handler.ControlInfo.CodeBehind : handler.ControlInfo.CodeFile;

            if (codeFile == null)
            {
                return;
            }


            Uri codeItemUri;

            Uri.TryCreate(new Uri(sourcePath, UriKind.Absolute), new Uri(codeFile, UriKind.Relative), out codeItemUri);
            ProjectItem codeItem = solution.FindProjectItem(Uri.UnescapeDataString(codeItemUri.ToString()));

            if (codeItem == null)
            {
                throw new InvalidOperationException("Cannot find declared code behind file " + codeItem.GetFullPath());
            }

            // ensure the window is open to get the code model
            bool          fileOpened;
            FileCodeModel model = codeItem.GetCodeModel(false, true, out fileOpened);

            if (model == null)
            {
                return;
            }

            CodeClass classElement = null;

            foreach (CodeElement el in model.CodeElements)
            {
                if (el.Kind == vsCMElement.vsCMElementClass && el.Name == handler.ControlInfo.Inherits)
                {
                    classElement = (CodeClass)el;
                    break;
                }
            }
            if (classElement != null)
            {
                foreach (CodeElement el in classElement.Children)
                {
                    if (el.Kind == vsCMElement.vsCMElementProperty)
                    {
                        CodeProperty property = (CodeProperty)el;
                        ReflectionCache.Instance.AddType(classElement.FullName, property.Name, property);
                        ReflectionCache.Instance.AddType(TagName, property.Name, property);
                    }
                }
            }

            if (fileOpened)
            {
                DocumentViewsManager.CloseFile(codeItem.GetFullPath());
            }
        }