summaryrefslogtreecommitdiff
path: root/plugins/xpath/xpath-view/src/org/intellij/plugins/xpathView/search/SearchScope.java
blob: 4c42e86f5f58d46d8b2d7f2fbb88ca8406e92d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright 2006 Sascha Weinreuter
 *
 * 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 org.intellij.plugins.xpathView.search;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderEnumerator;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.*;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiSearchScopeUtil;
import com.intellij.util.Processor;
import gnu.trove.THashSet;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;

public class SearchScope implements JDOMExternalizable {

    public enum ScopeType {
        PROJECT,MODULE,DIRECTORY,CUSTOM
    }

    private ScopeType myScopeType;
    private String myModuleName;
    private String myPath;
    private boolean myRecursive;
    private String myScopeName;

    private com.intellij.psi.search.SearchScope myCustomScope;

    public SearchScope() {
        myScopeType = ScopeType.PROJECT;
        myRecursive = true;
    }

    public SearchScope(SearchScope scope) {
        myScopeType = scope.getScopeType();

        myModuleName = scope.getModuleName();
        myPath = scope.getPath();
        myRecursive = scope.isRecursive();
        myScopeName = scope.getScopeName();
    }

    public SearchScope(ScopeType scopeType, String directoryName, boolean recursive, String moduleName, String scopeName) {
        myScopeType = scopeType;
        myPath = directoryName;
        myRecursive = recursive;
        myModuleName = moduleName;
        myScopeName = scopeName;
    }

    public void setCustomScope(com.intellij.psi.search.SearchScope customScope) {
        myCustomScope = customScope;
    }

  @NotNull
    public String getName() {
        switch (getScopeType()) {
            case PROJECT:
                return "Project";
            case MODULE:
                return "Module '" + getModuleName() + "'";
            case DIRECTORY:
                return "Directory '" + getPath() + "'";
            case CUSTOM:
                return "Scope '" + getScopeName() + "'";
        }
        assert false;
        return null;
    }

    @NotNull
    public ScopeType getScopeType() {
        return myScopeType;
    }

    public String getModuleName() {
        return myModuleName;
    }

    @Nullable
    public String getScopeName() {
        return myScopeName;
    }

    @Nullable
    public String getPath() {
        return myPath;
    }

    public boolean isRecursive() {
        return myRecursive;
    }

    public void readExternal(Element element) throws InvalidDataException {
        myScopeType = ScopeType.valueOf(element.getAttributeValue("type"));

        if (myScopeType == ScopeType.MODULE) {
            final Element m = element.getChild("module");
            if (m != null) {
                myModuleName = m.getTextTrim();
            }
        } else if (myScopeType == ScopeType.DIRECTORY) {
            final Element path = element.getChild("path");
            if (path != null) {
                myPath = path.getTextTrim();
                myRecursive = "true".equals(path.getAttributeValue("recursive"));
            }
        } else if (myScopeType == ScopeType.CUSTOM) {
            myScopeName = element.getAttributeValue("scope-name");
        }
    }

    public void writeExternal(Element element) throws WriteExternalException {

        final ScopeType scopeType = getScopeType();
        element.setAttribute("type", scopeType.toString());

        if (scopeType == ScopeType.MODULE) {
            if (myModuleName != null) {
                final Element m = new Element("module");
                element.addContent(m);
                m.setText(myModuleName);
            }
        } else if (scopeType == ScopeType.DIRECTORY) {
            final Element p = new Element("path");
            element.addContent(p);
            p.setAttribute("recursive", Boolean.toString(myRecursive));
            if (myPath != null) {
                p.setText(myPath);
            }
        } else if (scopeType == ScopeType.CUSTOM) {
            if (myScopeName != null) {
                element.setAttribute("scope-name", myScopeName);
            }
        }
    }

