]> code.delx.au - gnu-emacs-elpa/blob - packages/ada-mode/ada-fix-error.el
db43473f069f56d9a988fd489872608b79622f80
[gnu-emacs-elpa] / packages / ada-mode / ada-fix-error.el
1 ;;; ada-fix-error.el --- utilities for automatically fixing
2 ;; errors reported by the compiler.
3
4 ;; Copyright (C) 1999-2009, 2012-2013 Free Software Foundation, Inc.
5
6 ;; Author : Stephen Leake <Stephen_Leake@stephe-leake.org>
7 ;; Maintainer : Stephen Leake <Stephen_Leake@stephe-leake.org>
8 ;; Web site : http://www.stephe-leake.org/
9 ;; Keywords : languages ada error
10
11 ;; This file is part of GNU Emacs
12
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;;;; code
28
29 (require 'ada-mode)
30 (require 'compile)
31
32 (eval-when-compile (require 'cl-macs))
33
34 (defcustom ada-fix-sort-context-clause t
35 "*If non-nil, sort context clause when inserting 'with'"
36 :type 'boolean
37 :group 'ada)
38
39 (defvar ada-fix-context-clause nil
40 "Function to return the region containing the context clause for the current buffer.
41 Called with no arguments; return (BEGIN . END). BEGIN and
42 END must be at beginning of line. If there is no context
43 clause, BEGIN = END, at start of compilation unit.")
44
45 (defun ada-fix-context-clause ()
46 (when ada-fix-context-clause
47 (funcall ada-fix-context-clause)))
48
49 (defun ada-fix-insert-unit-name (unit-name)
50 "Insert UNIT-NAME at point and capitalize it."
51 ;; unit-name is normally gotten from a file-name, and is thus all lower-case.
52 (let ((start-point (point))
53 search-bound)
54 (insert unit-name)
55 (setq search-bound (point))
56 (insert " ") ; separate from following words, if any, for ada-case-adjust-identifier
57 (goto-char start-point)
58 (while (search-forward "." search-bound t)
59 (forward-char -1)
60 (ada-case-adjust-identifier)
61 (forward-char 1))
62 (goto-char search-bound)
63 (ada-case-adjust-identifier)
64 (delete-char 1)))
65
66 (defun ada-fix-add-with-clause (package-name)
67 "Add a with_clause for PACKAGE_NAME.
68 If ada-fix-sort-context-clause, sort the context clauses using
69 sort-lines."
70 (let ((context-clause (ada-fix-context-clause)))
71 (when (not context-clause)
72 (error "no compilation unit found"))
73
74 (goto-char (cdr context-clause))
75 (insert "with ")
76 (ada-fix-insert-unit-name package-name)
77 (insert ";\n")
78
79 (when (and (< (car context-clause) (cdr context-clause))
80 ada-fix-sort-context-clause)
81 ;; FIXME (later): this puts "limited with", "private with" at top of list; prefer at bottom
82 (sort-lines nil (car context-clause) (point)))
83 ))
84
85 (defun ada-fix-extend-with-clause (child-name)
86 "Assuming point is in a selected name, just before CHILD-NAME, add or
87 extend a with_clause to include CHILD-NAME . "
88 (let ((parent-name-end (point)))
89 ;; Find the full parent name; skip back to whitespace, then match
90 ;; the name forward.
91 (skip-syntax-backward "w_.")
92 (search-forward-regexp ada-name-regexp parent-name-end)
93 (let ((parent-name (match-string 0))
94 (context-clause (ada-fix-context-clause)))
95 (goto-char (car context-clause))
96 (if (search-forward-regexp (concat "^with " parent-name ";") (cdr context-clause) t)
97 ;; found exisiting 'with' for parent; extend it
98 (progn
99 (forward-char -1) ; skip back over semicolon
100 (insert "." child-name))
101
102 ;; not found; we are in a package body, with_clause for parent is in spec.
103 ;; insert a new one
104 (ada-fix-add-with-clause (concat parent-name "." child-name)))
105 )))
106
107 (defun ada-fix-add-use-type (type)
108 "Insert 'use type' clause for TYPE at start of declarative part for current construct."
109 (ada-goto-declarative-region-start); leaves point after 'is'
110 (newline)
111 (insert "use type " type ";")
112 (newline-and-indent)
113 (forward-line -1)
114 (indent-according-to-mode))
115
116 (defun ada-fix-add-use (package)
117 "Insert 'use' clause for PACKAGE at start of declarative part for current construct."
118 (ada-goto-declarative-region-start); leaves point after 'is'
119 (newline)
120 (insert "use " package ";")
121 (newline-and-indent)
122 (forward-line -1)
123 (indent-according-to-mode))
124
125 (defvar ada-fix-error-hook nil
126 ;; determined by ada_compiler, set by *-select-prj-compiler
127 "Hook to recognize and fix errors.
128 Hook functions are called with three args:
129
130 MSG, the `compilation--message' struct for the current error
131
132 SOURCE-BUFFER, the buffer containing the source to be fixed
133
134 SOURCE-WINDOW, the window displaying SOURCE-BUFFER.
135
136 Point in SOURCE-BUFFER is at error location; point in
137 `compilation-last-buffer' is at MSG location. Focus is in
138 compilation buffer.
139
140 Hook functions should return t if the error is recognized and
141 fixed, leaving point at fix. Otherwise, they should preserve
142 point and return nil.")
143
144 (defun ada-get-compilation-message ()
145 "Get compilation message at point.
146 Compatible with Emacs 23.4 and 24.x."
147 (cl-case emacs-major-version
148 (23 (get-text-property (point) 'message))
149 (24 (get-text-property (point) 'compilation-message))))
150
151 (defun ada-fix-compiler-error ()
152 "Attempt to fix the current compiler error. Leave point at fixed code."
153 (interactive)
154
155 (let ((source-buffer (current-buffer))
156 (source-window (selected-window))
157 (line-move-visual nil)); screws up next-line otherwise
158
159 (with-current-buffer compilation-last-buffer
160 (when (not (ada-get-compilation-message))
161 ;; not clear why this can happens, but it does
162 (compilation-next-error 1))
163 (let ((comp-buf-pt (point))
164 (success
165 (run-hook-with-args-until-success
166 ada-fix-error-hook
167 (compilation-next-error 0)
168 source-buffer
169 source-window)))
170 ;; restore compilation buffer point
171 (set-buffer compilation-last-buffer)
172 (goto-char comp-buf-pt)
173
174 (unless success
175 ;; none of the hooks handled the error
176 (error "error not recognized"))
177 ))))
178
179 (provide 'ada-fix-error)
180 ;; end of file