public boolean intersects(Region range) {
   int tMin = offset;
   int tMax = offset + length - 1;
   int oMin = range.getOffset();
   int oMax = range.getOffset() + range.getLength() - 1;
   return tMin <= oMax && oMin <= tMax;
 }
  private void deleteTag(NLSSubstitution substitution, TextChange change) {
    Region textRegion = substitution.getNLSElement().getTagPosition();

    TextChangeCompatibility.addTextEdit(
        change,
        NLSMessages.NLSSourceModifier_remove_tag,
        new DeleteEdit(textRegion.getOffset(), textRegion.getLength()));
  }
 private boolean isAffected(ASTNode node) {
   if (fSubRange == null) {
     return true;
   }
   int nodeStart = node.getStartPosition();
   int offset = fSubRange.getOffset();
   return nodeStart + node.getLength() > offset && offset + fSubRange.getLength() > nodeStart;
 }
  private void deleteAccessor(NLSSubstitution substitution, TextChange change, ICompilationUnit cu)
      throws CoreException {
    AccessorClassReference accessorClassRef = substitution.getAccessorClassReference();
    if (accessorClassRef != null) {
      Region region = accessorClassRef.getRegion();
      String[] args = {
        substitution.getValueNonEmpty(),
        BasicElementLabels.getJavaElementName(substitution.getKey())
      };
      String label = Messages.format(NLSMessages.NLSSourceModifier_remove_accessor, args);
      String replaceString = '\"' + unwindEscapeChars(substitution.getValueNonEmpty()) + '\"';
      TextChangeCompatibility.addTextEdit(
          change, label, new ReplaceEdit(region.getOffset(), region.getLength(), replaceString));
      if (fIsEclipseNLS && substitution.getState() != NLSSubstitution.INTERNALIZED) {

        Region position = substitution.getNLSElement().getPosition();
        int lineStart = getLineStart(cu.getBuffer(), position.getOffset());
        int lineEnd = getLineEnd(cu.getBuffer(), position.getOffset());
        String cuLine = cu.getBuffer().getText(lineStart, lineEnd - lineStart);
        StringBuffer buf = new StringBuffer(cuLine);
        buf.replace(
            region.getOffset() - lineStart,
            region.getOffset() + region.getLength() - lineStart,
            replaceString);
        try {
          NLSLine[] allLines = NLSScanner.scan(buf.toString());

          NLSLine nlsLine = allLines[0];
          NLSElement element =
              findElement(
                  nlsLine,
                  position.getOffset() - lineStart - accessorClassRef.getName().length() - 1);
          if (element == null || element.hasTag()) return;

          NLSElement[] elements = nlsLine.getElements();
          int indexInElementList = Arrays.asList(elements).indexOf(element);
          String editText =
              ' ' + NLSElement.createTagText(indexInElementList + 1); // tags are 1-based
          TextChangeCompatibility.addTextEdit(change, label, new InsertEdit(lineEnd, editText));

        } catch (InvalidInputException e) {
        } catch (BadLocationException e) {
        }
      }
    }
  }
 @Override
 public void visit(Tree.DocLink that) {
   super.visit(that);
   // TODO: copy/paste from EnterAliasRefactoring
   Declaration base = that.getBase();
   if (base != null && dec.equals(base)) {
     Region region = DocLinks.nameRegion(that, 0);
     change.addEdit(new ReplaceEdit(region.getOffset(), region.getLength(), dec.getName()));
   }
 }
  private void replaceKey(NLSSubstitution substitution, TextChange change) {
    Region region = substitution.getNLSElement().getPosition();
    String[] args = {
      substitution.getInitialKey(), BasicElementLabels.getJavaElementName(substitution.getKey())
    };

    ReplaceEdit replaceEdit;
    if (fIsEclipseNLS)
      replaceEdit = new ReplaceEdit(region.getOffset(), region.getLength(), substitution.getKey());
    else
      replaceEdit =
          new ReplaceEdit(
              region.getOffset(),
              region.getLength(),
              '\"' + unwindEscapeChars(substitution.getKey()) + '\"'); //

    TextChangeCompatibility.addTextEdit(
        change, Messages.format(NLSMessages.NLSSourceModifier_replace_key, args), replaceEdit);
  }
  protected Region getNewSelectionRegion(
      IndexedRegion indexedRegion, ITextSelection textSelection) {
    Region newRegion = null;
    if (indexedRegion instanceof Node) {
      Node cursorNode = (Node) indexedRegion;

      // use parent node for empty text node
      if ((cursorNode.getNodeType() == Node.TEXT_NODE)
          && (cursorNode.getNodeValue().trim().length() == 0)) {
        cursorNode = cursorNode.getParentNode();

        if (cursorNode instanceof IndexedRegion) {
          indexedRegion = (IndexedRegion) cursorNode;
        }
      }

      Region cursorNodeRegion =
          new Region(
              indexedRegion.getStartOffset(),
              indexedRegion.getEndOffset() - indexedRegion.getStartOffset());

      if ((cursorNodeRegion.getOffset() >= textSelection.getOffset())
          && (cursorNodeRegion.getOffset() <= textSelection.getOffset() + textSelection.getLength())
          && (cursorNodeRegion.getOffset() + cursorNodeRegion.getLength()
              >= textSelection.getOffset())
          && (cursorNodeRegion.getOffset() + cursorNodeRegion.getLength()
              <= textSelection.getOffset() + textSelection.getLength())) {
        Node newNode = cursorNode.getParentNode();

        if (newNode instanceof IndexedRegion) {
          IndexedRegion newIndexedRegion = (IndexedRegion) newNode;
          newRegion =
              new Region(
                  newIndexedRegion.getStartOffset(),
                  newIndexedRegion.getEndOffset() - newIndexedRegion.getStartOffset());
        }
      } else {
        newRegion = cursorNodeRegion;
      }
    }
    return newRegion;
  }
  private void addAccessor(NLSSubstitution sub, TextChange change, String accessorName) {
    if (sub.getState() == NLSSubstitution.EXTERNALIZED) {
      NLSElement element = sub.getNLSElement();
      Region position = element.getPosition();
      String[] args = {sub.getValueNonEmpty(), BasicElementLabels.getJavaElementName(sub.getKey())};
      String text = Messages.format(NLSMessages.NLSSourceModifier_externalize, args);

      String resourceGetter = createResourceGetter(sub.getKey(), accessorName);

      TextEdit edit = new ReplaceEdit(position.getOffset(), position.getLength(), resourceGetter);
      if (fIsEclipseNLS && element.getTagPosition() != null) {
        MultiTextEdit multiEdit = new MultiTextEdit();
        multiEdit.addChild(edit);
        Region tagPosition = element.getTagPosition();
        multiEdit.addChild(new DeleteEdit(tagPosition.getOffset(), tagPosition.getLength()));
        edit = multiEdit;
      }
      TextChangeCompatibility.addTextEdit(change, text, edit);
    }
  }
 private void replaceValue(NLSSubstitution substitution, TextChange change) {
   Region region = substitution.getNLSElement().getPosition();
   String[] args = {substitution.getInitialValue(), substitution.getValueNonEmpty()};
   TextChangeCompatibility.addTextEdit(
       change,
       Messages.format(NLSMessages.NLSSourceModifier_replace_value, args),
       new ReplaceEdit(
           region.getOffset(),
           region.getLength(),
           '\"' + unwindEscapeChars(substitution.getValueNonEmpty()) + '\"')); //
 }
