/**
   * @return int
   * @param node org.eclipse.wst.css.core.model.interfaces.ICSSNode
   * @param insertPos int
   */
  public int getLengthToReformatAfter(ICSSNode node, int insertPos) {
    if (node == null) return 0;
    IndexedRegion nnode = (IndexedRegion) node;
    if (insertPos < 0 || !nnode.contains(insertPos)) return 0;

    IStructuredDocumentRegion flatNode =
        node.getOwnerDocument()
            .getModel()
            .getStructuredDocument()
            .getRegionAtCharacterOffset(insertPos);
    if (flatNode == null) return 0;
    ITextRegion region = flatNode.getRegionAtCharacterOffset(insertPos);
    if (region == null) return 0;
    RegionIterator it = new RegionIterator(flatNode, region);
    boolean found = false;
    while (it.hasNext()) {
      region = it.next();
      // if (region.getType() != CSSRegionContexts.CSS_S &&
      // region.getType() != CSSRegionContexts.CSS_DELIMITER &&
      // region.getType() !=
      // CSSRegionContexts.CSS_DECLARATION_DELIMITER) {
      if (region.getType() != CSSRegionContexts.CSS_S) {
        found = true;
        break;
      }
    }
    int pos =
        (found
                ? it.getStructuredDocumentRegion().getStartOffset(region)
                : it.getStructuredDocumentRegion().getTextEndOffset(region))
            - insertPos;
    return (pos >= 0) ? pos : 0;
  }
 public void reset(IStructuredDocument structuredDocument, int index) {
   documentRegion = structuredDocument.getRegionAtCharacterOffset(index);
   curDocumentRegion = documentRegion;
   if (documentRegion != null) {
     ITextRegion region = documentRegion.getRegionAtCharacterOffset(index);
     current = documentRegion.getRegions().indexOf(region);
   }
 }
Exemplo n.º 3
0
  public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    ITextEditor textEditor = null;
    if (editor instanceof ITextEditor) textEditor = (ITextEditor) editor;
    else {
      Object o = editor.getAdapter(ITextEditor.class);
      if (o != null) textEditor = (ITextEditor) o;
    }
    if (textEditor != null) {
      IDocument document =
          textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());

      if (document != null) {
        // get current text selection
        ITextSelection textSelection = getCurrentSelection(textEditor);

        // If there is alternating or more then one block in the text
        // selection, action is aborted !
        if (isMoreThanOneContextBlockSelected(document, textSelection)) {
          // displayCommentActinosErrorDialog(editor);
          // return null;
          org.eclipse.wst.sse.ui.internal.handlers.AddBlockCommentHandler
              addBlockCommentHandlerWST =
                  new org.eclipse.wst.sse.ui.internal.handlers
                      .AddBlockCommentHandler(); // org.eclipse.wst.sse.ui.internal.handlers.AddBlockCommentHandler();
          return addBlockCommentHandlerWST.execute(event);
        }

        if (textSelection.isEmpty()) {
          return null;
        }
        if (document instanceof IStructuredDocument) {
          int selectionOffset = textSelection.getOffset();
          IStructuredDocument sDoc = (IStructuredDocument) document;
          IStructuredDocumentRegion sdRegion = sDoc.getRegionAtCharacterOffset(selectionOffset);
          ITextRegion textRegion = sdRegion.getRegionAtCharacterOffset(selectionOffset);

          ITextRegionCollection container = sdRegion;

          if (textRegion instanceof ITextRegionContainer) {
            container = (ITextRegionContainer) textRegion;
            textRegion = container.getRegionAtCharacterOffset(selectionOffset);
          }
          if (textRegion.getType() == PHPRegionContext.PHP_CONTENT) {
            processAction(textEditor, document, textSelection);
          } else {
            org.eclipse.wst.sse.ui.internal.handlers.AddBlockCommentHandler
                addBlockCommentHandlerWST =
                    new org.eclipse.wst.sse.ui.internal.handlers.AddBlockCommentHandler();
            return addBlockCommentHandlerWST.execute(event);
          }
        }
      }
    }
    return null;
  }
  /**
   * Returns the subregion at the given offset, with a bias to the left or a bias to the right. In
   * other words, if | represents the caret position, in the XML {@code <foo>|</bar>} then the
   * subregion with bias left is the closing {@code >} and the subregion with bias right is the
   * opening {@code </}.
   *
   * @param doc the document
   * @param offset the offset in the document
   * @param biasLeft whether we should look at the token on the left or on the right
   * @return the subregion at the given offset, or null if not found
   */
  private static ITextRegion getRegionAt(IStructuredDocument doc, int offset, boolean biasLeft) {
    if (biasLeft) {
      offset--;
    }
    IStructuredDocumentRegion region = doc.getRegionAtCharacterOffset(offset);
    if (region != null) {
      return region.getRegionAtCharacterOffset(offset);
    }

    return null;
  }
  /**
   * Creates an IMessage from an IProblem
   *
   * @param problem
   * @param f
   * @param translation
   * @param textDoc
   * @return message representation of the problem, or null if it could not create one
   */
  private IMessage createMessageFromProblem(
      IProblem problem, IFile f, IJsTranslation translation, IDocument textDoc) {
    int sourceStart = problem.getSourceStart();
    int sourceEnd = problem.getSourceEnd();
    if (sourceStart == -1) {
      return null;
    }
    sourceStart = translation.getWebPageOffset(sourceStart);
    sourceEnd = translation.getWebPageOffset(sourceEnd);
    /*
     * Bug 241794 - Validation shows errors when using JSP Expressions
     * inside JavaScript code
     */
    IStructuredDocument doc = (IStructuredDocument) textDoc;
    IStructuredDocumentRegion documentRegion = doc.getRegionAtCharacterOffset(sourceStart);
    if (documentRegion != null) {
      ITextRegion textRegion = documentRegion.getRegionAtCharacterOffset(sourceStart);
      /*
       * Filter out problems from areas that aren't simple JavaScript,
       * e.g. JSP.
       */
      if (textRegion != null && textRegion instanceof ITextRegionCollection) return null;
    }

    int sev =
        problem.isError()
            ? IMessage.HIGH_SEVERITY
            : (problem.isWarning() ? IMessage.NORMAL_SEVERITY : IMessage.LOW_SEVERITY);
    IMessage m = new LocalizedMessage(sev, problem.getMessage(), f);
    // line numbers for marker starts @ 1
    // line numbers from document starts @ 0
    try {
      int lineNo = textDoc.getLineOfOffset(sourceStart) + 1;
      m.setLineNo(lineNo);
      m.setOffset(sourceStart);
      m.setLength(sourceEnd - sourceStart + 1);
    } catch (BadLocationException e) {
      Logger.logException(e);
    }
    return m;
  }