Ejemplo n.º 1
0
        /// <summary>
        /// Append a source buffer at the end of a target buffer
        /// </summary>
        /// <param name="dstBuffer"></param>
        /// <param name="srcBuffer"></param>
        private void AppendBufferContent(SourceText dstBuffer, SourceText srcBuffer)
        {
            if (BufferExceedMap != null)
            {
                if (BufferExceedMap.ContainsKey(srcBuffer))
                {//Create all start positions in the buffer.
                    //originalPosition keep the original position of the position so that we can calculate
                    //the exceed length, for the minimal spliting.
                    Dictionary <Position, int> originalPositions = new Dictionary <Position, int>();
                    foreach (var linePos in BufferExceedMap[srcBuffer])
                    {
                        foreach (var start in linePos.Value)
                        {
                            originalPositions[start] = start.Pos;
                            srcBuffer.AddPosition(start);
                        }
                    }
                    bool bLineStillTooLong = false;
                    //Split each exceeded line based on the right most exceed position.
                    foreach (var linePos in BufferExceedMap[srcBuffer])
                    {
                        List <Position>  splittingPositionStack = new List <Position>();
                        int              lineNumber             = linePos.Key;
                        Tuple <int, int> lineStartLength        = ExceedLines[lineNumber];
                        int              lineStartPos           = lineStartLength.Item1;
                        int              lineLength             = lineStartLength.Item2;
                        int              lastEndOrgPos          = lineStartPos + lineLength;
                        int              firstSplitablePos      = originalPositions[linePos.Value[0]];
                        int              targeSplittColumn      = 0;
                        for (int i = linePos.Value.Count - 1; i >= 0; i--)
                        {
                            Position start       = linePos.Value[i];
                            int      originalPos = originalPositions[start];

                            if (IsPlitablePosition(firstSplitablePos, lineLength, originalPos, out targeSplittColumn))
                            {   //we can split here => so check if the rest at the left still exceed
                                //Push this splitting position
                                splittingPositionStack.Add(start);
                                if ((originalPos - lineStartPos) < LEGAL_COBOL_LINE_LENGTH)
                                {          //The line is till too long?
                                    break; //No ==> Stop splitting this line
                                }
                            }
                            else
                            {//This means that the line will stil too long.
                                bLineStillTooLong = true;
                            }
                        }
                        if (splittingPositionStack.Count > 0)
                        {//This line can be splitted so split it.
                            int rightPos = lineLength;
                            for (int i = splittingPositionStack.Count - 1; i >= 0; i--)
                            {
                                Position start       = splittingPositionStack[i];
                                int      originalPos = originalPositions[start];
                                for (int j = 0; j <= i; j++)
                                {
                                    if (IsPlitablePosition(firstSplitablePos, rightPos, originalPos, out targeSplittColumn))
                                    {
                                        string pad = new StringBuilder().Append(Environment.NewLine).Append(new string(' ', Math.Abs(targeSplittColumn - lineStartPos))).ToString();
                                        //So break here
                                        srcBuffer.Insert(pad, start.Pos, start.Pos);
                                        i = j;
                                        break;
                                    }
                                    else
                                    {
                                        Position jstart = splittingPositionStack[j];
                                        rightPos = originalPositions[jstart];
                                    }
                                }
                            }
                            //Remove it from exceed, if the line has been correctly splitted
                            if (!bLineStillTooLong)
                            {
                                ExceedLines.Remove(lineNumber);
                            }
                        }
                    }
                }
            }
            int pos = dstBuffer.Size;

            dstBuffer.Insert(srcBuffer, pos, pos);
        }