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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
12546f89
Commit
12546f89
authored
Jan 15, 2016
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests
parent
5de8971d
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
0 deletions
+53
-0
lib/gitlab/diff/inline_diff.rb
lib/gitlab/diff/inline_diff.rb
+1
-0
lib/gitlab/diff/inline_diff_marker.rb
lib/gitlab/diff/inline_diff_marker.rb
+10
-0
spec/lib/gitlab/diff/inline_diff_marker_spec.rb
spec/lib/gitlab/diff/inline_diff_marker_spec.rb
+15
-0
spec/lib/gitlab/diff/inline_diff_spec.rb
spec/lib/gitlab/diff/inline_diff_spec.rb
+27
-0
No files found.
lib/gitlab/diff/inline_diff.rb
View file @
12546f89
...
@@ -38,6 +38,7 @@ module Gitlab
...
@@ -38,6 +38,7 @@ module Gitlab
private
private
# Find runs of single line edits
def
local_edit_indexes
def
local_edit_indexes
line_prefixes
=
@lines
.
map
{
|
line
|
line
.
match
(
/\A([+-])/
)
?
$1
:
' '
}
line_prefixes
=
@lines
.
map
{
|
line
|
line
.
match
(
/\A([+-])/
)
?
$1
:
' '
}
joined_line_prefixes
=
"
#{
line_prefixes
.
join
}
"
joined_line_prefixes
=
"
#{
line_prefixes
.
join
}
"
...
...
lib/gitlab/diff/inline_diff_marker.rb
View file @
12546f89
...
@@ -11,9 +11,12 @@ module Gitlab
...
@@ -11,9 +11,12 @@ module Gitlab
def
mark
(
line_inline_diffs
)
def
mark
(
line_inline_diffs
)
offset
=
0
offset
=
0
line_inline_diffs
.
each
do
|
inline_diff_range
|
line_inline_diffs
.
each
do
|
inline_diff_range
|
# Map the inline-diff range based on the raw line to character positions in the rich line
inline_diff_positions
=
position_mapping
[
inline_diff_range
]
inline_diff_positions
=
position_mapping
[
inline_diff_range
]
# Turn the array of character positions into ranges
marker_ranges
=
collapse_ranges
(
inline_diff_positions
)
marker_ranges
=
collapse_ranges
(
inline_diff_positions
)
# Mark each range
marker_ranges
.
each
do
|
range
|
marker_ranges
.
each
do
|
range
|
offset
=
insert_around_range
(
rich_line
,
range
,
"<span class='idiff'>"
,
"</span>"
,
offset
)
offset
=
insert_around_range
(
rich_line
,
range
,
"<span class='idiff'>"
,
"</span>"
,
offset
)
end
end
...
@@ -22,6 +25,9 @@ module Gitlab
...
@@ -22,6 +25,9 @@ module Gitlab
rich_line
rich_line
end
end
private
# Mapping of character positions in the raw line, to the rich (highlighted) line
def
position_mapping
def
position_mapping
@position_mapping
||=
begin
@position_mapping
||=
begin
mapping
=
[]
mapping
=
[]
...
@@ -31,6 +37,8 @@ module Gitlab
...
@@ -31,6 +37,8 @@ module Gitlab
raw_char
=
raw_line
[
raw_pos
]
raw_char
=
raw_line
[
raw_pos
]
rich_char
=
rich_line
[
rich_pos
]
rich_char
=
rich_line
[
rich_pos
]
# The raw and rich lines are the same except for HTML tags,
# so skip over any `<...>` segment
while
rich_char
==
'<'
while
rich_char
==
'<'
until
rich_char
==
'>'
until
rich_char
==
'>'
rich_pos
+=
1
rich_pos
+=
1
...
@@ -50,6 +58,7 @@ module Gitlab
...
@@ -50,6 +58,7 @@ module Gitlab
end
end
end
end
# Takes an array of integers, and returns an array of ranges covering the same integers
def
collapse_ranges
(
positions
)
def
collapse_ranges
(
positions
)
return
[]
if
positions
.
empty?
return
[]
if
positions
.
empty?
ranges
=
[]
ranges
=
[]
...
@@ -71,6 +80,7 @@ module Gitlab
...
@@ -71,6 +80,7 @@ module Gitlab
ranges
ranges
end
end
# Inserts tags around the characters identified by the given range
def
insert_around_range
(
text
,
range
,
before
,
after
,
offset
=
0
)
def
insert_around_range
(
text
,
range
,
before
,
after
,
offset
=
0
)
text
.
insert
(
offset
+
range
.
begin
,
before
)
text
.
insert
(
offset
+
range
.
begin
,
before
)
offset
+=
before
.
length
offset
+=
before
.
length
...
...
spec/lib/gitlab/diff/inline_diff_marker_spec.rb
0 → 100644
View file @
12546f89
require
'spec_helper'
describe
Gitlab
::
Diff
::
InlineDiffMarker
,
lib:
true
do
describe
'#inline_diffs'
do
let
(
:raw
)
{
"abc def"
}
let
(
:rich
)
{
%{<span class="abc">abc</span><span class="space"> </span><span class="def">def</span>}
}
let
(
:inline_diffs
)
{
[
2
..
4
]
}
let
(
:subject
)
{
Gitlab
::
Diff
::
InlineDiffMarker
.
new
(
raw
,
rich
).
mark
(
inline_diffs
)
}
it
'marks the inline diffs'
do
expect
(
subject
).
to
eq
(
%{<span class="abc">ab<span class='idiff'>c</span></span><span class="space"><span class='idiff'> </span></span><span class="def"><span class='idiff'>d</span>ef</span>}
)
end
end
end
spec/lib/gitlab/diff/inline_diff_spec.rb
0 → 100644
View file @
12546f89
require
'spec_helper'
describe
Gitlab
::
Diff
::
InlineDiff
,
lib:
true
do
describe
'#inline_diffs'
do
let
(
:diff
)
do
<<
eos
class Test
- def initialize(test = true)
+ def initialize(test = false)
@test = test
end
end
eos
end
let
(
:subject
)
{
Gitlab
::
Diff
::
InlineDiff
.
new
(
diff
.
lines
).
inline_diffs
}
it
'finds all inline diffs'
do
expect
(
subject
[
0
]).
to
be_nil
expect
(
subject
[
1
]).
to
eq
([
25
..
27
])
expect
(
subject
[
2
]).
to
eq
([
25
..
28
])
expect
(
subject
[
3
]).
to
be_nil
expect
(
subject
[
4
]).
to
be_nil
expect
(
subject
[
5
]).
to
be_nil
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