当我摆弄这个flymake的东西时,我真正的目标是得到一个“快速修复”选项菜单,当flymake显示错误时弹出。如果单击alt-shift-f10或类似的按钮,Visual Studio将执行此操作。
而且,我让它在一些基本场景中发挥作用。
以下是用户体验:
步骤1:使用未解析的类型引用编写代码-在本例中,为stream。Flymake标记问题,如下所示:
alt text http://i41.tinypic.com/8xo29t.jpg
步骤2:通过弹出flymake错误菜单
(flymake-display-err-menu-for-current-line)
alt text http://i40.tinypic.com/mwryw0.jpg
步骤3:选择菜单项,自动应用快速修复。
alt text http://i39.tinypic.com/w1z4n5.jpg
我安排为一些特殊情况提供“快速修复”选项:
-
错误CS0246:找不到类型或命名空间“XXXX”
-
错误CS1002:需要分号
-
错误CS0103:当前上下文中不存在名称“identifier”。
诀窍还是建议。这次在
flymake-make-emacs-menu
FN。Flymake中的函数准备直接传递给
x-popup-menu
. 通知(“after”advice)解析错误列表,查找已知的错误代码,如果找到,“monkey patches”弹出菜单,插入用于修复错误的选项。
;; The flymake-make-emacs-menu function prepares the menu for display in
;; x-popup-menu. But the menu from flymake is really just a static list
;; of errors. Clicking on any of the items, does nothing. This advice
;; re-jiggers the menu structure to add dynamic actions into the menu,
;; for some error cases. For example, with an unrecognized type error
;; (CS0246), this fn injects a submenu item that when clicked, will
;; insert a using statement into the top of the file. Other errors are
;; also handled.
;;
;; This won't work generally. It required some changes to flymake.el,
;; so that flymake-goto-next-error would go to the line AND column. The
;; original flymake only goes to the line, not the column. Therefore,
;; quickfixes like inserting a semicolon or a namespace in front of a
;; typename, won't work because the position is off.
;;
(defadvice flymake-make-emacs-menu (after
flymake-csharp-offer-quickfix-menu
()
activate compile)
(let* ((menu ad-return-value)
(title (car menu))
(items (cadr menu))
action
new-items
)
(setq new-items (mapcar
'(lambda (x)
(let ((msg (car x)) missing-type namespace m2 m3)
(cond ((or (string-match "error CS0246:" msg)
(string-match "error CS0103:" msg))
(progn
(string-match "^\\(.+'\\([^']+\\)'[^(]+\\)" msg)
(setq missing-type (substring msg
(match-beginning 2)
(match-end 2)))
;; trim the message to get rid of the (did you forget to ...?)
(setq msg
(substring msg
(match-beginning 1)
(match-end 1)))
(setq namespace (csharp-get-namespace-for-type missing-type))
(if namespace
;; the namespace was found
(progn
(setq m2 (concat "insert using " namespace ";"))
(setq m3 (concat namespace "." missing-type))
(list msg
(list m2 'csharp-insert-using-clause-for-type missing-type)
(list m3 'csharp-insert-fully-qualified-type namespace)
(list "resolve this type reference manually")))
;; couldn't find the namespace; maybe it's just a typo
(list msg
(list "resolve this type reference manually")))))
;; missing semicolon
((string-match "error CS1002:" msg)
(progn
(list msg
(list "insert ; " 'insert ";"))
))
;; all other cases
(t
;; no quick fixes for this error
(list msg
(list "resolve this error manually"))))))
(cdr items)))
;; If there's only one menu item, it sometimes won't display
;; properly. The main error message is hidden, and the submenu
;; items become the menu items. I don't know why. Appending a list
;; of ("" nil) to the end, insures that the menu will display
;; properly.
(setq new-items (append new-items (list (list "" nil))))
;; finally, set the return value
(setq ad-return-value (cons title new-items))
;; (setq ad-return-value (list title
;; (list "item1" (list "choice 1.A" 1) (list "choice 1.B" 2))
;; (list "item2" (list "choice 2.A" 3) (list "choice 2.B" 4))
;; (list "item3")
;; ))
))
“insert using”修复程序还依赖于查找功能,该功能解析短类型名,例如
Stream
完全限定的类型名,例如
System.IO.Stream
. 这是另外一个问题。
如果用户选择菜单项应用快速修复,则运行fn以插入新的“using”子句:
(defun csharp-insert-using-clause (namespace)
"inserts a new using clause, for the given namespace"
(interactive "sInsert using clause; Namespace: ")
(save-excursion
(let ((beginning-of-last-using (re-search-backward "^[ \t]*using [^ ]+;")))
(end-of-line)
(newline)
(insert (concat "using " namespace ";"))
)
)
)
我认为这可以扩展到处理其他类型错误的快速修复。但我不知道那些容易修复的错误可能是什么。如果有人有任何想法或想帮忙,请告诉我。