Esempio n. 1
0
        public static Chunk FromNewFile(GitModule module, string fileText, int selectionPosition, int selectionLength, bool reset)
        {
            Chunk result = new Chunk();

            result.StartLine = 0;
            int    currentPos = 0;
            string gitEol     = module.GetEffectiveSetting("core.eol");
            string eol;

            if ("crlf".Equals(gitEol))
            {
                eol = "\r\n";
            }
            else if ("native".Equals(gitEol))
            {
                eol = Environment.NewLine;
            }
            else
            {
                eol = "\n";
            }

            int eolLength = eol.Length;

            string[] lines = fileText.Split(new string[] { eol }, StringSplitOptions.None);
            int      i     = 0;

            while (i < lines.Length)
            {
                string    line      = lines[i];
                PatchLine patchLine = new PatchLine()
                {
                    Text = (reset ? "-" : "+") + line
                };
                //do not refactor, there are no breakpoints condition in VS Experss
                if (currentPos <= selectionPosition + selectionLength && currentPos + line.Length >= selectionPosition)
                {
                    patchLine.Selected = true;
                }

                if (i == lines.Length - 1)
                {
                    if (!line.Equals(string.Empty))
                    {
                        result.CurrentSubChunk.IsNoNewLineAtTheEnd = "\\ No newline at end of file";
                        result.AddDiffLine(patchLine, reset);
                    }
                }
                else
                {
                    result.AddDiffLine(patchLine, reset);
                }

                currentPos += line.Length + eolLength;
                i++;
            }
            return(result);
        }
Esempio n. 2
0
        public static Chunk FromNewFile(GitModule module, string fileText, int selectionPosition, int selectionLength, bool reset, byte[] FilePreabmle, Encoding fileContentEncoding)
        {
            Chunk result = new Chunk();

            result.StartLine = 0;
            int    currentPos = 0;
            string gitEol     = module.GetEffectiveSetting("core.eol");
            string eol;

            if ("crlf".Equals(gitEol))
            {
                eol = "\r\n";
            }
            else if ("native".Equals(gitEol))
            {
                eol = Environment.NewLine;
            }
            else
            {
                eol = "\n";
            }

            int eolLength = eol.Length;

            string[] lines = fileText.Split(new string[] { eol }, StringSplitOptions.None);
            int      i     = 0;

            while (i < lines.Length)
            {
                string    line      = lines[i];
                string    preamble  = (i == 0 ? new string(fileContentEncoding.GetChars(FilePreabmle)) : string.Empty);
                PatchLine patchLine = new PatchLine()
                {
                    Text = (reset ? "-" : "+") + preamble + line
                };
                //do not refactor, there are no breakpoints condition in VS Experss
                if (currentPos <= selectionPosition + selectionLength && currentPos + line.Length >= selectionPosition)
                {
                    patchLine.Selected = true;
                }

                if (i == lines.Length - 1)
                {
                    if (!line.Equals(string.Empty))
                    {
                        result.CurrentSubChunk.IsNoNewLineAtTheEnd = GitModule.NoNewLineAtTheEnd;
                        result.AddDiffLine(patchLine, reset);
                        if (reset && patchLine.Selected)
                        {
                            //if the last line is selected to be reset and there is no new line at the end of file
                            //then we also have to remove the last not selected line in order to add it right again with the "No newline.." indicator
                            PatchLine lastNotSelectedLine = result.CurrentSubChunk.RemovedLines.LastOrDefault(aLine => !aLine.Selected);
                            if (lastNotSelectedLine != null)
                            {
                                lastNotSelectedLine.Selected = true;
                                PatchLine clonedLine = lastNotSelectedLine.Clone();
                                clonedLine.SetOperation("+");
                                result.CurrentSubChunk.AddedLines.Add(clonedLine);
                            }
                            result.CurrentSubChunk.WasNoNewLineAtTheEnd = GitModule.NoNewLineAtTheEnd;
                        }
                    }
                }
                else
                {
                    result.AddDiffLine(patchLine, reset);
                }

                currentPos += line.Length + eolLength;
                i++;
            }
            return(result);
        }
Esempio n. 3
0
        public static Chunk ParseChunk(string chunkStr, int currentPos, int selectionPosition, int selectionLength)
        {
            string[] lines = chunkStr.Split('\n');
            if (lines.Length < 2)
            {
                return(null);
            }

            bool inPatch      = true;
            bool inPreContext = true;
            int  i            = 1;

            Chunk result = new Chunk();

            result.ParseHeader(lines[0]);
            currentPos += lines[0].Length + 1;

            while (i < lines.Length)
            {
                string line = lines[i];
                if (inPatch)
                {
                    PatchLine patchLine = new PatchLine()
                    {
                        Text = line
                    };
                    //do not refactor, there are no break points condition in VS Experss
                    if (currentPos <= selectionPosition + selectionLength && currentPos + line.Length >= selectionPosition)
                    {
                        patchLine.Selected = true;
                    }

                    if (line.StartsWith(" "))
                    {
                        result.AddContextLine(patchLine, inPreContext);
                    }
                    else if (line.StartsWith("-"))
                    {
                        inPreContext = false;
                        result.AddDiffLine(patchLine, true);
                    }
                    else if (line.StartsWith("+"))
                    {
                        inPreContext = false;
                        result.AddDiffLine(patchLine, false);
                    }
                    else if (line.StartsWith(GitModule.NoNewLineAtTheEnd))
                    {
                        if (result.CurrentSubChunk.AddedLines.Count > 0 && result.CurrentSubChunk.PostContext.Count == 0)
                        {
                            result.CurrentSubChunk.IsNoNewLineAtTheEnd = line;
                        }
                        else
                        {
                            result.CurrentSubChunk.WasNoNewLineAtTheEnd = line;
                        }
                    }
                    else
                    {
                        inPatch = false;
                    }
                }

                currentPos += line.Length + 1;
                i++;
            }

            return(result);
        }