nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.wst.css.core.internal.formatter; |
| 12 | |
| 13 | |
| 14 | |
| 15 | import org.eclipse.jface.text.IRegion; |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 16 | import org.eclipse.wst.css.core.internal.CSSCorePlugin; |
david_williams | d8a74fb | 2005-03-18 02:41:46 +0000 | [diff] [blame] | 17 | import org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy; |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 18 | import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts; |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames; |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 20 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 21 | import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
| 22 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| 23 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | /** |
| 27 | * |
| 28 | */ |
| 29 | abstract public class FunctionFormatter extends AbstractCSSSourceFormatter { |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | */ |
| 34 | FunctionFormatter() { |
| 35 | super(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | */ |
| 41 | protected void formatBefore(ICSSNode node, ICSSNode child, String toAppend, StringBuffer source, IRegion exceptFor) { |
| 42 | ICSSNode prev = (child != null) ? child.getPreviousSibling() : node.getLastChild(); |
| 43 | int start = (prev != null) ? ((IndexedRegion) prev).getEndOffset() : 0; |
| 44 | int end = (child != null) ? ((IndexedRegion) child).getStartOffset() : 0; |
| 45 | if (start > 0 && start < end) { // format source |
| 46 | CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| 47 | |
| 48 | IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| 49 | // get meaning regions |
| 50 | CompoundRegion[] regions = null; |
| 51 | if (exceptFor == null) |
| 52 | regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy); |
| 53 | else { |
| 54 | String pickupType = CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR; |
| 55 | if (prev == null || child == null) |
| 56 | pickupType = null; |
| 57 | regions = getRegions(structuredDocument, new FormatRegion(start, end - start), exceptFor, pickupType); |
| 58 | } |
| 59 | // extract source |
| 60 | for (int i = 0; i < regions.length; i++) { |
| 61 | appendSpaceBefore(node, regions[i], source); |
| 62 | source.append(decoratedRegion(regions[i], 2, stgy)); |
| 63 | } |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 64 | } else if (prev != null && child != null) { // generate source between |
| 65 | // parameters |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 66 | source.append(",");//$NON-NLS-1$ |
| 67 | } |
| 68 | appendSpaceBefore(node, toAppend, source); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * |
| 73 | */ |
| 74 | protected void formatBefore(ICSSNode node, ICSSNode child, IRegion region, String toAppend, StringBuffer source) { |
| 75 | CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| 76 | |
| 77 | IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| 78 | CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy); |
| 79 | CompoundRegion[] outside = getOutsideRegions(structuredDocument, region); |
| 80 | for (int i = 0; i < regions.length; i++) { |
| 81 | if (i != 0 || needS(outside[0])) |
| 82 | appendSpaceBefore(node, regions[i], source); |
| 83 | source.append(decoratedRegion(regions[i], 2, stgy)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * |
| 89 | */ |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 90 | protected void formatPost(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode node, java.lang.StringBuffer source) { |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 91 | CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| 92 | |
| 93 | int end = ((IndexedRegion) node).getEndOffset(); |
| 94 | int start = (node.getLastChild() != null && ((IndexedRegion) node.getLastChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getLastChild()).getEndOffset() : getChildInsertPos(node); |
| 95 | if (end > 0 && start < end) { // format source |
| 96 | IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| 97 | CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy); |
| 98 | for (int i = 0; i < regions.length; i++) { |
| 99 | appendSpaceBefore(node, regions[i], source); |
| 100 | source.append(decoratedRegion(regions[i], 2, stgy)); |
| 101 | } |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 102 | } else { // generate source |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 103 | source.append(")");//$NON-NLS-1$ |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * |
| 109 | */ |
| 110 | protected void formatPost(ICSSNode node, IRegion region, StringBuffer source) { |
| 111 | CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| 112 | |
| 113 | IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| 114 | CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy); |
| 115 | CompoundRegion[] outside = getOutsideRegions(structuredDocument, region); |
| 116 | for (int i = 0; i < regions.length; i++) { |
| 117 | if (i != 0 || needS(outside[0])) |
| 118 | appendSpaceBefore(node, regions[i], source); |
| 119 | source.append(decoratedRegion(regions[i], 2, stgy)); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * |
| 125 | */ |
| 126 | protected void formatPre(ICSSNode node, StringBuffer source) { |
| 127 | int start = ((IndexedRegion) node).getStartOffset(); |
| 128 | int end = (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getFirstChild()).getStartOffset() : getChildInsertPos(node); |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 129 | |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 130 | if (end > 0) { // format source |
| 131 | CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| 132 | |
| 133 | IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| 134 | CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy); |
| 135 | for (int i = 0; i < regions.length; i++) { |
| 136 | if (i != 0) |
| 137 | appendSpaceBefore(node, regions[i], source); |
| 138 | source.append(decoratedPropValueRegion(regions[i], stgy)); |
| 139 | } |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 140 | } else { // generate source |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 141 | String func = getFunc(); |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 142 | if (CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER) |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 143 | func = func.toUpperCase(); |
| 144 | source.append(func); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * |
| 150 | */ |
| 151 | protected void formatPre(ICSSNode node, IRegion region, StringBuffer source) { |
| 152 | CSSCleanupStrategy stgy = getCleanupStrategy(node); |
| 153 | |
| 154 | IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument(); |
| 155 | CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, region, stgy); |
| 156 | CompoundRegion[] outside = getOutsideRegions(structuredDocument, region); |
| 157 | for (int i = 0; i < regions.length; i++) { |
| 158 | if (i != 0 || needS(outside[0])) |
| 159 | appendSpaceBefore(node, regions[i], source); |
| 160 | source.append(decoratedPropValueRegion(regions[i], stgy)); |
| 161 | } |
| 162 | if (needS(outside[1]) && !isIncludesPreEnd(node, region)) |
| 163 | appendSpaceBefore(node, outside[1], source); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * |
| 168 | */ |
| 169 | public int getChildInsertPos(ICSSNode node) { |
| 170 | int n = ((IndexedRegion) node).getEndOffset(); |
| 171 | if (n > 0) { |
| 172 | IStructuredDocumentRegion flatNode = node.getOwnerDocument().getModel().getStructuredDocument().getRegionAtCharacterOffset(n - 1); |
| 173 | if (flatNode.getRegionAtCharacterOffset(n - 1).getType() == CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE) |
| 174 | return n - 1; |
david_williams | d3484c7 | 2005-05-25 21:11:11 +0000 | [diff] [blame] | 175 | return n; |
nitind | 958d79a | 2004-11-23 19:23:00 +0000 | [diff] [blame] | 176 | } |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * |
| 182 | */ |
| 183 | protected abstract String getFunc(); |
| 184 | } |