Commit 707d2dcd authored by Vitali Tatarintev's avatar Vitali Tatarintev

Fix stack trace highlight for PHP

Pick a first alternative language for Sentry stack trace highlight,
when Rouge can't decide which lexer to choose.

`StackTraceHighlightDecorator` now works with PHP and Hack languages
parent 13f136e1
...@@ -28,7 +28,7 @@ module Gitlab ...@@ -28,7 +28,7 @@ module Gitlab
end end
def highlight_entry_context(filename, context) def highlight_entry_context(filename, context)
language = Rouge::Lexer.guess_by_filename(filename).tag language = guess_language_by_filename(filename)
context.map do |line_number, line_of_code| context.map do |line_number, line_of_code|
[ [
...@@ -38,6 +38,12 @@ module Gitlab ...@@ -38,6 +38,12 @@ module Gitlab
] ]
end end
end end
def guess_language_by_filename(filename)
Rouge::Lexer.guess_by_filename(filename).tag
rescue Rouge::Guesser::Ambiguous => e
e.alternatives.min_by(&:tag)&.tag
end
end end
end end
end end
...@@ -35,6 +35,16 @@ FactoryBot.define do ...@@ -35,6 +35,16 @@ FactoryBot.define do
[8, "}\n"] [8, "}\n"]
] ]
}, },
{
'function' => 'print',
'lineNo' => 3,
'filename' => 'hello_world.php',
'context' => [
[1, "// PHP/Hack example\n"],
[2, "<?php\n"],
[3, "echo 'Hello, World!';\n"]
]
},
{ {
'filename' => 'blank.txt' 'filename' => 'blank.txt'
} }
......
...@@ -48,6 +48,16 @@ describe Gitlab::ErrorTracking::StackTraceHighlightDecorator do ...@@ -48,6 +48,16 @@ describe Gitlab::ErrorTracking::StackTraceHighlightDecorator do
[8, '<span id="LC1" class="line" lang="swift"><span class="p">}</span></span>'] [8, '<span id="LC1" class="line" lang="swift"><span class="p">}</span></span>']
] ]
}, },
{
'function' => 'print',
'lineNo' => 3,
'filename' => 'hello_world.php',
'context' => [
[1, '<span id="LC1" class="line" lang="hack"><span class="c1">// PHP/Hack example</span></span>'],
[2, '<span id="LC1" class="line" lang="hack"><span class="cp">&lt;?php</span></span>'],
[3, '<span id="LC1" class="line" lang="hack"><span class="k">echo</span> <span class="s1">\'Hello, World!\'</span><span class="p">;</span></span>']
]
},
{ {
'filename' => 'blank.txt' 'filename' => 'blank.txt'
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment