Package org.eclipse.wst.xml.core.internal.provisional.document

Examples of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode


        if (command.text != null) {
          smartInsertForEndTag(command, document, model);

          if (command.text.equals("%") && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_SCRIPTLETS)) { //$NON-NLS-1$
            // scriptlet - add end %>
            IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
            if (node != null && prefixedWith(document, command.offset, "<") && !node.getSource().endsWith("%>")) { //$NON-NLS-1$ //$NON-NLS-2$
              command.text += " %>"; //$NON-NLS-1$
              command.shiftsCaret = false;
              command.caretOffset = command.offset + 1;
              command.doit = false;
            }
          }
          if (command.text.equals("{") && isPreferenceEnabled(JSPUIPreferenceNames.TYPING_COMPLETE_EL_BRACES)) { //$NON-NLS-1$
            IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset);
            if (node != null && (prefixedWith(document, command.offset, "$") || prefixedWith(document, command.offset, "#")) && //$NON-NLS-1$ //$NON-NLS-2$
                  !node.getSource().endsWith("}")) { //$NON-NLS-1$ //$NON-NLS-2$
              command.text += " }"; //$NON-NLS-1$
              command.shiftsCaret = false;
              command.caretOffset = command.offset + 1;
              command.doit = false;
            }
View Full Code Here


  }
 
  private void smartInsertForEndTag(DocumentCommand command, IDocument document, IStructuredModel model) {
    try {
      if (command.text.equals("/") && (document.getLength() >= 1) && document.get(command.offset - 1, 1).equals("<") && HTMLUIPlugin.getDefault().getPreferenceStore().getBoolean(HTMLUIPreferenceNames.TYPING_COMPLETE_END_TAGS)) { //$NON-NLS-1$ //$NON-NLS-2$
        IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(command.offset - 1)).getParentNode();
        if (isCommentNode(parentNode)) {
          // loop and find non comment node parent
          while ((parentNode != null) && isCommentNode(parentNode)) {
            parentNode = (IDOMNode) parentNode.getParentNode();
          }
        }

        if (!isDocumentNode(parentNode)) {
          // only add end tag if one does not already exist or if
          // add '/' does not create one already
          IStructuredDocumentRegion endTagStructuredDocumentRegion = parentNode.getEndStructuredDocumentRegion();
          IDOMNode ancestor = parentNode;
          boolean smartInsertForEnd = false;
          if(endTagStructuredDocumentRegion != null) {
            // Look for ancestors by the same name that are missing end tags
            while((ancestor = (IDOMNode) ancestor.getParentNode()) != null) {
              if(ancestor.getEndStructuredDocumentRegion() == null && parentNode.getNodeName().equals(ancestor.getNodeName())) {
                smartInsertForEnd = true;
                break;
              }
            }
          }
View Full Code Here

      IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
     
      // get results from JSP completion processor
      results = computeJavaCompletionProposals(viewer, documentPosition, 0);

      IDOMNode xNode = null;
      IStructuredDocumentRegion flat = null;
      if (treeNode instanceof IDOMNode) {
        xNode = (IDOMNode) treeNode;
        flat = xNode.getFirstStructuredDocumentRegion();
        if (flat != null && flat.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
          flat = flat.getPrevious();
        }
      }
View Full Code Here

    if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE) && (node.getParentNode() != null) && (node.getParentNode().getNodeType() != Node.DOCUMENT_NODE)) {
      // find the text node before the previous non-text sibling
      // if that's not found, we will try the text node before the next
      // non-text sibling
      IDOMNode sibling = (IDOMNode) node.getPreviousSibling();
      while ((sibling != null) && (sibling.getNodeType() == Node.TEXT_NODE || sibling.getNodeType() == Node.COMMENT_NODE)) {
        if (sibling.getNodeType() == Node.COMMENT_NODE && sibling.getPreviousSibling() != null && sibling.getPreviousSibling().getNodeType() == Node.TEXT_NODE && StringUtils.containsLineDelimiter(sibling.getPreviousSibling().getNodeValue()))
          break;
        sibling = (IDOMNode) sibling.getPreviousSibling();
      }
      if (sibling == null) {
        sibling = (IDOMNode) node.getNextSibling();
        while ((sibling != null) && (sibling.getNodeType() == Node.TEXT_NODE))
          sibling = (IDOMNode) sibling.getNextSibling();
      }
      String singleIndent = getFormatPreferences().getIndent();
      String parentLineIndent = getNodeIndent(node.getParentNode());

      if (sibling != null) {
View Full Code Here

    fErrorMessage = fJspCompletionProcessor.getErrorMessage();
    if (results.length == 0 && (fErrorMessage == null || fErrorMessage.length() == 0)) {
      fErrorMessage = UNKNOWN_CONTEXT;
    }

    IDOMNode xNode = null;
    IStructuredDocumentRegion flat = null;
    if (treeNode instanceof IDOMNode) {
      xNode = (IDOMNode) treeNode;
      flat = xNode.getFirstStructuredDocumentRegion();
      if (flat != null && flat.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
        flat = flat.getPrevious();
      }
    }
View Full Code Here

  protected static final String START_TAG_OPEN = "<"; //$NON-NLS-1$
  protected static final String TAG_CLOSE = ">"; //$NON-NLS-1$

  public Node cleanup(Node node) {
    Node newNode = cleanupChildren(node);
    IDOMNode renamedNode = newNode instanceof IDOMNode ? (IDOMNode) newNode : null;

    // call quoteAttrValue() first so it will close any unclosed attr
    // quoteAttrValue() will return the new start tag if there is a
    // structure change
    renamedNode = quoteAttrValue(renamedNode);

    // insert tag close if missing
    // if node is not comment tag
    // and not implicit tag
    if (!isCommentTag(renamedNode) && !isImplicitTag(renamedNode)) {
      IDOMModel structuredModel = renamedNode.getModel();

      // save start offset before insertTagClose()
      // or else renamedNode.getStartOffset() will be zero if
      // renamedNode replaced by insertTagClose()
      int startTagStartOffset = renamedNode.getStartOffset();

      // for start tag
      IStructuredDocumentRegion startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
      insertTagClose(structuredModel, startTagStructuredDocumentRegion);

      // update renamedNode and startTagStructuredDocumentRegion after
      // insertTagClose()
      renamedNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
      startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();

      // for end tag
      IStructuredDocumentRegion endTagStructuredDocumentRegion = renamedNode.getEndStructuredDocumentRegion();
      if (endTagStructuredDocumentRegion != startTagStructuredDocumentRegion)
        insertTagClose(structuredModel, endTagStructuredDocumentRegion);
    }

    // call insertMissingTags() next, it will generate implicit tags if
View Full Code Here

    return parentNode;
  }

  private IDOMNode compressEmptyElementTag(IDOMNode node) {
    boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
    IDOMNode newNode = node;

    IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();

    if (compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && startTagStructuredDocumentRegion != null) {
      ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
      ITextRegion lastRegion = regions.get(regions.size() - 1);
      // format children and end tag if not empty element tag
      if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
        NodeList childNodes = newNode.getChildNodes();
        if (childNodes == null || childNodes.getLength() == 0 || (childNodes.getLength() == 1 && (childNodes.item(0)).getNodeType() == Node.TEXT_NODE && ((childNodes.item(0)).getNodeValue().trim().length() == 0))) {
          IDOMModel structuredModel = newNode.getModel();
          IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();

          int startTagStartOffset = newNode.getStartOffset();
          int offset = endTagStructuredDocumentRegion.getStart();
          int length = endTagStructuredDocumentRegion.getLength();
          structuredDocument.replaceText(structuredDocument, offset, length, ""); //$NON-NLS-1$
          newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset); // save
View Full Code Here

    return result;
  }

  private IDOMNode insertEndTag(IDOMNode node) {
    IDOMNode newNode = node;
    IDOMElement element = (IDOMElement) node;
    if (element.isCommentTag())
      return node; // do nothing

    int startTagStartOffset = node.getStartOffset();
    IDOMModel structuredModel = node.getModel();

    if (isEmptyElement(element)) {
      IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
      IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
      ITextRegionList regions = startStructuredDocumentRegion.getRegions();
      ITextRegion lastRegion = regions.get(regions.size() - 1);
      structuredDocument.replaceText(structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), lastRegion.getLength(), EMPTY_TAG_CLOSE);

      if (regions.size() > 1) {
        ITextRegion regionBeforeTagClose = regions.get(regions.size() - 1 - 1);

        // insert a space separator before tag close if the previous
        // region does not have extra spaces
        if (regionBeforeTagClose.getTextLength() == regionBeforeTagClose.getLength())
          structuredDocument.replaceText(structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), 0, " "); //$NON-NLS-1$
      }
    } else {
      String tagName = node.getNodeName();
      String endTag = END_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);

      IDOMNode lastChild = (IDOMNode) node.getLastChild();
      int endTagStartOffset = 0;
      if (lastChild != null)
        // if this node has children, insert the end tag after the
        // last child
        endTagStartOffset = lastChild.getEndOffset();
      else
        // if this node does not has children, insert the end tag
        // after the start tag
        endTagStartOffset = node.getEndOffset();
View Full Code Here

    return newNode;
  }

  private IDOMNode insertMissingTags(IDOMNode node) {
    boolean insertMissingTags = getCleanupPreferences().getInsertMissingTags();
    IDOMNode newNode = node;

    if (insertMissingTags) {
      IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
      if (startTagStructuredDocumentRegion == null) {
        // implicit start tag; generate tag for it
        newNode = insertStartTag(node);
        startTagStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
      }

      IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getEndStructuredDocumentRegion();
      ITextRegionList startStructuredDocumentRegionRegions = startTagStructuredDocumentRegion.getRegions();
      if (startTagStructuredDocumentRegion != null && startStructuredDocumentRegionRegions != null && (startStructuredDocumentRegionRegions.get(startStructuredDocumentRegionRegions.size() - 1)).getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {

      } else {
        if (startTagStructuredDocumentRegion == null) {
View Full Code Here

    return newNode;
  }

  private IDOMNode insertRequiredAttrs(IDOMNode node) {
    boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
    IDOMNode newNode = node;

    if (insertRequiredAttrs) {
      List requiredAttrs = getRequiredAttrs(newNode);
      if (requiredAttrs.size() > 0) {
        NamedNodeMap currentAttrs = node.getAttributes();
        List insertAttrs = new ArrayList();
        if (currentAttrs.getLength() == 0)
          insertAttrs.addAll(requiredAttrs);
        else {
          for (int i = 0; i < requiredAttrs.size(); i++) {
            String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
            boolean found = false;
            for (int j = 0; j < currentAttrs.getLength(); j++) {
              String currentAttrName = currentAttrs.item(j).getNodeName();
              if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
                found = true;
                break;
              }
            }
            if (!found)
              insertAttrs.add(requiredAttrs.get(i));
          }
        }
        if (insertAttrs.size() > 0) {
          IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
          int index = startStructuredDocumentRegion.getEndOffset();
          ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
          if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
            index--;
            lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
          } else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
            index = index - 2;
            lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
          }
          MultiTextEdit multiTextEdit = new MultiTextEdit();
          try {
            for (int i = insertAttrs.size() - 1; i >= 0; i--) {
              CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) insertAttrs.get(i);
              String requiredAttributeName = attrDecl.getAttrName();
              String defaultValue = attrDecl.getDefaultValue();
              if (defaultValue == null)
                defaultValue = ""; //$NON-NLS-1$
              String nameAndDefaultValue = " "; //$NON-NLS-1$
              if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
                nameAndDefaultValue = ""; //$NON-NLS-1$
              nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\""; //$NON-NLS-1$ //$NON-NLS-2$
              multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
              // BUG3381: MultiTextEdit applies all child
              // TextEdit's basing on offsets
              //          in the document before the first TextEdit, not
              // after each
              //          child TextEdit. Therefore, do not need to
              // advance the index.
              //index += nameAndDefaultValue.length();
            }
            multiTextEdit.apply(newNode.getStructuredDocument());
          } catch (BadLocationException e) {
            // log for now, unless we find reason not to
            Logger.log(Logger.INFO, e.getMessage());
          }
        }
View Full Code Here

TOP

Related Classes of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.