增量式forward-line,放置overlays (semantic highlighting)会导致显示异常

cquery.el semantic highlighting 问题

  1. 安装cquery.el,参照wiki/Emacs,需要编译cquery server。用git版吧,Melpa版本不知道更新有多频繁
  2. (setq cquery-sem-highlight-method 'overlay)
  3. 打开一个 .cc 文件,M-x lsp-cquery-enable

新打开文件或保存时cquery server会发送 $cquery/publishSemanticHighlighting,即一系列区间和符号信息。cquery.el 会对于每个区间,用 overlay 贴上色块。原来采取的方法是:

for each range
  (goto-char (point-min))
  (forward-line start-line)  (forward-char start-col) 设置start-point
  (goto-char (point-min))
  (forward-line end-line)  (forward-char end-col) 设置end-point
  放置一个overlay覆盖 start-point 和 end-point 

goto-char (point-min)次数太多,光标移动幅度大,效率很低,因此 `cquery--publish-semantic-highlighting' is very slow · Issue #7 · cquery-project/emacs-cquery · GitHub @yyjjl 对区间起点排序后用了一个增量式 forward-line 的方法。代码见 Revert "Improve performance of cquery--publish-semantic-highlighting … · cquery-project/[email protected] · GitHub 被revert的部分。这里摘录部分:

                (setq ranges	
                      (sort ranges (lambda (x y) (< (caar x) (caar y)))))	
                (ignore-errors	
                  (save-excursion	
                    (goto-char (point-min))	
                    (cl-loop	
                     for (start end face) in ranges do	
                     (forward-line (- (car start) last-line-number))	
                     (forward-char (cdr start))	
                     ;; start of range	
                     (setq range-start (point))	
                     (setq last-line-number (car start))	
                     (save-excursion	
                       (forward-line (- (car end) (car start)))	
                       (forward-char (cdr end))	
                       ;; end of range	
                       (setq range-end (point)))	
                     (cquery--make-sem-highlight (cons range-start range-end) buffer face)	
                     )))))))))))

然而这样做,某些.cc文件会显示异常,例如如图的indexer.h

如果用原来的愚蠢办法则显示正常。forward-line 有什么奇怪特性吗?

issue 见 `cquery--publish-semantic-highlighting' is very slow · Issue #7 · cquery-project/emacs-cquery · GitHub

1 个赞

我试了 feature/noverlay 的 branch, 但是好像没出现图中的问题 (没有太多编辑 indexer.h ,只看了看打开之后的高亮情况)

已經 revert 了。revert 前即使這部分不出問題,之後也會。我試過 master 和 feature/noverlay 都會顯示異常