Exemplo n.º 1
0
 @Override
 protected int destination(int offset, TextContent content, int count) {
   int result = Math.max(0, offset - count);
   LineInformation lineInformation = content.getLineInformationOfOffset(offset);
   if (result < lineInformation.getBeginOffset()) {
     LineInformation resultLine = content.getLineInformation(lineInformation.getNumber() - 1);
     result = resultLine.getEndOffset();
   }
   return result;
 }
Exemplo n.º 2
0
 /**
  * Vim doesn't start a delimited range on a newline or end a range on an empty line (try 'vi{'
  * while within a function for proof).
  */
 public static Position fixLeftDelimiter(TextContent model, CursorService cursor, Position delim) {
   // check if the character after delimiter is a newline
   if (isNewLine(model.getText(delim.getModelOffset() + 1, 1))) {
     // start after newline
     LineInformation line = model.getLineInformationOfOffset(delim.getModelOffset());
     LineInformation nextLine = model.getLineInformation(line.getNumber() + 1);
     delim = cursor.newPositionForModelOffset(nextLine.getBeginOffset());
   } else {
     delim = delim.addModelOffset(1);
   }
   return delim;
 }
Exemplo n.º 3
0
 private Selection createSelection(EditorAdaptor editor, int startLine, int endLine) {
   TextContent modelContent = editor.getModelContent();
   Position start =
       editor
           .getCursorService()
           .newPositionForModelOffset(modelContent.getLineInformation(startLine).getBeginOffset());
   Position end =
       editor
           .getCursorService()
           .newPositionForModelOffset(modelContent.getLineInformation(endLine).getBeginOffset());
   return new SimpleSelection(new StartEndTextRange(start, end));
 }
Exemplo n.º 4
0
 @Override
 public TextRange leftDelimiter(EditorAdaptor editorAdaptor, int count)
     throws CommandExecutionException {
   TextRange result = super.leftDelimiter(editorAdaptor, count);
   int offset = result.getRightBound().getModelOffset();
   TextContent modelContent = editorAdaptor.getModelContent();
   int length = modelContent.getLineInformationOfOffset(offset).getEndOffset() - offset;
   String content = modelContent.getText(offset, length);
   int i = 0;
   while (Character.isWhitespace(content.charAt(i))) if (++i == length) return result;
   return new StartEndTextRange(result.getLeftBound(), result.getRightBound().addModelOffset(i));
 }
Exemplo n.º 5
0
 private int[] getPosition() {
   Position position = vimTestCase.adaptor.getCursorService().getPosition();
   TextContent modelContent = vimTestCase.adaptor.getModelContent();
   String text = modelContent.getText(0, modelContent.getTextLength());
   int offset = position.getModelOffset();
   int[] rowcol = new int[] {1, 1};
   for (int i = 0; i < offset; i++) {
     if (text.charAt(i) == '\n') {
       ++rowcol[0];
       rowcol[1] = 1;
     } else ++rowcol[1];
   }
   return rowcol;
 }
Exemplo n.º 6
0
 public static SearchResult wrapAroundSearch(
     final EditorAdaptor vim, final Search search, final Position position) {
   final SearchAndReplaceService searcher = vim.getSearchAndReplaceService();
   SearchResult result = searcher.find(search, position);
   if (!result.isFound() && vim.getConfiguration().get(Options.WRAP_SCAN)) {
     // redo search from beginning / end of document
     final TextContent p = vim.getModelContent();
     final int index =
         search.isBackward()
             ? p.getLineInformation(p.getNumberOfLines() - 1).getEndOffset() - 1
             : 0;
     result = searcher.find(search, position.setModelOffset(index));
   }
   return result;
 }
Exemplo n.º 7
0
 @Override
 public TextRange rightDelimiter(EditorAdaptor editorAdaptor, int count)
     throws CommandExecutionException {
   TextRange result = super.rightDelimiter(editorAdaptor, count);
   int offset = result.getLeftBound().getModelOffset();
   TextContent modelContent = editorAdaptor.getModelContent();
   int beginOffset = modelContent.getLineInformationOfOffset(offset).getBeginOffset();
   int length = offset - beginOffset;
   String content = modelContent.getText(beginOffset, length);
   int i = length - 1;
   while (Character.isWhitespace(content.charAt(i))) {
     if (--i < 0) return result;
   }
   return new StartEndTextRange(
       result.getLeftBound().addModelOffset(i - length + 1), result.getRightBound());
 }
Exemplo n.º 8
0
  public static Position fixRightDelimiter(
      TextContent model, CursorService cursor, Position delim) {
    int delimIndex = delim.getModelOffset();
    LineInformation line = model.getLineInformationOfOffset(delimIndex);
    int lineStart = line.getBeginOffset();

    if (delimIndex > lineStart) {
      // is everything before the delimiter just whitespace?
      String text = model.getText(lineStart, delimIndex - lineStart);
      if (VimUtils.isBlank(text)) {
        // end on previous line
        LineInformation previousLine = model.getLineInformation(line.getNumber() - 1);
        delimIndex = previousLine.getEndOffset();
        delim = cursor.newPositionForModelOffset(delimIndex);
      }
    }
    return delim;
  }
