Search in sources :

Example 1 with QueryExpression

use of org.eclipse.che.ide.api.project.QueryExpression in project che by eclipse.

the class ResourceManager method search.

protected Promise<Resource[]> search(final Container container, String fileMask, String contentMask) {
    QueryExpression queryExpression = new QueryExpression();
    if (!isNullOrEmpty(contentMask)) {
        queryExpression.setText(contentMask);
    }
    if (!isNullOrEmpty(fileMask)) {
        queryExpression.setName(fileMask);
    }
    if (!container.getLocation().isRoot()) {
        queryExpression.setPath(container.getLocation().toString());
    }
    return ps.search(queryExpression).thenPromise(new Function<List<ItemReference>, Promise<Resource[]>>() {

        @Override
        public Promise<Resource[]> apply(final List<ItemReference> references) throws FunctionException {
            if (references.isEmpty()) {
                return promises.resolve(NO_RESOURCES);
            }
            int maxDepth = 0;
            final Path[] paths = new Path[references.size()];
            for (int i = 0; i < paths.length; i++) {
                final Path path = Path.valueOf(references.get(i).getPath());
                paths[i] = path;
                if (path.segmentCount() > maxDepth) {
                    maxDepth = path.segmentCount();
                }
            }
            return getRemoteResources(container, maxDepth, true).then(new Function<Resource[], Resource[]>() {

                @Override
                public Resource[] apply(Resource[] resources) throws FunctionException {
                    Resource[] filtered = NO_RESOURCES;
                    Path[] mutablePaths = paths;
                    outer: for (Resource resource : resources) {
                        if (resource.getResourceType() != FILE) {
                            continue;
                        }
                        for (int i = 0; i < mutablePaths.length; i++) {
                            Path path = mutablePaths[i];
                            if (path.segmentCount() == resource.getLocation().segmentCount() && path.equals(resource.getLocation())) {
                                Resource[] tmpFiltered = copyOf(filtered, filtered.length + 1);
                                tmpFiltered[filtered.length] = resource;
                                filtered = tmpFiltered;
                                //reduce the size of mutablePaths by removing already checked item
                                int size = mutablePaths.length;
                                int numMoved = mutablePaths.length - i - 1;
                                if (numMoved > 0) {
                                    arraycopy(mutablePaths, i + 1, mutablePaths, i, numMoved);
                                }
                                mutablePaths = copyOf(mutablePaths, --size);
                                continue outer;
                            }
                        }
                    }
                    return filtered;
                }
            });
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) List(java.util.List) ArrayList(java.util.ArrayList) QueryExpression(org.eclipse.che.ide.api.project.QueryExpression)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ItemReference (org.eclipse.che.api.project.shared.dto.ItemReference)1 Function (org.eclipse.che.api.promises.client.Function)1 FunctionException (org.eclipse.che.api.promises.client.FunctionException)1 Promise (org.eclipse.che.api.promises.client.Promise)1 QueryExpression (org.eclipse.che.ide.api.project.QueryExpression)1 Resource (org.eclipse.che.ide.api.resources.Resource)1 Path (org.eclipse.che.ide.resource.Path)1