Ejemplo n.º 1
0
 public void findSplitPoint(WrapItem info, String restOfText, int lineStart, int[] results)
 {
     int splitPoint=info.getAlternateFirstItemWrapPoint();
     if (splitPoint<0)
         splitPoint=info.mStartPos;
     int lengthAtSplit=getColumnLength(0, restOfText, 0, splitPoint-lineStart);
     if (info.isBreakBefore())
     {
         //look for whitespace before split point, that shouldn't be counted
         int removedWSCount=0;
         for (int i=splitPoint-lineStart-1;i>=0;i--)
         {
             char c = restOfText[i];
             if (c==' ' || c=='\t')
                 removedWSCount++;
             else
                 break;
         }
     
         //if we found some whitespace to trim, then recalculate the split position
         if (removedWSCount>0)
         {
             lengthAtSplit=getColumnLength(0, restOfText, 0, (splitPoint-lineStart)-removedWSCount);
             splitPoint=splitPoint-removedWSCount;
         }
     }
     else
     {
         lengthAtSplit+=info.getText().Length;
     }       
 
     results[0]=splitPoint;
     results[1]=lengthAtSplit;
 }
Ejemplo n.º 2
0
 public void addWrapChar(IToken op, int depth, int position, int breakType, bool breakBefore, bool indentToFirstParm, int firstParmLocation, int currentIndent, String commaContextType)
 {
     WrapItem item = new WrapItem(op, depth, position, breakType, breakBefore, currentIndent);
     item.setIndentToFirstParm(indentToFirstParm);
     item.setFirstParmPos(firstParmLocation);
     item.setCommaContextType(commaContextType);
     mWrapItems.Add(item);
 }
Ejemplo n.º 3
0
     private int updateEarlyWrapPoint(WrapItem firstItem, bool isArray) 
     {
         for (int i=firstItem.getStartPos()-1;i>=0;i--)
         {
             char ch = mOutputBuffer[i];
             if (!AntlrUtilities.isASWhitespace(ch) && ((isArray && ch!='{') || (!isArray && ch!='[')))
             {
                 firstItem.setAlternateFirstItemWrapPoint(i);
 //              firstItem.setStartPos(i);
 //              firstItem.setNextItemPos(i);
                 break;
             }
         }
     
         int alignmentOffset=1; //always 1 for the [ or {
         for (int i=firstItem.getStartPos()-1;i>=0;i--)
         {
             char ch = mOutputBuffer[i];
             if (!AntlrUtilities.isASWhitespace(ch))
             {
                 int adjustedStartPos=i;
                 if ((isArray && ch=='[') || (!isArray && ch=='{'))
                     adjustedStartPos++;
                 firstItem.setStartPos(adjustedStartPos);
                 firstItem.setNextItemPos(adjustedStartPos);
                 return alignmentOffset;
             }
             alignmentOffset++; //for additional spaces
         }
         return alignmentOffset;
     }
Ejemplo n.º 4
0
 private void findWrapItemsAndAdjustChildIndent(WrapInfo wi, WrapItem info, List<WrapItem> wrapItems) 
 {
     bool seenFirstItemAtDepth=false;
     for (int i=0;i<wi.mWrapItems.Count;i++)
     {
         WrapItem testItem = wi.mWrapItems[i];
         //only the same if the same type, depth, AND if the first parm pos is the same.  Otherwise, two arg lists at the same level would appear the same
         if (info.getBreakType()==testItem.getBreakType() && info.getDepth()==testItem.getDepth() && info.getFirstParmPos()==testItem.getFirstParmPos())
         {
             seenFirstItemAtDepth=true;
             wrapItems.Add(testItem);
         }
         else if (!testItem.isBreakUsed() && testItem.getDepth()>info.getDepth())
         {
             testItem.setIndent(info.getIndent()+getTabSize());
         }
         //quit if we have seen the first item at the proper scope and now we have 
         //unwound and seen the end of that item's scope
         if (seenFirstItemAtDepth && testItem.getDepth()<info.getDepth())
             break;
     }
 }
