Skip to content

Instantly share code, notes, and snippets.

@sanryuu
Created December 5, 2013 17:33
Show Gist options
  • Save sanryuu/7809739 to your computer and use it in GitHub Desktop.
Save sanryuu/7809739 to your computer and use it in GitHub Desktop.
reference-user-comment.el ユーザ定義した関数の上のコメントをポップアップで表示します(PHPのみ対応)
;; reference-user-comment.el
;(global-set-key "\C-c\C-r" 'ruc-reference-document)
(require 'popup)
(require 'gtags)
(defun ruc-reference-document ()
"This function display document comment
from user defined function
@return (popup) document"
(interactive)
(let (current-point func-name defined-files selected)
(setq current-point (point))
(setq func-name (gtags-current-token))
(setq defined-files (ruc-find-tag-list func-name))
(goto-char current-point)
(when (not (equal nil defined-files))
(setq selected (popup-menu* defined-files))
)
(popup-tip
(ruc-replace-comment-symbol
(ruc-get-document-from-file selected func-name))
:scroll-bar t :margin t)
))
(defun ruc-get-document-from-file (file function)
"get document function explain from file
return: string document, or nil"
(let (point-define
point-plural-comment-begin
point-plural-comment-end
point-oneline-comment-begin
point-oneline-comment-end
point-comment-begin
point-comment-end)
(with-temp-buffer
(insert-file-contents file)
(goto-char (point-max))
;; 関数の定義位置の取得
(re-search-backward
(format "function.*%s *(" function)
nil t nil)
(setq point-define (point))
;; /* */形式の一番近いコメント終了位置
(if (not (equal nil (re-search-backward "\\*/" nil t nil)))
(setq point-plural-comment-end (+ (point) 2)) ;"*/"の分だけ戻る
(setq point-plural-comment-end (point-min)))
;; /* */形式の一番近いコメント開始位置
(if (not (equal nil (re-search-backward "/\\*" nil t nil)))
(setq point-plural-comment-begin (point))
(setq point-plural-comment-end (point-min)))
;; //形式の一番近いコメント終了位置
(goto-char point-define)
(cond ((not (equal nil (re-search-backward "//" nil t nil)))
(re-search-forward "\n" nil t nil)
(forward-char -1) ;; "\n"の分だけ戻る
(setq point-oneline-comment-end (point))
;; //形式の一番近いコメント開始位置
(while (ruc-include-oneline-comment-p
(buffer-substring-line (line-number-at-pos)
(+ 1 (line-number-at-pos))))
(previous-line))
(re-search-forward "//" nil t nil)
(setq point-oneline-comment-begin (- (point) 2)))
(t
(setq point-oneline-comment-end (point-min)))
)
;; /* */形式と//形式 の定義位置から近い方を開始位置に
(cond ((< point-plural-comment-end point-oneline-comment-end)
(setq point-comment-begin point-oneline-comment-begin)
(setq point-comment-end point-oneline-comment-end))
(t
(setq point-comment-begin point-plural-comment-begin)
(setq point-comment-end point-plural-comment-end)))
;; 関数定義の上に2行以上差があった場合には、ドキュメントとみなさない。
(if (>= 2 (- (count-lines (point-max) point-comment-end)
(count-lines (point-max) point-define)))
(setq document-doc
(buffer-substring-no-properties
point-comment-begin point-comment-end))
(setq document-doc "No Document")))))
(defun ruc-find-tag-list (function-name)
"find function define file list.
Using global command.
return: (list) file-name
(list \"sample1.php\" \"sample2.php\")"
(interactive)
(setq global-command-result
(shell-command-to-string
(format "global %s" function-name)))
(setq finded-tag-lists '())
(when (not (equal "" global-command-result))
(when (not (string-match "GTAGS not found." global-command-result))
(setq finded-tag-lists
(split-string
(substring global-command-result 0 -1)
"\n"))
)
)
finded-tag-lists)
;; 行数指定でバッファの部分文字列を取得する
(defun buffer-substring-line (begin-line end-line)
"buffer-substring at line designation
return: (string) buffer substring from line number
begin-line: begin line numberfor substring
end-line: end line number for substring
use: (buffer-substring-line 1 2)"
(interactive)
(let (current-point current-line-point upper-line-point buffer-string)
(setq current-point (point))
(goto-line begin-line)
(setq current-line-point (point))
(goto-line end-line)
(setq upper-line-point (- (point) 1))
(setq buffer-string (buffer-substring-no-properties
current-line-point upper-line-point))
(goto-char current-point)
buffer-string
))
;; コメント識別記号の削除
(defun ruc-replace-comment-symbol (taget)
"replace comment symbol from string
taget (string) replace-taget
return: params"
(interactive)
(replace-regexp-in-string "^\s*/?\\**/?\n??" "" taget))
(defun ruc-include-oneline-comment-p (string)
"judge oneline comment is included
return bool
use: (ruc-include-oneline-comment-p \" // \") ;=> t
use: (ruc-include-oneline-comment-p \" abcd \") ;=> nil"
(not (equal nil (string-match "//" string))))
(provide 'reference-user-comment)
@pogin503
Copy link

https://gist.github.com/pogin503/8516977
ヘッダ情報とフッタ情報付け加えてみたんですけど、どうでしょうか。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment