File: autoformat.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 (34 lines) | stat: -rw-r--r-- 1,042 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
"""This plugin will automatically reformat a source file each time it is
saved on disk. See also autognatpp.py for another version using the external
gnatpp pretty-printer instead.
"""

import GPS

# Actions to be performed each time a file is saved


def on_file_saved(hook, file):
    buf = GPS.EditorBuffer.get()
    if buf.file().language() == "python":
        # Deactivate on Python files: the formatting action
        # indents the entire selection - this is intended for user
        # selection, but it is not suitable to do this automatically.
        return

    # Save the cursor location
    view = buf.current_view()
    cursor = view.cursor().create_mark()

    # Select the whole buffer
    buf.select(buf.beginning_of_buffer(), buf.end_of_buffer())

    # Reformat the buffer
    GPS.execute_action("Format Selection")

    # Restore the cursor location
    view.goto(cursor.location())
    view.center(view.cursor())

# Register the callback on the "before_file_saved" hook
GPS.Hook("before_file_saved").add(on_file_saved)