From 8f05ce10599e37aeef3691e6108eff0dd9ee449b Mon Sep 17 00:00:00 2001 From: Tim Warren Date: Wed, 29 Apr 2015 17:03:15 -0400 Subject: [PATCH] Refactor Config classes --- Makefile | 14 ++-- src/{ => base}/network/SFTP.cpp | 0 src/{ => base}/network/SFTP.h | 2 +- src/{ => base}/settings/Config.cpp | 0 src/{ => base}/settings/Config.h | 2 +- src/settings/LangConfig.cpp | 102 +++++++++++++++++++++++++++++ src/settings/LangConfig.h | 20 ++++++ src/widgets/EditPane.cpp | 86 ++++-------------------- src/widgets/EditPane.h | 11 ++-- tests/SFTPTest.cpp | 2 +- 10 files changed, 146 insertions(+), 93 deletions(-) rename src/{ => base}/network/SFTP.cpp (100%) rename src/{ => base}/network/SFTP.h (97%) rename src/{ => base}/settings/Config.cpp (100%) rename src/{ => base}/settings/Config.h (91%) create mode 100644 src/settings/LangConfig.cpp create mode 100644 src/settings/LangConfig.h diff --git a/Makefile b/Makefile index 85d8003..d62c7c3 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ -SOURCES = $(wildcard include/**/*.cpp include/*.cpp src/network/*.cpp src/settings/*.cpp) +SOURCES = $(wildcard include/**/*.cpp include/*.cpp src/base/**/*.cpp) OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES)) TYRO_LIB = build/Tyro.a JSON_FILES = $(patsubst config/%.json,%.json, $(wildcard config/*.json)) -PROGRAM_SRC = $(wildcard src/*.cpp src/widgets/*.cpp) +PROGRAM_SRC = $(wildcard src/*.cpp src/widgets/*.cpp src/settings/*.cpp) PROGRAM = build/Tyro PROGRAM_OBJECTS = $(patsubst %.cpp,%.o, $(PROGRAM_SRC)) @@ -29,11 +29,7 @@ else WX_LDLIBS = $(shell wx-config --libs base core aui stc adv) endif -ifeq ($(OS),Darwin) - LDLIBS += /usr/local/lib/libssh2.a -else - LDLIBS += -lssh2 -endif +LDLIBS += -lssh2 # Platform compiler flags ifeq ($(OS),Darwin) @@ -52,7 +48,7 @@ CXX += -I include -I. all: build json_wrapper $(TYRO_LIB) $(PROGRAM) -dev: CXXFLAGS= $(DEV_CXXFLAGS) +dev: CXXFLAGS = $(DEV_CXXFLAGS) dev: all json_wrapper: json_wrapper_build @@ -121,7 +117,7 @@ Tyro.app: all cp resources/platform/osx/tyro.icns build/Tyro.app/Contents/Resources/ $(TESTS): $(TYRO_LIB) - $(foreach var, $(TEST_SRC), $(CXX) $(CXXFLAGS) $(var) $(TYRO_LIB) $(LDLIBS) -o $(patsubst %.cpp,%, $(var));) + $(foreach var, $(TEST_SRC), $(CXX) $(CXXFLAGS) $(var) $(TYRO_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(patsubst %.cpp,%, $(var));) .PHONY: tests tests: $(TESTS) diff --git a/src/network/SFTP.cpp b/src/base/network/SFTP.cpp similarity index 100% rename from src/network/SFTP.cpp rename to src/base/network/SFTP.cpp diff --git a/src/network/SFTP.h b/src/base/network/SFTP.h similarity index 97% rename from src/network/SFTP.h rename to src/base/network/SFTP.h index d81aa17..424b28e 100644 --- a/src/network/SFTP.h +++ b/src/base/network/SFTP.h @@ -8,7 +8,7 @@ #ifndef SFTP_H #define SFTP_H -#include "../common.h" +#include "../../common.h" // Socket includes #include diff --git a/src/settings/Config.cpp b/src/base/settings/Config.cpp similarity index 100% rename from src/settings/Config.cpp rename to src/base/settings/Config.cpp diff --git a/src/settings/Config.h b/src/base/settings/Config.h similarity index 91% rename from src/settings/Config.h rename to src/base/settings/Config.h index 1cac70c..82bcb5b 100644 --- a/src/settings/Config.h +++ b/src/base/settings/Config.h @@ -1,7 +1,7 @@ #ifndef TYRO_CONFIG_H #define TYRO_CONFIG_H -#include "../common.h" +#include "../../common.h" class TyroConfig { public: diff --git a/src/settings/LangConfig.cpp b/src/settings/LangConfig.cpp new file mode 100644 index 0000000..d76574c --- /dev/null +++ b/src/settings/LangConfig.cpp @@ -0,0 +1,102 @@ +/** + * Lexer configuration object + * + * @extends TyroConfig + */ + +#include "LangConfig.h" +#include + +LangConfig::LangConfig() +{ + this->LoadJson(languages_json); + this->lang = ""; +} + +LangConfig::~LangConfig() {} + +/** + * Determine the format of the current file by + * matching its extension against the patterns + * in the configuration files + * + * @return string + */ +string LangConfig::GetLangByFile(wxFileName &fileName) +{ + JsonValue langList = this->GetRoot(); + JsonValue::iterator it; + + wxString curr_file = fileName.GetFullName(); + + // Loop through each language to find a matching file pattern + for (it = langList.begin(); it != langList.end(); ++it) + { + string lang = it.key().asString(); + + // Parse the file pattern + wxString file_pattern((*it)["file_pattern"].asString()); + + file_pattern.Lower(); + + while ( ! file_pattern.empty()) + { + wxString cur = file_pattern.BeforeFirst(';'); + if ( + (cur == curr_file) || + (cur == (curr_file.BeforeLast('.') + ".*")) || + (cur == ("*." + curr_file.AfterLast('.'))) + ) + { + this->SetLang(lang); + return this->lang; + } + + // Go to the next pattern for this language + file_pattern = file_pattern.AfterFirst(';'); + } + } + + this->SetLang(lang); + return this->lang; +} + +/** + * Get the list of keywords for the selected language + * + * @param string lang + * @return JsonValue + */ +JsonValue LangConfig::GetKeywordList(string lang) +{ + if (lang == "none") lang = this->lang; + + return this->GetRoot() + .get(lang, JsonValue()) + .get("keywords", JsonValue()); +} + +/** + * Get the lexer theme map for the current language + * + * @param string lang + * @return JsonValue + */ +JsonValue LangConfig::GetLexerMap(string lang) +{ + if (lang == "none") lang = this->lang; + + return this->GetRoot() + .get(lang, JsonValue()) + .get("lexer_map", JsonValue()); +} + +void LangConfig::SetLang(string lang) +{ + this->lang = lang; +} + +string LangConfig::GetLang() +{ + return this->lang; +} \ No newline at end of file diff --git a/src/settings/LangConfig.h b/src/settings/LangConfig.h new file mode 100644 index 0000000..1354dfc --- /dev/null +++ b/src/settings/LangConfig.h @@ -0,0 +1,20 @@ +#ifndef TYRO_LANG_CONFIG_H +#define TYRO_LANG_CONFIG_H + +#include "../wx_common.h" +#include "../base/settings/Config.h" + +class LangConfig : TyroConfig { +public: + LangConfig(); + ~LangConfig(); + void SetLang(string lang); + string GetLang(); + string GetLangByFile(wxFileName &fileName); + JsonValue GetKeywordList(string lang="none"); + JsonValue GetLexerMap(string lang="none"); +private: + string lang; +}; + +#endif diff --git a/src/widgets/EditPane.cpp b/src/widgets/EditPane.cpp index 01a9dc5..94f1d1b 100644 --- a/src/widgets/EditPane.cpp +++ b/src/widgets/EditPane.cpp @@ -4,9 +4,7 @@ EditPane::EditPane( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size ) : wxStyledTextCtrl (parent, id, pos, size, wxBORDER_NONE) { - #include - lang_config = new TyroConfig(); - lang_config->LoadJson(languages_json); + lang_config = new LangConfig(); #include theme_config = new TyroConfig(); @@ -59,7 +57,7 @@ void EditPane::Highlight(wxString filePath) this->fileName.Assign(filePath); // Get the configuration name for the selected language - string lang = this->GetLangByFile(); + string lang = lang_config->GetLangByFile(this->fileName); this->StyleClearAll(); @@ -124,11 +122,10 @@ void EditPane::ApplyTheme(string lang, string theme) { this->SetTheme(theme); - JsonValue lang_list = lang_config->GetRoot(); - JsonValue lexer_map = lang_list.get(lang, JsonValue()).get("lexer_map", JsonValue()); - - // Get the list of keywords for the current language - JsonValue keywords_array = this->GetKeywordList(lang); + // Get the keywords and mapping for the selected language + JsonValue lexer_map = lang_config->GetLexerMap(lang); + JsonValue keywords_array = lang_config->GetKeywordList(lang); + if (keywords_array.isArray()) { for(unsigned int i = 0; i < keywords_array.size(); i++) @@ -147,11 +144,9 @@ void EditPane::ApplyTheme(string lang, string theme) wxLogDebug(output.str().c_str()); } - - int offset_count = 0; // Do the appropriate mappings to load the selected theme - this->_ApplyTheme(lexer_map, offset_count); + this->_ApplyTheme(lexer_map); } /** @@ -177,50 +172,6 @@ bool EditPane::Load(wxString filePath) return false; } -/** - * Determine the format of the current file by - * matching its extension against the patterns - * in the configuration files - * - * @return string - */ -string EditPane::GetLangByFile() -{ - JsonValue langList = lang_config->GetRoot(); - JsonValue::iterator it; - - wxString curr_file = this->fileName.GetFullName(); - - // Loop through each language to find a matching file pattern - for (it = langList.begin(); it != langList.end(); ++it) - { - string lang = it.key().asString(); - - // Parse the file pattern - wxString file_pattern((*it)["file_pattern"].asString()); - - file_pattern.Lower(); - - while ( ! file_pattern.empty()) - { - wxString cur = file_pattern.BeforeFirst(';'); - if ( - (cur == curr_file) || - (cur == (curr_file.BeforeLast('.') + ".*")) || - (cur == ("*." + curr_file.AfterLast('.'))) - ) - { - return lang; - } - - // Go to the next pattern for this language - file_pattern = file_pattern.AfterFirst(';'); - } - } - - return ""; -} - bool EditPane::SaveFile() { wxString fname; @@ -364,19 +315,6 @@ void EditPane::OnCharAdded(wxStyledTextEvent& event) } } -/** - * Get the list of keywords for the selected language - * - * @param string lang - * @return JsonValue - */ -JsonValue EditPane::GetKeywordList(string lang) -{ - return lang_config->GetRoot() - .get(lang, JsonValue()) - .get("keywords", JsonValue()); -} - /** * Retrieve a setting from the current theme * @@ -424,7 +362,7 @@ wxColor EditPane::GetThemeColor(string type, string key) * @param int addtoi - Offset for some languages * @return void */ -void EditPane::_ApplyTheme(JsonValue lexer_map, int addtoi) +void EditPane::_ApplyTheme(JsonValue &lexer_map) { // Font setup #ifdef __WXMAC__ @@ -467,13 +405,11 @@ void EditPane::_ApplyTheme(JsonValue lexer_map, int addtoi) this->StyleSetBackground (wxSTC_STYLE_LINENUMBER, line_number_background); this->SetMarginType (MARGIN_LINE_NUMBERS, wxSTC_MARGIN_NUMBER); - int min = 0 + addtoi; - int max = lexer_map.size() + addtoi; + int max = lexer_map.size(); - for (int i = min; i < max; i++) + for (int i = 0; i < max; i++) { - int n = i - addtoi; - string key = lexer_map[n].asString(); + string key = lexer_map[i].asString(); //wxLogDebug("Token type: %s", key); //wxLogDebug("Lexer constant: %i", i); diff --git a/src/widgets/EditPane.h b/src/widgets/EditPane.h index d7eb3f9..fb018bf 100644 --- a/src/widgets/EditPane.h +++ b/src/widgets/EditPane.h @@ -2,7 +2,8 @@ #define TYROEDIT_PANE_H #include "../wx_common.h" -#include "../settings/Config.h" +#include "../base/settings/Config.h" +#include "../settings/LangConfig.h" #include @@ -17,7 +18,6 @@ public: ); ~EditPane(); wxFileName fileName; - string GetLangByFile(); bool Load(wxString filePath); void Highlight(wxString filePath); bool SaveFile(); @@ -26,10 +26,10 @@ public: private: StringConstMap lexerMap; StringConstMap::iterator lexerMapIt; - TyroConfig *lang_config; + LangConfig *lang_config; TyroConfig *theme_config; JsonValue current_theme; - enum + enum myMargins { MARGIN_FOLD, MARGIN_SYMBOL, @@ -43,8 +43,7 @@ private: void SetTheme(string theme_name); JsonValue GetThemeValue(string type, string key); wxColor GetThemeColor(string type, string key); - JsonValue GetKeywordList(string lang); - void _ApplyTheme(JsonValue lexer_map, int addtoi=0); + void _ApplyTheme(JsonValue &lexer_map); }; #endif // TYRODOC_FRAME_H diff --git a/tests/SFTPTest.cpp b/tests/SFTPTest.cpp index 63199e5..5c3fd39 100644 --- a/tests/SFTPTest.cpp +++ b/tests/SFTPTest.cpp @@ -1,6 +1,6 @@ #define CATCH_CONFIG_MAIN #include "catch.hpp" -#include "../src/network/SFTP.h" +#include "../src/base/network/SFTP.h" TEST_CASE("ssh connections work", "[SFTP]") {