Ejemplo n.º 1
0
 public void reparseAll() {
   SwtMateTextLocation startLocation = new SwtMateTextLocation(0, this);
   SwtMateTextLocation endLocation = new SwtMateTextLocation(0 + document.getLength(), this);
   if (this.mateText.parser.enabled) {
     this.mateText.parser.changes.add(startLocation.getLine(), endLocation.getLine());
     this.mateText.parser.processChanges();
   }
 }
  @Test
  public void useCase_ex1() {
    when(stepLocator.findFirstStep(Mockito.anyString()))
        .thenAnswer(
            new Answer<StepCandidate>() {
              @Override
              public StepCandidate answer(InvocationOnMock invocation) throws Throwable {
                System.out.println("StepScannerStyledTest.useCase_ex1(" + invocation + ")");
                return candidate;
              }
            });

    String[] expected = { //
      "step_keyword ~ offset: 0, length: 6", //
      "step_default ~ offset: 6, length: 18", //
      "step_parameter_value ~ offset: 24, length: 6", //
      "step_default ~ offset: 30, length: 33", //
      "step_example_table_separator ~ offset: 63, length: 1", //
      "step_example_table_cell ~ offset: 64, length: 3", //
      "step_example_table_separator ~ offset: 67, length: 1", //
      "step_example_table_cell ~ offset: 68, length: 5", //
      "step_example_table_separator ~ offset: 73, length: 1", //
      "step_default ~ offset: 74, length: 1", //
      "comment ~ offset: 75, length: 17", //
      "step_example_table_separator ~ offset: 92, length: 1", //
      "step_example_table_cell ~ offset: 93, length: 5", //
      "step_example_table_separator ~ offset: 98, length: 1", //
      "step_example_table_cell ~ offset: 99, length: 6", //
      "step_example_table_separator ~ offset: 105, length: 1", //
      "step_default ~ offset: 106, length: 1", //
      "step_example_table_separator ~ offset: 107, length: 1", //
      "step_example_table_cell ~ offset: 108, length: 8", //
      "step_example_table_separator ~ offset: 116, length: 1", //
      "step_example_table_cell ~ offset: 117, length: 6", //
      "step_example_table_separator ~ offset: 123, length: 1", //
      "step_default ~ offset: 124, length: 1" //
    };

    int index = 0;
    scanner.setRange(document, 0, document.getLength());
    IToken token = scanner.nextToken();
    while (!token.isEOF()) {
      String actual =
          (token.getData()
              + " ~ offset: "
              + scanner.getTokenOffset()
              + ", length: "
              + scanner.getTokenLength());
      assertThat(actual, equalTo(expected[index++]));
      token = scanner.nextToken();
    }
    assertThat(index, equalTo(expected.length));
  }
Ejemplo n.º 3
0
  public int getLineLength(int line) {
    try {
      int startOffset = document.getLineOffset(line);
      int endOffset;

      if (line + 1 < getLineCount()) {
        endOffset = document.getLineOffset(line + 1);
      } else {
        endOffset = document.getLength();
      }

      return endOffset - startOffset;
    } catch (BadLocationException e) {
      System.out.printf("*** Warning BadLocationException");
      e.printStackTrace();
      return -1;
    }
  }
 /** Determine the value type for a give propertyName. */
 protected Type getValueType(String propertyName) {
   try {
     PropertyInfo prop = getIndex().get(propertyName);
     if (prop != null) {
       return TypeParser.parse(prop.getType());
     } else {
       prop = findLongestValidProperty(getIndex(), propertyName);
       if (prop != null) {
         Document doc = new Document(propertyName);
         PropertyNavigator navigator =
             new PropertyNavigator(doc, null, typeUtil, new Region(0, doc.getLength()));
         return navigator.navigate(prop.getId().length(), TypeParser.parse(prop.getType()));
       }
     }
   } catch (Exception e) {
     BootActivator.log(e);
   }
   return null;
 }
