Skip to content

Commit acb08fd

Browse files
C++, Python coloring test
1 parent 104dad8 commit acb08fd

2 files changed

Lines changed: 96 additions & 37 deletions

File tree

include/text-editor/editor.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ struct EditorObserver{
1414
struct Editor : turbo::TScintillaParent{
1515

1616
public:
17+
enum class Language {
18+
None,
19+
Cpp,
20+
Python,
21+
//
22+
};
23+
1724
Editor();
1825
void setSize(const TPoint& size);
1926
void paint(TDrawSurface& surface, TRect area);
@@ -38,8 +45,8 @@ struct Editor : turbo::TScintillaParent{
3845

3946
private:
4047
void updateAll();
41-
void configureStyling(Scintilla::ILexer5* lexer);
42-
Scintilla::ILexer5* getLexerForExtension(const std::string& path);
48+
void configureStyling(Scintilla::ILexer5* lexer, Language lang);
49+
std::pair<Scintilla::ILexer5*, Editor::Language> getLexerForExtension(const std::string& path);
4350

4451
private:
4552
std::string path;

source/text-editor/editor.cpp

Lines changed: 87 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <scintilla/lexlib/LexerModule.h>
55

66
extern Scintilla::LexerModule lmCPP;
7+
extern Scintilla::LexerModule lmPython;
78

89
Editor::Editor(){
910
scintilla.setParent(this);
@@ -84,46 +85,34 @@ void Editor::setHorizontalScrollPos(int delta, int limit) noexcept{
8485
static char buffer alignas(4*1024) [128*1024 + 1024];
8586
long buffSize = 128*1024;
8687

87-
Scintilla::ILexer5* Editor::getLexerForExtension(const std::string& path) {
88+
std::pair<Scintilla::ILexer5*, Editor::Language> Editor::getLexerForExtension(const std::string& path) {
8889

8990
// std::string ext = std::filesystem::path(path).extension().string();
9091

9192
size_t pos = path.rfind('.');
9293
std::string ext;
93-
if(pos != std::string::npos) {
94+
if(pos != std::string::npos)
9495
ext = path.substr(pos);
95-
}
96-
9796

98-
if(ext == ".cpp" || ext == ".cxx" || ext == ".cc" || ext == ".hpp" || ext == ".h") {
99-
return lmCPP.Create(); // lexer C++
100-
}
97+
if(ext == ".cpp" || ext == ".cxx" || ext == ".cc" || ext == ".hpp" || ext == ".h")
98+
return { lmCPP.Create(), Language::Cpp };
99+
if(ext == ".py")
100+
return { lmPython.Create(), Language::Python };
101101

102-
// other languages
103-
104-
return nullptr;
102+
return { nullptr, Language::None };
105103
}
106104

107105
void Editor::openFile(const std::string& path){
108106
this->path = path;
109107
// Scintilla::ILexer5* lexer = lmCPP.Create();
110108
// configureStyling(lexer);
111109

112-
Scintilla::ILexer5* lexer = getLexerForExtension(path);
113-
114-
// default coloring
115-
TColorAttr textColor = {0xFFFFFF, 0x000000};
116-
turbo::setStyleColor(scintilla, STYLE_DEFAULT, textColor);
117-
118-
TColorAttr selectionColor = {0xFFFFFF, 0x444466};
119-
turbo::setSelectionColor(scintilla, selectionColor);
110+
// Scintilla::ILexer5* lexer = getLexerForExtension(path);
111+
112+
// configureStyling(lexer);
120113

121-
if(lexer) {
122-
configureStyling(lexer);
123-
} else {
124-
turbo::call(scintilla, SCI_SETLEXER, SCLEX_NULL, 0);
125-
turbo::call(scintilla, SCI_STYLECLEARALL, 0, 0);
126-
}
114+
auto [lexer, lang] = getLexerForExtension(path);
115+
configureStyling(lexer, lang);
127116
}
128117

129118
void Editor::readFile(){
@@ -222,7 +211,50 @@ std::pair<std::string, std::string> propertiesC[] =
222211
{"lexer.cpp.escape.sequence", "1"},
223212
};
224213

225-
void Editor::configureStyling(Scintilla::ILexer5* lexer){
214+
215+
constexpr std::array<int, 4> stylesPython[] =
216+
{
217+
{SCE_P_DEFAULT, 0xFFFFFF, 0x000000},
218+
{SCE_P_COMMENTLINE, 0x008000, 0x000000},
219+
{SCE_P_NUMBER, 0x00FF00, 0x000000},
220+
{SCE_P_STRING, 0xFFFF00, 0x000000},
221+
{SCE_P_CHARACTER, 0x0000FF, 0x000000},
222+
{SCE_P_WORD, 0x0000FF, 0x000000},
223+
{SCE_P_TRIPLE, 0xFFFF00, 0x000000},
224+
{SCE_P_TRIPLEDOUBLE, 0xFFFF00, 0x000000},
225+
{SCE_P_CLASSNAME, 0x00FFFF, 0x000000},
226+
{SCE_P_DEFNAME, 0x00FFFF, 0x000000},
227+
{SCE_P_OPERATOR, 0xAA00BB, 0x000000},
228+
{SCE_P_IDENTIFIER, 0xFFFFFF, 0x000000},
229+
{SCE_P_COMMENTBLOCK, 0x008000, 0x000000},
230+
{SCE_P_STRINGEOL, 0xFFFF00, 0x000000},
231+
{SCE_P_WORD2, 0x00AAAA, 0x000000},
232+
{SCE_P_DECORATOR, 0xFF8800, 0x000000},
233+
{SCE_P_FSTRING, 0xFFFF00, 0x000000},
234+
{SCE_P_FCHARACTER, 0x0000FF, 0x000000},
235+
{SCE_P_FTRIPLE, 0xFFFF00, 0x000000},
236+
{SCE_P_FTRIPLEDOUBLE, 0xFFFF00, 0x000000}
237+
};
238+
239+
std::pair<int, std::string> keywordsPython[] =
240+
{
241+
{0,
242+
"and as assert break class continue def del elif else except exec finally for "
243+
"from global if import in is lambda not or pass print raise return try while "
244+
"with yield"
245+
},
246+
{1,
247+
"int float complex list tuple range str bytes bytearray memoryview set frozenset "
248+
"dict "
249+
},
250+
};
251+
252+
std::pair<std::string, std::string> propertiesPython[] =
253+
{
254+
{"lexer.python.keywords2.no.sub.identifiers", "1"},
255+
};
256+
257+
void Editor::configureStyling(Scintilla::ILexer5* lexer, Language lang){
226258
//Style configuration
227259
TColorAttr textColor = {0xFFFFFF, 0x000000};
228260
turbo::setStyleColor(scintilla, STYLE_DEFAULT, textColor);
@@ -231,6 +263,11 @@ void Editor::configureStyling(Scintilla::ILexer5* lexer){
231263
TColorAttr selectionColor = {0xFFFFFF, 0x444466};
232264
turbo::setSelectionColor(scintilla, selectionColor);
233265

266+
if(!lexer) {
267+
turbo::call(scintilla, SCI_SETLEXER, SCLEX_NULL, 0);
268+
return;
269+
}
270+
234271
//lexer configuration
235272
int id = turbo::call(scintilla, SCI_GETLEXER, 0, 0);
236273
std::cerr << "Id before setilexer " << id << "\n";
@@ -239,16 +276,31 @@ void Editor::configureStyling(Scintilla::ILexer5* lexer){
239276
std::cerr << "Id after setilexer " << id << "\n";
240277
turbo::call(scintilla, SCI_COLOURISE, 0, -1);
241278

242-
//color of comments
243-
for(int i = 0; i < 16; i++){
244-
TColorAttr color = {stylesC[i][2], stylesC[i][3]};
245-
turbo::setStyleColor(scintilla, stylesC[i][0], color);
246-
}
247279

248-
for (int i = 0; i < 3; i++){
249-
turbo::call(scintilla, SCI_SETKEYWORDS, keywordsC[i].first, (sptr_t)keywordsC[i].second.c_str());
250-
}
251-
for(int i = 0; i < 3; i++){
252-
turbo::call(scintilla, SCI_SETPROPERTY, (sptr_t) propertiesC[i].first.c_str(), (sptr_t)propertiesC[i].second.c_str());
280+
switch (lang) {
281+
case Language::Cpp:
282+
for (int i = 0; i < 16; i++) {
283+
TColorAttr color = {stylesC[i][2], stylesC[i][3]};
284+
turbo::setStyleColor(scintilla, stylesC[i][0], color);
285+
}
286+
for (int i = 0; i < 3; i++)
287+
turbo::call(scintilla, SCI_SETKEYWORDS, keywordsC[i].first, (sptr_t)keywordsC[i].second.c_str());
288+
for (int i = 0; i < 3; i++)
289+
turbo::call(scintilla, SCI_SETPROPERTY, (sptr_t)propertiesC[i].first.c_str(), (sptr_t)propertiesC[i].second.c_str());
290+
break;
291+
292+
case Language::Python:
293+
for (int i = 0; i < 20; i++) {
294+
TColorAttr color = {stylesPython[i][1], stylesPython[i][2]};
295+
turbo::setStyleColor(scintilla, stylesPython[i][0], color);
296+
}
297+
for (int i = 0; i < 2; i++)
298+
turbo::call(scintilla, SCI_SETKEYWORDS, keywordsPython[i].first, (sptr_t)keywordsPython[i].second.c_str());
299+
for (int i = 0; i < 1; i++)
300+
turbo::call(scintilla, SCI_SETPROPERTY, (sptr_t)propertiesPython[i].first.c_str(), (sptr_t)propertiesPython[i].second.c_str());
301+
break;
302+
303+
default:
304+
break;
253305
}
254306
}

0 commit comments

Comments
 (0)