/// <summary>
        /// Gets the using statements.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>A list of using statements.</returns>
        public static IEnumerable <string> GetUsingStatements(this ProjectItem instance)
        {
            TraceService.WriteLine("ProjectItemExtensions::GetUsingStatements in file " + instance.Name);

            List <string> statements = new List <string>();

            foreach (CodeElement codeElement in instance.FileCodeModel.CodeElements)
            {
                CodeImport import = codeElement as CodeImport;

                switch (codeElement.Kind)
                {
                case vsCMElement.vsCMElementImportStmt:

                    if (import != null)
                    {
                        EditPoint startEditPoint = import.GetStartPoint().CreateEditPoint();
                        TextPoint textPoint      = import.GetEndPoint();
                        string    text           = startEditPoint.GetText(textPoint);
                        statements.Add(text);
                    }

                    break;

                case vsCMElement.vsCMElementNamespace:

                    foreach (CodeElement childCodeElement in codeElement.Children)
                    {
                        import = childCodeElement as CodeImport;

                        if (import != null)
                        {
                            EditPoint startEditPoint = import.GetStartPoint().CreateEditPoint();
                            TextPoint textPoint      = import.GetEndPoint();
                            string    text           = startEditPoint.GetText(textPoint);
                            statements.Add(text);
                        }
                    }

                    break;
                }
            }

            return(statements);
        }
Пример #2
0
        private void AddUsingStatement(FileCodeModel model, CodeElements codeElements, string usingStatement)
        {
            bool       usingStatementFound = false;
            CodeImport lastCodeElement     = null;

            foreach (CodeElement codeElement in codeElements)
            {
                if (codeElement.Kind == vsCMElement.vsCMElementImportStmt)
                {
                    CodeImport codeImport = codeElement as CodeImport;
                    if (codeImport.Namespace == usingStatement)
                    {
                        usingStatementFound = true;
                    }
                    lastCodeElement = codeImport;
                }

                AddUsingStatement(model, codeElement.Children, usingStatement);
            }

            if (!usingStatementFound)
            {
                if (lastCodeElement != null)
                {
                    //FileCodeModel2 model2 = model as FileCodeModel2;
                    //model2.AddImport(usingStatement);

                    EditPoint2 editPoint = (EditPoint2)lastCodeElement.GetEndPoint().CreateEditPoint();
                    editPoint.InsertNewLine(1);
                    editPoint.Indent(null, 1);
                    editPoint.Insert("using " + usingStatement + ";");

                    Helpers.LogMessage(model.DTE, model.DTE, Helpers.GetFullPathOfProjectItem(lastCodeElement.ProjectItem) + ": Added using statement '" + usingStatement + "'");
                }
            }
        }
Пример #3
0
        public async Task GetEndPoint_Body()
        {
            CodeImport import = await GetCodeImportAsync(2);

            AssertEx.Throws <COMException>(() => import.GetEndPoint(vsCMPart.vsCMPartBody));
        }
Пример #4
0
        public async Task GetEndPoint_AttributesWithDelimiter()
        {
            CodeImport import = await GetCodeImportAsync(2);

            AssertEx.Throws <COMException>(() => import.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter));
        }