Skip to content

Commit b79d214

Browse files
committed
Implement documentation-for-tag
1 parent 48eb2c0 commit b79d214

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

semantic-php-test.el

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ buffer."
157157
;; Other tests
158158
(require 'test/class-members)
159159
(require 'test/context)
160+
(require 'test/docs)
160161
(require 'test/includes)
161162
(require 'test/lexer)
162163
(require 'test/local-variables)

semantic-php.el

+49
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,54 @@ file."
225225
(semantic-find-tags-by-class 'include
226226
(semantic-flatten-tags-table table)))
227227

228+
(define-mode-local-override semantic-documentation-for-tag
229+
php-mode (&optional tag nosnarf)
230+
"Find documentation from TAG and return it as a clean string.
231+
232+
TAG might have DOCUMENTATION set in it already. If not, there may be
233+
some documentation in a comment preceding TAG's definition which we
234+
can look for. When appropriate, this can be overridden by a language specific
235+
enhancement.
236+
Optional argument NOSNARF means to only return the lexical analyzer token for it.
237+
If nosnarf if 'lex, then only return the lex token."
238+
(let ((docstring (semantic-tag-get-attribute tag :documentation)))
239+
(unless (stringp docstring)
240+
(setq docstring (semantic-php-doc-snarf-comment-for-tag tag))
241+
(semantic-tag-put-attribute tag :documentation docstring))
242+
docstring))
243+
244+
(defun semantic-php-doc-snarf-comment-for-tag (tag)
245+
"Extract a docstring from the docblock preceding TAG.
246+
247+
semantic-php does not use semantic-doc-snarf-comment-for-tag:
248+
* snarf-comment-for-tag makes assumptions on the character
249+
classes inside the documentation string, this is very error
250+
prone and fails for common cases like when embedding URLs in
251+
the comment
252+
* semantic requires comment-end which is not set by cc-mode or php-mode"
253+
(let ((docblock "")
254+
(docstring ""))
255+
(save-excursion
256+
;; Find the tag.
257+
(semantic-go-to-tag tag)
258+
(beginning-of-line)
259+
(backward-char)
260+
261+
;; Extract the docblock contents.
262+
(when (looking-back "\*/")
263+
(let ((docend (match-beginning 0))
264+
docstart)
265+
(when (search-backward "/\*\*" nil t)
266+
(setq docstart (match-end 0)
267+
docblock (buffer-substring-no-properties docstart docend))))))
268+
269+
;; Split the contents and produce a docstring.
270+
(dolist (line (split-string docblock "\n" t) docstring)
271+
(setq line (string-trim line))
272+
(setq docstring (concat docstring
273+
"\n"
274+
(if (not (string= "*" line))
275+
(string-remove-prefix "* " line)))))))
276+
228277
(provide 'semantic-php)
229278
;;; semantic-php.el ends here

test/docs.el

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
;;; docs.el --- Test documentation for tags
2+
3+
;; Copyright (C) 2014 Joris Steyn
4+
5+
;; Author: Joris Steyn <[email protected]>
6+
7+
;; This file is not part of GNU Emacs.
8+
9+
;; This file is free software; you can redistribute it and/or
10+
;; modify it under the terms of the GNU General Public License
11+
;; as published by the Free Software Foundation; either version 3
12+
;; of the License, or (at your option) any later version.
13+
14+
;; This file is distributed in the hope that it will be useful,
15+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
;; GNU General Public License for more details.
18+
19+
;; You should have received a copy of the GNU General Public License
20+
;; along with this file; if not, write to the Free Software
21+
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22+
;; 02110-1301, USA.
23+
24+
(require 'semantic-php)
25+
(require 'ert)
26+
27+
(ert-deftest semantic-php-test-documentation-for-tag ()
28+
"Test documentation for tag"
29+
(with-test-buffer
30+
"
31+
/**
32+
* Test documentation string.
33+
*
34+
* Spanning multiple lines.
35+
* @see http://example.com
36+
*/
37+
function test() { /**/ }
38+
"
39+
(semantic-fetch-tags)
40+
(search-forward "/**/")
41+
(should (string=
42+
"
43+
Test documentation string.
44+
45+
Spanning multiple lines.
46+
@see http://example.com
47+
"
48+
(semantic-documentation-for-tag)))))
49+
50+
(provide 'test/docs)
51+
;; docs.el ends here

0 commit comments

Comments
 (0)