semantic功能介绍

semantic功能介绍

gnu Semantic Manual

1,代码自动补全

3,代码导航

启动semantic功能:(semantic-mode 1)

1,Semantic mode 是辅助模式,当这个模式启动了,你打开的每个缓冲区是否会被Semantic mode解析,取决于变量【semantic-new-buffer-setup-functions】,在被解析过的缓冲区里就可以使用semantic提供的函数来完成,代码跳转,查询等。

2,当启动Semantic mode 后,会自动启动一些全局的辅助模式,可以用变量【semantic-default-submodes 】,来决定启动哪些辅助模式。默认被启动的全局的辅助模式是【SemanticDB mode】,【 Global Semantic Idle Scheduler mode 】;当然也可以通过执行函数来手动启动这些辅助模式。在【Development】menu下,也可以找到这些辅助模式。

3,变量【semantic-new-buffer-setup-functions】是一个alist,它的默认值如下。也就是说当打开c,c++,java,js等后缀名的文件,semantic会自动解析这些缓冲区。当然可以添加或者删除主编辑模式到这个alist里。

  • c-mode
  • c++-mode
  • html-mode
  • java-mode
  • js-mode
  • python-mode

4,变量【semantic-default-submodes】是一个alist,它的默认值是【SemanticDB mode】,【 Global Semantic Idle Scheduler mode 】。

下面的值也可以添加到semantic-default-submodes】里。

  • global-semanticdb-minor-mode’ - Maintain tag database.

  • global-semantic-idle-scheduler-mode’ - Reparse buffer when idle.

  • global-semantic-idle-summary-mode’ - Show summary of tag at point.

  • global-semantic-idle-completions-mode’ - Show completions when idle.

  • global-semantic-decoration-mode’ - Additional tag decorations.

  • global-semantic-highlight-func-mode’ - Highlight the current tag.

  • global-semantic-stickyfunc-mode’ - Show current fun in header line.

  • global-semantic-mru-bookmark-mode’ - Provide ‘switch-to-buffer’-like

    keybinding for tag names.

  • global-cedet-m3-minor-mode’ - A mouse 3 context menu.

  • global-semantic-idle-local-symbol-highlight-mode’ - Highlight references

    of the symbol under point.

  • global-semantic-highlight-edits-mode’ - Visualize incremental parser by

    highlighting not-yet parsed changes.

  • global-semantic-show-unmatched-syntax-mode’ - Highlight unmatched lexical
    syntax tokens.global-semantic-show-parser-state-mode’ - Display the parser cache state.

Idle Scheduler(global-semantic-idle-scheduler-mode )

这个子模式默认是启动的,作用是:在用户不操作emacs时,会自动重新解析所有需要被重新解析的buffer,如果在重新解析的过程中,有任何用户的输入了,就自动停止解析。

有几个有用的变量:

1,semantic-idle-scheduler-idle-time :默认值是1秒,也就是说当用户1秒没有操作emacs后,就会自动重新解析这个buffer

2,semantic-idle-scheduler-max-buffer-size :可以被重新解析的buffer里面内容byte的最大值,如果超过这个值,就不去重新解析它。目的是,防止某个buffer过大,导致在重新解析它的时间过长,而影响了用户使用emacs的体验。如果是小于或者等于0的话,如果需要被重新解析的话,就无条件的解析。

3,semantic-idle-scheduler-no-working-message :解析时,是否显示工作消息,默认值是nil,不显示。

4,semantic-idle-scheduler-work-idle-time :当重新解析的同时,idle-scheduler还可以做如下的事情。做这些事特别耗时,所以当用户60秒没有操作emacs后,就会做下面的事。此变量的默认值是60秒。

  • Creating the include path caches required for symbol lookup.

    创建 symbol lookup所需要的路径缓存

  • Create data type caches.

    创建数据类型的缓存

  • Saving SemanticDB caches to disk.

    把缓存内容保存到本地磁盘

  • Speculatively parsing the files in the same directory as the current buffer.

    解析和当前buffer在同一目录下的所有文件

5,semantic-idle-work-parse-neighboring-files-flag :控制是否解析和当前buffer在同一目录下的所有文件,默认值是nil,也就是不解析

6,semantic-before-idle-scheduler-reparse-hook :在idle scheduler 开始解析前,运行这个钩子

7,semantic-after-idle-scheduler-reparse-hook :在idle scheduler 解析完成后,运行这个钩子

