File: listvars.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 (125 lines) | stat: -rw-r--r-- 4,054 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
"""List all variables referenced in the selected subprogram

This script adds a contextual menu when you click on a subprogram.
The submenu will list the first reference to all variables (local or global)
used in the selected subprogram.
"""

############################################################################
# No user customization below this line
############################################################################

import GPS
from gi.repository import GLib
import gps_utils
import six


def list_vars(subprogram, global_only=False):
    """List all variables referenced by the subprogram.
       subprogram is an instance of GPS.Entity"""

    try:
        locFile = subprogram.body().file()
        locFrom = subprogram.body().line()
        locTo = subprogram.end_of_scope().line()
    except:
        return

    category = "variables referenced in " + subprogram.full_name()
    if global_only:
        category = "non local " + category

    GPS.Locations.remove_category(category)
    added = False
    highlight = ""

    # to enable colors:
    # highlight = "light green"
    # Editor.register_highlighting ("var refs", "light green")

    for e in locFile.entities(local=False):
        if not e.is_type():
            found = False
            refs = e.references(
                include_implicit=True,
                in_file=locFile,
                show_kind=True)
            for loc, kind in six.iteritems (refs):
                if not found \
                   and loc.file() == locFile \
                   and loc.line() >= locFrom \
                   and loc.line() <= locTo:

                    decl = e.declaration()

                    if decl.file() != locFile \
                       or decl.line() < locFrom \
                       or decl.line() > locTo:
                        GPS.Locations.add(
                            category=category,
                            file=loc.file(),
                            line=loc.line(),
                            column=loc.column(),
                            message="%s (decl: %s) %s" % (
                                e.full_name(), e.declaration(), kind),
                            highlight=highlight,
                            length=0)
                        added = True
                    elif not global_only:
                        GPS.Locations.add(
                            category=category,
                            file=loc.file(),
                            line=loc.line(),
                            column=loc.column(),
                            message="%s %s" % (e.name(), kind),
                            highlight=highlight,
                            length=0)
                        added = True
                    found = True

    if added:
        GPS.MDI.get("Locations").raise_window()


def on_filter(context):
    return (context.entity_name() is not None and
            context.entity() and context.entity().is_subprogram())


def on_label(context):
    entity = context.entity()
    if entity:
        return "References/Variables used in <b>" + \
            GLib.markup_escape_text(entity.name()) + "</b>"
    else:
        return ""


def on_global_label(context):
    entity = context.entity()
    if entity:
        return "References/Non local variables used in <b>" + \
            GLib.markup_escape_text(entity.name()) + "</b>"
    else:
        return ""


@gps_utils.interactive(
    name='Variables referenced',
    contextual=on_label,
    filter=on_filter,
    static_path="References/Variables used in")
def __list_local_vars():
    """List all variables referenced by the subprogram."""
    list_vars(GPS.current_context().entity(), True)


@gps_utils.interactive(
    name='Non local Variables referenced',
    contextual=on_global_label,
    filter=on_filter,
    static_path="References/Non local variables used in")
def __list_global_vars():
    """List all non local variables referenced by the subprogram."""
    list_vars(GPS.current_context().entity(), False)