blob: 29499ba05f725f982d25364dd3a8c08b009e496e [file] [log] [blame]
nitind958d79a2004-11-23 19:23:00 +00001/*******************************************************************************
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 *******************************************************************************/
11package org.eclipse.wst.css.core.internal.formatter;
12
13
14
15import org.eclipse.jface.text.IRegion;
david_williamsd3484c72005-05-25 21:11:11 +000016import org.eclipse.wst.css.core.internal.CSSCorePlugin;
david_williamsd8a74fb2005-03-18 02:41:46 +000017import org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy;
david_williams63219a22005-04-10 01:59:51 +000018import org.eclipse.wst.css.core.internal.parserz.CSSRegionContexts;
david_williamsd3484c72005-05-25 21:11:11 +000019import org.eclipse.wst.css.core.internal.preferences.CSSCorePreferenceNames;
david_williams63219a22005-04-10 01:59:51 +000020import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
david_williams4ad020f2005-04-18 08:00:30 +000021import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
22import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
23import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
nitind958d79a2004-11-23 19:23:00 +000024
25
26/**
27 *
28 */
29abstract 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_williamsd3484c72005-05-25 21:11:11 +000064 } else if (prev != null && child != null) { // generate source between
65 // parameters
nitind958d79a2004-11-23 19:23:00 +000066 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_williams63219a22005-04-10 01:59:51 +000090 protected void formatPost(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode node, java.lang.StringBuffer source) {
nitind958d79a2004-11-23 19:23:00 +000091 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_williamsd3484c72005-05-25 21:11:11 +0000102 } else { // generate source
nitind958d79a2004-11-23 19:23:00 +0000103 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_williamsd3484c72005-05-25 21:11:11 +0000129
nitind958d79a2004-11-23 19:23:00 +0000130 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_williamsd3484c72005-05-25 21:11:11 +0000140 } else { // generate source
nitind958d79a2004-11-23 19:23:00 +0000141 String func = getFunc();
david_williamsd3484c72005-05-25 21:11:11 +0000142 if (CSSCorePlugin.getDefault().getPluginPreferences().getInt(CSSCorePreferenceNames.CASE_PROPERTY_VALUE) == CSSCorePreferenceNames.UPPER)
nitind958d79a2004-11-23 19:23:00 +0000143 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_williamsd3484c72005-05-25 21:11:11 +0000175 return n;
nitind958d79a2004-11-23 19:23:00 +0000176 }
177 return -1;
178 }
179
180 /**
181 *
182 */
183 protected abstract String getFunc();
184}