public override string AddBaseTypeToClass(string existingCode, IClass targetClass, IClass newBaseType)
        {
            NR.IParser parser = ParseFile(null, existingCode);
            if (parser == null)
            {
                return(null);
            }

            AddTypeToBaseTypesVisitor addTypeToBaseTypesVisitor = new AddTypeToBaseTypesVisitor(targetClass, newBaseType);

            parser.CompilationUnit.AcceptVisitor(addTypeToBaseTypesVisitor, null);

            // now use an output visitor for the appropriate language (based on
            // extension of the existing code file) to format the new interface.
            IOutputAstVisitor output = GetOutputVisitor();

            // run the output visitor with the specials inserter to insert comments
            using (SpecialNodesInserter.Install(parser.Lexer.SpecialTracker.RetrieveSpecials(), output)) {
                parser.CompilationUnit.AcceptVisitor(output, null);
            }

            parser.Dispose();

            if (output.Errors.Count > 0)
            {
                ShowSourceCodeErrors(null, output.Errors.ErrorOutput);
                return(null);
            }

            return(output.Text);
        }
Ejemplo n.º 2
0
 NR.IParser ParseFile(string fileContent)
 {
     NR.IParser parser = NR.ParserFactory.CreateParser(language, new StringReader(fileContent));
     parser.Parse();
     if (parser.Errors.Count > 0)
     {
         ShowSourceCodeErrors(parser.Errors.ErrorOutput);
         parser.Dispose();
         return(null);
     }
     else
     {
         return(parser);
     }
 }
Ejemplo n.º 3
0
 protected virtual Dictionary <PossibleTypeReference, object> FindPossibleTypeReferences(string fileContent)
 {
     NR.IParser parser = ParseFile(fileContent);
     if (parser == null)
     {
         return(null);
     }
     else
     {
         FindPossibleTypeReferencesVisitor visitor = new FindPossibleTypeReferencesVisitor();
         parser.CompilationUnit.AcceptVisitor(visitor, null);
         parser.Dispose();
         return(visitor.list);
     }
 }
 protected virtual HashSet <PossibleTypeReference> FindPossibleTypeReferences(IDomProgressMonitor progressMonitor, string fileContent, ParseInformation parseInfo)
 {
     NR.IParser parser = ParseFile(progressMonitor, fileContent);
     if (parser == null)
     {
         return(null);
     }
     else
     {
         FindPossibleTypeReferencesVisitor visitor = new FindPossibleTypeReferencesVisitor(parseInfo);
         parser.CompilationUnit.AcceptVisitor(visitor, null);
         parser.Dispose();
         return(visitor.list);
     }
 }
Ejemplo n.º 5
0
        public void RunVisitor()
        {
            if (searchedMember == null)
            {
                return;
            }

            // search if the member name exists in the file (otherwise it doesn't make sense to search it)
            FindReplace   findReplace   = new FindReplace();
            FilterOptions filterOptions = new FilterOptions {
                CaseSensitive  = true,
                WholeWordsOnly = true
            };

            findReplace.CompilePattern(searchedMemberName, filterOptions);
            IEnumerable <SearchResult> result = findReplace.Search(new FileProvider(null), text.Text, searchedMemberName, null, filterOptions);

            if (result == null || !result.Any())
            {
                return;
            }

            string parseText = text.Text;

            ICSharpCode.NRefactory.IParser parser = ICSharpCode.NRefactory.ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, new StringReader(parseText));
            parser.Lexer.EvaluateConditionalCompilation = true;
            parser.Parse();
            resolver.SetupParsedCompilationUnit(parser.CompilationUnit);
            VisitCompilationUnit(parser.CompilationUnit, null);

            List <HashSet <string> > usedIdentifiers = GetUsedDefineCombinations(parser);

            for (int i = 0; i < usedIdentifiers.Count; i++)
            {
                parser.Lexer.ConditionalCompilationSymbols.Clear();
                foreach (string define in usedIdentifiers[i])
                {
                    parser.Lexer.ConditionalCompilationSymbols.Add(define, true);
                }
                parser.Dispose();
                parser = ICSharpCode.NRefactory.ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, new StringReader(parseText));
                parser.Parse();

                VisitCompilationUnit(parser.CompilationUnit, null);
            }

            if (IncludeXmlDocumentation)
            {
                if (searchedMember is IParameter)
                {
                    IParameter parameter   = (IParameter)searchedMember;
                    var        docComments = from ICSharpCode.NRefactory.Comment cmt in
                                             (from ISpecial s in parser.Lexer.SpecialTracker.CurrentSpecials
                                              where s is ICSharpCode.NRefactory.Comment && s.StartPosition.Line <= parameter.DeclaringMember.Location.Line
                                              select s)
                                             select cmt;

                    ICSharpCode.NRefactory.Comment lastComment = null;
                    foreach (ICSharpCode.NRefactory.Comment curComment in docComments.Reverse())
                    {
                        if (lastComment != null && Math.Abs(lastComment.StartPosition.Line - curComment.StartPosition.Line) > 1)
                        {
                            break;
                        }
                        // Concat doesn't work on MatchCollections
                        foreach (var matchCol in new [] { paramRegex.Matches(curComment.CommentText), paramRefRegex.Matches(curComment.CommentText) })
                        {
                            foreach (Match match in matchCol)
                            {
                                if (match.Groups[1].Value == searchedMemberName)
                                {
                                    AddUniqueReference(curComment.StartPosition.Line, curComment.StartPosition.Column + match.Groups[1].Index, searchedMemberName);
                                }
                            }
                        }
                        lastComment = curComment;
                    }
                }
                else if (searchedMember is IMember)
                {
                    IMember member      = (IMember)searchedMember;
                    var     docComments = from ICSharpCode.NRefactory.Comment cmt in
                                          (from ISpecial s in parser.Lexer.SpecialTracker.CurrentSpecials
                                           where s is ICSharpCode.NRefactory.Comment
                                           select s)
                                          select cmt;

                    string fullName = member.FullName;

                    foreach (ICSharpCode.NRefactory.Comment curComment in docComments)
                    {
                        // Concat doesn't work on MatchCollections
                        foreach (var matchCol in new [] { seeRegex.Matches(curComment.CommentText), seeAlsoRegRegex.Matches(curComment.CommentText) })
                        {
                            foreach (Match match in matchCol)
                            {
                                if (match.Groups[1].Value.StartsWith(fullName))
                                {
                                    AddUniqueReference(curComment.StartPosition.Line, curComment.StartPosition.Column + match.Groups[1].Index + fullName.Length - searchedMemberName.Length, searchedMemberName);
                                }
                            }
                        }
                    }
                }
            }
            parser.Dispose();
        }