Ejemplo n.º 5
0
        private void wrapArrayOrObjectItems(WrapInfo wi, List<WrapItem> wrapItems, WrapItem info, bool wrapFirstItem, bool wrapItemsAlignStart, bool isArray) 
        {
            wrapItems.Clear();
        
            //we're taking over wrapping, so now we need to figure out where all the items are going to
            //go
    //      wrappingFirstParm=mWrapFirstObjectItem;
            //put in entry for first item to make things easier for alignment code and if the
            //first item needs to be wrapped.
            WrapItem firstItem=new WrapItem(null, info.getDepth(), info.getFirstParmPos(), info.getBreakType(), true, info.getIndent());
            firstItem.setNextItemPos(info.getFirstParmPos());
            wrapItems.Add(firstItem);
            int alignmentOffset=updateEarlyWrapPoint(firstItem, isArray);
        
            int itemIndent=info.getIndent();
            if ((!wrapFirstItem || isFirstItemOnLine(firstItem.getStartPos())) && info.getFirstParmPos()>0)
            {
                itemIndent=getColumnForPosition(firstItem.getStartPos());
                wrapFirstItem=false;
            }

    //      itemIndent-=alignmentOffset; //to adjust for the open brace/bracket and spaces
    //      if (!wrapFirstItem)
    //          itemIndent=getColumnForPosition(firstItem.getStartPos());
        
            findWrapItemsAndAdjustChildIndent(wi, info, wrapItems);
        
            //find the first text pos for each item
            foreach (WrapItem item in wrapItems) 
            {
                if (item.getNextItemPos()<0)
                {
                    //update item pos by finding the next non-whitespace char after the comma pos
                    int location=item.getStartPos()+1;
                    for (;location<mOutputBuffer.Length;location++)
                    {
                        if (!AntlrUtilities.isASWhitespace(mOutputBuffer[location]))
                            break;
                    }
                    item.setNextItemPos(location);
                }
            }                       
        
    //      if (mWrapObjectItemsPerLine>0) //predetermined how many to do per line (usually one)
    //      {
    //          //remove items that aren't used, and mark them 'used' so that they won't break again
    //          
    //          int[] maxItemWidths=new int[mWrapObjectItemsPerLine];
    //          findMaxItemWidths(wrapItems, maxItemWidths);
    //          setUsedItems(wrapItems, itemIndent, itemsPerLine);
    //          
    //          //determine (and add) extra spaces before removing items from list
    //          if (mWrapObjectItemsAlignStart)
    //          {
    //              addInternalSpaces(wi, wrapItems, maxItemWidths);                    
    //          }
    //          
    //          //remove items that won't receive breaks
    //          removeUsedItems(wrapItems);
    //      }
    //      else
            {
                if (wrapItemsAlignStart)
                {
                    if (wrapFirstItem && !isFirstItemOnLine(firstItem.getStartPos()))
                    {
                    }
                    else
                    {
                        firstItem.setBreakUsed(true);
                    }
                

                    //start with max items, then work down as I find a line that's tool long 
                    //because of the aligned columns.
                    int itemsPerLine=wrapItems.Count;
                
                    //decrement itemsPerLine as we try to find a fit
                    int[] maxItemWidths=new int[itemsPerLine];
                    while (itemsPerLine>1)
                    {
                        //find the max widths of items based on the given items per line
                        maxItemWidths=new int[itemsPerLine];
                        findMaxItemWidths(wrapItems, maxItemWidths);
                    
                        //now, check the max widths and see if this number of items will work for all lines
                        int column=itemIndent;
                        for (int i=0;i<itemsPerLine;i++)
                        {
                            column+=maxItemWidths[i];
                        }
                        if (column<=mMaxLineLength)
                            break;
                    
                        itemsPerLine--;
                    }
                
                    //mark the items as 'used' if we're not going to have to break them further
                    setUsedItems(wrapItems, itemIndent, itemsPerLine, wrapFirstItem, wrapFirstItem ? alignmentOffset : 0);
                
                    //I've picked a correct number of items per line--now, increment the positions where necessary.
                    addInternalSpaces(wi, wrapItems, maxItemWidths);
                
                    //remove items that won't receive breaks
                    removeUsedItems(wrapItems);
                }
                else //just wrapping items to line length, no alignment
                {
                    //determine each wrap item and remove the others
                    int originalIndent=info.getIndent();
                    int currentIndent=itemIndent;
                    int currentItemStartPos=firstItem.getStartPos();
                    if (wrapFirstItem && firstItem.getAlternateFirstItemWrapPoint()>=0)
                        currentItemStartPos=firstItem.getAlternateFirstItemWrapPoint();
                    int itemsSinceLastWrap=0;
                    for (int i=0;i<wrapItems.Count;i++)
                    {
                        WrapItem item=wrapItems[i];
                        int nextStartPos = mOutputBuffer.ToString().IndexOf('\n', item.getStartPos());
                        if (nextStartPos<0)
                            nextStartPos=mOutputBuffer.Length;
                        if (i+1<wrapItems.Count)
                        {
                            WrapItem nextItem = wrapItems[i+1];
                            nextStartPos=nextItem.getStartPos();
                        }
                    
                        int testLength=nextStartPos-currentItemStartPos+currentIndent+1;
    //                  String testString=mOutputBuffer.Substring(currentItemStartPos, nextStartPos);
    //                  int testLength=currentIndent+getColumnLength(0, testString, 0, testString.Length);  
                        if (testLength>mMaxLineLength && itemsSinceLastWrap>0 && !isFirstItemOnLine(item.getStartPos()))
                        {
                            currentItemStartPos=item.getNextItemPos();
                            if (!info.isIndentToFirstParm()) //switch indent if we are not indenting to first item
                            {
                                currentIndent=originalIndent+alignmentOffset;
                            }
                            itemsSinceLastWrap=1;
                        }
                        else
                        {
                            if (i>0 || !wrapFirstItem || isFirstItemOnLine(item.getStartPos()))
                                item.setBreakUsed(true); //don't need to break here
                        
                            if (item.isBreakUsed() || i==0) //1st item is special because it doesn't matter if it's wrapped or not, it's still an item on the line
                                itemsSinceLastWrap++;
                            else
                                itemsSinceLastWrap=1; //to account for case where already on a line by itself
                        }
                    }
                
                    for (int i=1;i<wrapItems.Count;i++) //start at 1 to skip the first item
                    {
                        WrapItem item = wrapItems[i];
                        item.setIndent(originalIndent+alignmentOffset);
                    }
                
                    //remove items that won't receive breaks
                    removeUsedItems(wrapItems);
                }
            }
        }
