File: PHPOutlineTree.cpp

package info (click to toggle)
codelite 14.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (164 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download
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
#include "PHPEntityFunction.h"
#include "PHPEntityVariable.h"
#include "PHPOutlineTree.h"
#include "PHPSourceFile.h"
#include "drawingutils.h"
#include "fileutils.h"
#include "globals.h"
#include "navigationmanager.h"
#include <algorithm>
#include <globals.h>
#include <ieditor.h>
#include <imanager.h>
#include <wx/wupdlock.h>

class QItemData : public wxTreeItemData
{
public:
    PHPEntityBase::Ptr_t m_entry;

public:
    QItemData(PHPEntityBase::Ptr_t entry)
        : m_entry(entry)
    {
    }
    virtual ~QItemData() {}
};

PHPOutlineTree::PHPOutlineTree(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style)
    : clThemedTreeCtrl(parent, id, pos, size, style)
{
    SetBitmaps(clGetManager()->GetStdIcons()->GetStandardMimeBitmapListPtr());
}

PHPOutlineTree::~PHPOutlineTree() {}

int PHPOutlineTree::GetImageId(PHPEntityBase::Ptr_t entry)
{
    BitmapLoader* bmpLoader = clGetManager()->GetStdIcons();
    if(entry->Is(kEntityTypeFunction)) {
        PHPEntityFunction* func = entry->Cast<PHPEntityFunction>();

        if(func->HasFlag(kFunc_Private))
            return bmpLoader->GetImageIndex(BitmapLoader::kFunctionPrivate);
        else if(func->HasFlag(kFunc_Protected))
            return bmpLoader->GetImageIndex(BitmapLoader::kFunctionProtected);
        else
            // public
            return bmpLoader->GetImageIndex(BitmapLoader::kFunctionPublic);

    } else if(entry->Is(kEntityTypeVariable)) {
        PHPEntityVariable* var = entry->Cast<PHPEntityVariable>();
        if(!var->IsMember() && !var->IsConst()) {
            // A global variale
            return bmpLoader->GetImageIndex(BitmapLoader::kMemberPublic);

        } else if(var->IsMember()) {
            if(var->HasFlag(kVar_Const)) return bmpLoader->GetImageIndex(BitmapLoader::kConstant); // constant
            // Member
            if(var->HasFlag(kVar_Private))
                return bmpLoader->GetImageIndex(BitmapLoader::kMemberPrivate);
            else if(var->HasFlag(kVar_Protected))
                return bmpLoader->GetImageIndex(BitmapLoader::kMemberProtected);
            else
                return bmpLoader->GetImageIndex(BitmapLoader::kMemberPublic);

        } else if(var->IsConst()) {
            // Constant
            return bmpLoader->GetImageIndex(BitmapLoader::kConstant);
        } else {
            return bmpLoader->GetImageIndex(BitmapLoader::kMemberPublic);
        }

    } else if(entry->Is(kEntityTypeNamespace)) {
        // Namespace
        return bmpLoader->GetImageIndex(BitmapLoader::kNamespace);
    } else if(entry->Is(kEntityTypeClass)) {
        return bmpLoader->GetImageIndex(BitmapLoader::kClass);
    }
    return wxNOT_FOUND; // Unknown
}

void PHPOutlineTree::BuildTree(const wxFileName& filename)
{
    m_filename = filename;
    PHPSourceFile sourceFile(filename, NULL);
    sourceFile.SetParseFunctionBody(false);
    sourceFile.Parse();
    wxWindowUpdateLocker locker(this);
    DeleteAllItems();

    wxTreeItemId root = AddRoot(wxT("Root"));

    // Build the tree view
    BuildTree(root, sourceFile.Namespace());

    if(HasChildren(GetRootItem())) { ExpandAll(); }
}

void PHPOutlineTree::Clear()
{
    wxWindowUpdateLocker locker(this);
    DeleteAllItems();
    m_filename.Clear();
}

void PHPOutlineTree::BuildTree(wxTreeItemId parentTreeItem, PHPEntityBase::Ptr_t entity)
{
    int imgID = GetImageId(entity);
    wxTreeItemId parent = AppendItem(parentTreeItem, entity->GetDisplayName(), imgID, imgID, new QItemData(entity));
    // dont add the children of the function (i.e. function arguments)
    if(entity->Is(kEntityTypeFunction)) return;
    const PHPEntityBase::List_t& children = entity->GetChildren();
    if(!children.empty()) {
        PHPEntityBase::List_t::const_iterator iter = children.begin();
        for(; iter != children.end(); ++iter) {
            BuildTree(parent, *iter);
        }
    }
}

void PHPOutlineTree::ItemSelected(const wxTreeItemId& item, bool focusEditor)
{
    QItemData* itemData = dynamic_cast<QItemData*>(GetItemData(item));
    CHECK_PTR_RET(itemData);

    IEditor* editor = m_manager->GetActiveEditor();
    CHECK_PTR_RET(editor);

    // Define the pattern to search

    editor->FindAndSelect(itemData->m_entry->GetShortName(), itemData->m_entry->GetShortName(),
                          editor->PosFromLine(itemData->m_entry->GetLine()), NavMgr::Get());
    // set the focus to the editor
    if(focusEditor) { CallAfter(&PHPOutlineTree::SetEditorActive, editor); }
}

void PHPOutlineTree::SetEditorActive(IEditor* editor) { editor->SetActive(); }

bool PHPOutlineTree::Select(const wxString& pattern)
{
    wxTreeItemId item = DoFind(pattern, GetRootItem());
    if(item.IsOk()) {
        // select this item
        EnsureVisible(item);
        SelectItem(item);
        return true;
    }
    return false;
}

wxTreeItemId PHPOutlineTree::DoFind(const wxString& pattern, const wxTreeItemId& parent)
{
    if((GetRootItem() != parent) && FileUtils::FuzzyMatch(pattern, GetItemText(parent))) { return parent; }
    if(ItemHasChildren(parent)) {
        wxTreeItemIdValue cookie;
        wxTreeItemId child = GetFirstChild(parent, cookie);
        while(child.IsOk()) {
            wxTreeItemId match = DoFind(pattern, child);
            if(match.IsOk()) { return match; }
            child = GetNextChild(parent, cookie);
        }
    }
    return wxTreeItemId();
}