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); }
public override async System.Threading.Tasks.Task <ParsedDocument> Parse(MonoDevelop.Ide.TypeSystem.ParseOptions options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { var fileName = options.FileName; var project = options.Project; var result = new CSharpParsedDocument(fileName); if (project != null) { var projectFile = project.Files.GetFile(fileName); if (projectFile != null && !TypeSystemParserNode.IsCompileBuildAction(projectFile.BuildAction)) { result.Flags |= ParsedDocumentFlags.NonSerializable; } } var compilerArguments = GetCompilerArguments(project); SyntaxTree unit = null; if (project != null) { var curDoc = options.RoslynDocument; if (curDoc == null) { var curProject = TypeSystemService.GetCodeAnalysisProject(project); if (curProject != null) { var documentId = TypeSystemService.GetDocumentId(project, fileName); if (documentId != null) { curDoc = curProject.GetDocument(documentId); } } } if (curDoc != null) { try { var model = await curDoc.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); unit = model.SyntaxTree; result.Ast = model; } catch (AggregateException ae) { ae.Flatten().Handle(x => x is OperationCanceledException); return(result); } catch (OperationCanceledException) { return(result); } catch (Exception e) { LoggingService.LogError("Error while getting the semantic model for " + fileName, e); } } } if (unit == null) { unit = CSharpSyntaxTree.ParseText(SourceText.From(options.Content.Text), compilerArguments, fileName); } result.Unit = unit; DateTime time; try { time = System.IO.File.GetLastWriteTimeUtc(fileName); } catch (Exception) { time = DateTime.UtcNow; } result.LastWriteTimeUtc = time; return(result); }