-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteLine.cpp
64 lines (58 loc) · 1.43 KB
/
DeleteLine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "DeleteLine.hpp"
#include "EditorException.hpp"
DeleteLine::DeleteLine()
: prevRow{1}, prevCol{1}
{
}
void DeleteLine::execute(EditorModel& model)
{
prevRow = model.cursorLine();
prevCol = model.cursorColumn();
prevText = model.line(prevRow);
if (model.lineCount() == 1 && model.line(prevRow) == "")
{
EditorException e{"Already empty"};
throw e;
}
unsigned int prevLineNum = prevRow - 1;
if (prevRow == 1)
{
if (model.lineCount() == 1)
{
std::string empty = "";
model.setText(empty, prevRow);
model.setCol(1);
}
else
{
if (prevCol > model.line(prevRow+1).size())
{
model.setCol(model.line(prevRow+1).size()+1);
}
model.deleteLine(prevRow);
}
}
else if (prevRow == model.lineCount())
{
if (prevCol > model.line(prevLineNum).size())
{
model.setCol(model.line(prevLineNum).size()+1);
}
model.deleteLine(prevRow);
model.setRow(prevLineNum);
}
else
{
if (prevCol > model.line(prevRow+1).size())
{
model.setCol(model.line(prevRow+1).size()+1);
}
model.deleteLine(prevRow);
}
}
void DeleteLine::undo(EditorModel& model)
{
model.setRow(prevRow);
model.setCol(prevCol);
model.createNewLine(prevText, prevRow-1);
}