Add syntax config loading

This commit is contained in:
Tim Warren 2015-04-16 11:23:08 -04:00
parent 72b1dbc4d7
commit af4934b16b
4 changed files with 114 additions and 37 deletions

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,18 @@ EditPane::EditPane(
) : wxStyledTextCtrl (parent, id, pos, size, style)
{
config = new TyroConfig();
lexerMap["batch"] = wxSTC_LEX_BATCH;
lexerMap["caml"] = wxSTC_LEX_CAML;
lexerMap["cmake"] = wxSTC_LEX_CMAKE;
lexerMap["cpp"] = wxSTC_LEX_CPP;
lexerMap["css"] = wxSTC_LEX_CSS;
lexerMap["js"] = wxSTC_LEX_CPP;
lexerMap["html"] = wxSTC_LEX_HTML;
lexerMap["makefile"] = wxSTC_LEX_MAKEFILE;
lexerMap["php"] = wxSTC_LEX_PHPSCRIPT;
lexerMap["perl"] = wxSTC_LEX_PERL;
lexerMap["python"] = wxSTC_LEX_PYTHON;
lexerMap["shell"] = wxSTC_LEX_BASH;
}
EditPane::~EditPane()
@ -13,6 +25,40 @@ EditPane::~EditPane()
delete config;
}
string EditPane::GetLangByFile(const wxString &filename)
{
JsonValue langList = config->GetRoot();
JsonValue::iterator it;
// 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 == filename) ||
(cur == (filename.BeforeLast('.') + _T(".*"))) ||
(cur == (_T("*.") + filename.AfterLast('.')))
)
{
return lang;
}
file_pattern = file_pattern.AfterFirst(';');
}
}
return "";
}
/**
* Encapsulate lexer selection when opening a file
*
@ -22,11 +68,21 @@ EditPane::~EditPane()
bool EditPane::LoadAndHighlight(wxString filePath)
{
fileName = filePath;
wxFileName file(filePath);
wxString ext = file.GetExt();
string lang = this->GetLangByFile(filePath);
this->StyleClearAll();
this->SetLexer(wxSTC_LEX_CPP);
if (lexerMap.count(lang) > 0)
{
this->SetLexer(lexerMap[lang]);
}
else
{
this->SetLexer(wxSTC_LEX_NULL);
}
// Get the list of keywords for the current language
JsonValue keywords_array = config->GetLangKeywords(lang);
// Make sure every background is the same color!
for(int i = 0; i <= wxSTC_STYLE_MAX; i++)
@ -35,9 +91,6 @@ bool EditPane::LoadAndHighlight(wxString filePath)
this->StyleSetFaceName(i, "Anonymous Pro");
}
// Get the list of keywords for the current language
JsonValue keywords_array = config->GetLangKeywords("cpp");
this->StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColor(101, 123, 131));
this->StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColor(147, 161, 161));

View File

@ -22,8 +22,11 @@ public:
);
~EditPane();
wxString fileName;
string GetLangByFile(const wxString &filename);
bool LoadAndHighlight(wxString filePath);
private:
StringConstMap lexerMap;
StringConstMap::iterator lexerMapIt;
TyroConfig *config;
enum
{

View File

@ -52,7 +52,6 @@ void MainFrame::SetupToolbar()
#include "../../resources/xpm/file_add.xpm"
#include "../../resources/xpm/folder.xpm"
#include "../../resources/xpm/diskette.xpm"
#include "../../resources/xpm/close.xpm"
#include "../../resources/xpm/copy.xpm"
#include "../../resources/xpm/scissors.xpm"
#include "../../resources/xpm/clipboard.xpm"
@ -60,7 +59,6 @@ void MainFrame::SetupToolbar()
wxBitmap new_file_icon(file_add);
wxBitmap open_file_icon(folder);
wxBitmap save_file_icon(diskette);
wxBitmap close_file_icon(close);
wxBitmap copy_icon(copy);
wxBitmap cut_icon(scissors);
wxBitmap paste_icon(clipboard);
@ -68,7 +66,6 @@ void MainFrame::SetupToolbar()
wxBitmap new_file_icon = wxArtProvider::GetBitmap(wxART_NEW);
wxBitmap open_file_icon = wxArtProvider::GetBitmap(wxART_FILE_OPEN);
wxBitmap save_file_icon = wxArtProvider::GetBitmap(wxART_FILE_SAVE);
wxBitmap close_file_icon = wxArtProvider::GetBitmap(wxART_CLOSE);
wxBitmap copy_icon = wxArtProvider::GetBitmap(wxART_COPY);
wxBitmap cut_icon = wxArtProvider::GetBitmap(wxART_CUT);
wxBitmap paste_icon = wxArtProvider::GetBitmap(wxART_PASTE);
@ -81,7 +78,6 @@ void MainFrame::SetupToolbar()
toolBar->AddTool(wxID_NEW, "New", new_file_icon, "New file");
toolBar->AddTool(wxID_OPEN, "Open", open_file_icon, "Open file");
toolBar->AddTool(wxID_SAVE, "Save", save_file_icon, "Save file");
toolBar->AddTool(wxID_CLOSE, "Close", close_file_icon, "Close file");
toolBar->AddSeparator();
toolBar->AddTool(wxID_COPY, "Copy", copy_icon, "Copy");
toolBar->AddTool(wxID_CUT, "Cut", cut_icon, "Cut");
@ -261,7 +257,7 @@ void MainFrame::EnableEditControls()
{
fileMenu->Enable(wxID_SAVE, true);
fileMenu->Enable(wxID_SAVEAS, true);
fileMenu->Enable(wxID_CLOSE, true);
//editMenu->Enable(wxID_UNDO, false);
//editMenu->Enable(wxID_REDO, false);
editMenu->Enable(wxID_CUT, true);
@ -285,7 +281,6 @@ void MainFrame::DisableEditControls()
{
fileMenu->Enable(wxID_SAVE, false);
fileMenu->Enable(wxID_SAVEAS, false);
fileMenu->Enable(wxID_CLOSE, false);
editMenu->Enable(wxID_UNDO, false);
editMenu->Enable(wxID_REDO, false);