/usr/share/gps/plug-ins/expanded_code.py is in gnat-gps-common 5.0-16.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | """This file provides support for displaying Ada expanded code as generated by
GNAT (-gnatGL switch).
"""
import os, os.path, re, string, distutils.dep_util
import GPS
from gps_utils import *
def create_dg (f, str):
res=file (f, 'wb')
first = str.find ("\n", str.find ("\n", str.find ("Source recreated from tree"))+1)+2
if first > 2:
last = str.find ("Source recreated from tree", first)
res.write (str [first:last-1])
res.close()
expanded_code_marks = {}
# A dictionary that associates a source filename with a list of marks
highlighting = ""
def subprogram_bounds (cursor):
"""Return the first and last line of the current subprogram, and (0,0) if
the current subprogram could not be determined."""
blocks = {"CAT_PROCEDURE":1, "CAT_FUNCTION":1, "CAT_ENTRY":1,
"CAT_PROTECTED":1, "CAT_TASK":1, "CAT_PACKAGE":1}
if cursor.block_type() == "CAT_UNKNOWN":
return (0,0)
min = cursor.buffer().beginning_of_buffer()
while not blocks.has_key (cursor.block_type()) and cursor > min:
cursor = cursor.block_start() - 1
if cursor > min: return (cursor.block_start_line(), cursor.block_end_line())
else: return (0,0)
def clear_dg (source_filename):
""" Clear dg information for filename """
global expanded_code_marks
if expanded_code_marks.has_key (source_filename):
# Remove special lines
srcbuf = GPS.EditorBuffer.get (GPS.File (source_filename))
for (mark, lines) in expanded_code_marks [source_filename]:
srcbuf.remove_special_lines (mark, lines)
# Empty entry in the dictionary
expanded_code_marks [source_filename] = []
def edit_dg (dg, source_filename, line, for_subprogram, in_external_editor):
global highlighting, expanded_code_marks
# If we are showing the dg in an external editor, simply open this editor and
# jump to the line
if in_external_editor:
buf = GPS.EditorBuffer.get (GPS.File (dg))
loc = GPS.EditorLocation (buf, 1, 1)
try:
(frm, to) = loc.search ("^-- " + `line` + ":", regexp=True)
if frm:
buf.current_view().goto (frm.forward_line (1))
except:
pass
return
# If the highlighting category does not exist, register it now
if highlighting == "":
highlighting = "expanded"
Editor.register_highlighting (highlighting, "#dddddd", False)
clear_dg (source_filename)
srcbuf = GPS.EditorBuffer.get (GPS.File (source_filename))
if for_subprogram:
(block_first, block_last) = subprogram_bounds (
srcbuf.current_view().cursor())
else:
(block_first, block_last) = (0, 0)
# Read the text of the dg file
f=open (dg)
txt=f.read()
f.close()
current_code = ""
current_line = 1
lines = 0
for line in txt.split ("\n"):
if line.startswith ("-- "):
if current_code != "":
if (block_first == 0
or (current_line > block_first and current_line < block_last)):
mark = srcbuf.add_special_line (current_line + 1,
current_code,
highlighting, "")
# Add mark to the list of marks
if expanded_code_marks.has_key (source_filename):
expanded_code_marks [source_filename] += [(mark, lines)]
else:
expanded_code_marks [source_filename] = [(mark, lines)]
current_line = int (line [3:line.find(":")])
current_code = ""
else:
if line != "":
lines = lines + 1
if current_code == "":
current_code = line
else:
current_code = current_code + "\n" + line
def on_exit (process, status, full_output):
create_dg (process.dg, full_output)
edit_dg (process.dg, process.source_filename,
process.line, process.for_subprogram, process.in_external_editor)
def show_gnatdg(for_subprogram = False, in_external_editor = False):
"""Show the .dg file of the current file"""
GPS.MDI.save_all (False)
context = GPS.current_context()
file = context.file().name()
line = context.location().line()
if context.project():
l = context.project().object_dirs (False)
prj = " -P" + GPS.Project.root().file().name()
else:
l = GPS.Project.root().object_dirs (False)
prj = " -a"
if l:
objdir = l[0]
else:
objdir = GPS.get_tmp_dir()
GPS.Console ("Messages").write (
"Could not find an object directory for %s, reverting to %s" %
(file, objdir))
dg = os.path.join (objdir, os.path.basename (file)) + '.dg'
if distutils.dep_util.newer (file, dg):
gnatmake = GPS.Project.root().get_attribute_as_string ("compiler_command",
package="ide", index="ada")
cmd = gnatmake + " -q" + prj + \
" -f -c -u -gnatcdx -gnatws -gnatGL " + file
GPS.Console ("Messages").write ("Generating " + dg + "...\n")
proc = GPS.Process (cmd, on_exit=on_exit)
proc.source_filename = file
proc.dg = dg
proc.line = line
proc.for_subprogram = for_subprogram
proc.in_external_editor = in_external_editor
else:
edit_dg (dg, file, line, for_subprogram, in_external_editor)
#################################
# Register the contextual menus #
#################################
@interactive ("Ada", in_ada_file, contextual="Expanded code/Show subprogram",
name="show expanded code for subprogram", before="Align")
def show_gnatdg_subprogram():
"""Show the expanded code of the current subprogram"""
show_gnatdg(True)
@interactive ("Ada", in_ada_file, contextual="Expanded code/Show entire file",
name="show expanded code for file", before="Align")
def show_gnatdg_file():
"""Show the .dg file of the current file"""
show_gnatdg(False)
@interactive ("Ada", in_ada_file, contextual="Expanded code/Show in separate editor",
name="show expanded code in separate editor", before="Align")
def show_gnatdg_separate_editor():
"""Show the expanded code of the current subprogram"""
show_gnatdg(False, True)
@interactive ("Ada", in_ada_file, contextual="Expanded code/Clear",
name="clear expanded code", before="Align")
def clear_expanded_code():
"""Show the expanded code of the current subprogram"""
context = GPS.current_context()
clear_dg (context.file().name())
|