From 0cd73885e6279c2a9a477f59eb76095f4e542858 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 23 May 2015 23:34:03 -0400 Subject: [PATCH] Fix git blame syntax highlighting when different commits break up lines Closes #1521 --- CHANGELOG | 1 + Gemfile | 2 +- Gemfile.lock | 4 +-- app/helpers/blob_helper.rb | 10 ++++---- app/views/projects/blame/show.html.haml | 2 +- spec/helpers/blob_helper_spec.rb | 33 +++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 spec/helpers/blob_helper_spec.rb diff --git a/CHANGELOG b/CHANGELOG index 7d7b8e0aef8..b18d50beab6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,7 @@ v 7.12.0 (unreleased) - Disable "New Issue" and "New Merge Request" buttons when features are disabled in project settings (Stan Hu) - Remove Rack Attack monkey patches and bump to version 4.3.0 (Stan Hu) - Fix clone URL losing selection after a single click in Safari and Chrome (Stan Hu) + - Fix git blame syntax highlighting when different commits break up lines (Stan Hu) - Allow to configure location of the `.gitlab_shell_secret` file. (Jakub Jirutka) - Disabled expansion of top/bottom blobs for new file diffs - Update Asciidoctor gem to version 1.5.2. (Jakub Jirutka) diff --git a/Gemfile b/Gemfile index 8eb1f04000c..26981f3e0a3 100644 --- a/Gemfile +++ b/Gemfile @@ -277,4 +277,4 @@ end gem "newrelic_rpm" gem 'octokit', '3.7.0' -gem "rugments" +gem "rugments", "~> 1.0.0.beta7" diff --git a/Gemfile.lock b/Gemfile.lock index 80e4a44c1da..7dbc3b4ffa9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -531,7 +531,7 @@ GEM rubyntlm (0.5.0) rubypants (0.2.0) rugged (0.22.2) - rugments (1.0.0.beta6) + rugments (1.0.0.beta7) safe_yaml (0.9.7) sanitize (2.1.0) nokogiri (>= 1.4.4) @@ -781,7 +781,7 @@ DEPENDENCIES rqrcode-rails3 rspec-rails (= 2.99) rubocop (= 0.28.0) - rugments + rugments (~> 1.0.0.beta7) sanitize (~> 2.0) sass-rails (~> 4.0.2) sdoc 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 %> diff --git a/spec/helpers/blob_helper_spec.rb b/spec/helpers/blob_helper_spec.rb new file mode 100644 index 00000000000..e49e4e6d5d8 --- /dev/null +++ b/spec/helpers/blob_helper_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe BlobHelper do + describe 'highlight' do + let(:blob_name) { 'test.lisp' } + let(:no_context_content) { ":type \"assem\"))" } + let(:blob_content) { "(make-pathname :defaults name\n#{no_context_content}" } + let(:split_content) { blob_content.split("\n") } + + it 'should return plaintext for unknown lexer context' do + result = highlight(blob_name, no_context_content, nowrap: true, continue: false) + expect(result).to eq(':type "assem"))') + end + + it 'should highlight single block' do + expected = %Q[(make-pathname :defaults name +:type "assem"))] + + expect(highlight(blob_name, blob_content, nowrap: true, continue: false)).to eq(expected) + end + + it 'should highlight continued blocks' do + # Both lines have LC1 as ID since formatter doesn't support continue at the moment + expected = [ + '(make-pathname :defaults name', + ':type "assem"))' + ] + + result = split_content.map{ |content| highlight(blob_name, content, nowrap: true, continue: true) } + expect(result).to eq(expected) + end + end +end