diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-10-26 10:32:12 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-10-26 10:33:50 +0200 |
commit | ed242d538a0c2afad57870e65ffa7e6a3d8a0eb8 (patch) | |
tree | b0be4352f81c39c29e60ca68cc3a394b31ed9ad7 /DevTools/HackStudio/main.cpp | |
parent | 2b19badd744ac461f75c27f1de768f1bc30c1d7b (diff) | |
download | serenity-ed242d538a0c2afad57870e65ffa7e6a3d8a0eb8.zip |
HackStudio: Assorted improvements to C++ highlighting
Add a list of hard-coded standard types (including AK types) and show
them in a different style.
Rehighligt the file whenever it changes. (This is very inefficient but
makes it much easier to experiment.)
Also keep tweaking the colors. :^)
Diffstat (limited to 'DevTools/HackStudio/main.cpp')
-rw-r--r-- | DevTools/HackStudio/main.cpp | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/DevTools/HackStudio/main.cpp b/DevTools/HackStudio/main.cpp index f13fbbec18..d3076de284 100644 --- a/DevTools/HackStudio/main.cpp +++ b/DevTools/HackStudio/main.cpp @@ -178,6 +178,8 @@ int main(int argc, char** argv) g_window->set_icon(small_icon); g_window->show(); + + open_file("main.cpp"); return app.exec(); } @@ -201,21 +203,46 @@ static TextStyle style_for_token_type(CppToken::Type type) switch (type) { case CppToken::Type::Keyword: return { Color::Black, &Font::default_bold_fixed_width_font() }; + case CppToken::Type::KnownType: + return { Color::from_rgb(0x929200), &Font::default_bold_fixed_width_font() }; case CppToken::Type::Identifier: - return { Color::Blue }; + return { Color::from_rgb(0x000092), &Font::default_bold_fixed_width_font() }; case CppToken::Type::DoubleQuotedString: case CppToken::Type::SingleQuotedString: case CppToken::Type::Number: - return { Color::Red }; + return { Color::from_rgb(0x920000) }; case CppToken::Type::PreprocessorStatement: - return { Color::Magenta }; + return { Color::from_rgb(0x009292) }; case CppToken::Type::Comment: - return { Color::from_rgb(0x008200) }; + return { Color::from_rgb(0x009200) }; default: return { Color::Black }; } } +static void rehighlight() +{ + auto text = g_text_editor->text(); + CppLexer lexer(text); + auto tokens = lexer.lex(); + + Vector<GTextEditor::Span> spans; + for (auto& token : tokens) { +#ifdef DEBUG_SYNTAX_HIGHLIGHTING + dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column; +#endif + GTextEditor::Span span; + span.start = { token.m_start.line, token.m_start.column }; + span.end = { token.m_end.line, token.m_end.column }; + auto style = style_for_token_type(token.m_type); + span.color = style.color; + span.font = style.font; + spans.append(span); + } + g_text_editor->set_spans(spans); + g_text_editor->update(); +} + void open_file(const String& filename) { auto file = CFile::construct(filename); @@ -225,25 +252,15 @@ void open_file(const String& filename) } auto contents = file->read_all(); g_text_editor->set_text(contents); - g_currently_open_file = filename; - g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters())); - g_project_list_view->update(); if (filename.ends_with(".cpp")) { - CppLexer lexer(contents); - auto tokens = lexer.lex(); - - Vector<GTextEditor::Span> spans; - for (auto& token : tokens) { - dbg() << token.to_string() << " @ " << token.m_start.line << ":" << token.m_start.column << " - " << token.m_end.line << ":" << token.m_end.column; - GTextEditor::Span span; - span.start = { token.m_start.line, token.m_start.column }; - span.end = { token.m_end.line, token.m_end.column }; - auto style = style_for_token_type(token.m_type); - span.color = style.color; - span.font = style.font; - spans.append(span); - } - g_text_editor->set_spans(spans); + g_text_editor->on_change = [] { rehighlight(); }; + rehighlight(); + } else { + g_text_editor->on_change = nullptr; } + + g_currently_open_file = filename; + g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters())); + g_project_list_view->update(); } |