Exemple #1
0
        public static Chunk FromNewFile([NotNull] GitModule module, [NotNull] string fileText, int selectionPosition, int selectionLength, bool reset, [NotNull] byte[] filePreamble, [NotNull] Encoding fileContentEncoding)
        {
            var result = new Chunk {
                _startLine = 0
            };

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

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

            int eolLength = eol.Length;

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

            while (i < lines.Length)
            {
                string line      = lines[i];
                string preamble  = i == 0 ? new string(fileContentEncoding.GetChars(filePreamble)) : string.Empty;
                var    patchLine = new PatchLine((reset ? "-" : "+") + preamble + line);

                // do not refactor, there are no breakpoints condition in VS Express
                if (currentPos <= selectionPosition + selectionLength && currentPos + line.Length >= selectionPosition)
                {
                    patchLine.Selected = true;
                }

                if (i == lines.Length - 1)
                {
                    if (line != 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(l => !l.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);
        }
Exemple #2
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;

            var result = new Chunk();

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

            while (i < lines.Length)
            {
                string line = lines[i];
                if (inPatch)
                {
                    var patchLine = new PatchLine(line);

                    // do not refactor, there are no break points condition in VS Express
                    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);
        }