Ejemplo n.º 1
0
        public String ToCSharp()
        {
            IOutputAstVisitor outputVisitor = new CSharpOutputVisitor();

            SetPrettyPrintOptions(outputVisitor);

            using (SpecialNodesByMapInserter.Install(new Hashtable(), outputVisitor)) {
                unit.AcceptVisitor(outputVisitor, null);
            }
            String code = outputVisitor.Text;

            return(NRefactoryUtil.FixSourceFormatting(code));
        }
Ejemplo n.º 2
0
        public SourceUnit(String content)
        {
            using (ICSharpCode.NRefactory.IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(content))) {
                parser.Parse();
                if (parser.Errors.Count > 0)
                {
                    throw new Exception(parser.Errors.ErrorOutput);
                }
                specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
                unit     = parser.CompilationUnit;
                NRefactoryUtil.InsertSpecials(unit, specials);
                //List<ISpecial> rightSpecials = new List<ISpecial>();
                //rightSpecials.AddRange(specials);
                //MapSpecials(unit.Children, rightSpecials);
                //
                //if (rightSpecials.Count > 0) {
                //    System.Diagnostics.Debug.WriteLine("There are specials that did not get mapped to members.");
                //}
            }

            foreach (INode child in unit.Children)
            {
                if (child is UsingDeclaration)
                {
                    imports.Add(child as UsingDeclaration);
                }
                else if (child is NamespaceDeclaration)
                {
                    namespaces.Add(new NamespaceUnit(child as NamespaceDeclaration));
                }
                else if (child is TypeDeclaration)
                {
                    if (namespaces.Count == 0)
                    {
                        namespaces.Add(new NamespaceUnit());
                    }
                    namespaces[0].AddType(new TypeUnit(child as TypeDeclaration));
                }
                else
                {
                    throw new Exception("Unknown node type: " + child.GetType() + " - " + child.ToString());
                }
            }
        }
Ejemplo n.º 3
0
        public Boolean Write(FileInfo file, String contents, IStyler styler)
        {
            // Create the directory if it doesn't exist.
            if (!file.Directory.Exists)
            {
                file.Directory.Create();
            }
            Console.Out.WriteLine("processing file " + file.FullName);

            MemoryStream stream = new MemoryStream();
            StreamWriter sw     = new StreamWriter(stream);

            sw.Write(contents);
            sw.Flush();
            stream.Position = 0;

            if (!file.Exists)
            {
                //CodeUnit unit1 = null;
                try {
                    //unit1 = new CodeUnit(file.Name, stream, Log, cgOptions);
                    //String mergedContent = unit1.Generate();
                    String       mergedContent = contents;
                    StreamWriter writer        = new StreamWriter(file.FullName, false);
                    writer.Write(mergedContent);
                    writer.Close();
                    DoPostProcessingNew(file.FullName);
                    return(true);
                } catch (Exception ex) {
                    Console.Out.WriteLine("Error in File Name " + file.Name);
                    Console.Out.WriteLine(ex);
                    Log.Add("Error in File Name " + file.Name + ":" + ex.ToString());
                    return(false);
                }
            }
            else
            {
                //FileStream fs = null;
                //CodeUnit unit1 = null;
                //CodeUnit unit2 = null;
                try {
                    //fs = file.OpenRead();
                    //unit1 = new CodeUnit(file.Name, fs, Log, cgOptions);
                    //fs.Close();
                    //fs = null;
                    //unit2 = new CodeUnit(file.Name, stream, Log, cgOptions);
                    //unit1.Merge(unit2);

                    StreamReader sr = file.OpenText();
                    String       exitingContents = sr.ReadToEnd();
                    sr.Close();

                    String mergedContent = NRefactoryUtil.Merge(contents, exitingContents);

                    // determine whether anything has actually changed in the file
                    bool   fileHasChanged = false;
                    String mergedContentIgnoreWhitespace   = mergedContent.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
                    String exitingContentsIgnoreWhitespace = exitingContents.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "");
                    if (mergedContentIgnoreWhitespace != exitingContentsIgnoreWhitespace)
                    {
                        fileHasChanged = true;
                    }

                    if (fileHasChanged)
                    {
                        // only write out if the formatted contents of both are different (avoids the "DTG reformatting" commit messages, at least some of the time)
                        String fixedFormattingExitingContents = NRefactoryUtil.FixSourceFormatting(exitingContents);
                        String fixedFormattingMergedContents  = NRefactoryUtil.FixSourceFormatting(mergedContent);
                        if (!fixedFormattingMergedContents.Equals(fixedFormattingExitingContents))
                        {
                            // make backup
                            if (file.Exists && backupFilePath != "")
                            {
                                FileInfo backup = new FileInfo(backupFilePath);
                                if (!backup.Directory.Exists)
                                {
                                    backup.Directory.Create();
                                }
                                if (File.Exists(backupFilePath) && (File.GetAttributes(backupFilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    File.SetAttributes(backupFilePath, File.GetAttributes(backupFilePath) ^ FileAttributes.ReadOnly);
                                }
                                file.CopyTo(backupFilePath, true);
                                if ((File.GetAttributes(backupFilePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    File.SetAttributes(backupFilePath, File.GetAttributes(backupFilePath) ^ FileAttributes.ReadOnly);
                                }
                            }

                            // Style the contents.
                            string styledContent = styler.Style(mergedContent);
                            DoPreProcessingExisting(file.FullName);
                            StreamWriter writer = new StreamWriter(file.FullName, false);
                            writer.Write(styledContent);
                            writer.Close();
                            return(true);
                        }
                    }
                } catch (Exception ex) {
                    Console.Out.WriteLine("Error in File Name " + file.Name);
                    Console.Out.WriteLine(ex);
                    Log.Add("Error in File Name " + file.Name + ":" + ex.ToString());
                } finally {
                    //if (fs != null) {
                    //    fs.Close();
                    //}
                }

                return(false);
            }
        }