Logo Search packages:      
Sourcecode: jedit version File versions  Download package

boolean org::gjt::sp::jedit::textarea::TextArea::doWordWrap ( boolean  spaceInserted  )  [inline, protected]

Does hard wrap.

Definition at line 5414 of file TextArea.java.

References org::gjt::sp::jedit::buffer::JEditBuffer::beginCompoundEdit(), end(), org::gjt::sp::jedit::buffer::JEditBuffer::endCompoundEdit(), getLineEndOffset(), getLineStartOffset(), org::gjt::sp::jedit::buffer::JEditBuffer::getLineText(), org::gjt::sp::jedit::buffer::JEditBuffer::getStringProperty(), org::gjt::sp::jedit::buffer::JEditBuffer::getTabSize(), org::gjt::sp::jedit::buffer::JEditBuffer::indentLine(), and org::gjt::sp::jedit::buffer::JEditBuffer::insert().

Referenced by userInput().

      {
            if(!hardWrap || maxLineLen <= 0)
                  return false;

            buffer.getLineText(caretLine,lineSegment);

            int start = getLineStartOffset(caretLine);
            int end = getLineEndOffset(caretLine);
            int len = end - start - 1;

            int caretPos = caret - start;

            // only wrap if we're at the end of a line, or the rest of the
            // line text is whitespace
            for(int i = caretPos; i < len; i++)
            {
                  char ch = lineSegment.array[lineSegment.offset + i];
                  if(ch != ' ' && ch != '\t')
                        return false;
            }

            int tabSize = buffer.getTabSize();

            String wordBreakChars = buffer.getStringProperty("wordBreakChars");

            int lastInLine = 0; // last character before wrap
            int logicalLength = 0; // length with tabs expanded
            int lastWordOffset = -1;
            boolean lastWasSpace = true;
            for(int i = 0; i < caretPos; i++)
            {
                  char ch = lineSegment.array[lineSegment.offset + i];
                  if(ch == '\t')
                  {
                        logicalLength += tabSize - (logicalLength % tabSize);
                        if(!lastWasSpace && logicalLength <= maxLineLen)
                        {
                              lastInLine = i;
                              lastWordOffset = i;
                              lastWasSpace = true;
                        }
                  }
                  else if(ch == ' ')
                  {
                        logicalLength++;
                        if(!lastWasSpace &&
                              logicalLength <= maxLineLen + 1)
                        {
                              lastInLine = i;
                              lastWordOffset = i;
                              lastWasSpace = true;
                        }
                  }
                  else if(wordBreakChars != null && wordBreakChars.indexOf(ch) != -1)
                  {
                        logicalLength++;
                        if(!lastWasSpace && logicalLength <= maxLineLen)
                        {
                              lastInLine = i;
                              lastWordOffset = i;
                              lastWasSpace = true;
                        }
                  }
                  else
                  {
                        lastInLine = i;
                        logicalLength++;
                        lastWasSpace = false;
                  }
            }

            boolean returnValue;

            int insertNewLineAt;
            if(spaceInserted && logicalLength == maxLineLen
                  && lastInLine == caretPos - 1)
            {
                  insertNewLineAt = caretPos;
                  returnValue = true;
            }
            else if(logicalLength >= maxLineLen && lastWordOffset != -1)
            {
                  insertNewLineAt = lastWordOffset;
                  returnValue = false;
            }
            else
                  return false;

            try
            {
                  buffer.beginCompoundEdit();
                  buffer.insert(start + insertNewLineAt,"\n");
                  // caretLine would have been incremented
                  // since insertNewLineAt <= caretPos
                  buffer.indentLine(caretLine,true);
            }
            finally
            {
                  buffer.endCompoundEdit();
            }

            /* only ever return true if space was pressed
             * with logicalLength == maxLineLen */
            return returnValue;
      } //}}}


Generated by  Doxygen 1.6.0   Back to index