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
13f10efc
Commit
13f10efc
authored
Jan 15, 2016
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move inline diff marker logic to its own class
parent
7d31f372
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
95 deletions
+11
-95
lib/gitlab/diff/highlight.rb
lib/gitlab/diff/highlight.rb
+11
-95
No files found.
lib/gitlab/diff/highlight.rb
View file @
13f10efc
...
...
@@ -12,32 +12,24 @@ module Gitlab
end
def
highlight
return
[]
if
@diff_lines
.
empty?
find_inline_diffs
process_lines
@diff_lines
end
private
def
find_inline_diffs
@inline_diffs
=
InlineDiff
.
new
(
@raw_lines
).
inline_diffs
end
def
process_lines
@diff_lines
.
each_with_index
do
|
diff_line
,
i
|
# ignore highlighting for "match" lines
next
if
diff_line
.
type
==
'match'
rich_line
=
highlight_line
(
diff_line
,
i
)
rich_line
=
mark_inline_diffs
(
rich_line
,
diff_line
,
i
)
if
line_inline_diffs
=
inline_diffs
[
i
]
rich_line
=
InlineDiffMarker
.
new
(
diff_line
.
text
,
rich_line
).
mark
(
line_inline_diffs
)
end
diff_line
.
text
=
rich_line
.
html_safe
end
@diff_lines
end
private
def
highlight_line
(
diff_line
,
index
)
line_prefix
=
diff_line
.
text
.
match
(
/\A([+-])/
)
?
$1
:
' '
...
...
@@ -53,50 +45,8 @@ module Gitlab
rich_line
?
line_prefix
+
rich_line
:
diff_line
.
text
end
def
mark_inline_diffs
(
rich_line
,
diff_line
,
index
)
line_inline_diffs
=
@inline_diffs
[
index
]
return
rich_line
unless
line_inline_diffs
raw_line
=
diff_line
.
text
position_mapping
=
map_character_positions
(
raw_line
,
rich_line
)
offset
=
0
line_inline_diffs
.
each
do
|
inline_diff_range
|
inline_diff_positions
=
position_mapping
[
inline_diff_range
]
marker_ranges
=
collapse_ranges
(
inline_diff_positions
)
marker_ranges
.
each
do
|
range
|
offset
=
insert_around_range
(
rich_line
,
range
,
"<span class='idiff'>"
,
"</span>"
,
offset
)
end
end
rich_line
end
def
map_character_positions
(
raw_line
,
rich_line
)
mapping
=
[]
raw_pos
=
0
rich_pos
=
0
(
0
..
raw_line
.
length
).
each
do
|
raw_pos
|
raw_char
=
raw_line
[
raw_pos
]
rich_char
=
rich_line
[
rich_pos
]
while
rich_char
==
'<'
until
rich_char
==
'>'
rich_pos
+=
1
rich_char
=
rich_line
[
rich_pos
]
end
rich_pos
+=
1
rich_char
=
rich_line
[
rich_pos
]
end
mapping
[
raw_pos
]
=
rich_pos
rich_pos
+=
1
end
mapping
def
inline_diffs
@inline_diffs
||=
InlineDiff
.
new
(
@raw_lines
).
inline_diffs
end
def
old_lines
...
...
@@ -107,40 +57,6 @@ module Gitlab
@new_lines
||=
Gitlab
::
Highlight
.
highlight_lines
(
*
processing_args
(
:new
))
end
def
collapse_ranges
(
positions
)
return
[]
if
positions
.
empty?
ranges
=
[]
start
=
prev
=
positions
[
0
]
range
=
start
..
prev
positions
[
1
..-
1
].
each
do
|
pos
|
if
pos
==
prev
+
1
range
=
start
..
pos
prev
=
pos
else
ranges
<<
range
start
=
prev
=
pos
range
=
start
..
prev
end
end
ranges
<<
range
ranges
end
def
insert_around_range
(
text
,
range
,
before
,
after
,
offset
=
0
)
from
=
range
.
begin
to
=
range
.
end
text
.
insert
(
offset
+
from
,
before
)
offset
+=
before
.
length
text
.
insert
(
offset
+
to
+
1
,
after
)
offset
+=
after
.
length
offset
end
def
processing_args
(
version
)
ref
=
send
(
"diff_
#{
version
}
_ref"
)
path
=
send
(
"diff_
#{
version
}
_path"
)
...
...
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