diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-05-28 09:40:00 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-05-28 09:40:00 +0000 |
commit | 06250eef2e12ed509b88f3770ae6d3fa4614b74d (patch) | |
tree | bfc65a0467e8f5122c84f9881ef58ffceddaf022 /app | |
parent | 5322099464ffa5f39076678f648243168ccdc506 (diff) | |
parent | 0cd73885e6279c2a9a477f59eb76095f4e542858 (diff) | |
download | gitlab-ce-06250eef2e12ed509b88f3770ae6d3fa4614b74d.tar.gz |
Merge branch 'fix-git-blame-syntax-highlighting' into 'master'
Fix git blame syntax highlighting when different commits break up lines
### What does this MR do?
This MR fixes a bug where syntax highlighting would not work properly in certain lines when doing a `git blame` view of a file. Also, this MR reuses the Rugments lexer and formatter for each `git blame` commit block, which should make things faster and reduce overhead.
### Why was this MR needed?
When doing a `git blame`, GitLab feeds the Rugments lexer/formatter blocks of code based on the latest commit for each line. However, this can cause lexer to fail because the state is cleared between each block of code, which causes `highlight` to fallback to the plaintext lexer. For example, notice this unhighlighted line in [this file](https://gitlab.common-lisp.net/cmucl/cmucl/blame/master/src/assembly/assemfile.lisp):
![image](https://gitlab.com/gitlab-org/gitlab-ce/uploads/b403fe5c2b883882a9eeea7e0409c583/image.png)
The fixed version looks like this:
![image](https://gitlab.com/stanhu/gitlab-ce/uploads/32c4d7c8ff15a7d59b364dd988f7c657/image.png)
This MR requires a patch to rugments to allow the `continue` option to be passed to the lexer.
* https://github.com/rumpelsepp/rugments/pull/24
* https://github.com/rumpelsepp/rugments/pull/23
* Also submitted to rouge: https://github.com/jneen/rouge/pull/267
### What are the relevant issue numbers?
Closes #1521
See merge request !697
Diffstat (limited to 'app')
-rw-r--r-- | app/helpers/blob_helper.rb | 10 | ||||
-rw-r--r-- | app/views/projects/blame/show.html.haml | 2 |
2 files changed, 6 insertions, 6 deletions
diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index 9fe5f82f02f..50df3801703 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -1,6 +1,6 @@ module BlobHelper - def highlight(blob_name, blob_content, nowrap = false) - formatter = Rugments::Formatters::HTML.new( + def highlight(blob_name, blob_content, nowrap: false, continue: false) + @formatter ||= Rugments::Formatters::HTML.new( nowrap: nowrap, cssclass: 'code highlight', lineanchors: true, @@ -8,11 +8,11 @@ module BlobHelper ) begin - lexer = Rugments::Lexer.guess(filename: blob_name, source: blob_content) - result = formatter.format(lexer.lex(blob_content)).html_safe + @lexer ||= Rugments::Lexer.guess(filename: blob_name, source: blob_content).new + result = @formatter.format(@lexer.lex(blob_content, continue: continue)).html_safe rescue lexer = Rugments::Lexers::PlainText - result = formatter.format(lexer.lex(blob_content)).html_safe + result = @formatter.format(lexer.lex(blob_content)).html_safe end result diff --git a/app/views/projects/blame/show.html.haml b/app/views/projects/blame/show.html.haml index 462f5b7afb0..8019c7f4569 100644 --- a/app/views/projects/blame/show.html.haml +++ b/app/views/projects/blame/show.html.haml @@ -32,5 +32,5 @@ %code :erb <% lines.each do |line| %> - <%= highlight(@blob.name, line, true).html_safe %> + <%= highlight(@blob.name, line, nowrap: true, continue: true).html_safe %> <% end %> |