Exemplo n.º 10
0
  /**
   * Add enclosing comment tags for the whole area to be commented
   *
   * @param commentArea initial comment area which can be adjusted
   * @param lastPartition last partition
   * @param doc document
   * @param factory Edit factory
   * @param edits List of edits to update the document
   * @return new possibly adjusted comment area
   * @throws BadLocationException
   */
  private Region handleEnclosingPartitions(
      Region commentArea,
      ITypedRegion lastPartition,
      IDocument doc,
      Edit.EditFactory factory,
      List<Edit> edits)
      throws BadLocationException {

    int commentAreaStart = commentArea.getOffset();
    int commentAreaEnd = commentArea.getOffset() + commentArea.getLength();

    String commentStartTag = getCommentStart(); // "/*"
    String commentEndTag = getCommentEnd(); // "*/"

    String startLineEOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaStart));
    if (startLineEOL == null) startLineEOL = ""; // $NON-NLS-1$
    String endLineEOL = doc.getLineDelimiter(doc.getLineOfOffset(commentAreaEnd - 1));
    if (endLineEOL == null) endLineEOL = ""; // $NON-NLS-1$

    boolean isLeftEol =
        commentAreaStart < startLineEOL.length()
            || doc.get(commentAreaStart - startLineEOL.length(), startLineEOL.length())
                .equals(startLineEOL);
    boolean isRightEol =
        doc.get(commentAreaEnd - endLineEOL.length(), endLineEOL.length()).equals(endLineEOL);

    if (isLeftEol && isRightEol) {
      // Block of full lines found
      int areaStartLine = doc.getLineOfOffset(commentAreaStart + startLineEOL.length());
      int areaEndLine = doc.getLineOfOffset(commentAreaEnd - endLineEOL.length());
      if (areaStartLine != areaEndLine) {
        // If multiple full lines arrange inserting comment tags on their own lines
        commentStartTag = getCommentStart() + startLineEOL;
        commentEndTag = getCommentEnd() + endLineEOL;
      } else {
        // If one full line insert end comment tag on the same line (before the EOL)
        commentAreaEnd = commentAreaEnd - endLineEOL.length();
      }
    } else {
      if (lastPartition.getType() == ICPartitions.C_SINGLE_LINE_COMMENT
          || lastPartition.getType() == ICPartitions.C_SINGLE_LINE_DOC_COMMENT) {
        // C++ comments "//" partition ends with EOL, insert end comment tag before it
        // on the same line, so we get something like /*// text*/
        commentAreaEnd = commentAreaEnd - endLineEOL.length();
      }
    }

    edits.add(factory.createEdit(commentAreaStart, 0, commentStartTag));
    edits.add(factory.createEdit(commentAreaEnd, 0, commentEndTag));

    return new Region(commentAreaStart, commentAreaEnd - commentAreaStart);
  }
  private void consolidateFragments() {
    log.debug("Consolidating fragments ({}@{})", getClass(), System.identityHashCode(this));
    int start = range.getOffset();
    int length = range.getLength();

    try {
      if (defaultToken == null) throw new IllegalStateException("No default token defined");

      if (fragments.isEmpty()) {
        emitCommentAware(defaultToken, start, document.get(start, length));
        return;
      }
      Fragment firstFragment = fragments.get(0);
      if (firstFragment.offset > start) {
        fragments.add(0, new Fragment(defaultToken, start, firstFragment.offset - start));
      }

      Fragment lastFragment = fragments.get(fragments.size() - 1);
      int endOffset = start + length;
      int endFragmentOffset = lastFragment.offset + lastFragment.length;
      if (endFragmentOffset < endOffset) {
        log.debug(
            "Emitting consolidation fragment at offset: {} of length: {} (document length: {})",
            o(endFragmentOffset, endOffset - endFragmentOffset, document.getLength()));
        emitCommentAware(
            defaultToken,
            endFragmentOffset,
            document.get(endFragmentOffset, endOffset - endFragmentOffset));
      }
    } catch (BadLocationException e) {
      log.error("Failed to consolidate fragments", e);
    }

    for (Fragment fragment : fragments) {
      log.debug(
          "fragment: {}, {}, {}", o(fragment.offset, fragment.length, fragment.token.getData()));
    }
  }