Idle Summary Mode(global-semantic-idle-summary-mode )

启动这个辅助mode后,当光标移到到某个函数调用的地方,或者变量使用的地方的时候,函数的原型(返回值,参数),变量的类型,会显示在emacs最下面的辅助入力区。如下图,当光标落在printf函数调用的地方时,会显示处printf的原型

1,变量semantic-idle-summary-function 的作用:指定用哪种方式(哪个函数)来显示函数的原型或者变量的定义等,它的默认值:semantic-format-tag-summarize-with-file。下面的函数付给semantic-format-tag-summarize-with-file。下面的函数存放在变量semantic-format-tag-functions 中。

  • semantic-format-tag-name
  • semantic-format-tag-canonical-name
  • semantic-format-tag-abbreviate
  • semantic-format-tag-summarize
  • semantic-format-tag-summarize-with-file
  • semantic-format-tag-short-doc
  • semantic-format-tag-prototype
  • semantic-format-tag-concise-prototype
  • semantic-format-tag-uml-abbreviate
  • semantic-format-tag-uml-prototype
  • semantic-format-tag-uml-concise-prototype
  • semantic-format-tag-prin1

2,变量semantic-idle-summary-out-of-context-faces 的作用:告诉Idle Summary Mode,当光标移动到哪些文本上的时候,不要尝试去显示它的摘要。比如当光标移动到注释位置时,或者双引号里面的字符串的时候,没有必要让Idle Summary Mode执行。这个变量是一个alist,可以添加或者删除元素。它的默认值如下:

  • font-lock-comment-face :注释区域
  • font-lock-string-face :双引号里面的字符串区域
  • font-lock-doc-string-face :注释区域?
  • font-lock-doc-face :注释区域?

Idle Completions Mode(global-semantic-idle-completions-mode )

启动这个辅助mode后,就可以有了代码补齐的功能。但是需要注意:代码补齐是在空间的时候才会生效,如果想立即生效请M-x semantic-complete-analyze-inline-idle

变量【semantic-complete-inline-analyzer-idle-displayor-class 】控制着补齐的样式,补齐的样式有3种:

  • 第一种:Display completions in a tooltip (semantic-displayor-tooltip )

  • 第二种:Display completions “inline” with the buffer text. This is the default value.(semantic-displayor-ghost)

    从下图可以看出来,当输入n后,有4个补齐选项。当补齐选项显示出来后,下面的命令就被激活了。

    • 回车,ctrl-m:Accept the current completion (semantic-complete-inline-done), placing it in the buffer and moving point to the end of the completed tag
    • M-n :Select the next possible completion (semantic-complete-inline-down). The new completion is shown inline, replacing the old completion.
    • M-p :Select the previous possible completion (semantic-complete-inline-up).
    • TAB,ctrl-i:Accept as much of the completion as possible. If no additional completion can be accepted without ambiguity, select the next possible completion (semantic-complete-inline-TAB).
    • ctrl-g :Quit without completing (semantic-complete-inline-quit).

  • 第三种:Display completions in a separate window. (semantic-displayor-traditional )

