Refactor Config classes

This commit is contained in:
Tim Warren 2015-04-29 17:03:15 -04:00
parent 285350a585
commit 8f05ce1059
10 changed files with 146 additions and 93 deletions

View File

@ -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)

View File

@ -8,7 +8,7 @@
#ifndef SFTP_H
#define SFTP_H
#include "../common.h"
#include "../../common.h"
// Socket includes
#include <cstring>

View File

@ -1,7 +1,7 @@
#ifndef TYRO_CONFIG_H
#define TYRO_CONFIG_H
#include "../common.h"
#include "../../common.h"
class TyroConfig {
public:

102
src/settings/LangConfig.cpp Normal file
View File

@ -0,0 +1,102 @@
/**
* Lexer configuration object
*
* @extends TyroConfig
*/
#include "LangConfig.h"
#include <config/languages_json.h>
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;
}

20
src/settings/LangConfig.h Normal file
View File

@ -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

View File

@ -4,9 +4,7 @@ EditPane::EditPane(
wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size
) : wxStyledTextCtrl (parent, id, pos, size, wxBORDER_NONE)
{
#include <config/languages_json.h>
lang_config = new TyroConfig();
lang_config->LoadJson(languages_json);
lang_config = new LangConfig();
#include <config/themes_json.h>
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);

View File

@ -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 <wx/stc/stc.h>
@ -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

View File

@ -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]") {