private StringBuilder Add_ImportsVB(RewriteFileCommandConfiguration commandConfiguration, out bool performedReplacement) { performedReplacement = false; var compilationUnit = commandConfiguration.SyntaxTree.GetCompilationUnitRoot(); var existingImports = compilationUnit.DescendantNodes().OfType <ImportsStatementSyntax>(); var newImportsStatement = SyntaxFactory.ImportsStatement(); // TODO(crhodes) // Learn how to add leading trivia to name. Without the space goes in as Imports<Name> not Imports <Name> newImportsStatement = newImportsStatement.AddImportsClauses( SyntaxFactory.SimpleImportsClause(SyntaxFactory.ParseName(string.Format(" {0}\n", teImportsName.Text))) ); bool alreadyExists = false; foreach (ImportsStatementSyntax item in existingImports) { if (item.ImportsClauses.ToString().Contains(teImportsName.Text)) { alreadyExists = true; break; } } if (!alreadyExists) { var newCompilationUnit = compilationUnit.AddImports(newImportsStatement); performedReplacement = VNCSR.Helpers.SaveFileChanges(commandConfiguration, newCompilationUnit.SyntaxTree.GetRoot()); } // TODO(crhodes) // Add a new method to add imports //var rewriter = new VNC.CodeAnalysis.SyntaxRewriters.VB.AddImportsStatement(teImportsName.Text); //rewriter.Messages = commandConfiguration.Results; //rewriter._configurationOptions = commandConfiguration.CodeAnalysisOptions; //SyntaxNode newNode = rewriter.Visit(commandConfiguration.SyntaxTree.GetRoot()); //performedReplacement = VNCSR.Helpers.SaveFileChanges(commandConfiguration, newNode); return(commandConfiguration.Results); }
public static bool SaveFileChanges(RewriteFileCommandConfiguration commandConfiguration, Microsoft.CodeAnalysis.SyntaxNode newNode) { Boolean performedReplacement = false; if (newNode != commandConfiguration.SyntaxTree.GetRoot()) { string fileSuffix = commandConfiguration.CodeAnalysisOptions.AddFileSuffix ? commandConfiguration.CodeAnalysisOptions.FileSuffix : ""; string newFilePath = commandConfiguration.FilePath + fileSuffix; File.WriteAllText(newFilePath, newNode.ToFullString()); performedReplacement = true; } return(performedReplacement); }
private StringBuilder Remove_MethodBlockVB(RewriteFileCommandConfiguration commandConfiguration, out bool performedReplacement) { performedReplacement = false; var rewriter = new VNC.CodeAnalysis.SyntaxRewriters.VB.RemoveMethodBlock( commandConfiguration.TargetPattern, (Boolean)ceCommentOut_MethodBlock.IsChecked, teComment.Text); rewriter.Messages = commandConfiguration.Results; rewriter._configurationOptions = commandConfiguration.CodeAnalysisOptions; SyntaxNode newNode = rewriter.Visit(commandConfiguration.SyntaxTree.GetRoot()); performedReplacement = VNCSR.Helpers.SaveFileChanges(commandConfiguration, newNode); return(commandConfiguration.Results); }
StringBuilder RewriteStopOrEndStatementVB(RewriteFileCommandConfiguration commandConfiguration, out bool performedReplacement) { performedReplacement = false; var rewriter = new VNC.CodeAnalysis.SyntaxRewriters.VB.RewriteStopOrEndStatement(); rewriter._configurationOptions = commandConfiguration.CodeAnalysisOptions; rewriter.Messages = commandConfiguration.Results; rewriter.Replacements = commandConfiguration.Replacements; SyntaxNode newNode = rewriter.Visit(commandConfiguration.SyntaxTree.GetRoot()); string fileSuffix = CodeExplorer.configurationOptions.ceAddFileSuffix.IsChecked.Value ? CodeExplorer.configurationOptions.teFileSuffix.Text : ""; performedReplacement = VNCSR.Helpers.SaveFileChanges(commandConfiguration, newNode); return(commandConfiguration.Results); }