From d78e6aeffb17eb233d9ea2904ceaae0be2c79456 Mon Sep 17 00:00:00 2001 From: Andrew Gvozdev Date: Wed, 28 Dec 2011 00:20:15 -0500 Subject: [PATCH] bug 367253: Source folders are reshuffled when build settings are edited. --- .../core/settings/model/util/CDataUtil.java | 206 +++++++++--------- 1 file changed, 108 insertions(+), 98 deletions(-) diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/CDataUtil.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/CDataUtil.java index 4081606adac..f7565fcabfc 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/CDataUtil.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/CDataUtil.java @@ -14,6 +14,7 @@ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -71,7 +72,7 @@ import org.eclipse.core.runtime.preferences.IScopeContext; public class CDataUtil { private static final String EMPTY = ""; //$NON-NLS-1$ private static final String DELIM = " "; //$NON-NLS-1$ - + private static Random randomNumber; public static final String[] EMPTY_STRING_ARRAY = new String[0]; @@ -87,14 +88,14 @@ public class CDataUtil { } return i; } - + public static String genId(String baseId){ String suffix = new Integer(genRandomNumber()).toString(); - return baseId != null ? + return baseId != null ? new StringBuffer(baseId).append(".").append(suffix).toString() //$NON-NLS-1$ : suffix; } - + public static boolean objectsEqual(Object o1, Object o2){ if(o1 == null) return o2 == null; @@ -117,7 +118,7 @@ public class CDataUtil { for(int i = 1; i < array.length; i++){ buf.append(separator).append(array[i]); } - + return buf.toString(); } @@ -137,7 +138,7 @@ public class CDataUtil { public static ICSettingEntry[] resolveEntries(ICSettingEntry entries[], ICConfigurationDescription cfgDes){ if(entries.length == 0) return entries; - + ArrayList out = new ArrayList(entries.length); ICdtVariableManager mngr = CCorePlugin.getDefault().getCdtVariableManager(); @@ -181,14 +182,14 @@ public class CDataUtil { private static ICSettingEntry[] createResolvedEntry(ICSettingEntry entry, ICConfigurationDescription cfg, ICdtVariableManager mngr){ if(entry.isResolved()) return new ICSettingEntry[] { entry }; - + String name = entry.getName(); - + String[] names = new String[] { name }; // default value try { if ((entry.getKind() != ICSettingEntry.MACRO) && mngr.isStringListValue(name, cfg)) { - names = mngr.resolveStringListValue(name, EMPTY, DELIM, cfg); + names = mngr.resolveStringListValue(name, EMPTY, DELIM, cfg); } else { names[0] = mngr.resolveValue(name, EMPTY, DELIM, cfg); } @@ -197,7 +198,7 @@ public class CDataUtil { } ICSettingEntry[] result = new ICSettingEntry[names.length]; - + for (int k=0; k map = mngr.getContentTypeIdToLanguageDescriptionsMap(); - + CResourceData[] rcDatas = cfg.getResourceDatas(); for(int i = 0; i < rcDatas.length; i++){ if(rcDatas[i].getType() == ICSettingBase.SETTING_FOLDER){ adjustFolderData(cfg, (CFolderData)rcDatas[i], factory, dess, new HashMap(map)); } } - + return cfg; } - + private static void adjustFolderData(CConfigurationData cfgData, CFolderData data, CDataFactory factory, ILanguageDescriptor dess[], HashMap map){ Map langMap = new HashMap(); for(int i = 0; i < dess.length; i++){ @@ -531,38 +532,38 @@ public class CDataUtil { for(int q = 0; q < langs.length; q++){ langMap.remove(langs[q].getId()); } - + adjustLanguageData(data, lData, langs[0]); } } } } - + if(!langMap.isEmpty()){ addLangs(cfgData, data, factory, langMap, map); } - + } - + private static CLanguageData adjustLanguageData(CFolderData data, CLanguageData lData, ILanguageDescriptor des){ String [] cTypeIds = des.getContentTypeIds(); String srcIds[] = lData.getSourceContentTypeIds(); - + Set landTypes = new HashSet(Arrays.asList(cTypeIds)); landTypes.removeAll(Arrays.asList(srcIds)); - + if(landTypes.size() != 0){ List srcList = new ArrayList(); srcList.addAll(landTypes); lData.setSourceContentTypeIds(srcList.toArray(new String[srcList.size()])); } - + if(!des.getId().equals(lData.getLanguageId())){ lData.setLanguageId(des.getId()); } return lData; } - + private static void addLangs(CConfigurationData cfgData, CFolderData data, CDataFactory factory, Map langMap, Map cTypeToLangMap){ List list = new ArrayList(langMap.values()); ILanguageDescriptor des; @@ -579,10 +580,10 @@ public class CDataUtil { } } } - + if(addLang){ - CLanguageData lData = factory.createLanguageData(cfgData, data, genId(data.getId()), des.getName(), des.getId(), - ICLanguageSettingEntry.INCLUDE_FILE + CLanguageData lData = factory.createLanguageData(cfgData, data, genId(data.getId()), des.getName(), des.getId(), + ICLanguageSettingEntry.INCLUDE_FILE | ICLanguageSettingEntry.INCLUDE_PATH | ICLanguageSettingEntry.MACRO | ICLanguageSettingEntry.MACRO_FILE, @@ -591,7 +592,7 @@ public class CDataUtil { } } } - + public static boolean isExcluded(IPath path, ICSourceEntry[] entries){ for(int i = 0; i < entries.length; i++){ if(!isExcluded(path, entries[i])) @@ -599,16 +600,16 @@ public class CDataUtil { } return true; } - + public static boolean isExcluded(IPath path, ICSourceEntry entry){ IPath entryPath = new Path(entry.getName()); - + if(path.isPrefixOf(entryPath)) return false; - + if(!entryPath.isPrefixOf(path)) return true; - + if(path.segmentCount() == 0) return false; char[][] exclusions = entry.fullExclusionPatternChars(); @@ -617,13 +618,13 @@ public class CDataUtil { public static boolean isOnSourceEntry(IPath path, ICSourceEntry entry){ IPath entryPath = new Path(entry.getName()); - + if(path.equals(entryPath)) return true; - + if(!entryPath.isPrefixOf(path)) return false; - + if(path.segmentCount() == 0) return true; char[][] exclusions = entry.fullExclusionPatternChars(); @@ -639,10 +640,10 @@ public class CDataUtil { } /** - * + * * @param ein - initial source entries * @param aus - resulting source entries - * @return - true if they are equal + * @return - true if they are equal */ public static boolean isEqual(ICSourceEntry[] ein, ICSourceEntry[] aus) { if (ein == null || aus == null) return (ein == null && aus == null); @@ -656,14 +657,14 @@ public class CDataUtil { found = true; break; } - return false; // contents is changed ! + return false; // contents is changed ! } - if (!found) + if (!found) return false; // name is not found ! } return true; // all entries are equal by name and contents } - + public static ICSourceEntry[] setExcluded(IPath path, boolean isFolder, boolean excluded, ICSourceEntry[] entries) throws CoreException { return setExcluded(path, isFolder, excluded, entries, true); } @@ -682,14 +683,14 @@ public class CDataUtil { public static ICSourceEntry[] setExcluded(IPath path, boolean isFolder, boolean excluded, ICSourceEntry[] entries, boolean throwExceptionOnErr) throws CoreException { if(isExcluded(path, entries) == excluded) return entries; - + ICSourceEntry[] newEntries; if(excluded){ List includeList = new ArrayList(entries.length); List excludeList = new ArrayList(entries.length); - + sortEntries(path, false, entries, includeList, excludeList); - + for(int i = 0; i < includeList.size(); i++){ ICSourceEntry oldEntry = includeList.get(i); List tmp = new ArrayList(1); @@ -698,7 +699,7 @@ public class CDataUtil { if(newEntry != null) excludeList.add(newEntry); } - + newEntries = excludeList.toArray(new ICSourceEntry[excludeList.size()]); } else { List includeList = new ArrayList(entries.length + 1); @@ -710,7 +711,7 @@ public class CDataUtil { if(includeExclusion(path, includeList) >= 0) included = true; } - + if(!included){ if(isFolder){ includeList.add(new CSourceEntry(path, null, ICSettingEntry.VALUE_WORKSPACE_PATH | ICSettingEntry.RESOLVED)); @@ -720,14 +721,14 @@ public class CDataUtil { return null; } } - + includeList.addAll(excludeList); newEntries = includeList.toArray(new ICSourceEntry[includeList.size()]); } - + return newEntries; } - + private static int includeExclusion(IPath path, List entries){ for(int i = 0; i < entries.size(); i++){ ICSourceEntry entry = entries.get(i); @@ -739,7 +740,7 @@ public class CDataUtil { } return -1; } - + private static ICSourceEntry include(IPath path, ICSourceEntry entry){ IPath[] exclusions = entry.getExclusionPatterns(); IPath entryPath = new Path(entry.getName()); @@ -758,7 +759,7 @@ public class CDataUtil { } return null; } - + private static void sortIncludingExcludingEntries(IPath path, ICSourceEntry[] entries, List including, List excluding){ for(int i = 0; i < entries.length; i++){ IPath entryPath = new Path(entries[i].getName()); @@ -785,7 +786,7 @@ public class CDataUtil { } return new ICSourceEntry[]{entry}; } - + private static ICOutputEntry[] getDefaultOutputEntries(boolean absolute, IProject project){ ICOutputEntry entry; if(absolute){ @@ -802,14 +803,14 @@ public class CDataUtil { public static ICOutputEntry[] adjustEntries(ICOutputEntry entries[], boolean makeAbsolute, IProject project){ if(entries == null || entries.length == 0) return getDefaultOutputEntries(makeAbsolute, project); - + return makeAbsolute ? makeAbsolute(project, entries) : makeRelative(project, entries); } public static ICSourceEntry[] adjustEntries(ICSourceEntry entries[], boolean makeAbsolute, IProject project){ if(entries == null || entries.length == 0) return getDefaultSourceEntries(makeAbsolute, project); - + ICSourceEntry ei, ej; LinkedHashMap> map = new LinkedHashMap>(); for(int i = 0; i < entries.length; i++){ @@ -819,7 +820,7 @@ public class CDataUtil { ej = entries[j]; if(ei == ej) continue; - + IPath ejPath = new Path(ej.getName()); if(!isExcluded(ejPath, ei)){ if(list == null) @@ -827,7 +828,7 @@ public class CDataUtil { list.add(ejPath); } } - + map.put(ei, list); } List resultList = new ArrayList(entries.length); @@ -843,19 +844,28 @@ public class CDataUtil { resultList.add(se); } } - + if(makeAbsolute){ if(project != null) resultList = makeAbsolute(project, resultList); } else { resultList = makeRelative(project, resultList); } - return resultList.toArray(new ICSourceEntry[resultList.size()]); + + ICSourceEntry[] resultArray = resultList.toArray(new ICSourceEntry[resultList.size()]); + Arrays.sort(resultArray, new Comparator() { + @Override + public int compare(ICSourceEntry o1, ICSourceEntry o2) { + return o1.getFullPath().toString().compareTo(o2.getFullPath().toString()); + } + }); + + return resultArray; } private static List makeRelative(IProject project, List list){ int size = list.size(); - + for(int i = 0; i < size; i++){ list.set(i, makeRelative(project, list.get(i))); } @@ -864,7 +874,7 @@ public class CDataUtil { private static List makeAbsolute(IProject project, List list){ int size = list.size(); - + for(int i = 0; i < size; i++){ list.set(i, makeAbsolute(project, list.get(i))); } @@ -919,26 +929,26 @@ public class CDataUtil { public static ICSourceEntry addExcludePaths(ICSourceEntry entry, Collection paths, boolean removePrefix){ IPath entryPath = new Path(entry.getName()); IPath[] oldExclusions = entry.getExclusionPatterns(); -// List newExList = new ArrayList(oldExclusions.length + paths.size()); +// List newExList = new ArrayList(oldExclusions.length + paths.size()); LinkedHashSet newSet = new LinkedHashSet(); if(removePrefix){ removePrefix(entryPath, paths, newSet); } else { newSet.addAll(paths); } - + for(Iterator iter = newSet.iterator(); iter.hasNext();){ IPath path = iter.next(); if(path.segmentCount() == 0) return null; } - + newSet.addAll(Arrays.asList(oldExclusions)); - + IPath[] newExclusions = newSet.toArray(new IPath[newSet.size()]); return new CSourceEntry(entry.getName(), newExclusions, entry.getFlags()); } - + private static void sortEntries(IPath path, boolean byExclude, ICSourceEntry[] entries, List included, List excluded){ for(int i = 0; i < entries.length; i++){ if(byExclude ? isExcluded(path, entries[i]) : !isOnSourceEntry(path, entries[i])){ @@ -950,11 +960,11 @@ public class CDataUtil { } } } - + public static Map fillEntriesMapByNameKey(Map map, ICSettingEntry[] entries){ if(map == null) map = new LinkedHashMap(); - + for(int i = 0; i < entries.length; i++){ ICSettingEntry entry = entries[i]; map.put(new EntryNameKey(entry), entry); @@ -965,14 +975,14 @@ public class CDataUtil { public static Map fillEntriesMapByContentsKey(Map map, ICSettingEntry[] entries){ if(map == null) map = new LinkedHashMap(); - + for(int i = 0; i < entries.length; i++){ ICSettingEntry entry = entries[i]; map.put(new EntryContentsKey(entry), entry); } return map; } - + public static boolean getBoolean(ICStorageElement el, String attr, boolean defaultValue){ if(el != null){ String tmp = el.getAttribute(attr); @@ -1003,14 +1013,14 @@ public class CDataUtil { public static void setInteger(ICStorageElement el, String attr, int value){ el.setAttribute(attr, new Integer(value).toString()); } - + public static ICExclusionPatternPathEntry addRemoveExclusionsToEntry(ICExclusionPatternPathEntry entry, IPath[] paths, boolean add) throws IllegalArgumentException{ if(paths == null || paths.length == 0) return entry; - + Set set = mergeRemovingDups(entry.getExclusionPatterns(), paths, add); IPath exclusions[] = set.toArray(new IPath[set.size()]); - + return (ICExclusionPatternPathEntry)createEntry(entry.getKind(), entry.getName(), null, exclusions, entry.getFlags()); } @@ -1023,7 +1033,7 @@ public class CDataUtil { set.removeAll(Arrays.asList(o2)); return set; } - + public static ICExclusionPatternPathEntry makeAbsolute(IProject project, ICExclusionPatternPathEntry entry, boolean force){ if(!entry.isValueWorkspacePath() && !force) return entry; @@ -1036,14 +1046,14 @@ public class CDataUtil { } return entry; } - + public static ICExclusionPatternPathEntry makeRelative(IProject project, ICExclusionPatternPathEntry entry, boolean force){ if(!entry.isValueWorkspacePath() && !force) return entry; - + IPath path = new Path(entry.getName()); IPath projPath = project.getFullPath(); - + if(path.isAbsolute()){ if(projPath.isPrefixOf(path)) path = path.removeFirstSegments(projPath.segmentCount()).makeRelative(); @@ -1053,22 +1063,22 @@ public class CDataUtil { } return entry; } - + public static ICExclusionPatternPathEntry[] makeRelative(IProject project, ICExclusionPatternPathEntry[] entries, boolean force){ if(entries == null) return null; - + ICExclusionPatternPathEntry[] relEntries = (ICExclusionPatternPathEntry[])Array.newInstance(entries.getClass().getComponentType(), entries.length); for(int i = 0; i < entries.length; i++){ relEntries[i] = makeRelative(project, entries[i], force); } return relEntries; } - + public static ICExclusionPatternPathEntry[] makeAbsolute(IProject project, ICExclusionPatternPathEntry[] entries, boolean force){ if(entries == null) return null; - + ICExclusionPatternPathEntry[] relEntries = (ICExclusionPatternPathEntry[])Array.newInstance(entries.getClass().getComponentType(), entries.length); for(int i = 0; i < entries.length; i++){ relEntries[i] = makeAbsolute(project, entries[i], force);