Semantic Database(global-semanticdb-minor-mode

启动这个辅助mode后,Semantic 会把解析的结果存储到硬盘上(路径: ~/.emacs.d/semanticdb/ ),下次emacs再启动后,还可以继续使用上次的解析结果。

1,变量semanticdb-default-save-directory :控制Semantic Database的存储路径。

2,变量semanticdb-default-file-name :当变量semanticdb-default-save-directory的值是nil时,控制存储解析结果的文件的名字(路径是buffer所在的路径)

3,变量semanticdb-persistent-path :指定哪些路径下的解析结果可以被保存到磁盘。它是一个alist,里面放以字符串的形式放路径。有个特殊的用法,它的值可以是never,always,project 。never:都不保存到磁盘;always:都保存到磁盘;project :变量【semanticdb-project-predicate-functions 】里指定的路径被保存到磁盘。默认值是:always。

4,变量【semanticdb-project-predicate-functions 】:当变量【semanticdb-persistent-path】的值为project 时,它是一个alist,它里面放的路径才会被保存到磁盘。默认值是nil。

Semanticdb Search Configuration

解析时,不只是解析当前buffer,还会自动解析包含的头文件,比如c/c++的头文件。在c/c++的编辑模式中,它会根据头文件是【""】还是【<>】来区分是系统头文件,还是自己项目里定义的头文件。如果是系统头文件,则去系统目录去找。

变量【semanticdb-find-default-throttle 】决定了,怎么去搜索。默认是以时间为代价,去搜索到最大的准确性。

  • local:查找当前buffer所在目录下的所有文件

  • project:查找自己指定的路径。用变量【semanticdb-project-roots 】来控制具体的路径信息。它是一个alist。假如使用了boost,则可以把boost的头文件所在的路径。

    变量【semanticdb-project-root-functions 】是一个alist,里面放的是自己定义的函数,由一个参数,返回值必须是一个project-root或者nil,或者是a list of string( multiple project roots, for complex systems),函数的返回值覆盖变量【semanticdb-project-roots 】。

    用途:估计是根据参数,来动态决定【semanticdb-project-roots 】。

  • unloaded:如果要的解析结果没加载到内存,则加载到内存,如果还没解析,则马上解析。

  • system:查找系统的头文件。

    • 函数semantic-add-system-include :添加路径到系统路径
    • 函数semantic-remove-system-include :从系统路径里,删除某个路径。
    • 函数semantic-customize-system-include-path :让当前buffer里的主mode,使用系统路径。
  • recursive:递归查找头文件里由include了别的头文件。

变量【semanticdb-find-default-throttle 】的默认值:(local project unloaded system recursive)

Smart Jump

函数semantic-ia-fast-jump :跳到变量或者函数定义的地方

怎么跳回来呢??为了跳回去,要使用函数semantic-mrub-switch-tags(c-x B),但老是报出【Semantic Bookmark ring is currently empty】错误。原因是函数semantic-ia-fast-jump调用了函数push-mark,而函数push-mark里面没有做push bookmark的操作,所以要修改函数push-mark,把bookmark放入semantic-mru-bookmark-ring里。放入后再执行semantic-mrub-switch-tags就不会出错了。

注意:执行函数semantic-mrub-switch-tags时,可以指定要跳过去的tags,每次执行semantic-ia-fast-jump后,都把位置push到了变量semantic-mru-bookmark-ring里了,所以执行semantic-mrub-switch-tags时,可以指定这些被push进去的位置

(global-set-key [f12] 'semantic-ia-fast-jump)

(defadvice push-mark (around semantic-mru-bookmark activate)
  "Push a mark at LOCATION with NOMSG and ACTIVATE passed to `push-mark'.
If `semantic-mru-bookmark-mode' is active, also push a tag onto
the mru bookmark stack."
  (semantic-mrub-push semantic-mru-bookmark-ring
                      (point)
                      'mark)
  ad-do-it)

;;跳转后再跳回原来的地方
(global-set-key [f11]
      (lambda()
    (interactive)
    (if (ring-empty-p (oref semantic-mru-bookmark-ring ring))
        (error "Semantic Bookmark ring is currently empty"))
    (let* ((ring (oref semantic-mru-bookmark-ring ring))
           (alist (semantic-mrub-ring-to-assoc-list ring))
           (first (cdr (car alist))))
    (if (semantic-equivalent-tag-p (oref first tag)
                       (semantic-current-tag))
        (setq first (cdr (car (cdr alist)))))
    (semantic-mrub-switch-tags first))))

Smart Completion

1,函数semantic-complete-analyze-inline :也是自动补齐,那么和Idle Completions Mode里的自动补齐有什么区别呢?Idle Completions Mode是在空闲时间内执行的自动补齐,而这个是立即启动自动补齐。绑定键:【C-c , 空格】。

补齐方式,也是由变量【semantic-complete-inline-analyzer-idle-displayor-class 】来控制。

Smart Summary

1,函数semantic-ia-show-summary :功能和Idle Summary Mode一样,显示函数,变量的信息。

2,函数semantic-ia-show-doc :显示函数,变量的注释信息。

3,函数semantic-ia-describe-class :显示结构体,类里面的成员信息。

4,当发生不能解析的情况,参考Debugging the Semantic Analyzer

speedbar

让speedbar使用由semantic解析出来的tags,semantic解析出来的tags里的信息比speedbar自己的tags要详细

使用semantic后speedbar的效果如下,比不用semantic多出了【variables】【functions】【misc】,详细了好多哦。

Sticky Function mode

启动(M-x global-semantic-stickyfunc-mode)这个模式后,在emacs最上面一行显示如下图:

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

2085156153.png)

原文地址:https://www.cnblogs.com/xiaoshiwang/p/11899111.html