File: projectfilter.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (80 lines) | stat: -rw-r--r-- 2,190 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "projectfilter.h"

#include <interfaces/iproject.h>

#include <QFile>

using namespace KDevelop;

ProjectFilter::ProjectFilter( const IProject* const project, const QVector<Filter>& filters )
    : m_filters( filters )
    , m_projectFile( project->projectFile() )
    , m_project( project->path() )
{

}

ProjectFilter::~ProjectFilter()
{

}

bool ProjectFilter::isValid( const Path &path, const bool isFolder ) const
{
    if (!isFolder && path == m_projectFile) {
        // do not show the project file
        ///TODO: enable again once the project page is ready for consumption
        return false;
    } else if (isFolder && path == m_project) {
        // always show the project root
        return true;
    }

    if (isFolder && path.isLocalFile() && QFile::exists(path.toLocalFile() + QLatin1String("/.kdev_ignore"))) {
        return false;
    }

    // from here on the user can configure what he wants to see or not.

    // we operate on the path relative to the project base
    // by prepending a slash we can filter hidden files with the pattern "*/.*"

    const QString relativePath = makeRelative(path);

    if (isFolder && relativePath.endsWith(QLatin1String("/.kdev4"))) {
        return false;
    }

    bool isValid = true;
    for (const Filter& filter : m_filters) {
        if (isFolder && !(filter.targets & Filter::Folders)) {
            continue;
        } else if (!isFolder && !(filter.targets & Filter::Files)) {
            continue;
        }
        if ((!isValid && filter.type == Filter::Inclusive) || (isValid && filter.type == Filter::Exclusive)) {
            const bool match = filter.pattern.exactMatch( relativePath );
            if (filter.type == Filter::Inclusive) {
                isValid = match;
            } else {
                isValid = !match;
            }
        }
    }
    return isValid;
}

QString ProjectFilter::makeRelative(const Path& path) const
{
    if (!m_project.isParentOf(path)) {
        return path.path();
    }

    return QLatin1Char('/') + m_project.relativePath(path);
}