Ejemplo n.º 5
0
  private int getPeerPosition(IDocument document, DocumentCommand command) {
    if (document.getLength() == 0) return 0;
    /*
     * Search for scope closers in the pasted text and find their opening peers
     * in the document.
     */
    Document pasted = new Document(command.text);
    installJavaStuff(pasted);
    int firstPeer = command.offset;

    JavaHeuristicScanner pScanner = new JavaHeuristicScanner(pasted);
    JavaHeuristicScanner dScanner = new JavaHeuristicScanner(document);

    // add scope relevant after context to peer search
    int afterToken =
        dScanner.nextToken(command.offset + command.length, JavaHeuristicScanner.UNBOUND);
    try {
      switch (afterToken) {
        case Symbols.TokenRBRACE:
          pasted.replace(pasted.getLength(), 0, "}"); // $NON-NLS-1$
          break;
        case Symbols.TokenRPAREN:
          pasted.replace(pasted.getLength(), 0, ")"); // $NON-NLS-1$
          break;
        case Symbols.TokenRBRACKET:
          pasted.replace(pasted.getLength(), 0, "]"); // $NON-NLS-1$
          break;
      }
    } catch (BadLocationException e) {
      // cannot happen
      Assert.isTrue(false);
    }

    int pPos = 0; // paste text position (increasing from 0)
    int dPos = Math.max(0, command.offset - 1); // document position (decreasing from paste offset)
    while (true) {
      int token = pScanner.nextToken(pPos, JavaHeuristicScanner.UNBOUND);
      pPos = pScanner.getPosition();
      switch (token) {
        case Symbols.TokenLBRACE:
        case Symbols.TokenLBRACKET:
        case Symbols.TokenLPAREN:
          pPos = skipScope(pScanner, pPos, token);
          if (pPos == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          break; // closed scope -> keep searching
        case Symbols.TokenRBRACE:
          int peer = dScanner.findOpeningPeer(dPos, '{', '}');
          dPos = peer - 1;
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching
        case Symbols.TokenRBRACKET:
          peer = dScanner.findOpeningPeer(dPos, '[', ']');
          dPos = peer - 1;
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching
        case Symbols.TokenRPAREN:
          peer = dScanner.findOpeningPeer(dPos, '(', ')');
          dPos = peer - 1;
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching
        case Symbols.TokenCASE:
        case Symbols.TokenDEFAULT:
          JavaIndenter indenter = new JavaIndenter(document, dScanner, fProject);
          peer = indenter.findReferencePosition(dPos, false, false, false, true);
          if (peer == JavaHeuristicScanner.NOT_FOUND) return firstPeer;
          firstPeer = peer;
          break; // keep searching

        case Symbols.TokenEOF:
          return firstPeer;
        default:
          // keep searching
      }
    }
  }
Ejemplo n.º 6
0
  private void smartPaste(IDocument document, DocumentCommand command) {
    int newOffset = command.offset;
    int newLength = command.length;
    String newText = command.text;

    try {
      JavaHeuristicScanner scanner = new JavaHeuristicScanner(document);
      JavaIndenter indenter = new JavaIndenter(document, scanner, fProject);
      int offset = newOffset;

      // reference position to get the indent from
      int refOffset = indenter.findReferencePosition(offset);
      if (refOffset == JavaHeuristicScanner.NOT_FOUND) return;
      int peerOffset = getPeerPosition(document, command);
      peerOffset = indenter.findReferencePosition(peerOffset);
      if (peerOffset != JavaHeuristicScanner.NOT_FOUND) refOffset = Math.min(refOffset, peerOffset);

      // eat any WS before the insertion to the beginning of the line
      int firstLine =
          1; // don't format the first line per default, as it has other content before it
      IRegion line = document.getLineInformationOfOffset(offset);
      String notSelected = document.get(line.getOffset(), offset - line.getOffset());
      if (notSelected.trim().length() == 0) {
        newLength += notSelected.length();
        newOffset = line.getOffset();
        firstLine = 0;
      }

      // prefix: the part we need for formatting but won't paste
      IRegion refLine = document.getLineInformationOfOffset(refOffset);
      String prefix = document.get(refLine.getOffset(), newOffset - refLine.getOffset());

      // handle the indentation computation inside a temporary document
      Document temp = new Document(prefix + newText);
      DocumentRewriteSession session =
          temp.startRewriteSession(DocumentRewriteSessionType.STRICTLY_SEQUENTIAL);
      scanner = new JavaHeuristicScanner(temp);
      indenter = new JavaIndenter(temp, scanner, fProject);
      installJavaStuff(temp);

      // indent the first and second line
      // compute the relative indentation difference from the second line
      // (as the first might be partially selected) and use the value to
      // indent all other lines.
      boolean isIndentDetected = false;
      StringBuffer addition = new StringBuffer();
      int insertLength = 0;
      int firstLineInsertLength = 0;
      int firstLineIndent = 0;
      int first = document.computeNumberOfLines(prefix) + firstLine; // don't format first line
      int lines = temp.getNumberOfLines();
      int tabLength = getVisualTabLengthPreference();
      boolean changed = false;
      for (int l = first;
          l < lines;
          l++) { // we don't change the number of lines while adding indents

        IRegion r = temp.getLineInformation(l);
        int lineOffset = r.getOffset();
        int lineLength = r.getLength();

        if (lineLength == 0) // don't modify empty lines
        continue;

        if (!isIndentDetected) {

          // indent the first pasted line
          String current = getCurrentIndent(temp, l);
          StringBuffer correct = indenter.computeIndentation(lineOffset);
          if (correct == null) return; // bail out

          insertLength = subtractIndent(correct, current, addition, tabLength);
          if (l == first) {
            firstLineInsertLength = insertLength;
            firstLineIndent = current.length();
          }
          if (l != first && temp.get(lineOffset, lineLength).trim().length() != 0) {
            isIndentDetected = true;
            if (firstLineIndent >= current.length()) insertLength = firstLineInsertLength;
            if (insertLength == 0) {
              // no adjustment needed, bail out
              if (firstLine == 0) {
                // but we still need to adjust the first line
                command.offset = newOffset;
                command.length = newLength;
                if (changed) break; // still need to get the leading indent of the first line
              }
              return;
            }
          } else {
            changed = insertLength != 0;
          }
        }

        // relatively indent all pasted lines
        if (insertLength > 0) addIndent(temp, l, addition, tabLength);
        else if (insertLength < 0) cutIndent(temp, l, -insertLength, tabLength);
      }

      removeJavaStuff(temp);
      temp.stopRewriteSession(session);
      newText = temp.get(prefix.length(), temp.getLength() - prefix.length());

      command.offset = newOffset;
      command.length = newLength;
      command.text = newText;

    } catch (BadLocationException e) {
      JavaPlugin.log(e);
    }
  }