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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Scintilla source code edit control
/** @file EditModel.cxx
** Defines the editor state that must be visible to EditorView.
**/
// Copyright 1998-2014 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <cstddef>
#include <cstdlib>
#include <cstdint>
#include <cassert>
#include <cstring>
#include <cmath>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <map>
#include <set>
#include <optional>
#include <algorithm>
#include <memory>
#include "ScintillaTypes.h"
#include "ILoader.h"
#include "ILexer.h"
#include "Debugging.h"
#include "Geometry.h"
#include "Platform.h"
#include "CharacterCategoryMap.h"
#include "Position.h"
#include "UniqueString.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
#include "ContractionState.h"
#include "CellBuffer.h"
#include "Indicator.h"
#include "LineMarker.h"
#include "Style.h"
#include "ViewStyle.h"
#include "CharClassify.h"
#include "Decoration.h"
#include "CaseFolder.h"
#include "Document.h"
#include "UniConversion.h"
#include "Selection.h"
#include "PositionCache.h"
#include "EditModel.h"
using namespace Scintilla;
using namespace Scintilla::Internal;
Caret::Caret() noexcept :
active(false), on(false), period(500) {}
void ModelState::RememberSelectionForUndo(int index, const Selection &sel) {
historyForUndo.indexCurrent = index;
historyForUndo.ssCurrent = sel.ToString();
}
void ModelState::ForgetSelectionForUndo() noexcept {
historyForUndo.indexCurrent = -1;
}
void ModelState::RememberSelectionOntoStack(int index, Sci::Line topLine) {
if ((historyForUndo.indexCurrent >= 0) && (index == historyForUndo.indexCurrent + 1)) {
// Don't overwrite initial selection save if most recent action was coalesced
historyForUndo.stack[index] = { historyForUndo.ssCurrent, topLine };
}
}
void ModelState::RememberSelectionForRedoOntoStack(int index, const Selection &sel, Sci::Line topLine) {
historyForRedo.stack[index] = { sel.ToString(), topLine };
}
SelectionWithScroll ModelState::SelectionFromStack(int index, UndoRedo history) const {
const SelectionHistory &sh = history == UndoRedo::undo ? historyForUndo : historyForRedo;
const SelectionStack::const_iterator it = sh.stack.find(index);
if (it != sh.stack.end()) {
return it->second;
}
return {};
}
void ModelState::TruncateUndo(int index) {
const SelectionStack::const_iterator itUndo = historyForUndo.stack.find(index);
historyForUndo.stack.erase(itUndo, historyForUndo.stack.end());
const SelectionStack::const_iterator itRedo = historyForRedo.stack.find(index);
historyForRedo.stack.erase(itRedo, historyForRedo.stack.end());
}
EditModel::EditModel() : braces{} {
inOverstrike = false;
xOffset = 0;
trackLineWidth = false;
posDrag = SelectionPosition(Sci::invalidPosition);
braces[0] = Sci::invalidPosition;
braces[1] = Sci::invalidPosition;
bracesMatchStyle = StyleBraceBad;
highlightGuideColumn = 0;
hasFocus = false;
primarySelection = true;
imeInteraction = IMEInteraction::Windowed;
bidirectional = Bidirectional::Disabled;
foldFlags = FoldFlag::None;
foldDisplayTextStyle = FoldDisplayTextStyle::Hidden;
hotspot = Range(Sci::invalidPosition);
hotspotSingleLine = true;
hoverIndicatorPos = Sci::invalidPosition;
wrapWidth = LineLayout::wrapWidthInfinite;
reprs = std::make_unique<SpecialRepresentations>();
pdoc = new Document(DocumentOption::Default);
pdoc->AddRef();
pcs = ContractionStateCreate(pdoc->IsLarge());
}
EditModel::~EditModel() {
try {
// Erasing the view state won't throw even though SetViewState
// and the resulting map::erase aren't marked noexcept.
pdoc->SetViewState(this, {});
// This never throws but isn't marked noexcept for compatibility
pdoc->Release();
} catch (...) {
// Ignore any exception
}
pdoc = nullptr;
}
bool EditModel::BidirectionalEnabled() const noexcept {
return (bidirectional != Bidirectional::Disabled) &&
(CpUtf8 == pdoc->dbcsCodePage);
}
bool EditModel::BidirectionalR2L() const noexcept {
return bidirectional == Bidirectional::R2L;
}
SurfaceMode EditModel::CurrentSurfaceMode() const noexcept {
return SurfaceMode(pdoc->dbcsCodePage, BidirectionalR2L());
}
void EditModel::SetDefaultFoldDisplayText(const char *text) {
defaultFoldDisplayText = IsNullOrEmpty(text) ? UniqueString() : UniqueStringCopy(text);
}
const char *EditModel::GetDefaultFoldDisplayText() const noexcept {
return defaultFoldDisplayText.get();
}
const char *EditModel::GetFoldDisplayText(Sci::Line lineDoc) const noexcept {
if (foldDisplayTextStyle == FoldDisplayTextStyle::Hidden || pcs->GetExpanded(lineDoc)) {
return nullptr;
}
const char *text = pcs->GetFoldDisplayText(lineDoc);
return text ? text : defaultFoldDisplayText.get();
}
InSelection EditModel::LineEndInSelection(Sci::Line lineDoc) const {
const Sci::Position posAfterLineEnd = pdoc->LineStart(lineDoc + 1);
return sel.InSelectionForEOL(posAfterLineEnd);
}
int EditModel::GetMark(Sci::Line line) const {
return pdoc->GetMark(line, FlagSet(changeHistoryOption, ChangeHistoryOption::Markers));
}
void EditModel::EnsureModelState() {
if (!modelState && (undoSelectionHistoryOption != UndoSelectionHistoryOption::Disabled)) {
if (ViewStateShared vss = pdoc->GetViewState(this)) {
modelState = std::dynamic_pointer_cast<ModelState>(vss);
} else {
modelState = std::make_shared<ModelState>();
pdoc->SetViewState(this, std::static_pointer_cast<ViewState>(modelState));
}
}
}
void EditModel::ChangeUndoSelectionHistory(Scintilla::UndoSelectionHistoryOption undoSelectionHistoryOptionNew) {
undoSelectionHistoryOption = undoSelectionHistoryOptionNew;
if (undoSelectionHistoryOption == UndoSelectionHistoryOption::Disabled) {
modelState.reset();
pdoc->SetViewState(this, {});
}
}
|