/
XSIFormatterParticipant.java
264 lines (249 loc) · 10 KB
/
XSIFormatterParticipant.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.xsi.participants;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.extensions.xsi.XSISchemaModel;
import org.eclipse.lemminx.extensions.xsi.settings.XSISchemaLocationSplit;
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.services.format.XMLFormatterDocument;
import org.eclipse.lemminx.services.format.XMLFormattingConstraints;
import org.eclipse.lemminx.settings.XMLFormattingOptions;
import org.eclipse.lemminx.settings.XMLFormattingOptions.SplitAttributes;
import org.eclipse.lemminx.utils.StringUtils;
import org.eclipse.lemminx.utils.XMLBuilder;
import org.eclipse.lsp4j.TextEdit;
/**
* Formatter participant implementation to format xsi:schemaLocation attribute
* value. The format of the xsi:schemaLocation attribute value depends on the
* {@link XSISchemaLocationSplit} setting:
*
* <ul>
* <li>{@link XSISchemaLocationSplit#none} : don't format the xsi:schemaLocation
* attribute value.</li>
* <li>{@link XSISchemaLocationSplit#onElement} : generate a line feed for each
* namespace declaration:
*
* <pre>
* <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
* </pre>
*
* </li>
* <li>{@link XSISchemaLocationSplit#onPair} : generate a line feed for each
* location declaration:
*
* <pre>
* <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
* </pre>
*
* </li>
* </ul>
*
* @author Angelo ZERR
*
*/
public class XSIFormatterParticipant implements IFormatterParticipant {
@Override
public boolean formatAttributeValue(String name, String valueWithoutQuote, Character quote, DOMAttr attr,
XMLBuilder xml) {
if (XSISchemaModel.isXSISchemaLocationAttr(name, attr)) {
// The attribute is a xsi:schemaLocation
XMLFormattingOptions formattingOptions = xml.getSharedSettings().getFormattingSettings();
XSISchemaLocationSplit split = XSISchemaLocationSplit.getSplit(formattingOptions);
if (split == XSISchemaLocationSplit.none) {
// don't format the xsi:schemaLocation attribute value
return false;
}
int lineFeed = split == XSISchemaLocationSplit.onElement ? 1 : 2;
if (quote != null) {
xml.append(quote);
}
List<String> locations = getLocations(valueWithoutQuote);
String indent = "";
boolean insertSpaces = formattingOptions.isInsertSpaces();
int tabSize = formattingOptions.getTabSize();
for (int i = 0; i < locations.size(); i++) {
if (i % lineFeed == 0) {
if (i == 0) {
indent = getCurrentLineIndent(xml, insertSpaces, tabSize);
} else {
xml.linefeed();
xml.append(indent);
}
} else {
xml.appendSpace();
}
xml.append(locations.get(i));
}
if (quote != null) {
xml.append(quote);
}
return true;
}
return false;
}
private static List<String> getLocations(String value) {
List<String> locations = new ArrayList<>();
int start = -1;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (Character.isWhitespace(c)) {
if (start != -1) {
locations.add(value.substring(start, i));
start = -1;
}
} else if (start == -1) {
start = i;
}
}
if (start != -1) {
locations.add(value.substring(start, value.length()));
}
return locations;
}
public String getCurrentLineIndent(XMLBuilder xml, boolean insertSpaces, int tabSize) {
int nbChars = 0;
for (int i = xml.length() - 1; i >= 0; i--) {
if (xml.charAt(i) == '\r' || xml.charAt(i) == '\n') {
break;
}
if (!insertSpaces && xml.charAt(i) == '\t') {
nbChars = nbChars + tabSize;
} else {
nbChars++;
}
}
StringBuilder indent = new StringBuilder();
if (insertSpaces || tabSize <= 0) {
for (int i = 0; i < nbChars; i++) {
indent.append(" ");
}
} else {
int nbTabs = nbChars / tabSize;
nbChars = nbChars % tabSize;
for (int i = 0; i < nbTabs; i++) {
indent.append("\t");
}
for (int i = 0; i < nbChars; i++) {
indent.append(" ");
}
}
return indent.toString();
}
@Override
public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocument formatterDocument,
XMLFormattingConstraints parentConstraints, XMLFormattingOptions formattingOptions, List<TextEdit> edits) {
XSISchemaLocationSplit split = XSISchemaLocationSplit.getSplit(formattingOptions);
if (split == XSISchemaLocationSplit.none || !XSISchemaModel.isXSISchemaLocationAttr(attr.getName(), attr)) {
if (formatterDocument.isMaxLineWidthSupported() && attr.getValue() != null) {
parentConstraints
.setAvailableLineWidth(parentConstraints.getAvailableLineWidth() - attr.getValue().length());
}
return false;
}
int firstContentOffset = getFirstContentOffset(attr.getOriginalValue());
if (firstContentOffset == -1) {
return false;
}
int attrValueStart = attr.getNodeAttrValue().getStart();
// Remove extra spaces between start of xsi:schemaLocation attribute value quote
// and actual value
formatterDocument.removeLeftSpaces(attrValueStart + 1, // <... xsi:schemaLocation="| value"
// <... xsi:schemaLocation=" |value"
attrValueStart + firstContentOffset, edits);
int tabSize = formattingOptions.getTabSize();
int indentSpaceOffset;
int startOfLineOffset = formatterDocument.getLineAtOffset(attr.getOwnerElement().getStart());
if (formattingOptions.getSplitAttributes() != SplitAttributes.preserve) {
if (formattingOptions.getSplitAttributes() == SplitAttributes.splitNewLine) {
indentSpaceOffset = (attrValueStart + 1) - attr.getNodeAttrName().getStart()
+ formattingOptions.getSplitAttributesIndentSize() * tabSize;
} else {
indentSpaceOffset = (attrValueStart + 1) - attr.getNodeAttrName().getStart()
+ attr.getOwnerElement().getTagName().length() + 2;
}
} else if (formattingOptions.isPreserveAttributeLineBreaks()) {
indentSpaceOffset = attrValueStart - formatterDocument.getOffsetWithPreserveLineBreaks(startOfLineOffset,
attrValueStart, tabSize, formattingOptions.isInsertSpaces());
} else {
indentSpaceOffset = formatterDocument.getNormalizedLength(startOfLineOffset, attrValueStart + 1)
- startOfLineOffset;
}
int lineFeed = split == XSISchemaLocationSplit.onElement ? 1 : 2;
int locationNum = 1;
String attrValue = attr.getOriginalValue();
int lastAttrValueTermIndex = 0;
int availableLineWidth = parentConstraints.getAvailableLineWidth();
for (int i = firstContentOffset; i < attrValue.length(); i++) {
int from = formatterDocument.adjustOffsetWithLeftWhitespaces(attrValueStart, attrValueStart + i + 1);
if (Character.isWhitespace(attrValue.charAt(i)) && !Character.isWhitespace(attrValue.charAt(i + 1))
&& !StringUtils.isQuote(attrValue.charAt(from - attrValueStart))) {
availableLineWidth -= i - lastAttrValueTermIndex;
lastAttrValueTermIndex = i;
if (availableLineWidth < 0 && formatterDocument.isMaxLineWidthSupported()
&& formattingOptions.getSplitAttributes() == SplitAttributes.preserve) {
indentSpaceOffset = (attrValueStart + 1) - attr.getNodeAttrName().getStart()
+ (parentConstraints.getIndentLevel() + 1) * tabSize;
}
int attrValuelength = attrValueStart - indentSpaceOffset + attr.getValue().length();
// Insert newline and indent where required based on setting
if (locationNum % lineFeed == 0) {
formatterDocument.replaceLeftSpacesWithIndentationWithOffsetSpaces(indentSpaceOffset,
attrValueStart, attrValueStart + i + 1, true, edits);
availableLineWidth = formatterDocument.getMaxLineWidth() - attrValuelength;
} else {
formatterDocument.replaceLeftSpacesWithOneSpace(indentSpaceOffset, attrValueStart + i + 1, edits);
availableLineWidth = availableLineWidth - attrValuelength;
}
locationNum++;
}
}
if (formatterDocument.isMaxLineWidthSupported() && attr.getValue() != null) {
parentConstraints
.setAvailableLineWidth(availableLineWidth);
}
return true;
}
/**
* Returns the offset from opening quote to first non-whitespace character of an
* attribute value
*
* @param originalValue
* @return offset from opening quote to first non-whitespace character of an
* attribute value
*/
private static int getFirstContentOffset(String originalValue) {
if (originalValue == null) {
return -1;
}
for (int i = 1; i < originalValue.length(); i++) {
if (!Character.isWhitespace(originalValue.charAt(i)) && !StringUtils.isQuote(originalValue.charAt(i))) {
return i;
}
}
return -1;
}
}