File: navigation_utils.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 (70 lines) | stat: -rw-r--r-- 2,553 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
"""This module implements high level actions related to source navigation

This script defines a number of functions and GPS actions that you can
reuse in your own scripts.
In particular, it provides the following GPS actions, to which you can
bind key shortcuts through the menu /Edit/Key shortcuts:
  - "goto declaration or body"
  - "goto other file"
"""

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

import GPS
import re
from gps_utils import interactive

# ??? At the moment, this is ada-specific, and ad-hoc. We should use
# the GPS engine to get that sort of functionality with any language.

subprograms_re = re.compile(
    "^([ \t]*)(procedure|function) ([a-zA-Z0-9_]+)", re.IGNORECASE)


def __find_subprogram_decl():
    """ Return the subprogram declaration closest to the cursor. This returns
        a (MatchObject, line) tuple for the regexp subprograms_re """
    f = GPS.current_context().file().path
    line = GPS.current_context().location().line()
    while line > 0:
        match = re.search(subprograms_re, GPS.Editor.get_chars(f, line, 1))
        if match is not None:
            return (match, line)
        line = line - 1
    return (None, 0)


@interactive("Editor", "Source editor", name="goto declaration or body")
def goto_declaration_body():
    """
    Jump to the declaration of the current entity. If the cursor
    is already on the declaration, jump to the body/implementation
    of the entity instead.
    """
    current_file = GPS.current_context().file()
    current_line = GPS.current_context().location().line()

    try:
        entity = GPS.current_context().entity()

        decl = entity.declaration().file()
        decl_line = entity.declaration().line()

        GPS.Editor.mark_current_location()
        if current_file == decl and current_line == decl_line:
            body = entity.body().file()
            body_line = entity.body().line()

            GPS.Editor.edit(body.name(),
                            line=body_line,
                            column=entity.body().column())
        else:
            GPS.Editor.edit(decl.name(),
                            line=decl_line,
                            column=entity.declaration().column())
        GPS.Editor.mark_current_location()
    except Exception:
        print ("Not found %s:%s" % (current_file.path, current_line))
        GPS.Editor.edit(current_file.other_file().path)