Better handling of diagnostics in the REPL.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Deukhoofd 2021-01-05 18:17:40 +01:00
parent 340d37f4a1
commit bfe27ec20f
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 36 additions and 22 deletions

View File

@ -17,6 +17,7 @@ namespace MalachScriptRepl {
int _lineCount; int _lineCount;
std::function<void(const std::vector<std::u8string>&)> _onChange; std::function<void(const std::vector<std::u8string>&)> _onChange;
const MalachScript::Diagnostics::Diagnostic* _diag;
public: public:
InputWindow(int height, int width, int y, int x) { InputWindow(int height, int width, int y, int x) {
@ -111,6 +112,7 @@ namespace MalachScriptRepl {
} }
wmove(_window, _row, _col); wmove(_window, _row, _col);
} }
_onChange(_lines);
} }
inline void Tab() { inline void Tab() {
wmove(_window, _row, 0); wmove(_window, _row, 0);
@ -158,14 +160,14 @@ namespace MalachScriptRepl {
} }
inline void ResetText() { inline void ResetText() {
auto pos = GetCursorPosition(); std::u8string script;
wclear(_window); for (size_t i = 0; i < _lines.size(); i++) {
for (size_t i = 0; i < _lines.size() && (int)i < _lineCount; i++) { script += _lines[i];
wmove(_window, i, 0); if (i != _lines.size() - 1) {
waddnstr(_window, (char*)_lines[i + _scroll].c_str(), _lines[i + _scroll].size()); script += '\n';
}
} }
MoveCursorPosition(pos.first, pos.second); SetScriptWithDiagnostics(script, _diag);
Refresh();
} }
inline void ScrollDown() { inline void ScrollDown() {
@ -194,15 +196,16 @@ namespace MalachScriptRepl {
wmove(_window, _row, _col); wmove(_window, _row, _col);
} }
inline void SetScriptWithDiagnostics(const std::u8string& script, inline void SetScriptWithDiagnostics(std::u8string& script, const MalachScript::Diagnostics::Diagnostic* diag) {
const MalachScript::Diagnostics::Diagnostic* diag) { _diag = diag;
auto yx = GetCursorPosition(); auto yx = GetCursorPosition();
Clear(); Clear();
script += u8" ";
size_t linenum = 0; size_t linenum = 0;
size_t index = 0; size_t index = 0;
size_t linestart = 0; size_t linestart = 0;
size_t lineend = 0;
std::istringstream f((char*)script.data()); std::istringstream f((char*)script.data());
std::string line; std::string line;
@ -210,25 +213,36 @@ namespace MalachScriptRepl {
if ((int)linenum == _scroll) { if ((int)linenum == _scroll) {
linestart = index; linestart = index;
} }
if ((int)linenum == _scroll + _lineCount - 1 || linenum == _lines.size() - 1) {
lineend = index + line.size() + 1;
break;
}
index += line.size() + 1; index += line.size() + 1;
linenum++; linenum++;
} }
if (lineend == 0) {
lineend = script.size();
}
if (diag != nullptr) { if (diag != nullptr && diag->GetSpan().GetStart() >= linestart && diag->GetSpan().GetStart() <= lineend) {
auto start = diag->GetSpan().GetStart() - linestart; auto start = diag->GetSpan().GetStart() - linestart;
auto end = diag->GetSpan().GetEnd() - linestart; auto end = diag->GetSpan().GetEnd() - linestart + 1;
if (end > script.size()) { if (diag->GetSpan().GetStart() > script.size()) {
end = script.size(); start = script.size() - linestart - 1;
if (start == end && start > 0) { }
start--; if (diag->GetSpan().GetEnd() >= script.size()) {
} end = script.size() - linestart;
} }
waddnstr(_window, (char*)script.substr(linestart).data(), start); waddnstr(_window, (char*)script.substr(linestart, start).data(), start);
wattron(_window, COLOR_PAIR(1)); wattron(_window, COLOR_PAIR(1));
waddnstr(_window, (char*)script.substr(start + linestart).data(), end - start); wattron(_window, A_UNDERLINE);
waddnstr(_window, (char*)script.substr(start + linestart, end - start).data(), end - start);
wattroff(_window, A_UNDERLINE);
wattroff(_window, COLOR_PAIR(1)); wattroff(_window, COLOR_PAIR(1));
waddnstr(_window, (char*)script.substr(end + linestart).data(), script.size() - end);
waddnstr(_window, (char*)script.substr(end + linestart, script.size() - end + linestart + 1).data(),
script.size() - end + linestart + 1);
if (start >= script.size() - 1) { if (start >= script.size() - 1) {
wattron(_window, COLOR_PAIR(1)); wattron(_window, COLOR_PAIR(1));

View File

@ -70,7 +70,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char* argv[]) {
set_tabsize(4); set_tabsize(4);
start_color(); start_color();
init_pair(1, COLOR_BLACK, COLOR_RED); init_pair(1, COLOR_RED, COLOR_BLACK);
clear(); clear();
int maxX; int maxX;

View File

@ -25,7 +25,7 @@ namespace MalachScript::Parser {
auto start = _position; auto start = _position;
auto c = Consume(); auto c = Consume();
switch (c) { switch (c) {
case u8'\0': return Create<LexTokenImpl<LexTokenKind::EndOfFile>>(TextSpan(start, start + 1)); case u8'\0': return Create<LexTokenImpl<LexTokenKind::EndOfFile>>(TextSpan(start + 1, start + 2));
case u8'*': { case u8'*': {
auto n = Peek(); auto n = Peek();
if (n == u8'*') { if (n == u8'*') {