1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:37:37 +00:00

HackStudio: Use Syntax::Language instead of our own one

The one behavior difference here is that the statusbar used to display
"Unknown" for unknown file types, and "Markdown" for md, but we now
display "Plain Text" for all file types without syntax highlighters.
This commit is contained in:
Sam Atkins 2023-03-08 20:59:05 +00:00 committed by Sam Atkins
parent ffce6cc977
commit 08c1effc04
9 changed files with 35 additions and 160 deletions

View file

@ -34,7 +34,6 @@ set(SOURCES
Git/GitWidget.cpp Git/GitWidget.cpp
GMLPreviewWidget.cpp GMLPreviewWidget.cpp
HackStudioWidget.cpp HackStudioWidget.cpp
Language.cpp
LanguageClient.cpp LanguageClient.cpp
Locator.cpp Locator.cpp
Project.cpp Project.cpp

View file

@ -24,8 +24,7 @@ CodeDocument::CodeDocument(DeprecatedString const& file_path, Client* client)
, m_file_path(file_path) , m_file_path(file_path)
{ {
auto lexical_path = LexicalPath(file_path); auto lexical_path = LexicalPath(file_path);
m_language = language_from_file(lexical_path); m_language = Syntax::language_from_filename(lexical_path);
m_language_name = language_name_from_file(lexical_path);
} }
CodeDocument::CodeDocument(Client* client) CodeDocument::CodeDocument(Client* client)

View file

@ -7,9 +7,9 @@
#pragma once #pragma once
#include "Language.h"
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <LibGUI/TextDocument.h> #include <LibGUI/TextDocument.h>
#include <LibSyntax/Language.h>
namespace HackStudio { namespace HackStudio {
@ -25,8 +25,7 @@ public:
void set_execution_position(size_t line) { m_execution_position = line; } void set_execution_position(size_t line) { m_execution_position = line; }
void clear_execution_position() { m_execution_position.clear(); } void clear_execution_position() { m_execution_position.clear(); }
DeprecatedString const& file_path() const { return m_file_path; } DeprecatedString const& file_path() const { return m_file_path; }
DeprecatedString const& language_name() const { return m_language_name; }; Optional<Syntax::Language> const& language() const { return m_language; }
Language language() const { return m_language; }
virtual bool is_code_document() const override final { return true; } virtual bool is_code_document() const override final { return true; }
@ -35,8 +34,7 @@ private:
explicit CodeDocument(Client* client = nullptr); explicit CodeDocument(Client* client = nullptr);
DeprecatedString m_file_path; DeprecatedString m_file_path;
DeprecatedString m_language_name; Optional<Syntax::Language> m_language;
Language m_language { Language::Unknown };
Vector<size_t> m_breakpoint_lines; Vector<size_t> m_breakpoint_lines;
Optional<size_t> m_execution_position; Optional<size_t> m_execution_position;
}; };

View file

@ -9,7 +9,6 @@
#include "Debugger/Debugger.h" #include "Debugger/Debugger.h"
#include "EditorWrapper.h" #include "EditorWrapper.h"
#include "HackStudio.h" #include "HackStudio.h"
#include "Language.h"
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/JsonParser.h> #include <AK/JsonParser.h>
@ -36,6 +35,7 @@
#include <LibJS/SyntaxHighlighter.h> #include <LibJS/SyntaxHighlighter.h>
#include <LibMarkdown/Document.h> #include <LibMarkdown/Document.h>
#include <LibSQL/AST/SyntaxHighlighter.h> #include <LibSQL/AST/SyntaxHighlighter.h>
#include <LibSyntax/Language.h>
#include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h> #include <LibWeb/CSS/SyntaxHighlighter/SyntaxHighlighter.h>
#include <LibWeb/DOM/Text.h> #include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLHeadElement.h> #include <LibWeb/HTML/HTMLHeadElement.h>
@ -629,44 +629,50 @@ void Editor::set_cursor(const GUI::TextPosition& a_position)
void Editor::set_syntax_highlighter_for(CodeDocument const& document) void Editor::set_syntax_highlighter_for(CodeDocument const& document)
{ {
switch (document.language()) { if (!document.language().has_value()) {
case Language::Cpp: set_syntax_highlighter({});
force_rehighlight();
return;
}
switch (document.language().value()) {
case Syntax::Language::Cpp:
if (m_use_semantic_syntax_highlighting) { if (m_use_semantic_syntax_highlighting) {
set_syntax_highlighter(make<Cpp::SemanticSyntaxHighlighter>()); set_syntax_highlighter(make<Cpp::SemanticSyntaxHighlighter>());
on_token_info_timer_tick(); on_token_info_timer_tick();
m_tokens_info_timer->restart(); m_tokens_info_timer->restart();
} else } else {
set_syntax_highlighter(make<Cpp::SyntaxHighlighter>()); set_syntax_highlighter(make<Cpp::SyntaxHighlighter>());
}
break; break;
case Language::CMake: case Syntax::Language::CMake:
set_syntax_highlighter(make<CMake::SyntaxHighlighter>()); set_syntax_highlighter(make<CMake::SyntaxHighlighter>());
break; break;
case Language::CMakeCache: case Syntax::Language::CMakeCache:
set_syntax_highlighter(make<CMake::Cache::SyntaxHighlighter>()); set_syntax_highlighter(make<CMake::Cache::SyntaxHighlighter>());
break; break;
case Language::CSS: case Syntax::Language::CSS:
set_syntax_highlighter(make<Web::CSS::SyntaxHighlighter>()); set_syntax_highlighter(make<Web::CSS::SyntaxHighlighter>());
break; break;
case Language::GitCommit: case Syntax::Language::GitCommit:
set_syntax_highlighter(make<GUI::GitCommitSyntaxHighlighter>()); set_syntax_highlighter(make<GUI::GitCommitSyntaxHighlighter>());
break; break;
case Language::GML: case Syntax::Language::GML:
set_syntax_highlighter(make<GUI::GML::SyntaxHighlighter>()); set_syntax_highlighter(make<GUI::GML::SyntaxHighlighter>());
break; break;
case Language::HTML: case Syntax::Language::HTML:
set_syntax_highlighter(make<Web::HTML::SyntaxHighlighter>()); set_syntax_highlighter(make<Web::HTML::SyntaxHighlighter>());
break; break;
case Language::JavaScript: case Syntax::Language::JavaScript:
set_syntax_highlighter(make<JS::SyntaxHighlighter>()); set_syntax_highlighter(make<JS::SyntaxHighlighter>());
break; break;
case Language::Ini: case Syntax::Language::INI:
set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>()); set_syntax_highlighter(make<GUI::IniSyntaxHighlighter>());
break; break;
case Language::Shell: case Syntax::Language::Shell:
set_syntax_highlighter(make<Shell::SyntaxHighlighter>()); set_syntax_highlighter(make<Shell::SyntaxHighlighter>());
break; break;
case Language::SQL: case Syntax::Language::SQL:
set_syntax_highlighter(make<SQL::AST::SyntaxHighlighter>()); set_syntax_highlighter(make<SQL::AST::SyntaxHighlighter>());
break; break;
default: default:
@ -678,11 +684,9 @@ void Editor::set_syntax_highlighter_for(CodeDocument const& document)
void Editor::set_autocomplete_provider_for(CodeDocument const& document) void Editor::set_autocomplete_provider_for(CodeDocument const& document)
{ {
switch (document.language()) { if (document.language() == Syntax::Language::GML) {
case Language::GML:
set_autocomplete_provider(make<GUI::GML::AutocompleteProvider>()); set_autocomplete_provider(make<GUI::GML::AutocompleteProvider>());
break; } else {
default:
set_autocomplete_provider({}); set_autocomplete_provider({});
} }
} }
@ -692,10 +696,10 @@ void Editor::set_language_client_for(CodeDocument const& document)
if (m_language_client && m_language_client->language() == document.language()) if (m_language_client && m_language_client->language() == document.language())
return; return;
if (document.language() == Language::Cpp) if (document.language() == Syntax::Language::Cpp)
m_language_client = get_language_client<LanguageClients::Cpp::ConnectionToServer>(project().root_path()); m_language_client = get_language_client<LanguageClients::Cpp::ConnectionToServer>(project().root_path());
if (document.language() == Language::Shell) if (document.language() == Syntax::Language::Shell)
m_language_client = get_language_client<LanguageClients::Shell::ConnectionToServer>(project().root_path()); m_language_client = get_language_client<LanguageClients::Shell::ConnectionToServer>(project().root_path());
if (m_language_client) { if (m_language_client) {

View file

@ -1614,7 +1614,7 @@ void HackStudioWidget::update_statusbar()
} }
m_statusbar->set_text(0, builder.to_deprecated_string()); m_statusbar->set_text(0, builder.to_deprecated_string());
m_statusbar->set_text(1, current_editor_wrapper().editor().code_document().language_name()); m_statusbar->set_text(1, Syntax::language_to_string(current_editor_wrapper().editor().code_document().language().value_or(Syntax::Language::PlainText)));
m_statusbar->set_text(2, DeprecatedString::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column())); m_statusbar->set_text(2, DeprecatedString::formatted("Ln {}, Col {}", current_editor().cursor().line() + 1, current_editor().cursor().column()));
} }

View file

@ -1,93 +0,0 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Language.h"
namespace HackStudio {
Language language_from_file(LexicalPath const& file)
{
if (file.title() == "COMMIT_EDITMSG")
return Language::GitCommit;
auto extension = file.extension();
VERIFY(!extension.starts_with('.'));
if (extension == "c" || extension == "cc" || extension == "cxx" || extension == "cpp" || extension == "c++"
|| extension == "h" || extension == "hh" || extension == "hxx" || extension == "hpp" || extension == "h++")
return Language::Cpp;
if (extension == "cmake" || (extension == "txt" && file.title() == "CMakeLists"))
return Language::CMake;
if (extension == "txt" && file.title() == "CMakeCache")
return Language::CMakeCache;
if (extension == "js" || extension == "mjs" || extension == "json")
return Language::JavaScript;
if (extension == "html" || extension == "htm")
return Language::HTML;
if (extension == "css")
return Language::CSS;
if (extension == "gml")
return Language::GML;
if (extension == "ini" || extension == "af")
return Language::Ini;
if (extension == "sh" || extension == "bash")
return Language::Shell;
if (extension == "sql")
return Language::SQL;
return Language::Unknown;
}
Language language_from_name(DeprecatedString const& name)
{
if (name == "Cpp")
return Language::Cpp;
if (name == "Javascript")
return Language::JavaScript;
if (name == "Shell")
return Language::Shell;
if (name == "GitCommit")
return Language::GitCommit;
return Language::Unknown;
}
DeprecatedString language_name_from_file(LexicalPath const& file)
{
if (file.title() == "COMMIT_EDITMSG")
return "GitCommit";
auto extension = file.extension();
VERIFY(!extension.starts_with('.'));
if (extension == "c" || extension == "cc" || extension == "cxx" || extension == "cpp" || extension == "c++"
|| extension == "h" || extension == "hh" || extension == "hxx" || extension == "hpp" || extension == "h++")
return "C++";
if (extension == "cmake" || (extension == "txt" && file.title() == "CMakeLists"))
return "CMake";
if (extension == "txt" && file.title() == "CMakeCache")
return "CMakeCache";
if (extension == "js" || extension == "mjs" || extension == "json")
return "JavaScript";
if (extension == "gml")
return "GML";
if (extension == "ini")
return "Ini";
if (extension == "sh" || extension == "bash")
return "Shell";
if (extension == "md")
return "Markdown";
if (extension == "html" || extension == "htm")
return "HTML";
if (extension == "css")
return "CSS";
if (extension == "sql")
return "SQL";
if (extension == "txt")
return "Plaintext";
return "Unknown";
}
}

View file

@ -1,32 +0,0 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/LexicalPath.h>
namespace HackStudio {
enum class Language {
Unknown,
CMake,
CMakeCache,
Cpp,
CSS,
JavaScript,
HTML,
GitCommit,
GML,
Ini,
Shell,
SQL,
};
Language language_from_file(LexicalPath const&);
Language language_from_name(DeprecatedString const&);
DeprecatedString language_name_from_file(LexicalPath const&);
}

View file

@ -226,7 +226,7 @@ void ConnectionToServerWrapper::show_crash_notification() const
} }
ConnectionToServerWrapper::ConnectionToServerWrapper(DeprecatedString const& language_name, Function<NonnullRefPtr<ConnectionToServer>()> connection_creator) ConnectionToServerWrapper::ConnectionToServerWrapper(DeprecatedString const& language_name, Function<NonnullRefPtr<ConnectionToServer>()> connection_creator)
: m_language(language_from_name(language_name)) : m_language(Syntax::language_from_name(language_name).value())
, m_connection_creator(move(connection_creator)) , m_connection_creator(move(connection_creator))
{ {
create_connection(); create_connection();

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include "AutoCompleteResponse.h" #include "AutoCompleteResponse.h"
#include "Language.h"
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/Types.h> #include <AK/Types.h>
@ -16,6 +15,7 @@
#include <LibCore/ElapsedTimer.h> #include <LibCore/ElapsedTimer.h>
#include <LibCpp/Preprocessor.h> #include <LibCpp/Preprocessor.h>
#include <LibIPC/ConnectionToServer.h> #include <LibIPC/ConnectionToServer.h>
#include <LibSyntax/Language.h>
#include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h> #include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
#include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h> #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
@ -69,7 +69,7 @@ public:
template<typename LanguageServerType> template<typename LanguageServerType>
static ConnectionToServerWrapper& get_or_create(DeprecatedString const& project_path); static ConnectionToServerWrapper& get_or_create(DeprecatedString const& project_path);
Language language() const { return m_language; } Syntax::Language language() const { return m_language; }
ConnectionToServer* connection(); ConnectionToServer* connection();
void on_crash(); void on_crash();
void try_respawn_connection(); void try_respawn_connection();
@ -83,7 +83,7 @@ private:
void show_crash_notification() const; void show_crash_notification() const;
void show_frequent_crashes_notification() const; void show_frequent_crashes_notification() const;
Language m_language; Syntax::Language m_language;
Function<NonnullRefPtr<ConnectionToServer>()> m_connection_creator; Function<NonnullRefPtr<ConnectionToServer>()> m_connection_creator;
RefPtr<ConnectionToServer> m_connection; RefPtr<ConnectionToServer> m_connection;
@ -125,7 +125,7 @@ public:
m_connection_wrapper.set_active_client(*m_previous_client); m_connection_wrapper.set_active_client(*m_previous_client);
} }
Language language() const { return m_connection_wrapper.language(); } Syntax::Language language() const { return m_connection_wrapper.language(); }
void set_active_client(); void set_active_client();
bool is_active_client() const; bool is_active_client() const;
virtual void open_file(DeprecatedString const& path, int fd); virtual void open_file(DeprecatedString const& path, int fd);