Exemplo n.º 9
0
 /**
  * @param line a line in the text.
  * @return the offset where the first non-whitespace character occurs in the given line.
  */
 public static int getFirstNonWhiteSpaceOffset(
     final TextContent content, final LineInformation line) {
   int index = line.getBeginOffset();
   final int end = line.getEndOffset();
   while (index < end) {
     final String s = content.getText(index, 1);
     if (!isWhiteSpace(s)) {
       break;
     }
     index += 1;
   }
   return index;
 }
Exemplo n.º 10
0
 /**
  * Calculates an offset position. Line breaks are not counted.
  *
  * @param position TODO
  */
 public static int calculatePositionForOffset(
     final TextContent p, int position, final int offset) {
   LineInformation line = p.getLineInformationOfOffset(position);
   if (offset < 0) {
     int i = -offset;
     while (i > 0) {
       if (position > line.getBeginOffset()) {
         position -= 1;
       } else {
         final int nextLine = line.getNumber() - 1;
         if (nextLine < 0) {
           break;
         }
         line = p.getLineInformation(nextLine);
         position = Math.max(line.getBeginOffset(), line.getEndOffset() - 1);
       }
       i -= 1;
     }
   } else if (offset > 0) {
     int i = offset;
     int end = Math.max(line.getBeginOffset(), line.getEndOffset() - 1);
     while (i > 0) {
       if (position < end) {
         position += 1;
       } else {
         final int nextLine = line.getNumber() + 1;
         if (nextLine > p.getNumberOfLines() - 1) {
           break;
         }
         line = p.getLineInformation(nextLine);
         end = Math.max(line.getBeginOffset(), line.getEndOffset() - 1);
         position = line.getBeginOffset();
       }
       i -= 1;
     }
   }
   return position;
 }
Exemplo n.º 11
0
 /**
  * @param line a line in the text.
  * @return the offset of the last non-whitespace character.
  */
 public static int getLastNonWhiteSpaceOffset(
     final TextContent content, final LineInformation line) {
   if (line.getLength() == 0) {
     return line.getBeginOffset();
   }
   int index = line.getEndOffset() - 1;
   final int begin = line.getBeginOffset();
   while (index > begin) {
     final String s = content.getText(index, 1);
     if (!isWhiteSpace(s)) {
       break;
     }
     index--;
   }
   return index;
 }
Exemplo n.º 12
0
  void execute(EditorAdaptor editorAdaptor, TextRange region, int count)
      throws CommandExecutionException {
    try {
      String action =
          left
              ? ITextEditorActionDefinitionIds.SHIFT_LEFT
              : ITextEditorActionDefinitionIds.SHIFT_RIGHT;
      editorAdaptor.getHistory().beginCompoundChange();
      editorAdaptor.getHistory().lock();
      TextContent modelContent = editorAdaptor.getModelContent();
      int startOffset = region.getLeftBound().getModelOffset();
      int startLine;
      // if the start is on a newline, put start on the next line
      // (otherwise, >i{ will indent the line ending with a '{')
      if (VimUtils.isNewLine(modelContent.getText(startOffset, 1))) {
        startLine = modelContent.getLineInformationOfOffset(startOffset).getNumber() + 1;
      } else {
        startLine = modelContent.getLineInformationOfOffset(startOffset).getNumber();
      }

      int rightBoundOffset = region.getRightBound().getModelOffset();
      LineInformation endLineInformation =
          modelContent.getLineInformationOfOffset(rightBoundOffset);
      String endText =
          modelContent.getText(
              endLineInformation.getBeginOffset(),
              rightBoundOffset - endLineInformation.getBeginOffset());
      int endLine = endLineInformation.getNumber();
      if (rightBoundOffset > endLineInformation.getBeginOffset() && !endText.matches("\\s*")) {
        // if the right bound is beyond the first character of a line, include that entire line
        // (unless everything within the bounds of this line is whitespace)
        endLine++;
      }
      for (int i = 0; i < count; i++) {
        editorAdaptor.setSelection(createSelection(editorAdaptor, startLine, endLine));
        EclipseCommand.doIt(1, action, editorAdaptor);
      }
      editorAdaptor.setPosition(
          editorAdaptor
              .getCursorService()
              .newPositionForModelOffset(
                  VimUtils.getFirstNonWhiteSpaceOffset(
                      modelContent, modelContent.getLineInformation(startLine))),
          true);
    } finally {
      editorAdaptor.getHistory().unlock();
      editorAdaptor.getHistory().endCompoundChange();
    }
  }
