1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:48:11 +00:00

HackStudio: Create Language enum from file extension or language name

This commit is contained in:
Itamar 2021-03-05 17:18:22 +02:00 committed by Andreas Kling
parent 1edaefca3a
commit ba6cbf160b
4 changed files with 68 additions and 12 deletions

View file

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

View file

@ -42,18 +42,7 @@ CodeDocument::CodeDocument(const String& file_path, Client* client)
: TextDocument(client)
, m_file_path(file_path)
{
LexicalPath lexical_path(file_path);
if (lexical_path.has_extension(".cpp") || lexical_path.has_extension(".h"))
m_language = Language::Cpp;
else if (lexical_path.has_extension(".js"))
m_language = Language::JavaScript;
else if (lexical_path.has_extension(".gml"))
m_language = Language::GML;
else if (lexical_path.has_extension(".ini"))
m_language = Language::Ini;
else if (lexical_path.has_extension(".sh"))
m_language = Language::Shell;
m_language = language_from_file_extension(LexicalPath { file_path }.extension());
}
CodeDocument::CodeDocument(Client* client)

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Language.h"
namespace HackStudio {
Language language_from_file_extension(const String& extension)
{
VERIFY(!extension.starts_with("."));
if (extension == "cpp" || extension == "h")
return Language::Cpp;
else if (extension == "js")
return Language::JavaScript;
else if (extension == "gml")
return Language::GML;
else if (extension == "ini")
return Language::Ini;
else if (extension == "sh")
return Language::Shell;
return Language::Unknown;
}
Language language_from_name(const String& name)
{
if (name == "Cpp")
return Language::Cpp;
if (name == "Javascript")
return Language::JavaScript;
if (name == "Shell")
return Language::Shell;
return Language::Unknown;
}
}

View file

@ -26,6 +26,8 @@
#pragma once
#include <AK/String.h>
namespace HackStudio {
enum class Language {
Unknown,
@ -35,4 +37,8 @@ enum class Language {
Ini,
Shell,
};
Language language_from_file_extension(const String&);
Language language_from_name(const String&);
}