Beispiel #1
0
 private void AddContextLine([NotNull] PatchLine line, bool preContext)
 {
     if (preContext)
     {
         CurrentSubChunk.PreContext.Add(line);
     }
     else
     {
         CurrentSubChunk.PostContext.Add(line);
     }
 }
Beispiel #2
0
        private void AddDiffLine([NotNull] PatchLine line, bool removed)
        {
            // if postContext is not empty @line comes from next SubChunk
            if (CurrentSubChunk.PostContext.Count > 0)
            {
                _currentSubChunk = null; // start new SubChunk
            }

            if (removed)
            {
                CurrentSubChunk.RemovedLines.Add(line);
            }
            else
            {
                CurrentSubChunk.AddedLines.Add(line);
            }
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public string ToIndexPatch(ref int addedCount, ref int removedCount, ref bool wereSelectedLines, bool isIndex, bool isWholeFile)
        {
            string diff             = null;
            string removePart       = null;
            string addPart          = null;
            string prePart          = null;
            string postPart         = null;
            bool   inPostPart       = false;
            bool   selectedLastLine = false;

            addedCount   += PreContext.Count + PostContext.Count;
            removedCount += PreContext.Count + PostContext.Count;

            foreach (PatchLine line in PreContext)
            {
                diff = diff.Combine("\n", line.Text);
            }

            for (int i = 0; i < RemovedLines.Count; i++)
            {
                PatchLine removedLine = RemovedLines[i];
                selectedLastLine = removedLine.Selected;
                if (removedLine.Selected)
                {
                    wereSelectedLines = true;
                    inPostPart        = true;
                    removePart        = removePart.Combine("\n", removedLine.Text);
                    removedCount++;
                }
                else if (!isIndex)
                {
                    if (inPostPart)
                    {
                        removePart = removePart.Combine("\n", " " + removedLine.Text.Substring(1));
                    }
                    else
                    {
                        prePart = prePart.Combine("\n", " " + removedLine.Text.Substring(1));
                    }

                    addedCount++;
                    removedCount++;
                }
            }

            bool selectedLastRemovedLine = selectedLastLine;

            for (int i = 0; i < AddedLines.Count; i++)
            {
                PatchLine addedLine = AddedLines[i];
                selectedLastLine = addedLine.Selected;
                if (addedLine.Selected)
                {
                    wereSelectedLines = true;
                    inPostPart        = true;
                    addPart           = addPart.Combine("\n", addedLine.Text);
                    addedCount++;
                }
                else if (isIndex)
                {
                    if (inPostPart)
                    {
                        postPart = postPart.Combine("\n", " " + addedLine.Text.Substring(1));
                    }
                    else
                    {
                        prePart = prePart.Combine("\n", " " + addedLine.Text.Substring(1));
                    }

                    addedCount++;
                    removedCount++;
                }
            }

            diff = diff.Combine("\n", prePart);
            diff = diff.Combine("\n", removePart);
            if (PostContext.Count == 0 && (!isIndex || selectedLastRemovedLine))
            {
                diff = diff.Combine("\n", WasNoNewLineAtTheEnd);
            }

            diff = diff.Combine("\n", addPart);
            diff = diff.Combine("\n", postPart);
            foreach (PatchLine line in PostContext)
            {
                diff = diff.Combine("\n", line.Text);
            }

            // stage no new line at the end only if last +- line is selected
            if (PostContext.Count == 0 && (selectedLastLine || isIndex || isWholeFile))
            {
                diff = diff.Combine("\n", IsNoNewLineAtTheEnd);
            }

            if (PostContext.Count > 0)
            {
                diff = diff.Combine("\n", WasNoNewLineAtTheEnd);
            }

            return(diff);
        }