Esempio n. 1
0
        public ParsedDocument Parse(bool storeAst, string fileName, System.IO.TextReader content, MonoDevelop.Projects.Project project = null)
        {
            var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser(GetCompilerArguments(project));

            parser.GenerateTypeSystemMode = !storeAst;
            var result = new ParsedDocumentDecorator();

            if (project != null)
            {
                var projectFile = project.Files.GetFile(fileName);
                if (projectFile != null && projectFile.BuildAction != BuildAction.Compile)
                {
                    result.Flags |= ParsedDocumentFlags.NonSerializable;
                }
            }

            var tagComments = CommentTag.SpecialCommentTags.Select(t => t.Tag).ToArray();

            parser.CompilationUnitCallback = delegate(CompilerCompilationUnit top) {
                foreach (var special in top.SpecialsBag.Specials)
                {
                    var comment = special as SpecialsBag.Comment;
                    if (comment != null)
                    {
                        VisitComment(result, comment, tagComments);
                    }
                    else
                    {
                        if (storeAst)
                        {
                            VisitPreprocessorDirective(result, special as SpecialsBag.PreProcessorDirective);
                        }
                    }
                }
            };

            var unit = parser.Parse(content, fileName);

            unit.Freeze();
            var pf = unit.ToTypeSystem();

            try {
                pf.LastWriteTime = System.IO.File.GetLastWriteTimeUtc(fileName);
            } catch (Exception) {
                pf.LastWriteTime = DateTime.UtcNow;
            }

            result.LastWriteTimeUtc = pf.LastWriteTime.Value;
            result.ParsedFile       = pf;
            result.Add(GenerateFoldings(unit, result));
            if (storeAst)
            {
                result.Ast = unit;
            }
            return(result);
        }
Esempio n. 2
0
        public override ParsedDocument Parse(bool storeAst, string fileName, System.IO.TextReader content, MonoDevelop.Projects.Project project = null)
        {
            var parser = new ICSharpCode.NRefactory.CSharp.CSharpParser(GetCompilerArguments(project));

            parser.GenerateTypeSystemMode = !storeAst;
            var result = new ParsedDocumentDecorator();

            if (project != null)
            {
                var projectFile = project.Files.GetFile(fileName);
                if (projectFile != null && !TypeSystemParserNode.IsCompileBuildAction(projectFile.BuildAction))
                {
                    result.Flags |= ParsedDocumentFlags.NonSerializable;
                }
            }

            var tagComments = CommentTag.SpecialCommentTags.Select(t => t.Tag).ToArray();

            parser.CompilationUnitCallback = delegate(CompilerCompilationUnit top) {
                foreach (var special in top.SpecialsBag.Specials)
                {
                    var comment = special as SpecialsBag.Comment;
                    if (comment != null)
                    {
                        VisitComment(result, comment, tagComments);
                    }
                    else
                    {
                        if (storeAst)
                        {
                            var ppd = special as SpecialsBag.PreProcessorDirective;
                            if (ppd != null)
                            {
                                VisitPreprocessorDirective(result, ppd);
                            }
                        }
                    }
                }
            };

            var unit = parser.Parse(content, fileName);

            unit.Freeze();
            var pf = unit.ToTypeSystem();

            try {
                pf.LastWriteTime = System.IO.File.GetLastWriteTimeUtc(fileName);
            } catch (Exception) {
                pf.LastWriteTime = DateTime.UtcNow;
            }

            result.LastWriteTimeUtc = pf.LastWriteTime.Value;
            result.ParsedFile       = pf;
            result.Add(GetSemanticTags(unit));

            result.CreateRefactoringContext = delegate(MonoDevelop.Ide.Gui.Document doc, System.Threading.CancellationToken token) {
                var task = MDRefactoringContext.Create(doc, doc.Editor.Caret.Location, token);
                task.Wait(5000, token);
                if (!task.IsCompleted)
                {
                    return(null);
                }
                return(task.Result);
            };
            result.CreateRefactoringContextWithEditor = (data, resolver, token) => new MDRefactoringContext((DotNetProject)project, data, result, (CSharpAstResolver)resolver, TextLocation.Empty, token);

            if (storeAst)
            {
                result.Ast = unit;
                result.Add(GenerateFoldings(unit, result));
            }
            return(result);
        }