-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.hpp
More file actions
170 lines (136 loc) · 4.14 KB
/
Editor.hpp
File metadata and controls
170 lines (136 loc) · 4.14 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2022 Daniel Liu
// Object for representing an editor.
#include <array>
#include <filesystem>
#include <list>
#include <string>
#include <vector>
#include "dcurses/WindowManager.hpp"
#define NUM_REGS 10
#ifndef DVIM_EDITOR_HPP_
#define DVIM_EDITOR_HPP_
namespace dvim {
class Editor {
public:
/*
* Initialize the editor with the specified file path and window manager.
*/
Editor(const std::filesystem::path &path, dcurses::WindowManager &manager);
/*
* Handle a single character input action.
*/
void handleInput(char ch);
/*
* Get the usage hints for the current mode.
*/
std::vector<std::string> getUsageHints() const;
/*
* Returns a list of the current lines, with the cursor highlighted.
*/
std::vector<std::string> getLines(unsigned int width);
/*
* Returns the current cursor line.
*/
unsigned int getCursorLine() const { return cursorLine_; }
/*
* Returns the current cursor column.
*/
unsigned int getCursorColumn() const { return cursorColumn_; }
/*
* Returns the line that the cursor is on in the most recent call to getLines.
*/
unsigned int getCursorScroll() const { return cursorScroll_; }
/*
* Returns the queued actions currently.
*/
std::string getQueuedActions() const { return queuedActions_; }
/*
* Returns the current command contents.
*/
std::string getCommandContents() const { return queuedActions_; }
/*
* Returns the current error message (if any).
*/
std::string getErrorMessage() const { return errorMessage_; }
/*
* Returns the current mode.
*/
std::string getMode() const {
switch (mode) {
case EditorMode::STOPPED:
return "STOPPED";
case EditorMode::ERROR:
return "ERROR";
case EditorMode::NORMAL:
return "NORMAL";
case EditorMode::INSERT:
return "INSERT";
case EditorMode::COMMAND:
return "COMMAND";
case EditorMode::VISUAL:
return "VISUAL";
case EditorMode::REGWINDOW:
return "REG SHOW";
default:
return "UNKNOWN";
}
}
private:
enum EditorMode {
STOPPED,
ERROR,
NORMAL,
INSERT,
COMMAND,
VISUAL,
REGWINDOW
};
void normalInput(char c);
void insertInput(char c);
void commandInput(char c);
void visualInput(char c);
void regWindowInput(char c);
// Common movement
void moveCursorLeft();
void moveCursorRight();
void moveCursorUp();
void moveCursorDown();
bool isSingleNavigationAction(char c);
bool isSingleInPlaceEditAction(char c);
bool isQueueableNormalAction(char c);
void executeNormalAction(char c);
void executeDeleteAction(char c);
void executeCommand();
EditorMode mode = EditorMode::NORMAL;
dcurses::WindowManager &manager_;
std::filesystem::path path_;
std::list<std::list<char>> lines_;
unsigned int cursorLine_ = 0;
unsigned int cursorColumn_ = 0;
unsigned int cursorScroll_ = 0;
// Invariants:
// If NORMAL, COMMAND, or VISUAL mode:
// - cursorLineIterator_ refers to the line that the cursor is on.
// cursorLineIterator_ may be an end iterator if and only if there are no lines.
// - cursorColIterator_ refers to the column that the cursor is on.
// cursorColIterator_ may be an end iterator if and only if the line has no content.
// If INSERT mode:
// - cursorLineIterator_ refers to the line that the cursor is on.
// cursorLineIterator_ may be an end iterator if and only if there are no lines.
// - cursorColIterator_ refers to the column that the cursor is on.
// cursorColIterator_ may be an end iterator if and only if the cursor is at the end of the line (past the last character).
std::list<std::list<char>>::iterator cursorLineIterator_;
std::list<char>::iterator cursorColIterator_;
// Editor variables
std::array<std::string, NUM_REGS> registers_;
unsigned int activeRegister_ = 0;
// Mode-specific variables
std::string errorMessage_ = "";
unsigned int visualStartLine_ = 0;
unsigned int visualStartColumn_ = 0;
std::list<std::list<char>>::iterator visualStartLineIterator_;
std::list<char>::iterator visualStartColIterator_;
std::string queuedActions_ = "";
};
};
#endif