    public boolean isValid() {
        final String dirName = getPath();
        final String moduleName = getModuleName();

        switch (getScopeType()) {
          case MODULE:
            return moduleName != null && moduleName.length() > 0;
          case DIRECTORY:
            return dirName != null && dirName.length() > 0 && findFile(dirName) != null;
          case CUSTOM:
            return myCustomScope != null;
          case PROJECT:
            return true;
      }
      return false;
    }


    public void iterateContent(@NotNull final Project project, final Processor<VirtualFile> processor) {

        switch (getScopeType()) {
            case PROJECT:
                //noinspection unchecked
                ProjectRootManager.getInstance(project).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
                break;
            case MODULE:
                final Module module = ModuleManager.getInstance(project).findModuleByName(getModuleName());
                //noinspection unchecked
                ModuleRootManager.getInstance(module).getFileIndex().iterateContent(new MyFileIterator(processor, Conditions.<VirtualFile>alwaysTrue()));
                break;
            case DIRECTORY:
                final String dirName = getPath();
                assert dirName != null;

                final VirtualFile virtualFile = findFile(dirName);
                if (virtualFile != null) {
                    iterateRecursively(virtualFile, processor, isRecursive());
                }
                break;
            case CUSTOM:
                assert myCustomScope != null;

                final ContentIterator iterator;
                if (myCustomScope instanceof GlobalSearchScope) {
                    final GlobalSearchScope searchScope = (GlobalSearchScope)myCustomScope;
                    iterator = new MyFileIterator(processor, new Condition<VirtualFile>() {
                        public boolean value(VirtualFile virtualFile) {
                            return searchScope.contains(virtualFile);
                        }
                    });
                    if (searchScope.isSearchInLibraries()) {
                        final OrderEnumerator enumerator = OrderEnumerator.orderEntries(project).withoutModuleSourceEntries().withoutDepModules();
                        final Collection<VirtualFile> libraryFiles = new THashSet<VirtualFile>();
                        Collections.addAll(libraryFiles, enumerator.getClassesRoots());
                        Collections.addAll(libraryFiles, enumerator.getSourceRoots());
                        final Processor<VirtualFile> adapter = new Processor<VirtualFile>() {
                            public boolean process(VirtualFile virtualFile) {
                                return iterator.processFile(virtualFile);
                            }
                        };
                        for (final VirtualFile file : libraryFiles) {
                            iterateRecursively(file, adapter, true);
                        }
                    }
                } else {
                    final PsiManager manager = PsiManager.getInstance(project);
                    iterator = new MyFileIterator(processor, new Condition<VirtualFile>() {
                        public boolean value(VirtualFile virtualFile) {
                            final PsiFile element = manager.findFile(virtualFile);
                            return element != null && PsiSearchScopeUtil.isInScope(myCustomScope, element);
                        }
                    });
                }

                ProjectRootManager.getInstance(project).getFileIndex().iterateContent(iterator);
        }
    }

    @Nullable
    private static VirtualFile findFile(String dirName) {
        return LocalFileSystem.getInstance().findFileByPath(dirName.replace('\\', '/'));
    }

    private static void iterateRecursively(VirtualFile virtualFile, final Processor<VirtualFile> processor, boolean recursive) {
        VfsUtilCore.visitChildrenRecursively(virtualFile, new VirtualFileVisitor(recursive ? null : VirtualFileVisitor.ONE_LEVEL_DEEP) {
            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
                if (!file.isDirectory()) {
                    processor.process(file);
                }
                return true;
            }
        });
    }

    private static class MyFileIterator implements ContentIterator {
        private final Processor<VirtualFile> myProcessor;
        private final Condition<VirtualFile> myCondition;

        public MyFileIterator(Processor<VirtualFile> processor, Condition<VirtualFile> condition) {
            myCondition = condition;
            myProcessor = processor;
        }

        public boolean processFile(VirtualFile fileOrDir) {
            if (!fileOrDir.isDirectory() && myCondition.value(fileOrDir)) {
                myProcessor.process(fileOrDir);
            }
            return true;
        }
    }
}