Exemplo n.º 13
0
  /**
   * Grab the word currently under the cursor. If wholeWord, end at nearest whitespace. Otherwise,
   * use Options.KEYWORDS values.
   */
  public static String getWordUnderCursor(
      final EditorAdaptor editorAdaptor, final boolean wholeWord) {
    String word = "";
    TextContent p = editorAdaptor.getViewContent();
    int index = editorAdaptor.getCursorService().getPosition().getViewOffset();
    LineInformation line = p.getLineInformationOfOffset(index);
    int min = line.getBeginOffset();
    int max = line.getEndOffset();
    int first = -1;
    int last = -1;
    String s;
    boolean found = false;
    String keywords = wholeWord ? "\\S" : editorAdaptor.getConfiguration().get(Options.KEYWORDS);

    if (index < max) {
      s = p.getText(index, 1);
      if (Utils.characterType(s.charAt(0), keywords) == Utils.WORD) {
        found = true;
        first = index;
        last = index;
      }
    }
    while (index < max - 1) {
      index += 1;
      s = p.getText(index, 1);
      if (Utils.characterType(s.charAt(0), keywords) == Utils.WORD) {
        last = index;
        if (!found) {
          first = index;
          found = true;
        }
      } else if (found) {
        break;
      }
    }
    if (found) {
      index = first;
      while (index > min) {
        index -= 1;
        s = p.getText(index, 1);
        if (Utils.characterType(s.charAt(0), keywords) == Utils.WORD) {
          first = index;
        } else {
          break;
        }
      }
      word = p.getText(first, last - first + 1);
    }
    return word;
  }
Exemplo n.º 14
0
  /**
   * Given a word defined in the TextContent object bounded by startingIndex and endingIndex, return
   * the endingIndex that defines the end of the word without the last newline.
   *
   * <p>This deletes a single newline followed by arbitrary whitespace. It does not remove multiple
   * newlines, or whitespace without newlines
   *
   * @param startingIndex marks the beginning of the word
   * @param endingIndex marks the end of the word, which may be trimmed
   * @param content contains the buffer holding the word
   * @return the new ending offset, decremented if newlines and whitespace are present
   */
  public int offsetWithoutLastNewline(int startingIndex, int endingIndex, TextContent content) {
    int bufferLength = min(MoveWithBounds.BUFFER_LEN, endingIndex);
    if (bufferLength == 0) return endingIndex;

    // trim /\n{w}*/, but not /[^\n]{ws}*/
    // Also, do not trim /\n*/ only /\n/
    String buffer = content.getText(endingIndex - bufferLength, bufferLength);
    int lastBufferIndex = buffer.length() - 1;

    int trailingWS = numTrailingWhitespaceChars(buffer, lastBufferIndex);
    int trailingNL = numTrailingNewLines(buffer, lastBufferIndex - trailingWS);

    if (trailingNL > 0) {
      int newOffset = endingIndex - (trailingWS + 1); // only move back a single newline
      if (newOffset > startingIndex) // words only move right
      endingIndex = newOffset;
    }

    return endingIndex;
  }
Exemplo n.º 15
0
 /**
  * @param content textContent
  * @param line a line in the text.
  * @return the content of the given line, without preceeding whitespace.
  */
 public static String getWithoutIndent(final TextContent content, final LineInformation info) {
   final int offset = getFirstNonWhiteSpaceOffset(content, info);
   return content.getText(offset, info.getEndOffset() - offset);
 }
Exemplo n.º 16
0
 /** @return true, if line contains only whitespace characters */
 public static boolean isLineBlank(final TextContent content, final int lineNo) {
   final LineInformation line = content.getLineInformation(lineNo);
   return VimUtils.isBlank(content.getText(line.getBeginOffset(), line.getLength()));
 }
Exemplo n.º 17
0
 /** @return true, if the last character in the text buffer is newline */
 public static boolean endsWithEOL(final EditorAdaptor editor) {
   final TextContent content = editor.getModelContent();
   final LineInformation line = content.getLineInformation(content.getNumberOfLines() - 1);
   return line.getNumber() > 0 && line.getLength() == 0;
 }
Exemplo n.º 18
0
 public static int calculateColForOffset(final TextContent p, final int modelOffset) {
   final LineInformation line = p.getLineInformationOfOffset(modelOffset);
   return modelOffset - line.getBeginOffset();
 }
Exemplo n.º 19
0
 public static int calculateLine(final TextContent text, final int offset) {
   final LineInformation line = text.getLineInformationOfOffset(offset);
   return line.getNumber();
 }
Exemplo n.º 20
0
 /**
  * @param vim the vim emulator.
  * @param line a line in the text.
  * @return the whitespace at the begin of the given line.
  */
 public static String getIndent(final TextContent content, final LineInformation line) {
   final int offset = getFirstNonWhiteSpaceOffset(content, line);
   return content.getText(line.getBeginOffset(), offset - line.getBeginOffset());
 }