~valide/+junk/valide-packaging

« back to all changes in this revision

Viewing changes to plugins/completion/completion.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
 
/* completion.vala
2
 
 *
3
 
 * Copyright (C) 2008-2010 Nicolas Joseph
4
 
 *
5
 
 * This program is free software: you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation, either version 3 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 *
18
 
 * Author:
19
 
 *      Nicolas Joseph <nicolas.joseph@valaide.org>
20
 
 */
21
 
 
22
 
using Valide;
23
 
 
24
 
public class Completion : Plugin, Object
25
 
{
26
 
  private uint ui_id;
27
 
  private SourceBuffer buffer;
28
 
  private Gtk.ActionGroup action_group = null;
29
 
 
30
 
  const Gtk.ActionEntry[] entries = {
31
 
    { "search-goto-definition", null, "Go to Definition", "<ctrl>D",
32
 
      "Goto the definition of the current symbol", on_goto_definition },
33
 
    { "search-autocomplete", null, "_AutoComplete", "<ctrl>space",
34
 
      "Display method or symbol information", on_autocomplete }
35
 
  };
36
 
 
37
 
  const string ui = """
38
 
      <ui>
39
 
        <menubar name="menubar">
40
 
          <menu action="search">
41
 
            <placeholder name="search-ops-3">
42
 
              <menuitem action="search-goto-definition"/>
43
 
              <separator/>
44
 
              <menuitem action="search-autocomplete"/>
45
 
            </placeholder>
46
 
          </menu>
47
 
        </menubar>
48
 
      </ui>""";
49
 
 
50
 
  /**
51
 
   * @see Valide.Plugin.path
52
 
   */
53
 
  public string path { get; construct set; }
54
 
  /**
55
 
   * @see Valide.Plugin.window
56
 
   */
57
 
  public Window window { get; construct set; }
58
 
 
59
 
  private void get_buffer_info (SourceBuffer buffer, out int pos,
60
 
                                out string contents)
61
 
  {
62
 
    Gtk.TextIter iter;
63
 
 
64
 
    contents = buffer.get_buffer_contents ();
65
 
    this.buffer.get_iter_at_mark (out iter, this.buffer.get_insert ());
66
 
    pos = iter.get_offset ();
67
 
  }
68
 
 
69
 
  private void jump_to_symbol_definition ()
70
 
  {
71
 
    int pos;
72
 
    string source;
73
 
    Valencia.ScanInfo info;
74
 
 
75
 
    this.get_buffer_info (this.buffer, out pos, out source);
76
 
    info = Valencia.ScanInfo.get_scan_info (this.buffer, false);
77
 
    if (info != null && info.inner () != null)
78
 
    {
79
 
      Valencia.Symbol? sym;
80
 
      Valencia.SourceFile sf;
81
 
      Valencia.Program program;
82
 
 
83
 
      program = Valencia.Program.find_containing (this.buffer.path);
84
 
      sf = program.find_source (this.buffer.path);
85
 
      sym = sf.resolve (info.inner (), pos, false);
86
 
      if (sym != null)
87
 
      {
88
 
        try
89
 
        {
90
 
          Document document;
91
 
 
92
 
          document = this.window.documents.create (sym.source.filename);
93
 
          document.select_offsets (sym.start, sym.start + sym.name_length ());
94
 
        }
95
 
        catch (Error e)
96
 
        {
97
 
          warning (e.message);
98
 
        }
99
 
      }
100
 
    }
101
 
  }
102
 
 
103
 
  private void on_goto_definition ()
104
 
  {
105
 
    string filename;
106
 
    Document document;
107
 
    SourceView source_view;
108
 
 
109
 
    document = this.window.documents.current;
110
 
    source_view = document.text_view;
111
 
    filename = source_view.buffer.path;
112
 
 
113
 
    if (filename != null && Valencia.Program.is_vala (filename))
114
 
    {
115
 
      Valencia.Program program;
116
 
 
117
 
      program = Valencia.Program.find_containing (filename, true);
118
 
      this.buffer = document.buffer;
119
 
      if (program.is_parsing ())
120
 
      {
121
 
        program.system_parse_complete.connect (this.jump_to_symbol_definition);
122
 
      }
123
 
      else
124
 
      {
125
 
        this.jump_to_symbol_definition ();
126
 
      }
127
 
    }
128
 
  }
129
 
 
130
 
  private void on_autocomplete ()
131
 
  {
132
 
    Gtk.bindings_activate (this.window.get_focus (), Gdk.KeySyms.space,
133
 
                           Gdk.ModifierType.CONTROL_MASK);
134
 
  }
135
 
 
136
 
  private void setup_ui (DocumentManager sender, Document? document)
137
 
  {
138
 
    bool active = false;
139
 
 
140
 
    if (this.action_group == null)
141
 
    {
142
 
      Gtk.UIManager ui_manager;
143
 
 
144
 
      ui_manager = this.window.ui_manager;
145
 
      this.action_group = new Gtk.ActionGroup ("completion");
146
 
      this.action_group.add_actions (this.entries, this);
147
 
      ui_manager.insert_action_group (this.action_group, 0);
148
 
      try
149
 
      {
150
 
        this.ui_id = ui_manager.add_ui_from_string (ui, -1);
151
 
      }
152
 
      catch (Error e)
153
 
      {
154
 
        debug (e.message);
155
 
      }
156
 
    }
157
 
 
158
 
    if (sender.current != null)
159
 
    {
160
 
      active = true;
161
 
    }
162
 
    this.action_group.get_action ("search-goto-definition").sensitive = active;
163
 
    this.action_group.get_action ("search-autocomplete").sensitive = active;
164
 
  }
165
 
 
166
 
  private void on_tab_added (DocumentManager sender, Document document)
167
 
  {
168
 
    try
169
 
    {
170
 
      string filename;
171
 
      Gtk.SourceView source_view;
172
 
      Gtk.SourceCompletion completion;
173
 
 
174
 
      filename = document.path;
175
 
      source_view = document.split_view.active_view;
176
 
      completion = source_view.get_completion ();
177
 
      completion.show_headers = false;
178
 
      completion.remember_info_visibility = true;
179
 
      completion.add_provider ((Gtk.SourceCompletionProvider)new ValenciaProvider (document.buffer));
180
 
 
181
 
      this.setup_ui (this.window.documents, null);
182
 
    }
183
 
    catch (Error e)
184
 
    {
185
 
      debug (e.message);
186
 
    }
187
 
  }
188
 
 
189
 
  construct
190
 
  {
191
 
    this.window.documents.tab_added.connect (this.on_tab_added);
192
 
    this.window.documents.tab_removed.connect (this.setup_ui);
193
 
    this.setup_ui (this.window.documents, null);
194
 
  }
195
 
 
196
 
  ~Completion ()
197
 
  {
198
 
    this.window.ui_manager.remove_ui (this.ui_id);
199
 
  }
200
 
}
201
 
 
202
 
public Type register_plugin (TypeModule module)
203
 
{
204
 
  return typeof (Completion);
205
 
}