private void ReadSharpLine(string line, int codeBlock, ref CSharpFileInfo csFileInfo) { if (codeBlock <= 1) { var match = RegexPattern.UsingStatement.Match(line); if (match.Success) { csFileInfo.UsingNamespaces.Add(match.Groups[1].Value); } match = RegexPattern.Namespace.Match(line); if (match.Success) { csFileInfo.Namespace = match.Groups[1].Value; csFileInfo.UsingNamespaces.Add(csFileInfo.Namespace); } } if (string.IsNullOrEmpty(csFileInfo.ClassName)) { var match = RegexPattern.Class.Match(line); if (match.Success) { csFileInfo.ClassName = match.Groups[3].Value; csFileInfo.IsPartialClass = !string.IsNullOrEmpty(match.Groups[2].Value); csFileInfo.IsEntryPoint = match.Groups[4].Value == "GameScriptInterface"; } } }
public CSharpFileInfo ReadCSharpFile(ProjectInfo projectInfo, string filePath) { if (!File.Exists(filePath)) { return(new CSharpFileInfo()); } var csFileInfo = new CSharpFileInfo(); var sourceCode = new StringBuilder(); var breakpoints = projectInfo.Breakpoints .Where(b => b.File == filePath) .ToDictionary(b => b.LineNumber, b => b); using (var file = File.OpenText(filePath)) { var line = ""; var codeBlock = 0; var lineNumber = 1; while ((line = file.ReadLine()) != null) { ReadSharpLine(line, codeBlock, ref csFileInfo); var lineInfo = GetLineInfo(line); if (lineInfo.StartBlock) { codeBlock++; } if (codeBlock == 1 && !lineInfo.StartBlock && !lineInfo.EndBlock) { sourceCode.AppendLine(line); } if (codeBlock > 1) { sourceCode.AppendLine(line); } if (breakpoints.ContainsKey(lineNumber)) { sourceCode.AppendLine(BreakpointStatement); } if (lineInfo.EndBlock) { codeBlock--; } lineNumber++; } } csFileInfo.Content = sourceCode.ToString(); return(csFileInfo); }
public static SyntaxTree ParseFile(this CSharpFileInfo csharpFileInfo) { if (csharpFileInfo == null) { throw new ArgumentNullException(nameof(csharpFileInfo)); } var text = File.ReadAllText(csharpFileInfo.Value.FullName); var syntaxTree = CSharpSyntaxTree.ParseText(text); return(syntaxTree); }
public void TestWillParseAll() { string file = @"CSharp\single_namespace_file.txt"; CSharpFileInfo fileInfo = CSharpFileParser.ParseFile(file); Assert.AreEqual(1, fileInfo.Imports.Count()); Assert.AreEqual(true, fileInfo.HasMainMethod); Assert.AreEqual(1, fileInfo.NamespacesContent.Count()); Assert.AreEqual(2, fileInfo.NamespacesContent.Single().Imports.Count()); Assert.IsTrue(fileInfo.NamespacesContent.Single().Content.Contains("class Foo")); Assert.AreEqual("FooNamespace", fileInfo.NamespacesContent.Single().Name); Assert.IsFalse(fileInfo.NamespacesContent.Single().Content.Contains("using FooNamespace.Bar")); }