private void addImportant(List candidates) { ICSSNode targetNode = fContext.getTargetNode(); while (targetNode instanceof ICSSPrimitiveValue) { targetNode = targetNode.getParentNode(); } if (!(targetNode instanceof ICSSStyleDeclItem)) { return; } // 1. has no priority region // 2. cursor position is after of last child // 3. normal isMatch method String priority = ((ICSSStyleDeclItem) targetNode).getPriority(); if (priority == null || priority.length() == 0) { ICSSNode lastChild = targetNode.getLastChild(); if (lastChild instanceof IndexedRegion) { int startOffset = ((IndexedRegion) lastChild).getStartOffset(); // int endOffset = ((IndexedRegion)lastChild).getEndOffset(); if (startOffset < fContext.getCursorPos() && isMatch(IMPORTANT)) { CSSCACandidate item = new CSSCACandidate(); item.setReplacementString(IMPORTANT); item.setCursorPosition(IMPORTANT.length()); item.setDisplayString(IMPORTANT); item.setImageType(CSSImageType.VALUE_STRING); appendSemiColon(item); candidates.add(item); } } } }
private void addSemiColon(List candidates) { ICSSNode targetNode = fContext.getTargetNode(); if (targetNode instanceof ICSSStyleDeclItem) { ICSSNode firstChild = targetNode.getFirstChild(); if (firstChild == null) { return; } if (firstChild instanceof IndexedRegion) { int startOffset = ((IndexedRegion) firstChild).getStartOffset(); if (fContext.getCursorPos() <= startOffset) { return; } } } boolean bAddCloser = false; ITextRegion targetRegion = fContext.getTargetRegion(); if (targetRegion != null && targetRegion.getType() != CSSRegionContexts.CSS_DECLARATION_DELIMITER) { // find trailing ":" or ";" // if ":" before ";" is found, add ";" RegionIterator iterator = fContext.getRegionIterator(); IStructuredDocumentRegion container = iterator.getStructuredDocumentRegion(); while (iterator.hasNext()) { ITextRegion region = iterator.next(); if (iterator.getStructuredDocumentRegion() != container) { break; } if (region.getType() == CSSRegionContexts.CSS_DECLARATION_SEPARATOR) { bAddCloser = true; break; } } if (!bAddCloser) { // second chance: // leading IStructuredDocumentRegion is not ";" IStructuredDocumentRegion nextStructuredDocumentRegion = CSSUtil.findNextSignificantNode(container); if (CSSUtil.getStructuredDocumentRegionType(nextStructuredDocumentRegion) != CSSRegionContexts.CSS_DECLARATION_DELIMITER) { bAddCloser = true; } } } if (bAddCloser) { CSSCACandidate item = new CSSCACandidate(); String text = fContext.getTextToReplace() + ";"; // $NON-NLS-1$ item.setReplacementString(text); item.setCursorPosition(text.length()); item.setDisplayString(";"); // $NON-NLS-1$ item.setImageType(null); candidates.add(item); } }
/** @return java.lang.String */ private String getPropertyName() { ICSSNode targetNode = fContext.getTargetNode(); while (targetNode instanceof ICSSPrimitiveValue) { targetNode = targetNode.getParentNode(); } if (targetNode instanceof ICSSStyleDeclItem) { return ((ICSSStyleDeclItem) targetNode).getPropertyName(); } else { return null; } }
protected final void formatChildren(ICSSNode node, IRegion region, StringBuffer source) { ICSSNode child = node.getFirstChild(); int start = region.getOffset(); int end = region.getOffset() + region.getLength(); boolean first = true; while (child != null) { int curEnd = ((IndexedRegion) child).getEndOffset(); StringBuffer childSource = null; boolean toFinish = false; if (start < curEnd) { int curStart = ((IndexedRegion) child).getStartOffset(); if (curStart < end) { // append child CSSSourceFormatter formatter = (CSSSourceFormatter) ((INodeNotifier) child).getAdapterFor(CSSSourceFormatter.class); if (formatter == null) { formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) child); } if (includes(region, curStart, curEnd)) childSource = ((AbstractCSSSourceFormatter) formatter).formatProc(child); else childSource = ((AbstractCSSSourceFormatter) formatter) .formatProc(child, overlappedRegion(region, curStart, curEnd)); } else toFinish = true; } // append between children if (!first) { curEnd = ((IndexedRegion) child).getStartOffset(); // change // only // start if (start < curEnd) { int curStart = ((IndexedRegion) child.getPreviousSibling()).getEndOffset(); if (curStart < end) { String toAppend = (childSource != null) ? new String(childSource) : ""; // $NON-NLS-1$ if (includes(region, curStart, curEnd)) formatBefore(node, child, toAppend, source, null); else formatBefore( node, child, overlappedRegion(region, curStart, curEnd), toAppend, source); } } } if (childSource != null) { source.append(childSource); } first = false; if (toFinish) break; child = child.getNextSibling(); } }
protected String getIndent(ICSSNode node) { if (node == null) return ""; // $NON-NLS-1$ ICSSNode parent = node.getParentNode(); if (node instanceof ICSSAttr) parent = ((ICSSAttr) node).getOwnerCSSNode(); if (parent == null) return ""; // $NON-NLS-1$ if (node instanceof org.w3c.dom.css.CSSStyleDeclaration) parent = parent.getParentNode(); if (parent == null) return ""; // $NON-NLS-1$ String parentIndent = getIndent(parent); if (parent instanceof org.w3c.dom.css.CSSRule) return parentIndent + getIndentString(); if (node.getParentNode() instanceof ICSSStyleDeclaration) return parentIndent + getIndentString(); return parentIndent; }
private boolean isFontFaceRule() { ICSSNode targetNode = fContext.getTargetNode(); while (targetNode instanceof ICSSPrimitiveValue) { targetNode = targetNode.getParentNode(); } if (targetNode instanceof ICSSStyleDeclItem) { targetNode = targetNode.getParentNode(); // get Declaration if (targetNode != null) { // inline style has no rule node targetNode = targetNode.getParentNode(); // get rule } } return (targetNode instanceof CSSFontFaceRule); }
public ISelection getInputSelection(IWorkbenchPart selectingPart, ISelection selection) { // remove UI refresh adapters if (fSelectedNotifiers != null) { for (int i = 0; i < fSelectedNotifiers.length; i++) { fSelectedNotifiers[i].removeAdapter(fRefreshAdapter); } fSelectedNotifiers = null; } ISelection preferredSelection = super.getInputSelection(selectingPart, selection); if (preferredSelection instanceof IStructuredSelection) { Object[] selectedObjects = new Object[((IStructuredSelection) selection).size()]; System.arraycopy( ((IStructuredSelection) selection).toArray(), 0, selectedObjects, 0, selectedObjects.length); for (int i = 0; i < selectedObjects.length; i++) { if (selectedObjects[i] instanceof ICSSNode) { ICSSNode node = (ICSSNode) selectedObjects[i]; while (node.getNodeType() == ICSSNode.PRIMITIVEVALUE_NODE || node.getNodeType() == ICSSNode.STYLEDECLITEM_NODE) { node = node.getParentNode(); selectedObjects[i] = node; } } } /* * Add UI refresh adapters and remember notifiers for later removal */ if (selectedObjects.length > 0) { List selectedNotifiers = new ArrayList(1); for (int i = 0; i < selectedObjects.length; i++) { if (selectedObjects[i] instanceof INodeNotifier) { selectedNotifiers.add(selectedObjects[i]); ((INodeNotifier) selectedObjects[i]).addAdapter(fRefreshAdapter); } } fSelectedNotifiers = (INodeNotifier[]) selectedNotifiers.toArray(new INodeNotifier[selectedNotifiers.size()]); } preferredSelection = new StructuredSelection(selectedObjects); } return preferredSelection; }
protected void appendSpaceBefore(ICSSNode node, String toAppend, StringBuffer source) { if (node == null || source == null) return; if (isCleanup() && !getCleanupStrategy(node).isFormatSource()) return; // for not formatting case on cleanup action Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences(); if (toAppend != null && toAppend.startsWith("{") && preferences.getBoolean( CSSCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) { // $NON-NLS-1$ source.append(getLineDelimiter(node)); source.append(getIndent(node)); return; } else if ( /* ! mgr.isOnePropertyPerLine() && */ preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) { int n = getLastLineLength(node, source); int append = (toAppend != null) ? TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, toAppend, 0)[0] : 0; if (toAppend != null) append = (append < 0) ? toAppend.length() : append; if (n + append + 1 > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) { source.append(getLineDelimiter(node)); source.append(getIndent(node)); source.append(getIndentString()); return; } } source.append(" "); // $NON-NLS-1$ }
protected short preNode(ICSSNode node) { if (node.getNodeType() == ICSSNode.STYLERULE_NODE) { ICSSStyleRule rule = (ICSSStyleRule) node; ICSSSelectorList list = rule.getSelectors(); Iterator it = list.getIterator(); while (it.hasNext()) { Object obj = it.next(); if (selectorsToAvoid != null && selectorsToAvoid.contains(obj)) continue; if (!selectors.contains(obj)) selectors.add(obj); } return TRAV_PRUNE; } else if (node.getNodeType() == ICSSNode.STYLESHEET_NODE) { return TRAV_CONT; } return TRAV_PRUNE; }
String getLineDelimiter(ICSSNode node) { ICSSModel model = (node != null) ? node.getOwnerDocument().getModel() : null; return (model != null) ? model.getStructuredDocument().getLineDelimiter() : "\n"; // $NON-NLS-1$ // TODO : check whether to use model.getLineDelimiter() or // model.getStructuredDocument().getLineDelimiter() }
/** * @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; }
/** * Generate or format source between children('child' and its previous sibling) and append to * string buffer */ public final StringBuffer formatBefore(ICSSNode node, ICSSNode child, IRegion exceptFor) { Assert.isTrue(child == null || child.getParentNode() == node); StringBuffer buf = new StringBuffer(); formatBefore( node, child, /* (child != null) ? (child.getCssText()) : */ "", buf, exceptFor); //$NON-NLS-1$ return buf; }
protected final void formatChildren(ICSSNode node, StringBuffer source) { ICSSNode child = node.getFirstChild(); boolean first = true; while (child != null) { // append child CSSSourceFormatter formatter = (CSSSourceFormatter) ((INodeNotifier) child).getAdapterFor(CSSSourceFormatter.class); if (formatter == null) { formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) child); } StringBuffer childSource = ((AbstractCSSSourceFormatter) formatter).formatProc(child); if (!first) { formatBefore(node, child, new String(childSource), source, null); } source.append(childSource); // append between children child = child.getNextSibling(); first = false; } }
protected CSSSourceGenerator getParentFormatter(ICSSNode node) { ICSSNode parent = node.getParentNode(); if (parent != null) { CSSSourceGenerator formatter = (CSSSourceGenerator) ((INodeNotifier) parent).getAdapterFor(CSSSourceFormatter.class); if (formatter == null) { formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) parent); } return formatter; } return null; }
/** * Insert the method's description here. * * @return org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy * @param node org.eclipse.wst.css.core.model.interfaces.ICSSNode */ protected CSSCleanupStrategy getCleanupStrategy(ICSSNode node) { CSSCleanupStrategy currentStrategy = CSSCleanupStrategyImpl.getInstance(); ICSSDocument doc = node.getOwnerDocument(); if (doc == null) return currentStrategy; ICSSModel model = doc.getModel(); if (model == null) return currentStrategy; if (model.getStyleSheetType() != ICSSModel.EXTERNAL) { // TODO - TRANSITION Nakamori-san, or Kit, how can we move to // "HTML" plugin? // can we subclass? // currentStrategy = CSSInHTMLCleanupStrategyImpl.getInstance(); } return currentStrategy; }
/** * @return java.lang.StringBuffer * @param node org.eclipse.wst.css.core.model.interfaces.ICSSNode * @param region org.eclipse.jface.text.IRegion */ protected final StringBuffer formatProc(ICSSNode node, IRegion region) { StringBuffer source = new StringBuffer(); int curStart = ((IndexedRegion) node).getStartOffset(); int curEnd = ((IndexedRegion) node).getEndOffset(); if (node.getChildNodes().getLength() > 0) { curEnd = ((IndexedRegion) node.getFirstChild()).getStartOffset(); if (overlaps(region, curStart, curEnd)) { if (includes(region, curStart, curEnd)) formatPre(node, source); else formatPre(node, overlappedRegion(region, curStart, curEnd), source); } curStart = curEnd; curEnd = ((IndexedRegion) node.getLastChild()).getEndOffset(); if (overlaps(region, curStart, curEnd)) { if (includes(region, curStart, curEnd)) formatChildren(node, source); else formatChildren(node, overlappedRegion(region, curStart, curEnd), source); } curStart = curEnd; curEnd = ((IndexedRegion) node).getEndOffset(); if (overlaps(region, curStart, curEnd)) { if (includes(region, curStart, curEnd)) formatPost(node, source); else formatPost(node, overlappedRegion(region, curStart, curEnd), source); } } else { curEnd = getChildInsertPos(node); if (overlaps(region, curStart, curEnd)) { if (includes(region, curStart, curEnd)) formatPre(node, source); else formatPre(node, overlappedRegion(region, curStart, curEnd), source); } curStart = curEnd; curEnd = ((IndexedRegion) node).getEndOffset(); if (overlaps(region, curStart, curEnd)) { if (includes(region, curStart, curEnd)) formatPost(node, source); else formatPost(node, overlappedRegion(region, curStart, curEnd), source); } } return source; }
protected void appendDelimBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer source) { if (node == null || source == null) return; if (isCleanup() && !getCleanupStrategy(node).isFormatSource()) return; // for not formatting case on cleanup action String delim = getLineDelimiter(node); boolean needIndent = !(node instanceof ICSSStyleSheet); if (toAppend == null) { source.append(delim); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } else { String type = toAppend.getType(); if (type == CSSRegionContexts.CSS_COMMENT) { RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion()); it.prev(); ITextRegion prev = it.prev(); int[] result = null; if (prev == null || (prev.getType() == CSSRegionContexts.CSS_S && (result = TextUtilities.indexOf( DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)) [0] >= 0)) { // Collapse to one empty line if there's more than one. int offset = result[0] + DefaultLineTracker.DELIMITERS[result[1]].length(); if (offset < it.getStructuredDocumentRegion().getText(prev).length()) { if (TextUtilities.indexOf( DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), offset)[0] >= 0) { source.append(delim); } } source.append(delim); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } else if (prev.getType() == CSSRegionContexts.CSS_COMMENT) { String fullText = toAppend.getDocumentRegion().getFullText(prev); String trimmedText = toAppend.getDocumentRegion().getText(prev); String whiteSpaces = ""; // $NON-NLS-1$ if (fullText != null && trimmedText != null) whiteSpaces = fullText.substring(trimmedText.length()); int[] delimiterFound = TextUtilities.indexOf(DefaultLineTracker.DELIMITERS, whiteSpaces, 0); if (delimiterFound[0] != -1) { source.append(delim); } else { appendSpaceBefore(node, toAppend.getText(), source); /*If two comments can't be adjusted in one line(combined length exceeds line width), * a tab is also appended along with next line delimiter , we need to remove that. */ if (source.toString().endsWith(getIndentString())) { source.delete((source.length() - getIndentString().length()), source.length()); } } } else { appendSpaceBefore(node, toAppend.getText(), source); } } else if (type == CSSRegionContexts.CSS_DELIMITER || type == CSSRegionContexts.CSS_DECLARATION_DELIMITER) { RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion()); it.prev(); ITextRegion prev = it.prev(); Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences(); if (prev.getType() == CSSRegionContexts.CSS_S && TextUtilities.indexOf( DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[0] >= 0) { source.append(delim); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } else if (preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) { int length = getLastLineLength(node, source); int append = 1; if (length + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) { source.append(getLineDelimiter(node)); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } } } else if (type == CSSRegionContexts.CSS_RBRACE || type == CSSRegionContexts.CSS_LBRACE) { source.append(delim); source.append(getIndent(node)); } else { source.append(delim); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } } }
protected void appendSpaceBefore(ICSSNode node, CompoundRegion toAppend, StringBuffer source) { if (node == null || toAppend == null || source == null) return; if (isCleanup() && !getCleanupStrategy(node).isFormatSource()) return; // for not formatting case on cleanup action String type = toAppend.getType(); Preferences preferences = CSSCorePlugin.getDefault().getPluginPreferences(); boolean needIndent = !(node instanceof ICSSStyleSheet); if (type == CSSRegionContexts.CSS_COMMENT) { // check whether previous region is 'S' and has CR-LF String delim = getLineDelimiter(node); RegionIterator it = new RegionIterator(toAppend.getDocumentRegion(), toAppend.getTextRegion()); it.prev(); ITextRegion prev = it.prev(); // bug390904 if (prev.getType() == CSSRegionContexts.CSS_LBRACE && TextUtilities.indexOf( DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getFullText(prev), 0)[0] > 0) { source.append(delim); source.append(getIndent(node)); source.append(getIndentString()); } else if (prev.getType() == CSSRegionContexts.CSS_S && TextUtilities.indexOf( DefaultLineTracker.DELIMITERS, it.getStructuredDocumentRegion().getText(prev), 0)[ 0] >= 0) { source.append(delim); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } else { appendSpaceBefore(node, toAppend.getText(), source); } } else if (type == CSSRegionContexts.CSS_LBRACE && preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_NEWLINE_ON_OPEN_BRACE)) { String delim = getLineDelimiter(node); source.append(delim); source.append(getIndent(node)); // } else if (type == CSSRegionContexts.CSS_CURLY_BRACE_CLOSE) { // } else if (type == CSSRegionContexts.CSS_INCLUDES || type == // CSSRegionContexts.CSS_DASHMATCH) { } else if (type == CSSRegionContexts.CSS_DECLARATION_SEPARATOR && node instanceof ICSSStyleDeclItem) { int n = preferences.getInt(CSSCorePreferenceNames.FORMAT_PROP_PRE_DELIM); // no delimiter case while (n-- > 0) source.append(" "); // $NON-NLS-1$ } else if (type == CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR || type == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) { if (preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH) > 0 && (!preferences.getBoolean(CSSCorePreferenceNames.WRAPPING_PROHIBIT_WRAP_ON_ATTR) || node.getOwnerDocument().getNodeType() != ICSSNode.STYLEDECLARATION_NODE)) { int length = getLastLineLength(node, source); int append = 1; if (length + append > preferences.getInt(CSSCorePreferenceNames.LINE_WIDTH)) { source.append(getLineDelimiter(node)); source.append(getIndent(node)); if (needIndent) source.append(getIndentString()); } } } else if (CSSRegionContexts.CSS_FOREIGN_ELEMENT == type || CSSRegionContexts.CSS_DECLARATION_DELIMITER == type) { return; } else appendSpaceBefore(node, toAppend.getText(), source); }
protected boolean isIncludesPreEnd(ICSSNode node, IRegion region) { return (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getStartOffset() == (region.getOffset() + region.getLength())); }