summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-03-10 12:14:23 +0000
committerSam Atkins <atkinssj@gmail.com>2023-03-11 13:22:57 +0000
commitffce6cc9779c301bd91297dfc03f5578cf2a150a (patch)
treed27ce1acf546e41458aadfdf6bfb8a42978a7658
parent82f58b8af00b914043f4c2012a902f936c7f2ba2 (diff)
downloadserenity-ffce6cc9779c301bd91297dfc03f5578cf2a150a.zip
LibSyntax: Add Language-from-String/Path functions
These are taken from HackStudio, but slightly rearranged to be more alphabetical, and returning an empty Optional instead of "Unknown".
-rw-r--r--Userland/Libraries/LibSyntax/Language.cpp66
-rw-r--r--Userland/Libraries/LibSyntax/Language.h3
2 files changed, 69 insertions, 0 deletions
diff --git a/Userland/Libraries/LibSyntax/Language.cpp b/Userland/Libraries/LibSyntax/Language.cpp
index 2ab0fd1313..f06959453a 100644
--- a/Userland/Libraries/LibSyntax/Language.cpp
+++ b/Userland/Libraries/LibSyntax/Language.cpp
@@ -5,6 +5,7 @@
*/
#include "Language.h"
+#include <AK/LexicalPath.h>
#include <LibGUI/TextEditor.h>
#include <LibSyntax/Highlighter.h>
@@ -72,4 +73,69 @@ StringView common_language_extension(Language language)
VERIFY_NOT_REACHED();
}
+Optional<Language> language_from_name(StringView name)
+{
+ if (name.equals_ignoring_ascii_case("CMake"sv))
+ return Language::CMake;
+ if (name.equals_ignoring_ascii_case("CMakeCache"sv))
+ return Language::CMakeCache;
+ if (name.equals_ignoring_ascii_case("Cpp"sv))
+ return Language::Cpp;
+ if (name.equals_ignoring_ascii_case("CSS"sv))
+ return Language::CSS;
+ if (name.equals_ignoring_ascii_case("GitCommit"sv))
+ return Language::GitCommit;
+ if (name.equals_ignoring_ascii_case("GML"sv))
+ return Language::GML;
+ if (name.equals_ignoring_ascii_case("HTML"sv))
+ return Language::HTML;
+ if (name.equals_ignoring_ascii_case("INI"sv))
+ return Language::INI;
+ if (name.equals_ignoring_ascii_case("JavaScript"sv))
+ return Language::JavaScript;
+ if (name.equals_ignoring_ascii_case("PlainText"sv))
+ return Language::PlainText;
+ if (name.equals_ignoring_ascii_case("SQL"sv))
+ return Language::SQL;
+ if (name.equals_ignoring_ascii_case("Shell"sv))
+ return Language::Shell;
+
+ return {};
+}
+
+Optional<Language> language_from_filename(LexicalPath const& file)
+{
+ if (file.title() == "COMMIT_EDITMSG"sv)
+ return Language::GitCommit;
+
+ auto extension = file.extension();
+ VERIFY(!extension.starts_with('.'));
+ if (extension == "cmake"sv || (extension == "txt"sv && file.title() == "CMakeLists"sv))
+ return Language::CMake;
+ if (extension == "txt"sv && file.title() == "CMakeCache"sv)
+ return Language::CMakeCache;
+ if (extension.is_one_of("c"sv, "cc"sv, "cxx"sv, "cpp"sv, "c++", "h"sv, "hh"sv, "hxx"sv, "hpp"sv, "h++"sv))
+ return Language::Cpp;
+ if (extension == "css"sv)
+ return Language::CSS;
+ if (extension == "gml"sv)
+ return Language::GML;
+ if (extension.is_one_of("html"sv, "htm"sv))
+ return Language::HTML;
+ if (extension.is_one_of("ini"sv, "af"sv))
+ return Language::INI;
+ if (extension.is_one_of("js"sv, "mjs"sv, "json"sv))
+ return Language::JavaScript;
+ if (extension.is_one_of("sh"sv, "bash"sv))
+ return Language::Shell;
+ if (extension == "sql"sv)
+ return Language::SQL;
+
+ // Check "txt" after the CMake related files that use "txt" as their extension.
+ if (extension == "txt"sv)
+ return Language::PlainText;
+
+ return {};
+}
+
}
diff --git a/Userland/Libraries/LibSyntax/Language.h b/Userland/Libraries/LibSyntax/Language.h
index 9907b78d31..4c78e07ceb 100644
--- a/Userland/Libraries/LibSyntax/Language.h
+++ b/Userland/Libraries/LibSyntax/Language.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Forward.h>
#include <AK/StringView.h>
namespace Syntax {
@@ -27,5 +28,7 @@ enum class Language {
StringView language_to_string(Language);
StringView common_language_extension(Language);
+Optional<Language> language_from_name(StringView);
+Optional<Language> language_from_filename(LexicalPath const&);
}