Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tatuya Kamada
gitlab-ce
Commits
3fbcf52e
Commit
3fbcf52e
authored
Dec 31, 2015
by
Rubén Dávila
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply syntax highlighting when expanding diff plus some refactor. #3945
parent
fd100e1e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
27 deletions
+74
-27
app/controllers/projects/blob_controller.rb
app/controllers/projects/blob_controller.rb
+1
-1
app/views/projects/blob/diff.html.haml
app/views/projects/blob/diff.html.haml
+1
-1
lib/gitlab/diff/highlight.rb
lib/gitlab/diff/highlight.rb
+43
-12
spec/lib/gitlab/diff/highlight_spec.rb
spec/lib/gitlab/diff/highlight_spec.rb
+29
-13
No files found.
app/controllers/projects/blob_controller.rb
View file @
3fbcf52e
...
@@ -66,7 +66,7 @@ class Projects::BlobController < Projects::ApplicationController
...
@@ -66,7 +66,7 @@ class Projects::BlobController < Projects::ApplicationController
def
diff
def
diff
@form
=
UnfoldForm
.
new
(
params
)
@form
=
UnfoldForm
.
new
(
params
)
@lines
=
@blob
.
data
.
lines
[
@form
.
since
-
1
..
@form
.
to
-
1
]
@lines
=
Gitlab
::
Diff
::
Highlight
.
process_diff_lines
(
@blob
.
name
,
@blob
.
data
.
lines
[
@form
.
since
-
1
..
@form
.
to
-
1
])
if
@form
.
bottom?
if
@form
.
bottom?
@match_line
=
''
@match_line
=
''
...
...
app/views/projects/blob/diff.html.haml
View file @
3fbcf52e
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
%td
.old_line.diff-line-num
{
data:
{
linenumber:
line_old
}}
%td
.old_line.diff-line-num
{
data:
{
linenumber:
line_old
}}
=
link_to
raw
(
line_old
),
"#"
=
link_to
raw
(
line_old
),
"#"
%td
.new_line
=
link_to
raw
(
line_new
)
,
"#"
%td
.new_line
=
link_to
raw
(
line_new
)
,
"#"
%td
.line_content.noteable_line
=
' '
*
@form
.
indent
+
line
%td
.line_content.noteable_line
=
raw
(
"
#{
' '
*
@form
.
indent
}#{
line
}
"
)
-
if
@form
.
unfold?
&&
@form
.
bottom?
&&
@form
.
to
<
@blob
.
loc
-
if
@form
.
unfold?
&&
@form
.
bottom?
&&
@form
.
to
<
@blob
.
loc
%tr
.line_holder
{
id:
@form
.
to
}
%tr
.line_holder
{
id:
@form
.
to
}
...
...
lib/gitlab/diff/highlight.rb
View file @
3fbcf52e
module
Gitlab
module
Gitlab
module
Diff
module
Diff
class
Highlight
class
Highlight
def
self
.
process_diff_lines
(
file_name
,
diff_lines
)
# Apply syntax highlight to provided source code
processor
=
new
(
file_name
,
diff_lines
)
#
# file_name - The file name related to the code.
# lines - It can be an Array of Gitlab::Diff::Line objects or simple Strings.
# When passing Strings you need to provide the required 'end of lines'
# chars ("\n") for each String given that we don't append them automatically.
#
# Returns an Array with the processed items.
def
self
.
process_diff_lines
(
file_name
,
lines
)
processor
=
new
(
file_name
,
lines
)
processor
.
highlight
processor
.
highlight
end
end
def
initialize
(
file_name
,
diff_lines
)
def
initialize
(
file_name
,
lines
)
text_lines
=
diff_lines
.
map
(
&
:text
)
@file_name
=
file_name
@file_name
=
file_name
@lines
=
lines
@diff_lines
=
diff_lines
@diff_line_prefixes
=
text_lines
.
map
{
|
line
|
line
.
sub!
(
/\A((\+|\-)\s*)/
,
''
);
$1
}
@raw_lines
=
text_lines
.
join
(
"
\n
"
)
end
end
def
highlight
def
highlight
@code
=
unescape_html
(
@raw_lines
)
return
[]
if
@lines
.
empty?
extract_line_prefixes
@code
=
unescape_html
(
raw_content
)
@highlighted_code
=
formatter
.
format
(
lexer
.
lex
(
@code
))
@highlighted_code
=
formatter
.
format
(
lexer
.
lex
(
@code
))
update_diff_
lines
is_diff_line?
?
update_diff_lines
:
@highlighted_code
.
lines
end
end
private
private
def
is_diff_line?
@lines
.
first
.
is_a?
(
Gitlab
::
Diff
::
Line
)
end
def
text_lines
@text_lines
||=
(
is_diff_line?
?
@lines
.
map
(
&
:text
)
:
@lines
)
end
def
raw_content
@raw_content
||=
text_lines
.
join
(
is_diff_line?
?
"
\n
"
:
nil
)
end
def
extract_line_prefixes
@diff_line_prefixes
||=
begin
if
is_diff_line?
text_lines
.
map
{
|
line
|
line
.
sub!
(
/\A((\+|\-)\s*)/
,
''
);
$1
}
else
[]
end
end
end
def
update_diff_lines
def
update_diff_lines
@highlighted_code
.
lines
.
each_with_index
do
|
line
,
i
|
@highlighted_code
.
lines
.
each_with_index
do
|
line
,
i
|
diff_line
=
@
diff_
lines
[
i
]
diff_line
=
@lines
[
i
]
# ignore highlighting for "match" lines
# ignore highlighting for "match" lines
next
if
diff_line
.
type
==
'match'
next
if
diff_line
.
type
==
'match'
...
@@ -33,7 +64,7 @@ module Gitlab
...
@@ -33,7 +64,7 @@ module Gitlab
diff_line
.
text
=
"
#{
@diff_line_prefixes
[
i
]
}#{
line
}
"
diff_line
.
text
=
"
#{
@diff_line_prefixes
[
i
]
}#{
line
}
"
end
end
@
diff_
lines
@lines
end
end
def
lexer
def
lexer
...
...
spec/lib/gitlab/diff/highlight_spec.rb
View file @
3fbcf52e
...
@@ -9,25 +9,41 @@ describe Gitlab::Diff::Highlight, lib: true do
...
@@ -9,25 +9,41 @@ describe Gitlab::Diff::Highlight, lib: true do
let
(
:diff_file
)
{
Gitlab
::
Diff
::
File
.
new
(
diff
)
}
let
(
:diff_file
)
{
Gitlab
::
Diff
::
File
.
new
(
diff
)
}
describe
'.process_diff_lines'
do
describe
'.process_diff_lines'
do
let
(
:diff_lines
)
{
Gitlab
::
Diff
::
Highlight
.
process_diff_lines
(
diff_file
.
new_path
,
diff_file
.
diff_lines
)
}
context
'when processing Gitlab::Diff::Line objects'
do
let
(
:diff_lines
)
{
Gitlab
::
Diff
::
Highlight
.
process_diff_lines
(
diff_file
.
new_path
,
diff_file
.
diff_lines
)
}
it
'should return Gitlab::Diff::Line elements'
do
it
'should return Gitlab::Diff::Line elements'
do
expect
(
diff_lines
.
first
).
to
be_an_instance_of
(
Gitlab
::
Diff
::
Line
)
expect
(
diff_lines
.
first
).
to
be_an_instance_of
(
Gitlab
::
Diff
::
Line
)
end
end
it
'should highlight the code'
do
it
'should highlight the code'
do
code
=
%Q{<span id="LC3" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>
\n
}
code
=
%Q{<span id="LC3" class="line"> <span class="k">def</span> <span class="nf">popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="kp">nil</span><span class="p">)</span></span>
\n
}
expect
(
diff_lines
[
2
].
text
).
to
eq
(
code
)
expect
(
diff_lines
[
2
].
text
).
to
eq
(
code
)
end
end
it
'should keep the inline diff markup'
do
expect
(
diff_lines
[
5
].
text
).
to
match
(
Regexp
.
new
(
Regexp
.
escape
(
'<span class="idiff">RuntimeError, </span>'
)))
end
it
'should keep the inline diff markup'
do
it
'should not modify "match" lines'
do
expect
(
diff_lines
[
5
].
text
).
to
match
(
Regexp
.
new
(
Regexp
.
escape
(
'<span class="idiff">RuntimeError, </span>'
)))
expect
(
diff_lines
[
0
].
text
).
to
eq
(
'@@ -6,12 +6,18 @@ module Popen'
)
expect
(
diff_lines
[
22
].
text
).
to
eq
(
'@@ -19,6 +25,7 @@ module Popen'
)
end
end
end
it
'should not modify "match" lines'
do
context
'when processing raw lines'
do
expect
(
diff_lines
[
0
].
text
).
to
eq
(
'@@ -6,12 +6,18 @@ module Popen'
)
let
(
:lines
)
{
[
"puts 'Hello'
\n
"
,
"# A comment"
]
}
expect
(
diff_lines
[
22
].
text
).
to
eq
(
'@@ -19,6 +25,7 @@ module Popen'
)
let
(
:highlighted_lines
)
{
Gitlab
::
Diff
::
Highlight
.
process_diff_lines
(
'demo.rb'
,
lines
)
}
it
'should highlight the code'
do
line_1
=
%Q{<span id="LC1" class="line"><span class="nb">puts</span> <span class="s1">'Hello'</span></span>
\n
}
line_2
=
%Q{<span id="LC2" class="line"><span class="c1"># A comment</span></span>}
expect
(
highlighted_lines
[
0
]).
to
eq
(
line_1
)
expect
(
highlighted_lines
[
1
]).
to
eq
(
line_2
)
end
end
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment