Skip to content

Commit

Permalink
Allow using LexTerminal for wxVersion < 3.3
Browse files Browse the repository at this point in the history
Signed-off-by: Eran Ifrah <[email protected]>
  • Loading branch information
eranif committed Jan 4, 2025
1 parent 84daa92 commit ce720bd
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 46 deletions.
77 changes: 77 additions & 0 deletions Plugin/ConsoleLexer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "ConsoleLexer.hpp"

#include "ExtraLexers.h"
#include "file_logger.h"

namespace
{
class StcAccessor : public AccessorInterface
{
public:
StcAccessor(wxStyledTextCtrl* ctrl)
: m_ctrl(ctrl)
{
}
const char operator[](size_t index) const override { return SafeGetCharAt(index); }
char SafeGetCharAt(size_t index, char chDefault = ' ') const override
{
if (index >= m_ctrl->GetLength()) {
return chDefault;
}
return m_ctrl->GetCharAt(index);
}

/// Style from the current position to the `pos`
void ColourTo(size_t pos, int style) override
{
pos += 1;
size_t last_styled_pos = m_ctrl->GetEndStyled();
CHECK_COND_RET(pos > last_styled_pos);
m_ctrl->SetStyling(pos - last_styled_pos, style);
}

void StartAt(size_t start) override { m_ctrl->StartStyling(start); }
void StartSegment(size_t pos) override { m_ctrl->StartStyling(pos); }
int GetPropertyInt(const std::string& name, int defaultVal = 0) const override
{
wxUnusedVar(name);
wxUnusedVar(defaultVal);
return 1;
};

private:
wxStyledTextCtrl* m_ctrl = nullptr;
};
} // namespace

ConsoleLexer::ConsoleLexer(wxStyledTextCtrl* stc)
: m_stc(stc)
{
m_stc->Bind(wxEVT_STC_STYLENEEDED, &ConsoleLexer::OnStyleNeeded, this);
CallAfter(&ConsoleLexer::SetStyles);
}

ConsoleLexer::~ConsoleLexer() { m_stc->Unbind(wxEVT_STC_STYLENEEDED, &ConsoleLexer::OnStyleNeeded, this); }

/// Apply the lexer's "terminal" styles for this instance managed wxSTC
void ConsoleLexer::SetStyles()
{
m_stc->SetProperty("lexer.errorlist.escape.sequences", "1");
m_stc->SetProperty("lexer.errorlist.value.separate", "1");
m_stc->SetProperty("lexer.terminal.escape.sequences", "1");
m_stc->SetProperty("lexer.terminal.value.separate", "1");

// Hide escape sequence styles
m_stc->StyleSetVisible(wxSTC_TERMINAL_ESCSEQ, false);
m_stc->StyleSetVisible(wxSTC_TERMINAL_ESCSEQ_UNKNOWN, false);
}

void ConsoleLexer::OnStyleNeeded(wxStyledTextEvent& e)
{
e.Skip();
int startPos = m_stc->GetEndStyled();
int endPos = e.GetPosition();

StcAccessor styler(m_stc);
::LexerTerminalStyle(startPos, (endPos - startPos), styler);
}
66 changes: 66 additions & 0 deletions Plugin/ConsoleLexer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include "codelite_exports.h"

#include <memory>
#include <wx/stc/stc.h>

#define CONSOLE_LEXER_DEFAULT 0
#define CONSOLE_LEXER_PYTHON 1
#define CONSOLE_LEXER_GCC_ERROR 2
#define CONSOLE_LEXER_GCC_INCLUDED_FROM 22
#define CONSOLE_LEXER_GCC_WARNING 56
#define CONSOLE_LEXER_GCC_EXCERPT 25
#define CONSOLE_LEXER_GCC_NOTE 57
#define CONSOLE_LEXER_ESCSEQ 23
#define CONSOLE_LEXER_ESCSEQ_UNKNOWN 24
#define CONSOLE_LEXER_ANSI_BLACK 40
#define CONSOLE_LEXER_ANSI_RED 41
#define CONSOLE_LEXER_ANSI_GREEN 42
#define CONSOLE_LEXER_ANSI_BROWN 43
#define CONSOLE_LEXER_ANSI_BLUE 44
#define CONSOLE_LEXER_ANSI_MAGENTA 45
#define CONSOLE_LEXER_ANSI_CYAN 46
#define CONSOLE_LEXER_ANSI_GRAY 47
#define CONSOLE_LEXER_ANSI_DARK_GRAY 48
#define CONSOLE_LEXER_ANSI_BRIGHT_RED 49
#define CONSOLE_LEXER_ANSI_BRIGHT_GREEN 50
#define CONSOLE_LEXER_ANSI_YELLOW 51
#define CONSOLE_LEXER_ANSI_BRIGHT_BLUE 52
#define CONSOLE_LEXER_ANSI_BRIGHT_MAGENTA 53
#define CONSOLE_LEXER_ANSI_BRIGHT_CYAN 54
#define CONSOLE_LEXER_ANSI_WHITE 55

class WXDLLIMPEXP_SDK ConsoleLexer : public wxEvtHandler
{
using Ptr_t = std::unique_ptr<ConsoleLexer>;

public:
ConsoleLexer(wxStyledTextCtrl* stc);
virtual ~ConsoleLexer();

/**
* @brief initialize the control styles and colours
*/
void SetStyles();

protected:
void OnStyleNeeded(wxStyledTextEvent& e);

wxStyledTextCtrl* m_stc = nullptr;
};

class WXDLLIMPEXP_SDK ConsoleLexerClientData : public wxClientData
{
public:
ConsoleLexerClientData(wxStyledTextCtrl* ctrl)
: m_lexer(ctrl)
{
}
virtual ~ConsoleLexerClientData() {}

ConsoleLexer& GetLexer() { return m_lexer; }

private:
ConsoleLexer m_lexer;
};
83 changes: 56 additions & 27 deletions Plugin/ThemeImporters/ThemeImporterTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ LexerConf::Ptr_t ThemeImporterTerminal::Import(const wxFileName& theme_file)
AddProperty(lexer, wxSTC_TERMINAL_PYTHON, "Python error message", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_GCC, "GCC like error message", clColours::Red(is_dark), m_editor.bg_colour);

#if HAS_ILEXER
AddProperty(lexer, wxSTC_TERMINAL_GCC_WARNING, "GCC like warning message", clColours::Yellow(is_dark),
m_editor.bg_colour);
AddProperty(
lexer, wxSTC_TERMINAL_GCC_WARNING, "GCC like warning message", clColours::Yellow(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_GCC_NOTE, "GCC like note message", clColours::Cyan(is_dark), m_editor.bg_colour);
#endif

AddProperty(lexer, wxSTC_TERMINAL_MS, "MSVC error message", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_CMD, "CMD error message", clColours::Red(is_dark), m_editor.bg_colour);
Expand All @@ -40,56 +38,87 @@ LexerConf::Ptr_t ThemeImporterTerminal::Import(const wxFileName& theme_file)
AddProperty(lexer, wxSTC_TERMINAL_NET, ".NET error message", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_LUA, "LUA error message", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_CTAG, "CTags line", clColours::Cyan(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_DIFF_CHANGED, "Diff line changed", clColours::Yellow(is_dark),
m_editor.bg_colour);
AddProperty(
lexer, wxSTC_TERMINAL_DIFF_CHANGED, "Diff line changed", clColours::Yellow(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_DIFF_ADDITION, "Diff line added", clColours::Green(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_DIFF_DELETION, "Diff line deleted", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_DIFF_MESSAGE, "Diff line message", clColours::Cyan(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_PHP, "PHP error message", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ELF, "Essential Lahey Fortran error message", clColours::Red(is_dark),
AddProperty(lexer,
wxSTC_TERMINAL_ELF,
"Essential Lahey Fortran error message",
clColours::Red(is_dark),
m_editor.bg_colour);
AddProperty(lexer,
wxSTC_TERMINAL_IFC,
"Intel Fortran Compiler error/warning message",
clColours::Red(is_dark),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_IFC, "Intel Fortran Compiler error/warning message", clColours::Red(is_dark),
AddProperty(lexer,
wxSTC_TERMINAL_IFORT,
"Intel Fortran Compiler v8.0 error/warning message",
clColours::Red(is_dark),
m_editor.bg_colour);
AddProperty(lexer,
wxSTC_TERMINAL_ABSF,
"Absoft Pro Fortran 90/95 v8.2 error and/or warning message",
clColours::Red(is_dark),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_IFORT, "Intel Fortran Compiler v8.0 error/warning message",
clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ABSF, "Absoft Pro Fortran 90/95 v8.2 error and/or warning message",
clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_TIDY, "HTML tidy style", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_JAVA_STACK, "Java stack", clColours::Cyan(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_VALUE, "Value", m_editor);
AddProperty(lexer, wxSTC_TERMINAL_GCC_INCLUDED_FROM, "GCC 'included from'", clColours::Grey(is_dark),
m_editor.bg_colour);
AddProperty(
lexer, wxSTC_TERMINAL_GCC_INCLUDED_FROM, "GCC 'included from'", clColours::Grey(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ESCSEQ, "ANSI escape sequence", m_editor);
AddProperty(lexer, wxSTC_TERMINAL_ESCSEQ_UNKNOWN, "ANSI escape sequence unknown", clColours::Red(is_dark),
AddProperty(lexer,
wxSTC_TERMINAL_ESCSEQ_UNKNOWN,
"ANSI escape sequence unknown",
clColours::Red(is_dark),
m_editor.bg_colour);

#if HAS_ILEXER
AddProperty(lexer, wxSTC_TERMINAL_GCC_EXCERPT, "GCC code excerpt and pointer to issue", clColours::Cyan(is_dark),
AddProperty(lexer,
wxSTC_TERMINAL_GCC_EXCERPT,
"GCC code excerpt and pointer to issue",
clColours::Cyan(is_dark),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_BASH, "Bash diagnostic line", clColours::Red(is_dark), m_editor.bg_colour);
#endif

AddProperty(lexer, wxSTC_TERMINAL_ES_BLACK, "ANSI escape black", clColours::Black(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_RED, "ANSI escape red", clColours::Red(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_GREEN, "ANSI escape green", clColours::Green(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BROWN, "ANSI escape brown", clColours::Yellow(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BLUE, "ANSI escape blue", clColours::Blue(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_MAGENTA, "ANSI escape magenta", clColours::Magenta(is_dark),
m_editor.bg_colour);
AddProperty(
lexer, wxSTC_TERMINAL_ES_MAGENTA, "ANSI escape magenta", clColours::Magenta(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_CYAN, "ANSI escape cyan", clColours::Cyan(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_GRAY, "ANSI escape grey", clColours::Grey(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_YELLOW, "ANSI escape yellow", clColours::Yellow(is_dark), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_DARK_GRAY, "ANSI escape dark grey", clColours::Grey(is_dark),
AddProperty(
lexer, wxSTC_TERMINAL_ES_DARK_GRAY, "ANSI escape dark grey", clColours::Grey(is_dark), m_editor.bg_colour);
AddProperty(lexer,
wxSTC_TERMINAL_ES_BRIGHT_RED,
"ANSI escape bright red",
clColours::Red(is_dark, true),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BRIGHT_RED, "ANSI escape bright red", clColours::Red(is_dark, true),
AddProperty(lexer,
wxSTC_TERMINAL_ES_BRIGHT_GREEN,
"ANSI escape bright green",
clColours::Green(is_dark, true),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BRIGHT_GREEN, "ANSI escape bright green", clColours::Green(is_dark, true),
AddProperty(lexer,
wxSTC_TERMINAL_ES_BRIGHT_BLUE,
"ANSI escape bright blue",
clColours::Blue(is_dark, true),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BRIGHT_BLUE, "ANSI escape bright blue", clColours::Blue(is_dark, true),
AddProperty(lexer,
wxSTC_TERMINAL_ES_BRIGHT_MAGENTA,
"ANSI escape bright magenta",
clColours::Magenta(is_dark, true),
m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BRIGHT_MAGENTA, "ANSI escape bright magenta",
clColours::Magenta(is_dark, true), m_editor.bg_colour);
AddProperty(lexer, wxSTC_TERMINAL_ES_BRIGHT_CYAN, "ANSI escape bright cyan", clColours::Cyan(is_dark, true),
AddProperty(lexer,
wxSTC_TERMINAL_ES_BRIGHT_CYAN,
"ANSI escape bright cyan",
clColours::Cyan(is_dark, true),
m_editor.bg_colour);
FinalizeImport(lexer);
return lexer;
Expand Down
21 changes: 10 additions & 11 deletions Plugin/lexer_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//////////////////////////////////////////////////////////////////////////////
#include "lexer_configuration.h"

#include "ConsoleLexer.hpp"
#include "ExtraLexers.h"
#include "FontUtils.hpp"
#include "clSystemSettings.h"
Expand Down Expand Up @@ -99,6 +100,7 @@ wxColour to_wx_colour(const wxString& colour_as_string) { return wxColour(colour

void LexerConf::Apply(wxStyledTextCtrl* ctrl, bool applyKeywords)
{
ConsoleLexerClientData* console_lexer = nullptr;
#if HAS_ILEXER
// Apply the lexer
switch (GetLexerId()) {
Expand All @@ -116,8 +118,10 @@ void LexerConf::Apply(wxStyledTextCtrl* ctrl, bool applyKeywords)
// Apply the lexer
switch (GetLexerId()) {
case wxSTC_LEX_TERMINAL:
// use the old lexer
ctrl->SetLexer(wxSTC_LEX_ERRORLIST);
// Use a container lexer and attach it to the control
ctrl->SetLexer(wxSTC_LEX_CONTAINER);
console_lexer = new ConsoleLexerClientData(ctrl);
ctrl->SetClientObject(console_lexer);
break;
default:
// Standard lexers
Expand All @@ -127,6 +131,10 @@ void LexerConf::Apply(wxStyledTextCtrl* ctrl, bool applyKeywords)
#endif

ctrl->StyleClearAll();
if (console_lexer) {
console_lexer->GetLexer().SetStyles();
}

ctrl->FoldDisplayTextSetStyle(wxSTC_FOLDDISPLAYTEXT_BOXED);
ctrl->SetIdleStyling(wxSTC_IDLESTYLING_TOVISIBLE);

Expand Down Expand Up @@ -175,15 +183,6 @@ void LexerConf::Apply(wxStyledTextCtrl* ctrl, bool applyKeywords)
ctrl->StyleSetVisible(wxSTC_TERMINAL_ESCSEQ, false);
ctrl->StyleSetVisible(wxSTC_TERMINAL_ESCSEQ_UNKNOWN, false);
}
#else
if (ctrl->GetLexer() == wxSTC_LEX_ERRORLIST) {
ctrl->SetProperty("lexer.errorlist.escape.sequences", "1");
ctrl->SetProperty("lexer.errorlist.value.separate", "1");

// Hide escape sequence styles
ctrl->StyleSetVisible(wxSTC_ERR_ESCSEQ, false);
ctrl->StyleSetVisible(wxSTC_ERR_ESCSEQ_UNKNOWN, false);
}
#endif

// Find the default style
Expand Down
25 changes: 18 additions & 7 deletions Plugin/wxTerminalCtrl/wxTerminalCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ INITIALISE_MODULE_LOG(TERM_LOG, "Terminal", "terminal.log");

wxTerminalCtrl::wxTerminalCtrl() {}

wxTerminalCtrl::wxTerminalCtrl(wxWindow* parent, wxWindowID winid, const wxString& working_directory,
const wxPoint& pos, const wxSize& size, long style, const wxString& name)
wxTerminalCtrl::wxTerminalCtrl(wxWindow* parent,
wxWindowID winid,
const wxString& working_directory,
const wxPoint& pos,
const wxSize& size,
long style,
const wxString& name)
{
if (!Create(parent, winid, pos, size, style)) {
return;
Expand Down Expand Up @@ -61,15 +66,18 @@ wxTerminalCtrl::~wxTerminalCtrl()
Unbind(wxEVT_ASYNC_PROCESS_TERMINATED, &wxTerminalCtrl::OnProcessTerminated, this);
}

bool wxTerminalCtrl::Create(wxWindow* parent, wxWindowID winid, const wxPoint& pos, const wxSize& size, long style,
const wxString& name)
bool wxTerminalCtrl::Create(
wxWindow* parent, wxWindowID winid, const wxPoint& pos, const wxSize& size, long style, const wxString& name)
{
Bind(wxEVT_ASYNC_PROCESS_OUTPUT, &wxTerminalCtrl::OnProcessOutput, this);
Bind(wxEVT_ASYNC_PROCESS_STDERR, &wxTerminalCtrl::OnProcessError, this);
Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &wxTerminalCtrl::OnProcessTerminated, this);
EventNotifier::Get()->Bind(wxEVT_WORKSPACE_LOADED, &wxTerminalCtrl::OnWorkspaceLoaded, this);
m_style = style & ~wxWINDOW_STYLE_MASK; // Remove all wxWindow style masking (Hi Word)
return wxPanel::Create(parent, winid, pos, size,
return wxPanel::Create(parent,
winid,
pos,
size,
style & wxWINDOW_STYLE_MASK); // Pass only the Windows related styles
}

Expand All @@ -78,7 +86,8 @@ void wxTerminalCtrl::StartShell()
wxString shell_exec;
if (m_shellCommand.CmpNoCase("bash") == 0) {
if (!ThePlatform->Which(m_shellCommand, &shell_exec)) {
wxMessageBox(wxString() << _("Unable to find ") << m_shellCommand << ". Can't start a terminal", "CodeLite",
wxMessageBox(wxString() << _("Unable to find ") << m_shellCommand << ". Can't start a terminal",
"CodeLite",
wxICON_WARNING | wxOK | wxCENTRE | wxOK_DEFAULT);
return;
}
Expand Down Expand Up @@ -192,7 +201,9 @@ void wxTerminalCtrl::SetAttributes(const wxColour& bg_colour, const wxColour& te

void wxTerminalCtrl::OnProcessOutput(clProcessEvent& event)
{
m_processOutput << event.GetOutput();
wxString s = event.GetOutput();
s.Replace("\r", "");
m_processOutput << s;
ProcessOutputBuffer();
}

Expand Down
1 change: 1 addition & 0 deletions Plugin/wxTerminalCtrl/wxTerminalOutputCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ void wxTerminalOutputCtrl::ApplyTheme()
if (lexer) {
lexer->Apply(m_ctrl);
}
m_ctrl->SetEOLMode(wxSTC_EOL_LF);
m_ctrl->Refresh();
}

Expand Down
2 changes: 1 addition & 1 deletion submodules/lexilla

0 comments on commit ce720bd

Please sign in to comment.