Exemplo n.º 12
0
  public void insertAsTemplate(PotentialStep pStep) {
    IDocument document = getInputDocument();

    Point point = getSourceViewer().getSelectedRange();
    int lineNo = getSourceViewer().getTextWidget().getLineAtOffset(point.x);
    int lineOffset = getSourceViewer().getTextWidget().getOffsetAtLine(lineNo);

    Region replacementRegion = new Region(lineOffset, 0);
    TemplateContextType contextType = StoryContextType.getTemplateContextType();
    TemplateContext templateContext =
        new DocumentTemplateContext(
            contextType, document, replacementRegion.getOffset(), replacementRegion.getLength());

    String templateText = TemplateUtils.templatizeVariables(pStep.fullStep()) + "\n";
    Template template =
        new Template(
            pStep.stepPattern,
            pStep.fullStep(),
            StoryContextType.STORY_CONTEXT_TYPE_ID,
            templateText,
            false);
    new TemplateProposal(template, templateContext, replacementRegion, null, 0)
        .apply(getSourceViewer(), (char) 0, SWT.CONTROL, replacementRegion.getOffset());
  }
 protected void renameRegion(TextChange tfc, Region region, Tree.CompilationUnit root) {
   tfc.addEdit(new ReplaceEdit(region.getOffset(), region.getLength(), newName));
 }
 private static boolean isPositionInElement(NLSElement element, int position) {
   Region elementPosition = element.getPosition();
   return (elementPosition.getOffset() <= position
       && position <= elementPosition.getOffset() + elementPosition.getLength());
 }