~valide/+junk/valide-packaging

« back to all changes in this revision

Viewing changes to plugins/completion/valencia/browser.vala

  • Committer: Stéphane Marguet (Stemp)
  • Date: 2010-09-24 06:33:38 UTC
  • Revision ID: smarguet@gmail.com-20100924063338-86ifz4frpi8cwd4k
Move debian dir up

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2009 Yorba Foundation
2
 
 *
3
 
 * This software is licensed under the GNU Lesser General Public License
4
 
 * (version 2.1 or later).  See the COPYING file in this distribution. 
5
 
 */
6
 
 
7
 
using Valencia;
8
 
 
9
 
class SymbolBrowser {
10
 
    weak Instance parent;
11
 
 
12
 
    Gtk.Entry find_entry;
13
 
    ListViewString list;
14
 
    Gtk.VBox symbol_vbox;
15
 
    
16
 
    bool visible;
17
 
 
18
 
    public SymbolBrowser(Instance parent) {
19
 
        this.parent = parent;
20
 
 
21
 
        find_entry = new Gtk.Entry();
22
 
        find_entry.activate += on_entry_activated;
23
 
        find_entry.changed += on_text_changed;
24
 
        find_entry.focus_in_event += on_receive_focus;
25
 
 
26
 
        // A width of 175 pixels is a sane minimum; the user can always expand this to be bigger
27
 
        list = new ListViewString(Gtk.TreeViewColumnSizing.FIXED, 175);
28
 
        list.row_activated += on_list_activated;
29
 
        list.received_focus += on_list_receive_focus;
30
 
 
31
 
        symbol_vbox = new Gtk.VBox(false, 6);
32
 
        symbol_vbox.pack_start(find_entry, false, false, 0);
33
 
        symbol_vbox.pack_start(list.scrolled_window, true, true, 0);
34
 
        symbol_vbox.show_all();
35
 
 
36
 
        weak Gedit.Panel panel = this.parent.window.get_side_panel();
37
 
        panel.add_item_with_stock_icon(symbol_vbox, "Symbols", Gtk.STOCK_FIND);
38
 
        
39
 
        panel.show += on_panel_open;
40
 
        panel.hide += on_panel_hide;
41
 
    }
42
 
 
43
 
    void on_text_changed() {
44
 
        on_update_symbols();
45
 
    }
46
 
 
47
 
    void on_panel_open(Gedit.Panel panel) {
48
 
        visible = true;
49
 
        on_receive_focus();
50
 
    }
51
 
    
52
 
    void on_panel_hide() {
53
 
        visible = false;
54
 
    }
55
 
 
56
 
    void on_list_receive_focus(Gtk.TreePath? path) {
57
 
        on_receive_focus();
58
 
        if (path != null)
59
 
            list.select_path(path);
60
 
    }
61
 
 
62
 
    bool on_receive_focus() {
63
 
        if (parent.active_document_is_valid_vala_file()) {
64
 
            parent.reparse_modified_documents(parent.active_filename());
65
 
            on_update_symbols();
66
 
        }
67
 
        
68
 
        return false;
69
 
    }
70
 
 
71
 
    public static void on_active_tab_changed(Gedit.Window window, Gedit.Tab tab, 
72
 
                                             SymbolBrowser browser) {
73
 
        browser.on_update_symbols();
74
 
    }
75
 
 
76
 
    void on_update_symbols() {
77
 
        string document_path = parent.active_filename();
78
 
        if (document_path == null || !Program.is_vala(document_path))
79
 
            return;
80
 
        
81
 
        Program program = Program.find_containing(document_path);
82
 
 
83
 
        if (program.is_parsing())
84
 
            program.system_parse_complete += update_symbols;
85
 
        else update_symbols();
86
 
    }
87
 
 
88
 
    SourceFile get_current_sourcefile() {
89
 
        string document_path = parent.active_filename();
90
 
        Program program = Program.find_containing(document_path);    
91
 
        SourceFile? sf = program.find_source(document_path);
92
 
        if (sf == null) {
93
 
            Gedit.Document doc = parent.window.get_active_document();
94
 
            program.update(document_path, buffer_contents(doc));
95
 
            sf = program.find_source(document_path);
96
 
        }
97
 
 
98
 
        assert(sf != null);
99
 
        return sf;
100
 
    }
101
 
 
102
 
    void update_symbols() {
103
 
        if (!parent.active_document_is_valid_vala_file()) {
104
 
            list.clear();
105
 
            return;
106
 
        }
107
 
    
108
 
        string text = find_entry.get_text().substring(0);
109
 
        Expression id = new Id(text);
110
 
        SourceFile sf = get_current_sourcefile();
111
 
        SymbolSet symbol_set = sf.resolve_all_locals(id, 0);
112
 
 
113
 
        weak Vala.HashSet<Symbol> symbols = symbol_set.get_symbols();
114
 
        string[] symbol_names;
115
 
        if (symbols != null) {
116
 
            // Insert symbol names into the list in a sorted order
117
 
            symbol_names = new string[symbols.size];
118
 
 
119
 
            int i = 0;
120
 
            foreach (Symbol symbol in symbols)
121
 
                symbol_names[i++] = symbol.name;
122
 
           
123
 
            qsort(symbol_names, symbols.size, sizeof(string), (GLib.CompareFunc) compare_string);
124
 
        } else symbol_names = new string[0];
125
 
        
126
 
        list.collate(symbol_names);
127
 
    }
128
 
 
129
 
    void jump_to_symbol(string symbol_name) {
130
 
        if (!parent.active_document_is_valid_vala_file())
131
 
            return;
132
 
 
133
 
        Expression id = new Id(symbol_name);
134
 
        SourceFile sf = get_current_sourcefile();
135
 
        Symbol? symbol = sf.resolve_local(id, 0);
136
 
        
137
 
        if (symbol == null)
138
 
            return;
139
 
 
140
 
        parent.jump(symbol.source.filename, 
141
 
                    new CharRange(symbol.start, symbol.start + (int) symbol.name.length));
142
 
    }
143
 
 
144
 
    void on_entry_activated() {
145
 
        if (list.size() <= 0)
146
 
            return;
147
 
        list.select_first_cell();
148
 
        on_list_activated();
149
 
    }
150
 
 
151
 
    void on_list_activated() {
152
 
        jump_to_symbol(list.get_selected_item());
153
 
    }
154
 
    
155
 
    public void on_document_saved() {
156
 
        if (visible)
157
 
            on_update_symbols();
158
 
    }
159
 
 
160
 
    public void set_parent_instance_focus() {
161
 
        Gedit.Panel panel = parent.window.get_side_panel();
162
 
        panel.show();
163
 
        
164
 
        panel.activate_item(symbol_vbox);
165
 
        parent.window.set_focus(find_entry);
166
 
    }
167
 
 
168
 
}
169