Ejemplo n.º 6
0
        private int possiblyWrap(WrapInfo wi, WrapItem info, int infoIndex, int lineStart, String restOfText, int depth, bool forceWrap, int maxLineLength)
        {
            if (info.isBreakUsed())
                return -1;
            if ((depth<0 || (depth==info.mDepth)) && (forceWrap || (info.getBreakType() & getAdvancedWrappingElements())!=0) || info.getBreakType()==Break_XML_code)
            {
                //TODO: handle before/after calculation and processing
                int[] splitPointInfo=new int[2];
                findSplitPoint(info, restOfText, lineStart, splitPointInfo);
                int splitPoint=splitPointInfo[0];
                int lengthAtSplit=splitPointInfo[1];

                //TODO: need to split lines even if no split would satisfy the line length.  It appears as well that
                //my current algorithm should force a split if the alternative is not splitting at all.
                if (lengthAtSplit<=maxLineLength) // || wrapAllArgsOrParms || wrapAllObjectDef || wrapAllArrayDef)
                {
                    //at this point, we need to check to see if the current wrap point is for a comma in one of the special
                    //wrapping modes.  If so, we want to walk up to lower depths to find the highest level item of one of 
                    //these special wrap types.
                
                    bool wrapAllObjectDef=isWrapAllObject(info);
                    bool wrapAllArgsOrParms=isWrapAllArgsAndParms(info);
                    bool wrapAllArrayDef=isWrapAllArray(info);
    //              if (wrapAllArgsOrParms || wrapAllObjectDef || wrapAllArrayDef)
                    {
                        int workingDepth=info.getDepth();
                        WrapItem higherItem=info;
                        //search for higher level items further down the list
                        for (int i=infoIndex+1;i<wi.mWrapItems.Count;i++)
                        {
                            WrapItem testItem=wi.mWrapItems[i];
                            if (testItem.getDepth()<workingDepth)
                            {
                                workingDepth=testItem.getDepth();
                                if (!testItem.isBreakUsed())
                                {
                                    if (isWrapAllArgsAndParms(testItem) || isWrapAllObject(testItem) || isWrapAllArray(testItem))
                                    {
                                        higherItem=testItem;
                                    }
                                }
                            }
                        }
                    
                        if (higherItem!=info)
                        {
                            info=higherItem;
                            wrapAllObjectDef=isWrapAllObject(info);
                            wrapAllArgsOrParms=isWrapAllArgsAndParms(info);
                            wrapAllArrayDef=isWrapAllArray(info);
                        
                        }
                    }
                
                    List<WrapItem> wrapItems=new List<WrapItem>();
                    wrapItems.Add(info);
                
                    int returnedInsertPos=(-1); //want to return the first insert pos if we split multiple args in this one pass, because within an arg, the line could violate the length.
                
                    //arg/parm wrapping (if all items are to be wrapped)
                    bool wrappingFirstParm=wrapAllArgsOrParms && ((mWrapFirstArgument && info.getCommaContextType()==Break_SubType_Arguments) ||
                            (mWrapFirstParameter && info.getCommaContextType()==Break_SubType_Parameters));
                    if (wrapAllArgsOrParms)
                    {
                        wrapItems.Clear();
                        if (wrappingFirstParm && info.getFirstParmPos()>=0)
                        {
                            WrapItem firstItem=new WrapItem(null, info.getDepth(), info.getFirstParmPos(), info.getBreakType(), true, info.getIndent());
                            wrapItems.Add(firstItem);
                        }
                        findWrapItemsAndAdjustChildIndent(wi, info, wrapItems);
                        returnedInsertPos=info.getFirstParmPos();
                    }
                
                    //////////////////////////////////////////////////////////////////////////////
                    //object item wrapping
                    //////////////////////////////////////////////////////////////////////////////
                    if (wrapAllObjectDef)
                    {
                        wrapArrayOrObjectItems(wi, wrapItems, info, mWrapFirstObjectItem, mWrapObjectItemsAlignStart, false);
                        returnedInsertPos=info.getFirstParmPos();
                    }

                    ///////////////////////////////////////////////////////////////////////////////
                    //array wrapping
                    ///////////////////////////////////////////////////////////////////////////////
                    if (wrapAllArrayDef)
                    {
                        wrapArrayOrObjectItems(wi, wrapItems, info, mWrapFirstArrayItem, mWrapArrayItemsAlignStart, true);
                        returnedInsertPos=info.getFirstParmPos();
                    }


                    foreach (WrapItem wrapItem in wrapItems) 
                    {
                        restOfText = mOutputBuffer.ToString().Substring(lineStart);
                        findSplitPoint(wrapItem, restOfText, lineStart, splitPointInfo);
                        splitPoint=splitPointInfo[0];
                        lengthAtSplit=splitPointInfo[1];
                    
                        int insertPos=splitPoint;
                        if (wrapItem.isBreakBefore())
                        {
                            //nothing to do.
                        }
                        else
                        {
                            //move the split point past the operator/split token
                            insertPos+=wrapItem.getText().Length;
                        }
                    
                        //remove whitespace at insert point, because we will be starting a new line
                        int loopSafety=0;
                        while (insertPos<mOutputBuffer.Length)
                        {
                            char testChar=mOutputBuffer[insertPos];
                            if (testChar==' ' || testChar=='\t')
                            {
                                mOutputBuffer.Remove(insertPos, insertPos+1);
                                wi.incrementPositions(insertPos, -1);
                            }
                            else
                                break;
                            if (loopSafety++ > 1000) //kick out if I seem to be stuck
                                break;
                        }
                    
                        //handle hanging indent
                        int indentAmount=wrapItem.getIndent();
                        if (wrapItem.getFirstParmPos()>=0 && wrapItem.isIndentToFirstParm() && !wrappingFirstParm)
                        {
                            indentAmount=getColumnForPosition(wrapItem.getFirstParmPos());
                        }
                        String indentWS=generateIndent(indentAmount);
                    
                        mOutputBuffer.Insert(insertPos, "\n"+indentWS);
                        wrapItem.setBreakUsed(true);
                    
                        //adjust other positions based on the characters we've inserted
                        wi.incrementPositions(insertPos+1, 1+indentWS.Length);
                    
                        if (returnedInsertPos<0)
                        {
                            //return the position just after the \n
                            returnedInsertPos=(insertPos+1);
                        }
                    }
                
                    return returnedInsertPos;
    //              return insertPos+1;
                }
            }
            return -1;
        }
Ejemplo n.º 7
0
 private bool isWrapAllArgsAndParms(WrapItem info)
 {
     return (info.getBreakType()==Break_Commas_code && 
     ((mWrapAllArgumentsIfAny && info.getCommaContextType()==Break_SubType_Arguments) || 
     (mWrapAllParametersIfAny && info.getCommaContextType()==Break_SubType_Parameters)));        
 }
Ejemplo n.º 8
0
 private bool isWrapAllArray(WrapItem info)
 {
     return (info.getBreakType()==Break_Commas_code && 
             mWrapAllArrayItemsIfAny && info.getCommaContextType()==Break_SubType_Array);
 }
Ejemplo n.º 9
0
     private bool mWrapArrayItemsAlignStart=false; //align items on key start column.
 
     private bool isWrapAllObject(WrapItem info)
     {
         return (info.getBreakType()==Break_Commas_code && 
                 mWrapAllObjectItemsIfAny && info.getCommaContextType()==Break_SubType_Object);
     }