use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class GroupByWordPrefixes method group.
@Override
@NotNull
public Collection<Group> group(@NotNull final AbstractTreeNode parent, @NotNull Collection<TreeElement> children) {
List<Key> keys = new ArrayList<>();
String parentPrefix;
int parentPrefixLength;
if (parent.getValue() instanceof PropertiesPrefixGroup) {
parentPrefix = ((PropertiesPrefixGroup) parent.getValue()).getPrefix();
parentPrefixLength = StringUtil.split(parentPrefix, mySeparator).size();
} else {
parentPrefix = "";
parentPrefixLength = 0;
}
for (TreeElement element : children) {
if (!(element instanceof StructureViewTreeElement)) {
continue;
}
Object value = ((StructureViewTreeElement) element).getValue();
if (!(value instanceof IProperty)) {
continue;
}
final String text = ((IProperty) value).getUnescapedKey();
if (text == null)
continue;
LOG.assertTrue(text.startsWith(parentPrefix) || text.startsWith(mySeparator));
List<String> words = StringUtil.split(text, mySeparator);
keys.add(new Key(words, element));
}
Collections.sort(keys, (k1, k2) -> {
List<String> o1 = k1.words;
List<String> o2 = k2.words;
for (int i = 0; i < Math.max(o1.size(), o2.size()); i++) {
if (i == o1.size())
return 1;
if (i == o2.size())
return -1;
String s1 = o1.get(i);
String s2 = o2.get(i);
int res = s1.compareTo(s2);
if (res != 0)
return res;
}
return 0;
});
List<Group> groups = new ArrayList<>();
int groupStart = 0;
for (int i = 0; i <= keys.size(); i++) {
if (!isEndOfGroup(i, keys, parentPrefixLength)) {
continue;
}
// find longest group prefix
List<String> firstKey = groupStart == keys.size() ? Collections.<String>emptyList() : keys.get(groupStart).words;
int prefixLen = firstKey.size();
for (int j = groupStart + 1; j < i; j++) {
List<String> prevKey = keys.get(j - 1).words;
List<String> nextKey = keys.get(j).words;
for (int k = parentPrefixLength; k < prefixLen; k++) {
String word = k < nextKey.size() ? nextKey.get(k) : null;
String wordInPrevKey = k < prevKey.size() ? prevKey.get(k) : null;
if (!Comparing.strEqual(word, wordInPrevKey)) {
prefixLen = k;
break;
}
}
}
String[] strings = firstKey.subList(0, prefixLen).toArray(new String[prefixLen]);
String prefix = StringUtil.join(strings, mySeparator);
String presentableName = prefix.substring(parentPrefix.length());
presentableName = StringUtil.trimStart(presentableName, mySeparator);
if (i - groupStart > 1) {
groups.add(new PropertiesPrefixGroup(children, prefix, presentableName, mySeparator));
} else if (groupStart != keys.size()) {
TreeElement node = keys.get(groupStart).node;
if (node instanceof PropertiesStructureViewElement) {
((PropertiesStructureViewElement) node).setPresentableName(presentableName);
} else {
((ResourceBundlePropertyStructureViewElement) node).setPresentableName(presentableName);
}
}
groupStart = i;
}
return groups;
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class PropertiesFileStructureViewElement method getChildrenBase.
@NotNull
public Collection<StructureViewTreeElement> getChildrenBase() {
List<? extends IProperty> properties = getElement().getProperties();
Collection<StructureViewTreeElement> elements = new ArrayList<>(properties.size());
for (IProperty property : properties) {
elements.add(new PropertiesStructureViewElement((Property) property));
}
return elements;
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class TrailingSpacesInPropertyInspection method checkFile.
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
if (!(file instanceof PropertiesFile))
return null;
final List<IProperty> properties = ((PropertiesFile) file).getProperties();
final List<ProblemDescriptor> descriptors = new SmartList<>();
for (IProperty property : properties) {
ProgressManager.checkCanceled();
final PropertyImpl propertyImpl = (PropertyImpl) property;
for (ASTNode node : ContainerUtil.ar(propertyImpl.getKeyNode(), propertyImpl.getValueNode())) {
if (node != null) {
PsiElement key = node.getPsi();
TextRange textRange = getTrailingSpaces(key, myIgnoreVisibleSpaces);
if (textRange != null) {
descriptors.add(manager.createProblemDescriptor(key, textRange, "Trailing spaces", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true, new RemoveTrailingSpacesFix(myIgnoreVisibleSpaces)));
}
}
}
}
return descriptors.toArray(new ProblemDescriptor[descriptors.size()]);
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class DuplicatePropertyInspection method processDuplicateKeysWithDifferentValues.
private static void processDuplicateKeysWithDifferentValues(final Map<String, Set<String>> keyToDifferentValues, final Map<String, Set<PsiFile>> keyToFiles, final List<ProblemDescriptor> problemDescriptors, final InspectionManager manager, final PsiFile psiFile, final ProgressIndicator progress) {
for (String key : keyToDifferentValues.keySet()) {
if (progress != null) {
progress.setText2(InspectionsBundle.message("duplicate.property.diff.key.progress.indicator.text", key));
if (progress.isCanceled())
throw new ProcessCanceledException();
}
final Set<String> values = keyToDifferentValues.get(key);
if (values == null || values.size() < 2) {
keyToFiles.remove(key);
} else {
StringBuffer message = new StringBuffer();
final Set<PsiFile> psiFiles = keyToFiles.get(key);
boolean firstUsage = true;
for (PsiFile file : psiFiles) {
if (!(file instanceof PropertiesFile))
continue;
PropertiesFile propertiesFile = (PropertiesFile) file;
final List<IProperty> propertiesByKey = propertiesFile.findPropertiesByKey(key);
for (IProperty property : propertiesByKey) {
if (firstUsage) {
message.append(InspectionsBundle.message("duplicate.property.diff.key.problem.descriptor", key));
firstUsage = false;
}
surroundWithHref(message, property.getPsiElement().getFirstChild(), false);
}
}
problemDescriptors.add(manager.createProblemDescriptor(psiFile, message.toString(), false, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
}
}
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class AlphaUnsortedPropertiesFileInspection method sortPropertiesFile.
private static void sortPropertiesFile(final PropertiesFile file) {
final List<IProperty> properties = new ArrayList<>(file.getProperties());
Collections.sort(properties, (p1, p2) -> Comparing.compare(p1.getKey(), p2.getKey(), String.CASE_INSENSITIVE_ORDER));
final char delimiter = PropertiesCodeStyleSettings.getInstance(file.getProject()).getDelimiter();
final StringBuilder rawText = new StringBuilder();
for (int i = 0; i < properties.size(); i++) {
IProperty property = properties.get(i);
final String value = property.getValue();
final String commentAboveProperty = property.getDocCommentText();
if (commentAboveProperty != null) {
rawText.append(commentAboveProperty).append("\n");
}
final String key = property.getKey();
final String propertyText;
if (key != null) {
propertyText = PropertiesElementFactory.getPropertyText(key, value != null ? value : "", delimiter, null, false);
rawText.append(propertyText);
if (i != properties.size() - 1) {
rawText.append("\n");
}
}
}
final PropertiesFile fakeFile = PropertiesElementFactory.createPropertiesFile(file.getProject(), rawText.toString());
final PropertiesList propertiesList = PsiTreeUtil.findChildOfType(file.getContainingFile(), PropertiesList.class);
LOG.assertTrue(propertiesList != null);
final PropertiesList fakePropertiesList = PsiTreeUtil.findChildOfType(fakeFile.getContainingFile(), PropertiesList.class);
LOG.assertTrue(fakePropertiesList != null);
propertiesList.replace(fakePropertiesList);
}
Aggregations