Exemplo n.º 1
0
        public StatusEntryVM(Status.StatusEntry statusEntry, StatusVM statusVM, Area area)
        {
            StatusEntry = statusEntry;
            m_StatusVM  = statusVM;
            m_Area      = area;

            DiffCommand              = new DelegateCommand(Diff);
            LogCommand               = new DelegateCommand(Log);
            RevertCommand            = new DelegateCommand(() => Load(RevertSelected));
            OpenInExplorerCommand    = new DelegateCommand(OpenInExplorer);
            GeneratePatchFileCommand = new DelegateCommand(GeneratePatchFile);
        }
Exemplo n.º 2
0
        private void ProcessFile(bool addTabs, bool trim, int tabSize, Status.StatusEntry x, TabStats ts)
        {
            byte[] data = null;
            using (var fs = System.IO.File.OpenRead(x.FilesystemEntry.Info.FullName))
            {
                data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream(data.Length * 2);
            int  count   = 0;
            bool newline = false;
            bool trimmed = false;

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i] == (byte)'\n')
                {
                    newline = true;
                    ms.WriteByte((byte)'\n');
                }
                else if (data[i] == (byte)'\r')
                {
                    newline = true;
                    ms.WriteByte((byte)'\r');
                }
                else if (data[i] == (byte)' ')
                {
                    if (trim && !newline)
                    {
                        int trimcount = 1;
                        for (int j = i + 1; j < data.Length; j++)
                        {
                            if (data[j] == (byte)'\n' || data[j] == (byte)'\r')
                            {
                                break;
                            }
                            if (data[j] != (byte)' ' && data[j] != (byte)'\t')
                            {
                                trimcount = 0;
                                break;
                            }
                            trimcount++;
                        }
                        if (trimcount != 0)
                        {
                            trimmed = true;
                            i      += trimcount - 1;
                            continue;
                        }
                        else
                        {
                            ms.WriteByte(data[i]);
                            newline = false;
                        }
                    }
                    else if (addTabs && newline)
                    {
                        int span = 1;
                        for (int j = i + 1; j < i + tabSize && j < data.Length; j++)
                        {
                            if (data[j] != (byte)' ')
                            {
                                break;
                            }
                            span++;
                        }
                        if (span == tabSize)
                        {
                            count++;
                            ms.WriteByte((byte)'\t');
                        }
                        else
                        {
                            for (int j = 0; j < span; j++)
                            {
                                ms.WriteByte((byte)' ');
                            }
                        }
                        i += span - 1;
                    }
                    else
                    {
                        ms.WriteByte((byte)' ');
                    }
                }
                else if (data[i] == (byte)'\t')
                {
                    if (trim && !newline)
                    {
                        int trimcount = 1;
                        for (int j = i + 1; j < data.Length; j++)
                        {
                            if (data[j] == (byte)'\n' || data[j] == (byte)'\r')
                            {
                                break;
                            }
                            if (data[j] != (byte)' ' && data[j] != (byte)'\t')
                            {
                                trimcount = 0;
                                break;
                            }
                            trimcount++;
                        }
                        if (trimcount != 0)
                        {
                            trimmed = true;
                            i      += trimcount - 1;
                            continue;
                        }
                        else
                        {
                            ms.WriteByte(data[i]);
                            newline = false;
                        }
                    }
                    else if (!addTabs && newline)
                    {
                        for (int j = 0; j < tabSize; j++)
                        {
                            ms.WriteByte((byte)' ');
                        }
                        count++;
                    }
                    else
                    {
                        ms.WriteByte((byte)'\t');
                    }
                }
                else
                {
                    ms.WriteByte(data[i]);
                    newline = false;
                }
            }
            if (count > 0 || trimmed)
            {
                using (var fs = System.IO.File.Create(x.FilesystemEntry.Info.FullName))
                {
                    data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                }

                string mods = "";
                if (count > 0)
                {
                    if (addTabs)
                    {
                        mods = string.Format("added #b#{0}## tabs", count);
                    }
                    else
                    {
                        mods = string.Format("added #b#{0}## spaces", count * tabSize);
                    }
                }
                if (trimmed)
                {
                    if (!string.IsNullOrEmpty(mods))
                    {
                        mods += ", ";
                    }
                    mods += "trimmed trailing whitespace";
                }

                Printer.PrintMessage("#s#{0}## - {1}.", x.CanonicalName, mods);

                lock (ts)
                {
                    ts.Files++;
                    ts.Insertions += count;
                }
            }
        }