added multiple highlighter support

development
Michael Ochmann 8 years ago
parent 1077799f63
commit 154289558a
  1. 2
      CMakeLists.txt
  2. 1
      main.cpp
  3. 15
      src/Buffer.hpp
  4. 12
      src/Editor.cpp
  5. 42
      src/Highlighter.cpp
  6. 10
      src/Highlighter.hpp
  7. 5
      src/History.cpp
  8. 6
      src/History.hpp
  9. 12
      src/modes/Edit.cpp

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.7)
project(groove C CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 14)
include_directories(src)

@ -26,6 +26,7 @@
#include <iostream>
#include <ncurses/ncurses.hpp>
#include <Editor.hpp>
#include <Highlighter.hpp>
int main(int argc, char* argv[]) {
std::unique_ptr<groove::Editor> editor;

@ -3,12 +3,15 @@
#include <vector>
#include <string>
#include <memory>
#include <stack>
namespace groove {
class Buffer {
private:
std::vector<std::string> lines;
std::vector<std::string> reverted;
std::stack<std::vector<std::string>> history;
void removeTabs(std::string& line) {
ulong tab = line.find("\t");
if(tab != line.npos)
@ -16,7 +19,7 @@ namespace groove {
}
public:
bool changed;
Buffer() : lines(std::vector<std::string>()), changed(false) {}
Buffer() : lines(std::vector<std::string>()), reverted(std::vector<std::string>()), changed(false) {}
std::vector<std::string>& linebuffer() {
return this->lines;
}
@ -56,6 +59,16 @@ namespace groove {
return this->lines.at(line);
}
void swap(std::vector<std::string> buffer) {
this->history.push(this->lines);
this->lines = buffer;
}
void revert() {
this->lines = this->history.top();
this->history.pop();
}
unsigned long size() {
return this->lines.size();
}

@ -9,6 +9,18 @@ namespace groove {
buffer(std::make_unique<Buffer>()), mode_(Mode::EDIT),
filename(filename), offset(0), voffset(0),
history(History()), inComment(false) {
std::string ending;
if (filename.find_last_of('.') != std::string::npos)
ending = std::string(filename.begin() + filename.find_last_of('.'), filename.end());
else
ending = "";
if (ending == ".cpp" || ending == ".hpp" || ending == ".h" || ending == ".c")
groove::Highlighter::list = groove::Highlighter::Syntaxes.at(groove::Syntax::CPP);
else
groove::Highlighter::list = groove::Highlighter::Syntaxes.at(groove::Syntax::DEFAULT);
this->modes.emplace(Mode::INSERT, std::make_unique<modes::Insert>(*this));
this->modes.emplace(Mode::EDIT, std::make_unique<modes::Edit>(*this));
this->modes.emplace(Mode::QUIT, std::make_unique<modes::Quit>(*this));

@ -3,20 +3,32 @@
#include <iostream>
namespace groove {
std::vector<std::pair<std::regex, ncurses::Colors>> Highlighter::list = {
make_pair("([+-.<>,;=!:&*])", ncurses::Colors::CYAN),
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN),
make_pair("(^|\\s)(while|if|try|catch|void|this|else|using|namespace|private|public|protected|friend|class|char|bool|unsigned|long|short|int|return)\\s+\\*?", ncurses::Colors::MAGENTA),
make_pair("([a-zA-Z_][a-zA-Z_0-9]+)::", ncurses::Colors::GREEN),
make_pair("::([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
make_pair("\\.([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
make_pair("([_a-zA-Z][a-zA-Z0-9_-]+)\\(", ncurses::Colors::CYAN),
make_pair("\\\".*\\\"", ncurses::Colors::ORANGE),
make_pair("/\\*.*\\*//*", ncurses::Colors::COMMENTS),
make_pair("(//.*)", ncurses::Colors::ORANGE)
std::unordered_map<Syntax, RulesList> Highlighter::Syntaxes = {
std::make_pair(Syntax::CPP,
RulesList {
make_pair("([+-.<>,;=!:&*])", ncurses::Colors::CYAN),
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN),
make_pair("(^|\\s)(while|if|try|catch|void|this|else|using|namespace|private|public|protected|friend|class|char|bool|unsigned|long|short|int|return):?(\\s+|$)\\*?", ncurses::Colors::MAGENTA),
make_pair("([a-zA-Z_][a-zA-Z_0-9]+)::", ncurses::Colors::GREEN),
make_pair("::([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
make_pair("\\.([a-zA-Z_][a-zA-Z_0-9]+)", ncurses::Colors::CYAN),
make_pair("([_a-zA-Z][a-zA-Z0-9_-]+)\\(", ncurses::Colors::CYAN),
make_pair("\\\".*\\\"", ncurses::Colors::ORANGE),
make_pair("/\\*.*\\*//*", ncurses::Colors::COMMENTS),
make_pair("(//.*)", ncurses::Colors::ORANGE)
}),
std::make_pair(Syntax::DEFAULT,
RulesList {
make_pair("([.,;!\\*])", ncurses::Colors::MAGENTA),
make_pair("([\\{\\}\\[\\]\\(\\)])", ncurses::Colors::GREEN),
make_pair("\\\".*\\\"", ncurses::Colors::ORANGE),
make_pair("/\\*.*\\*//*", ncurses::Colors::COMMENTS),
})
};
RulesList Highlighter::list = RulesList();
std::unordered_map<long, std::pair<long, ncurses::Colors>> groove::Highlighter::get() {
std::unordered_map<long, std::pair<long, ncurses::Colors>> list;
@ -26,7 +38,7 @@ namespace groove {
return list;
}
for (auto& keyword : Highlighter::list) {
for (auto &keyword : Highlighter::list) {
try {
std::sregex_iterator next(this->line.begin(), this->line.end(), keyword.first);
std::sregex_iterator end;
@ -37,7 +49,7 @@ namespace groove {
}
next++;
}
} catch (std::regex_error& e) {
} catch (std::regex_error &e) {
// Syntax error in the regular expression
}
}
@ -47,3 +59,5 @@ namespace groove {
}
}

@ -8,15 +8,23 @@
namespace groove {
typedef std::vector<std::pair<std::regex, ncurses::Colors>> RulesList;
inline std::pair<std::regex, ncurses::Colors> make_pair(std::string regex, ncurses::Colors color) {
return std::make_pair(std::regex(regex), color);
};
enum class Syntax {
DEFAULT,
CPP
};
class Highlighter {
private:
std::string line;
static std::vector<std::pair<std::regex, ncurses::Colors>> list;
public:
static std::unordered_map<Syntax, RulesList> Syntaxes;
static RulesList list;
Highlighter(std::string line) : line(line) {}
std::unordered_map<long, std::pair<long, ncurses::Colors>> get();
};

@ -1,5 +0,0 @@
//
// Created by miko on 06.03.17.
//
#include "History.hpp"

@ -6,9 +6,11 @@
namespace groove {
typedef std::function<void()> lambda;
struct HistoryNode {
std::function<void()> undo, redo;
HistoryNode(std::function<void()> undo, std::function<void()> redo) :
lambda undo, redo;
HistoryNode(lambda undo, lambda redo) :
undo(std::move(undo)), redo(std::move(redo)) {}
};

@ -14,7 +14,17 @@ namespace groove {
this->editor.input(' ');
break;
case 'i':
this->editor.mode_ = groove::Mode::INSERT;
{
std::vector<std::string>& oldBuffer = this->editor.buffer->linebuffer();
this->editor.history.push(HistoryNode(
[this, oldBuffer](){
this->editor.buffer->swap(oldBuffer);
}, [this](){
this->editor.buffer->revert();
})
);
this->editor.mode_ = groove::Mode::INSERT;
}
break;
case 'a':
this->editor.x = static_cast<int>(this->editor.buffer->at(this->editor.y).length());

Loading…
Cancel
Save