1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

removed LanguageSettingsProvidersSerializer.buildResourceTree()

This commit is contained in:
Andrew Gvozdev 2012-05-02 14:18:42 -04:00
parent bd0b8b5ea8
commit 95886b4f65
2 changed files with 0 additions and 259 deletions

View file

@ -17,7 +17,6 @@ import java.util.List;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.AbstractExecutableExtensionBase;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.settings.model.CIncludePathEntry;
import org.eclipse.cdt.core.settings.model.CMacroEntry;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
@ -56,7 +55,6 @@ public class LanguageSettingsManagerTests extends BaseTestCase {
private static final String PROVIDER_NAME_2 = "test.provider.2.name";
private static final String CFG_ID = "test.configuration.id";
private static final String LANG_ID = "test.lang.id";
private static final String LANG_CPP = GPPLanguage.ID;
private static final IFile FILE_0 = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path("/project/path0"));
/**
@ -807,156 +805,4 @@ public class LanguageSettingsManagerTests extends BaseTestCase {
assertSame(provider, providers.get(0));
}
/**
* TODO - YAGNI?
*/
public void testBuildResourceTree_FileInFolder() throws Exception {
// sample entries
CMacroEntry entry = new CMacroEntry("MACRO", null, 0);
List<ICLanguageSettingEntry> entries = new ArrayList<ICLanguageSettingEntry>();
entries.add(entry);
// create resources
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
IFile file = ResourceHelper.createFile(project, "file.cpp");
assertNotNull(file);
// create a provider and set the entries
LanguageSettingsSerializableProvider provider = new LanguageSettingsSerializableProvider(PROVIDER_1, PROVIDER_NAME_1);
provider.setSettingEntries(null, file, null, entries);
// build the hierarchy
LanguageSettingsProvidersSerializer.buildResourceTree(provider, null, null, project);
// check that entries go to highest possible level
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file, null));
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, project, null));
}
/**
* TODO - YAGNI?
*/
public void testBuildResourceTree_FileInSubFolder() throws Exception {
// sample entries
CMacroEntry entry = new CMacroEntry("MACRO", null, 0);
List<ICLanguageSettingEntry> entries = new ArrayList<ICLanguageSettingEntry>();
entries.add(entry);
// create resources
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
IFolder folder = ResourceHelper.createFolder(project, "Folder");
IFile file = ResourceHelper.createFile(project, "Folder/file.cpp");
// create a provider and set the entries
LanguageSettingsSerializableProvider provider = new LanguageSettingsSerializableProvider(PROVIDER_1, PROVIDER_NAME_1);
provider.setSettingEntries(null, file, null, entries);
// build the hierarchy
LanguageSettingsProvidersSerializer.buildResourceTree(provider, null, null, project);
// check that entries go to highest possible level
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file, null));
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, folder, null));
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, project, null));
}
/**
* TODO - YAGNI?
*/
public void testBuildResourceTree_TwoSubFolders() throws Exception {
// sample entries
List<ICLanguageSettingEntry> entries1 = new ArrayList<ICLanguageSettingEntry>();
entries1.add(new CMacroEntry("MACRO_1", null, 0));
List<ICLanguageSettingEntry> entries2 = new ArrayList<ICLanguageSettingEntry>();
entries2.add(new CMacroEntry("MACRO_2", null, 0));
// create resources
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
IFolder folder1 = ResourceHelper.createFolder(project, "Folder1");
IFolder folder2 = ResourceHelper.createFolder(project, "Folder2");
IFile file1 = ResourceHelper.createFile(project, "Folder1/file1.cpp");
IFile file2 = ResourceHelper.createFile(project, "Folder2/file2.cpp");
// create a provider and set the entries
LanguageSettingsSerializableProvider provider = new LanguageSettingsSerializableProvider(PROVIDER_1, PROVIDER_NAME_1);
provider.setSettingEntries(null, file1, null, entries1);
provider.setSettingEntries(null, file2, null, entries2);
// build the hierarchy
LanguageSettingsProvidersSerializer.buildResourceTree(provider, null, null, project);
// check that entries go to highest possible level
assertEquals(entries1, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file1, null));
assertEquals(entries1, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, folder1, null));
assertEquals(entries2, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file2, null));
assertEquals(entries2, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, folder2, null));
assertEquals(0, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, project, null).size());
}
/**
* TODO - YAGNI?
*/
public void testBuildResourceTree_FlippingSettings() throws Exception {
// sample entries
List<ICLanguageSettingEntry> entries1 = new ArrayList<ICLanguageSettingEntry>();
entries1.add(new CMacroEntry("MACRO_1", null, 0));
List<ICLanguageSettingEntry> entries2 = new ArrayList<ICLanguageSettingEntry>();
entries2.add(new CMacroEntry("MACRO_2", null, 0));
// create resources
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
IFile file1 = ResourceHelper.createFile(project, "file1.cpp");
IFile file2 = ResourceHelper.createFile(project, "file2.cpp");
IFile file3 = ResourceHelper.createFile(project, "file3.cpp");
// create a provider
LanguageSettingsSerializableProvider provider = new LanguageSettingsSerializableProvider(PROVIDER_1, PROVIDER_NAME_1);
// set the entries for the first 2 files
provider.setSettingEntries(null, file1, null, entries1);
provider.setSettingEntries(null, file2, null, entries1);
// build the hierarchy
LanguageSettingsProvidersSerializer.buildResourceTree(provider, null, null, project);
// double-check where the entries go
assertEquals(entries1, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file1, null));
assertEquals(entries1, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file2, null));
assertEquals(entries1, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, project, null));
// set the entries for the second+third files (second file flips the settings)
provider.setSettingEntries(null, file2, null, entries2);
provider.setSettingEntries(null, file3, null, entries2);
// build the hierarchy
LanguageSettingsProvidersSerializer.buildResourceTree(provider, null, null, project);
// check where the entries go, it should not lose entries for the first file
assertEquals(entries1, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file1, null));
assertEquals(entries2, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file2, null));
assertEquals(entries2, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file3, null));
assertEquals(entries2, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, project, null));
}
/**
* TODO - YAGNI?
*/
public void testBuildResourceTree_WithLanguage() throws Exception {
// sample entries
CMacroEntry entry = new CMacroEntry("MACRO", null, 0);
List<ICLanguageSettingEntry> entries = new ArrayList<ICLanguageSettingEntry>();
entries.add(entry);
// create resources
IProject project = ResourceHelper.createCDTProjectWithConfig(this.getName());
IFolder folder = ResourceHelper.createFolder(project, "Folder");
IFile file = ResourceHelper.createFile(project, "Folder/file.cpp");
// create a provider and set the entries
LanguageSettingsSerializableProvider provider = new LanguageSettingsSerializableProvider(PROVIDER_1, PROVIDER_NAME_1);
provider.setSettingEntries(null, file, LANG_CPP, entries);
// build the hierarchy
LanguageSettingsProvidersSerializer.buildResourceTree(provider, null, LANG_CPP, project);
// check that entries go to highest possible level
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, file, LANG_CPP));
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, folder, LANG_CPP));
assertEquals(entries, LanguageSettingsManager.getSettingEntriesUpResourceTree(provider, null, project, LANG_CPP));
}
}

