public static ITextDocument ToDocument(this ITextBuffer self) { ITextDocument ret = self as ITextDocument; if (ret == null) { ret = new SeekableTextReader(self); } return ret; }
public static ITextDocument ToDocument(this ITextBuffer self) { ITextDocument ret = self as ITextDocument; if (ret == null) { ret = new SeekableTextReader(self); } return(ret); }
public void ConstructorInitializesProperties() { // Arrange SeekableTextReader expectedBuffer = new SeekableTextReader(TextReader.Null); CSharpCodeParser expectedCodeParser = new CSharpCodeParser(); HtmlMarkupParser expectedMarkupParser = new HtmlMarkupParser(); // Act ParserContext context = new ParserContext(expectedBuffer, expectedCodeParser, expectedMarkupParser, expectedCodeParser); // Assert Assert.NotNull(context.Source); Assert.Same(expectedCodeParser, context.CodeParser); Assert.Same(expectedMarkupParser, context.MarkupParser); Assert.Same(expectedCodeParser, context.ActiveParser); }
public override ParsedDocument Parse (bool storeAst, string fileName, System.IO.TextReader content, Projects.Project project = null) { currentDocument = openDocuments.FirstOrDefault (d => d.FileName == fileName); // We need document and project to be loaded to correctly initialize Razor Host. if (project == null || (currentDocument == null && !TryAddDocument (fileName))) return new RazorCSharpParsedDocument (fileName, new RazorCSharpPageInfo ()); this.project = project as AspMvcProject; EnsureParserInitializedFor (fileName); var errors = new List<Error> (); using (var source = new SeekableTextReader (content)) { var textChange = CreateTextChange (source); var parseResult = editorParser.CheckForStructureChanges (textChange); if (parseResult == PartialParseResult.Rejected) { parseComplete.WaitOne (); if (!capturedArgs.GeneratorResults.Success) GetRazorErrors (errors); } } CreateHtmlDocument (); GetHtmlErrors (errors); CreateCSharpParsedDocument (); ClearLastChange (); var pageInfo = new RazorCSharpPageInfo () { HtmlRoot = htmlParsedDocument, GeneratorResults = capturedArgs.GeneratorResults, Spans = editorParser.CurrentParseTree.Flatten (), CSharpParsedFile = parsedCodeFile, CSharpCode = csharpCode, Errors = errors, FoldingRegions = GetFoldingRegions (), Comments = comments, Compilation = CreateCompilation () }; return new RazorCSharpParsedDocument (fileName, pageInfo); }
private ITokenizer MakeTokenizer(bool markup, SeekableTextReader seekableTextReader) { if (markup) { return MarkupTokenizerFactory(seekableTextReader); } else { return CodeTokenizerFactory(seekableTextReader); } }
// Creates spans when the parsed document isn't available yet. void CreateSpans () { using (SeekableTextReader source = new SeekableTextReader (doc.Text)) { var markupParser = new HtmlMarkupParser (); var codeParser = new MvcCSharpRazorCodeParser (); var context = new ParserContext (source, codeParser, markupParser, markupParser) { DesignTimeMode = true }; codeParser.Context = context; markupParser.Context = context; context.ActiveParser.ParseDocument (); var results = context.CompleteParse (); currentSpans = results.Document.Flatten ().ToList (); } }
public ChangeInfo (int off, SeekableTextReader buffer) { offset = off; Length = 0; Buffer = buffer; }
TextChange CreateTextChange (SeekableTextReader source) { if (lastChange == null) return new TextChange (0, 0, new SeekableTextReader (String.Empty), 0, source.Length, source); if (lastChange.DeleteChange) return new TextChange (lastChange.StartOffset, lastChange.AbsoluteLength, lastChange.Buffer, lastChange.StartOffset, 0, source); return new TextChange (lastChange.StartOffset, 0, lastChange.Buffer, lastChange.StartOffset, lastChange.AbsoluteLength, source); }
static TextChange CreateTextChange (RazorCSharpParserContext context, SeekableTextReader source) { ChangeInfo lastChange = context.GetLastTextChange (); if (lastChange == null) return new TextChange (0, 0, new SeekableTextReader (String.Empty), 0, source.Length, source); if (lastChange.DeleteChange) return new TextChange (lastChange.StartOffset, lastChange.AbsoluteLength, lastChange.Buffer, lastChange.StartOffset, 0, source); return new TextChange (lastChange.StartOffset, 0, lastChange.Buffer, lastChange.StartOffset, lastChange.AbsoluteLength, source); }
System.Threading.Tasks.Task<ParsedDocument> Parse (RazorCSharpParserContext context, CancellationToken cancellationToken) { EnsureParserInitializedFor (context); var errors = new List<Error> (); using (var source = new SeekableTextReader (context.Content.CreateReader ())) { var textChange = CreateTextChange (context, source); var parseResult = context.EditorParser.CheckForStructureChanges (textChange); if (parseResult == PartialParseResult.Rejected) { context.RazorDocument.ParseComplete.WaitOne (); if (!context.CapturedArgs.GeneratorResults.Success) GetRazorErrors (context, errors); } } ParseHtmlDocument (context, errors); CreateCSharpParsedDocument (context); context.ClearLastTextChange (); RazorHostKind kind = RazorHostKind.WebPage; if (context.EditorParser.Host is WebCodeRazorHost) { kind = RazorHostKind.WebCode; } else if (context.EditorParser.Host is MonoDevelop.AspNet.Razor.Generator.PreprocessedRazorHost) { kind = RazorHostKind.Template; } var model = context.AnalysisDocument.GetSemanticModelAsync (cancellationToken).Result; var pageInfo = new RazorCSharpPageInfo () { HtmlRoot = context.HtmlParsedDocument, GeneratorResults = context.CapturedArgs.GeneratorResults, Spans = context.EditorParser.CurrentParseTree.Flatten (), CSharpSyntaxTree = context.ParsedSyntaxTree, ParsedDocument = new DefaultParsedDocument ("generated.cs") { Ast = model }, AnalysisDocument = context.AnalysisDocument, CSharpCode = context.CSharpCode, Errors = errors, FoldingRegions = GetFoldingRegions (context), Comments = context.Comments, HostKind = kind, }; return System.Threading.Tasks.Task.FromResult((ParsedDocument)new RazorCSharpParsedDocument (context.FileName, pageInfo)); }
protected virtual ParserResults RunParse(string document, Func<ParserBase, Action> parserActionSelector, bool designTimeParser) { // Create the source ParserResults results = null; using (SeekableTextReader reader = new SeekableTextReader(document)) { try { ParserBase codeParser = CreateCodeParser(); ParserBase markupParser = CreateMarkupParser(); ParserContext context = CreateParserContext(reader, codeParser, markupParser); context.DesignTimeMode = designTimeParser; codeParser.Context = context; markupParser.Context = context; // Run the parser parserActionSelector(context.ActiveParser)(); results = context.CompleteParse(); } finally { if (results != null && results.Document != null) { WriteTraceLine(String.Empty); WriteTraceLine("Actual Parse Tree:"); WriteNode(0, results.Document); } } } return results; }
private static string GenerateCodeFromRazorTemplate(WebPageRazorHost host, string virtualPath) { // create Razor engine and use it to generate a CodeCompileUnit var engine = new RazorTemplateEngine(host); var file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath); // load the source (eg cshtml) code string code; using (var stream = file.Open()) using (TextReader reader = new StreamReader(stream)) code = reader.ReadToEnd(); // generate the target (C#) code ITextBuffer textBuffer = new SeekableTextReader(code); var results = engine.GenerateCode(textBuffer, null, null, virtualPath); if (!results.Success) ThrowExceptionFromParserError(virtualPath, code, results.ParserErrors.Last()); // use CodeDom to generate source code from the CodeCompileUnit var codeDomProvider = new CSharpCodeProvider(); var srcFileWriter = new StringWriter(); codeDomProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, srcFileWriter, new CodeGeneratorOptions()); return srcFileWriter.ToString(); }