2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.psi.stubs;
18 import com.intellij.openapi.project.Project;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import com.intellij.psi.search.GlobalSearchScope;
22 import com.intellij.util.indexing.*;
23 import com.intellij.util.io.EnumeratorStringDescriptor;
24 import com.intellij.util.io.KeyDescriptor;
25 import com.jetbrains.python.psi.search.PyProjectScopeBuilder;
26 import org.jetbrains.annotations.NotNull;
28 import java.util.Collection;
29 import java.util.HashMap;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
37 public class PySetuptoolsNamespaceIndex extends ScalarIndexExtension<String> {
38 public static final ID<String, Void> NAME = ID.create("Py.setuptools.namespace");
39 private static final Pattern RE_NAMESPACE = Pattern.compile("sys\\.modules\\.setdefault\\('([^']*)'");
40 private static final String NAMESPACE_FILE_SUFFIX = "-nspkg.pth";
42 private final EnumeratorStringDescriptor myKeyDescriptor = new EnumeratorStringDescriptor();
44 private final DataIndexer<String, Void, FileContent> myDataIndexer = new DataIndexer<String, Void, FileContent>() {
47 public Map<String, Void> map(@NotNull FileContent inputData) {
48 final CharSequence content = inputData.getContentAsText();
49 final Matcher matcher = RE_NAMESPACE.matcher(content);
50 final Map<String, Void> results = new HashMap<String, Void>();
51 while (matcher.find()) {
52 final String packageName = matcher.group(1);
53 results.put(packageName, null);
59 private FileBasedIndex.InputFilter myInputFilter = new FileBasedIndex.InputFilter() {
61 public boolean acceptInput(@NotNull VirtualFile file) {
62 return StringUtil.endsWith(file.getNameSequence(), NAMESPACE_FILE_SUFFIX);
68 public ID<String, Void> getName() {
74 public DataIndexer<String, Void, FileContent> getIndexer() {
80 public KeyDescriptor<String> getKeyDescriptor() {
81 return myKeyDescriptor;
86 public FileBasedIndex.InputFilter getInputFilter() {
91 public boolean dependsOnFileContent() {
96 public int getVersion() {
101 public static Collection<VirtualFile> find(@NotNull String name, @NotNull Project project) {
102 final GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(project);
103 return FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);