File: first_subtype.py

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (52 lines) | stat: -rw-r--r-- 1,265 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
"""
This plugin adds a new contextual menu entry which points to the first
subtype of a type. For instance, if you have the following Ada code:

    type T is new Integer;
    type T1 is new T;

and you click on T1, that contextual menu would jump to the declaration
of T.
"""

import GPS
import gps_utils


def get_first_subtype(entity):
    try:
        while True:
            parents = entity.parent_types()
            if not parents:
                return None

            for p in parents:
                if p.is_predefined():
                    return entity

            entity = parents[0]

        return None
    except Exception:
        return None


def has_first_subtype(context):
    try:
        context.first_subtype = get_first_subtype(context.entity())
        return context.first_subtype is not None
    except:
        return False


@gps_utils.interactive(
    name='goto first subtype',
    contextual='Goto first subtype of %e',
    filter=has_first_subtype,
    contextual_ref='goto other file')
def __goto_first_subtype():
    context = GPS.current_context()
    decl = context.first_subtype.declaration()
    buffer = GPS.EditorBuffer.get(decl.file())
    buffer.current_view().goto(
        buffer.at(decl.line(), decl.column()))