PHP Provide statistics about file types (WI-7202)
authorNikolay Matveev <Nikolay.Matveev@jetbrains.com>
Wed, 17 Aug 2011 13:05:38 +0000 (17:05 +0400)
committerNikolay Matveev <Nikolay.Matveev@jetbrains.com>
Wed, 17 Aug 2011 13:12:45 +0000 (17:12 +0400)
platform/lang-impl/src/com/intellij/openapi/fileTypes/FileTypeUsagesCollector.java [new file with mode: 0644]
platform/lang-impl/testSrc/com/intellij/openapi/fileTypes/FileTypeUsagesCollectorTest.java [new file with mode: 0644]
platform/platform-resources/src/META-INF/LangExtensions.xml

diff --git a/platform/lang-impl/src/com/intellij/openapi/fileTypes/FileTypeUsagesCollector.java b/platform/lang-impl/src/com/intellij/openapi/fileTypes/FileTypeUsagesCollector.java
new file mode 100644 (file)
index 0000000..a29639c
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.openapi.fileTypes;
+
+import com.intellij.internal.statistic.AbstractApplicationUsagesCollector;
+import com.intellij.internal.statistic.beans.GroupDescriptor;
+import com.intellij.internal.statistic.beans.UsageDescriptor;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.psi.search.FileTypeIndex;
+import com.intellij.psi.search.GlobalSearchScope;
+import com.intellij.util.NotNullFunction;
+import com.intellij.util.containers.ContainerUtil;
+import com.intellij.util.indexing.FileBasedIndex;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Nikolay Matveev
+ */
+public class FileTypeUsagesCollector extends AbstractApplicationUsagesCollector {
+
+  private static final String GROUP_ID = "file-type";
+
+  @NotNull
+  @Override
+  public GroupDescriptor getGroupId() {
+    return GroupDescriptor.create(GROUP_ID);
+  }
+
+  @NotNull
+  @Override
+  public Set<UsageDescriptor> getProjectUsages(@NotNull Project project) {
+    final Set<FileType> usedFileTypes = new HashSet<FileType>();
+    final FileType[] registeredFileTypes = FileTypeManager.getInstance().getRegisteredFileTypes();
+    for (final FileType fileType : registeredFileTypes) {
+      FileBasedIndex.getInstance().processValues(
+        FileTypeIndex.NAME,
+        fileType,
+        null,
+        new FileBasedIndex.ValueProcessor<Void>() {
+          @Override
+          public boolean process(VirtualFile file, Void value) {
+            usedFileTypes.add(fileType);
+            return false;
+          }
+        }, GlobalSearchScope.projectScope(project));
+    }
+    usedFileTypes.add(UnknownFileType.INSTANCE);
+    return ContainerUtil.map2Set(usedFileTypes, new NotNullFunction<FileType, UsageDescriptor>() {
+      @NotNull
+      @Override
+      public UsageDescriptor fun(FileType fileType) {
+        return new UsageDescriptor(fileType.getName(), 1);
+      }
+    });
+  }
+}
diff --git a/platform/lang-impl/testSrc/com/intellij/openapi/fileTypes/FileTypeUsagesCollectorTest.java b/platform/lang-impl/testSrc/com/intellij/openapi/fileTypes/FileTypeUsagesCollectorTest.java
new file mode 100644 (file)
index 0000000..3e5e853
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2000-2011 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.openapi.fileTypes;
+
+import com.intellij.internal.statistic.beans.UsageDescriptor;
+import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
+import com.intellij.util.NotNullFunction;
+import com.intellij.util.containers.ContainerUtil;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * @author Nikolay Matveev
+ */
+public class FileTypeUsagesCollectorTest extends LightPlatformCodeInsightFixtureTestCase {
+
+  private void doTest(@NotNull Collection<FileType> fileTypes) {
+    final Set<UsageDescriptor> usages = new FileTypeUsagesCollector().getProjectUsages(getProject());
+    for (UsageDescriptor usage : usages) {
+      assertEquals(1, usage.getValue());
+    }
+    assertEquals(
+      ContainerUtil.map2Set(fileTypes, new NotNullFunction<FileType, String>() {
+        @NotNull
+        @Override
+        public String fun(FileType fileType) {
+          return fileType.getName();
+        }
+      }),
+      ContainerUtil.map2Set(usages, new NotNullFunction<UsageDescriptor, String>() {
+        @NotNull
+        @Override
+        public String fun(UsageDescriptor usageDescriptor) {
+          return usageDescriptor.getKey();
+        }
+      })
+    );
+  }
+
+  public void testEmptyProject() {
+    doTest(Arrays.asList(UnknownFileType.INSTANCE));
+  }
+
+  public void testSingleFileProject() {
+    myFixture.configureByText("a.txt", "");
+    doTest(Arrays.asList(UnknownFileType.INSTANCE, PlainTextFileType.INSTANCE));
+  }
+
+  public void testSeveralSameFilesProject() {
+    myFixture.configureByText("a.txt", "");
+    myFixture.configureByText("b.txt", "");
+    doTest(Arrays.asList(UnknownFileType.INSTANCE, PlainTextFileType.INSTANCE));
+  }
+}
index a374f757c574d36b870068fb8c23008a5983270f..1e6a9135d6d13980f98dd670b5ef968800040ba0 100644 (file)
   <statistics.usagesCollector  implementation="com.intellij.facet.impl.statistics.FrameworkUsagesCollector"/>
   <statistics.usagesCollector  implementation="com.intellij.facet.impl.statistics.ModuleTypeUsagesCollector"/>
   <statistics.usagesCollector implementation="com.intellij.execution.impl.statistics.RunConfigurationTypeUsagesCollector"/>
+  <statistics.usagesCollector implementation="com.intellij.openapi.fileTypes.FileTypeUsagesCollector"/>
 
   <renamePsiElementProcessor implementation="com.intellij.refactoring.rename.RenamePsiFileProcessor" order="last"/>
   <renameInputValidator implementation="com.intellij.refactoring.rename.RenameToIgnoredDirectoryFileInputValidator" order="last"/>