-
Notifications
You must be signed in to change notification settings - Fork 745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++ header files are not highlighted correctly #932
Comments
Defaulting to C++ for |
Hi there, It looks pretty safe to switch from C to C++ as the lexer for require 'rouge'
require 'minitest/autorun'
class HighlightTest < Minitest::Test
Dir.glob('postgres/**/*.h').each_with_index do |file, index|
define_method(:"test_#{file}") do
file = File.read(file).freeze
skip if file.include?("__cplusplus")
with_c = highlight(file, Rouge::Lexers::C)
with_cpp = highlight(file, Rouge::Lexers::Cpp)
assert_equal with_c, with_cpp
end
end
private
def highlight(file, lexer)
Rouge::Formatters::Null.new.format(lexer.lex(file))
end
end Here are the results :
|
This issue has been automatically marked as stale because it has not had any activity for more than a year. It will be closed if no additional activity occurs within the next 14 days. If you would like this issue to remain open, please reply and let us know if the issue is still reproducible. |
Issue still reproducible on GitLab |
Thanks @robin850—I'm sorry to be attending to this so late but that's been very helpful. Based on those tests, it does look like switching to C++ lexer for I'll try to get a PR together sometime this week to address this. |
@eliaskosunen I had a bit more of a think about the problem and it didn't feel right to treat all files as C++ when the header files may not be C++. My alternative solution is to provide a disambiguation that detects files as C++ based on the presence of certain keywords. This is the approach that Rouge already takes with Objective-C (which has a similar problem since its header files also use the I've submitted the fix as PR #1269. If you (or @robin850) has any comments on it, please let me know in that thread. I'm not very familiar with C++ and so the words I'm using to test for may not be complete (the test is only if the keyword is the first word on a line so as to avoid situations like @robin850 encountered when highlighting the Git codebase). |
This issue originally occurred on GitLab, which uses rouge for its syntax highlighting.
The problem is, that C++ header files with the file extension '.h' are highlighted as C files, even though that extension is very commonly used for C++ headers as well.
These following two snippets are from GitLab and highlighted by rouge.
Snippet 1,
example.h
: https://gitlab.com/snippets/1723020Highlighted as C code. Evident by the lack of highlighting of keywords such as
namespace
,template
,using
andclass
.Snippet 2,
example.hpp
: https://gitlab.com/snippets/1723021Highlighted correctly as C++ code.
In comparison, an identical GitHub gist with name
example.h
: https://gist.github.com/eliaskosunen/4f508cdfb59aad0599c964ffa7ce9a87 is highlighted correctly as C++ code.Possible solutions:
.h
should probably default to C++ rather than C. The only keyword I can think of that exists in C but not in C++ isrestrict
, but there are way more examples the other way aroundThe text was updated successfully, but these errors were encountered: