File: highlight.py

package info (click to toggle)
gnat-gps 4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 49,096 kB
  • ctags: 20,461
  • sloc: ada: 274,120; ansic: 154,849; python: 9,890; tcl: 9,812; sh: 8,192; xml: 7,970; cpp: 4,737; yacc: 3,520; makefile: 2,136; lex: 2,043; java: 1,638; perl: 302; awk: 265; sed: 161; asm: 14; fortran: 2; lisp: 1
file content (54 lines) | stat: -rw-r--r-- 1,657 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
# This script provides a new toolbar button to highlight Ada specific
# entities, like types and packages.
# This is just a quick example, and will not do the highlighting on the
# fly. Instead, you'll need to press the button regularly to recompute
# the highlighting

from GPS import *

parse_xml ("""<action name="Highlight Ada Types">
    <filter id="Source editor" />
    <shell lang="python">highlight.highlight_ada_types()</shell>
 </action>
 <button action="Highlight Ada Types">
    <title>Highlight Ada Types</title>
 </button>""")


overlay = None

def highlight_ada_types():
   global overlay
   buffer = EditorBuffer.get ()
   file = buffer.file()

   # Remove old highlights
   if overlay:
      loc = buffer.beginning_of_buffer ()
      is_on = loc.has_overlay (overlay)
      end = buffer.end_of_buffer ()
      while loc < end:
        loc2 = loc.forward_overlay (overlay)
        if is_on:
           buffer.remove_overlay (overlay, loc, loc2)
        is_on = not is_on
        loc = loc2

   # Create the overlay if it doesn't exist
   if not overlay:
      overlay = buffer.create_overlay ("Ada types")
      overlay.set_property ("foreground", "magenta") 

   # Highlight again
   for fileloc in file.search \
    ("(type|package)(\s+body)?\s+(\S+)\s+is", regexp=True, scope="code"):
      loc = EditorLocation (buffer, fileloc.line(), fileloc.column())

      if loc.get_char() == 't': loc = loc + 5
      else:                     loc = loc + 7

      loc2 = loc.forward_word()
      while loc2.get_char() == '_' or loc2.get_char() == '.':
         loc2 = (loc2 + 1).forward_word()
      buffer.apply_overlay (overlay, loc, loc2)