private SyntaxTree MakeFullCompilationUnit(SyntaxTree tree) { if (!_handlePartialConversion) { return(tree); } var root = tree.GetRoot(); var rootChildren = root.ChildNodes().ToList(); var requiresSurroundingClass = rootChildren.Any(_languageConversion.MustBeContainedByClass); var requiresSurroundingMethod = rootChildren.All(_languageConversion.CanBeContainedByMethod); if (requiresSurroundingMethod || requiresSurroundingClass) { var text = root.GetText().ToString(); if (requiresSurroundingMethod) { text = _languageConversion.WithSurroundingMethod(text); } text = _languageConversion.WithSurroundingClass(text); var fullCompilationUnit = _languageConversion.CreateTree(text).GetRoot(); var selectedNode = _languageConversion.GetSurroundedNode(fullCompilationUnit.DescendantNodes(), requiresSurroundingMethod); tree = fullCompilationUnit.WithAnnotatedNode(selectedNode, AnnotationConstants.SelectedNodeAnnotationKind, AnnotationConstants.AnnotatedNodeIsParentData); } return(tree); }
public static SyntaxTree MakeFullCompilationUnit(this ILanguageConversion languageConversion, string code, out TextSpan?textSpan) { var tree = languageConversion.CreateTree(code); var root = tree.GetRoot(); textSpan = null; var rootChildren = root.ChildNodes().ToList(); var requiresSurroundingClass = rootChildren.Any(languageConversion.MustBeContainedByClass); var requiresSurroundingMethod = rootChildren.All(languageConversion.CanBeContainedByMethod); if (requiresSurroundingMethod || requiresSurroundingClass) { var text = root.GetText().ToString(); if (requiresSurroundingMethod) { text = languageConversion.WithSurroundingMethod(text); } text = languageConversion.WithSurroundingClass(text); var fullCompilationUnit = languageConversion.CreateTree(text).GetRoot(); var selectedNode = languageConversion.GetSurroundedNode(fullCompilationUnit.DescendantNodes(), requiresSurroundingMethod); tree = fullCompilationUnit.WithAnnotatedNode(selectedNode, AnnotationConstants.SelectedNodeAnnotationKind, AnnotationConstants.AnnotatedNodeIsParentData); textSpan = selectedNode.Span; } return(tree); }