View file

@ -1636,109 +1636,4 @@ public class LanguageSettingsProvidersSerializer {
return new ArrayList<ILanguageSettingsProvider>(newProviders);
}
/**
* Check if the language is applicable for the file.
*/
private static boolean isLanguageInScope(IResource rc, ICConfigurationDescription cfgDescription, String languageId) {
if (rc instanceof IFile) {
ILanguage lang = null;
try {
lang = LanguageManager.getInstance().getLanguageForFile((IFile) rc, cfgDescription);
} catch (CoreException e) {
CCorePlugin.log("Error while determining language for a file", e); //$NON-NLS-1$
}
if (lang == null || (languageId != null && !languageId.equals(lang.getId()))) {
return false;
}
}
return true;
}
/**
* Builds for the provider a nicer-looking resource tree to present hierarchical view to the user.
*
* Note that it is not advisable to "compact" the tree because of potential loss of information
* which is especially important during partial or incremental builds.
*
* Note also that after using this method for a while for BOP parsers it appears that disadvantages
* outweigh benefits. In particular, it doesn't result in saving memory as the language settings
* (and the lists itself) are not duplicated in memory anyway but optimized with using WeakHashSet
* and SafeStringInterner.
*
* This method is a candidate for removal.
*
* @param provider - language settings provider to build the tree for.
* @param cfgDescription - configuration description.
* @param languageId - language ID.
* @param folder - container where the tree roots.
*/
public static void buildResourceTree(LanguageSettingsSerializableProvider provider,
ICConfigurationDescription cfgDescription, String languageId, IContainer folder) {
IResource[] members = null;
try {
members = folder.members();
} catch (Exception e) {
CCorePlugin.log(e);
}
if (members==null)
return;
for (IResource rc : members) {
if (rc instanceof IContainer) {
buildResourceTree(provider, cfgDescription, languageId, (IContainer) rc);
}
}
int rcNumber = members.length;
Map<List<ICLanguageSettingEntry>, Integer> listMap = new HashMap<List<ICLanguageSettingEntry>, Integer>();
// on the first pass find majority entry list, i.e. list present most often
List<ICLanguageSettingEntry> majorityEntries = null;
List<ICLanguageSettingEntry> candidate = null;
int candidateCount = 0;
for (IResource rc : members) {
if (!isLanguageInScope(rc, cfgDescription, languageId)) {
rcNumber--;
} else {
List<ICLanguageSettingEntry> entries = provider.getSettingEntries(null, rc, languageId);
if (entries==null && rc instanceof IContainer) {
rcNumber--;
} else {
Integer count = listMap.get(entries);
if (count==null) {
count = 0;
}
count++;
if (count>candidateCount) {
candidateCount = count;
candidate = entries;
}
listMap.put(entries, count);
}
}
if (candidateCount > rcNumber/2) {
majorityEntries = candidate;
break;
}
}
if (majorityEntries!=null) {
provider.setSettingEntries(cfgDescription, folder, languageId, majorityEntries);
}
// second pass - assign the entries to the folders
for (IResource rc : members) {
List<ICLanguageSettingEntry> entries = provider.getSettingEntries(null, rc, languageId);
if (entries!=null && entries==majorityEntries) {
if (!(rc instanceof IFile)) { // preserve information which files were collected
provider.setSettingEntries(cfgDescription, rc, languageId, null);
}
}
}
}
}