Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit 0cc915d

Browse files
committed
Use experimental ScintillaGateway
This also helps solve a lot of 64-bit casting, etc.
1 parent bedf5d4 commit 0cc915d

File tree

4 files changed

+3083
-58
lines changed

4 files changed

+3083
-58
lines changed

ElasticTabstops.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ClInclude Include="src\PluginInterface.h" />
3030
<ClInclude Include="src\resource.h" />
3131
<ClInclude Include="src\Scintilla.h" />
32+
<ClInclude Include="src\ScintillaGateway.h" />
3233
<ClInclude Include="src\Version.h" />
3334
</ItemGroup>
3435
<ItemGroup>

ElasticTabstops.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<ClInclude Include="src\Config.h">
4949
<Filter>Header Files</Filter>
5050
</ClInclude>
51+
<ClInclude Include="src\ScintillaGateway.h">
52+
<Filter>Header Files</Filter>
53+
</ClInclude>
5154
</ItemGroup>
5255
<ItemGroup>
5356
<ResourceCompile Include="src\resource.rc">

src/ElasticTabstops.cpp

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
#include <vector>
2121
#include <string>
2222
#include "ElasticTabstops.h"
23+
#include "ScintillaGateway.h"
2324

2425
#define MARK_UNDERLINE 20
2526
#define SC_MARGIN_SYBOL 1
2627
#define DBG_INDICATORS 8
2728

28-
static sptr_t edit;
29-
static SciFnDirect func;
29+
static ScintillaGateway editor;
3030
static int tab_width_minimum;
3131
static int tab_width_padding;
3232
static int char_width;
@@ -37,27 +37,23 @@ enum direction {
3737
FORWARDS
3838
};
3939

40-
static inline LONG_PTR call_edit(UINT msg, DWORD wp = 0, LONG_PTR lp = 0) {
41-
return func(edit, msg, wp, lp);
42-
}
43-
4440
static int get_line_start(int pos) {
45-
int line = call_edit(SCI_LINEFROMPOSITION, pos);
46-
return call_edit(SCI_POSITIONFROMLINE, line);
41+
int line = editor.LineFromPosition(pos);
42+
return editor.PositionFromLine(line);
4743
}
4844

4945
static int get_line_end(int pos) {
50-
int line = call_edit(SCI_LINEFROMPOSITION, pos);
51-
return call_edit(SCI_GETLINEENDPOSITION, line);
46+
int line = editor.LineFromPosition(pos);
47+
return editor.GetLineEndPosition(line);
5248
}
5349

5450
static void clear_debug_marks() {
5551
#ifdef _DEBUG
5652
// Clear all the debugging junk, this way it only shows updates when it is actually recomputed
57-
call_edit(SCI_MARKERDELETEALL, MARK_UNDERLINE);
53+
editor.MarkerDeleteAll(MARK_UNDERLINE);
5854
for (int i = 0; i < DBG_INDICATORS; ++i) {
59-
call_edit(SCI_SETINDICATORCURRENT, i);
60-
call_edit(SCI_INDICATORCLEARRANGE, 0, call_edit(SCI_GETTEXTLENGTH));
55+
editor.SetIndicatorCurrent(i);
56+
editor.IndicatorClearRange(0, editor.GetTextLength());
6157
}
6258
#endif
6359
}
@@ -69,11 +65,11 @@ static int get_text_width_prop(int start, int end) {
6965
range.chrg.cpMin = start;
7066
range.chrg.cpMax = end;
7167
range.lpstrText = &s[0];
72-
call_edit(SCI_GETTEXTRANGE, 0, (sptr_t)&range);
68+
editor.GetTextRange(&range);
7369

74-
LONG_PTR style = call_edit(SCI_GETSTYLEAT, start);
70+
int style = editor.GetStyleAt(start);
7571

76-
return call_edit(SCI_TEXTWIDTH, style, (LONG_PTR)range.lpstrText);
72+
return editor.TextWidth(style, range.lpstrText);
7773
}
7874

7975
static int get_text_width_mono(int start, int end) {
@@ -86,15 +82,15 @@ static int calc_tab_width(int text_width_in_tab) {
8682
}
8783

8884
static bool change_line(int& location, direction which_dir) {
89-
int line = call_edit(SCI_LINEFROMPOSITION, location);
85+
int line = editor.LineFromPosition(location);
9086
if (which_dir == FORWARDS) {
91-
location = call_edit(SCI_POSITIONFROMLINE, line + 1);
87+
location = editor.PositionFromLine(line + 1);
9288
}
9389
else {
9490
if (line <= 0) {
9591
return false;
9692
}
97-
location = call_edit(SCI_POSITIONFROMLINE, line - 1);
93+
location = editor.PositionFromLine(line - 1);
9894
}
9995
return (location >= 0);
10096
}
@@ -103,11 +99,11 @@ static int get_nof_tabs_between(int start, int end) {
10399
unsigned char current_char = 0;
104100
int tabs = 0;
105101

106-
while (start < end && (current_char = (unsigned char)call_edit(SCI_GETCHARAT, start))) {
102+
while (start < end && (current_char = (unsigned char)editor.GetCharAt(start))) {
107103
if (current_char == '\t') {
108104
tabs++;
109105
}
110-
start = call_edit(SCI_POSITIONAFTER, start);
106+
start = editor.PositionAfter(start);
111107
}
112108

113109
return tabs;
@@ -125,11 +121,11 @@ struct et_tabstop {
125121
};
126122

127123
static void measure_cells(std::vector<std::vector<et_tabstop>> &grid, int start_line, int end_line, size_t editted_cell) {
128-
int current_pos = call_edit(SCI_POSITIONFROMLINE, start_line);
124+
int current_pos = editor.PositionFromLine(start_line);
129125
direction which_dir = (start_line <= end_line ? FORWARDS : BACKWARDS);
130126

131127
do {
132-
unsigned char current_char = (unsigned char)call_edit(SCI_GETCHARAT, current_pos);
128+
unsigned char current_char = (unsigned char)editor.GetCharAt(current_pos);
133129
const int line_end = get_line_end(current_pos);
134130
int cell_start = current_pos;
135131
bool cell_empty = true;
@@ -141,9 +137,9 @@ static void measure_cells(std::vector<std::vector<et_tabstop>> &grid, int start_
141137
if (cell_num >= editted_cell) {
142138
#ifdef _DEBUG
143139
// Highlight the cell
144-
call_edit(SCI_SETINDICATORCURRENT, grid_line.size() % DBG_INDICATORS);
145-
if (cell_empty) call_edit(SCI_INDICATORFILLRANGE, current_pos, 1);
146-
else call_edit(SCI_INDICATORFILLRANGE, cell_start, current_pos - cell_start + 1);
140+
editor.SetIndicatorCurrent(grid_line.size() % DBG_INDICATORS);
141+
if (cell_empty) editor.IndicatorFillRange(current_pos, 1);
142+
else editor.IndicatorFillRange(cell_start, current_pos - cell_start + 1);
147143
#endif
148144
int text_width_in_tab = 0;
149145
if (!cell_empty) {
@@ -164,11 +160,11 @@ static void measure_cells(std::vector<std::vector<et_tabstop>> &grid, int start_
164160
}
165161
}
166162

167-
current_pos = call_edit(SCI_POSITIONAFTER, current_pos);
168-
current_char = (unsigned char)call_edit(SCI_GETCHARAT, current_pos);
163+
current_pos = editor.PositionAfter(current_pos);
164+
current_char = (unsigned char)editor.GetCharAt(current_pos);
169165
}
170166

171-
if (grid_line.size() <= editted_cell && !(which_dir == FORWARDS && call_edit(SCI_LINEFROMPOSITION, current_pos) <= end_line)) {
167+
if (grid_line.size() <= editted_cell && !(which_dir == FORWARDS && editor.LineFromPosition(current_pos) <= end_line)) {
172168
break;
173169
}
174170

@@ -184,7 +180,7 @@ static void stretch_cells(std::vector<std::vector<et_tabstop>> &grid, size_t sta
184180
// Find columns blocks and stretch to fit the widest cell
185181
for (size_t t = start_cell; t < max_tabs; t++) {
186182
bool starting_new_block = true;
187-
int first_line_in_block = 0;
183+
size_t first_line_in_block = 0;
188184
int max_width = 0;
189185
for (size_t l = 0; l < grid.size(); l++) {
190186
if (starting_new_block) {
@@ -210,7 +206,7 @@ static void stretch_cells(std::vector<std::vector<et_tabstop>> &grid, size_t sta
210206
static void stretch_tabstops(int block_edit_linenum, int block_min_end, int editted_cell) {
211207
std::vector<std::vector<et_tabstop>> grid;
212208
size_t max_tabs = 0;
213-
int block_start_linenum;
209+
size_t block_start_linenum;
214210

215211
if (block_edit_linenum > 0) {
216212
measure_cells(grid, block_edit_linenum - 1, -1, editted_cell);
@@ -227,8 +223,8 @@ static void stretch_tabstops(int block_edit_linenum, int block_min_end, int edit
227223

228224
#ifdef _DEBUG
229225
// Mark the start and end of the block being recomputed
230-
call_edit(SCI_MARKERADD, block_start_linenum - 1, MARK_UNDERLINE);
231-
call_edit(SCI_MARKERADD, block_start_linenum + grid.size() - 1, MARK_UNDERLINE);
226+
editor.MarkerAdd((int)block_start_linenum - 1, MARK_UNDERLINE);
227+
editor.MarkerAdd((int)(block_start_linenum + grid.size() - 1), MARK_UNDERLINE);
232228
#endif
233229

234230
stretch_cells(grid, editted_cell, max_tabs);
@@ -237,26 +233,26 @@ static void stretch_tabstops(int block_edit_linenum, int block_min_end, int edit
237233
std::vector<int> known_tabstops;
238234
int cur_tabstop = 0;
239235
for (int i = 0; i < editted_cell; i++) {
240-
cur_tabstop = call_edit(SCI_GETNEXTTABSTOP, block_start_linenum, cur_tabstop);
236+
cur_tabstop = editor.GetNextTabStop((int)block_start_linenum, cur_tabstop);
241237
known_tabstops.push_back(cur_tabstop);
242238
}
243239

244240
// Set tabstops
245241
for (size_t l = 0; l < grid.size(); l++) {
246-
int current_line_num = block_start_linenum + l;
242+
size_t current_line_num = block_start_linenum + l;
247243
int acc_tabstop = 0;
248244

249-
call_edit(SCI_CLEARTABSTOPS, current_line_num);
245+
editor.ClearTabStops((int)current_line_num);
250246

251247
// Set any known tabstops
252248
for (size_t t = 0; t < known_tabstops.size(); t++) {
253249
acc_tabstop = known_tabstops[t];
254-
call_edit(SCI_ADDTABSTOP, current_line_num, acc_tabstop);
250+
editor.AddTabStop((int)current_line_num, acc_tabstop);
255251
}
256252

257253
for (size_t t = known_tabstops.size(); t < grid[l].size(); t++) {
258254
acc_tabstop += *(grid[l][t].widest_width_pix);
259-
call_edit(SCI_ADDTABSTOP, current_line_num, acc_tabstop);
255+
editor.AddTabStop((int)current_line_num, acc_tabstop);
260256
}
261257
}
262258

@@ -266,14 +262,14 @@ static void stretch_tabstops(int block_edit_linenum, int block_min_end, int edit
266262
static void replace_nth_tab(int linenum, int cellnum, const char *text) {
267263
TextToFind ttf;
268264

269-
ttf.chrg.cpMin = call_edit(SCI_POSITIONFROMLINE, linenum);
270-
ttf.chrg.cpMax = call_edit(SCI_GETLINEENDPOSITION, linenum);
265+
ttf.chrg.cpMin = editor.PositionFromLine(linenum);
266+
ttf.chrg.cpMax = editor.GetLineEndPosition(linenum);
271267
ttf.lpstrText = "\t";
272268

273269
int position = INVALID_POSITION;
274270
int curcell = -1;
275271
do {
276-
position = call_edit(SCI_FINDTEXT, 0, (LONG_PTR)&ttf);
272+
position = editor.FindText(0, &ttf);
277273
ttf.chrg.cpMin = position + 1; // Move start of the search forward
278274
curcell++;
279275
} while (position != INVALID_POSITION && curcell != cellnum);
@@ -282,29 +278,27 @@ static void replace_nth_tab(int linenum, int cellnum, const char *text) {
282278
return;
283279
}
284280

285-
call_edit(SCI_SETTARGETRANGE, position, position + 1);
286-
call_edit(SCI_REPLACETARGET, -1, (LONG_PTR)text);
281+
editor.SetTargetRange(position, position + 1);
282+
editor.ReplaceTarget(-1, text);
287283
}
288284

289285
void ElasticTabstops_SwitchToScintilla(HWND sci, const Configuration *config) {
290-
// Get the direct pointer and function. Not the cleanest but it works for now
291-
edit = SendMessage(sci, SCI_GETDIRECTPOINTER, 0, 0);
292-
func = (SciFnDirect)SendMessage(sci, SCI_GETDIRECTFUNCTION, 0, 0);
286+
editor.SetScintillaInstance(sci);
293287

294288
// Adjust widths based on character size
295289
// The width of a tab is (tab_width_minimum + tab_width_padding)
296290
// Since the user can adjust the padding we adjust the minimum
297-
char_width = call_edit(SCI_TEXTWIDTH, STYLE_DEFAULT, (LONG_PTR)"A");
298-
tab_width_padding = char_width * config->min_padding;
299-
tab_width_minimum = __max(char_width * call_edit(SCI_GETTABWIDTH) - tab_width_padding, 0);
291+
char_width = editor.TextWidth(STYLE_DEFAULT, "A");
292+
tab_width_padding = (int)(char_width * config->min_padding);
293+
tab_width_minimum = __max(char_width * editor.GetTabWidth() - tab_width_padding, 0);
300294

301295
get_text_width = get_text_width_prop;
302296
}
303297

304298
void ElasticTabstops_ComputeEntireDoc() {
305299
clear_debug_marks();
306300

307-
stretch_tabstops(0, call_edit(SCI_GETLINECOUNT), 0);
301+
stretch_tabstops(0, editor.GetLineCount(), 0);
308302
}
309303

310304
void ElasticTabstops_OnModify(int start, int end, int linesAdded, const char *text) {
@@ -320,7 +314,7 @@ void ElasticTabstops_OnModify(int start, int end, int linesAdded, const char *te
320314
editted_cell = get_nof_tabs_between(get_line_start(start), start);
321315
}
322316

323-
int block_start_linenum = call_edit(SCI_LINEFROMPOSITION, start);
317+
int block_start_linenum = editor.LineFromPosition(start);
324318

325319
stretch_tabstops(block_start_linenum, block_start_linenum + (linesAdded > 0 ? linesAdded : 0), editted_cell);
326320
}
@@ -329,7 +323,7 @@ void ElasticTabstops_ConvertToSpaces(const Configuration *config) {
329323
std::vector<std::vector<et_tabstop>> grid;
330324

331325
// Recompute the entire document
332-
measure_cells(grid, 0, call_edit(SCI_GETLINECOUNT), 0);
326+
measure_cells(grid, 0, editor.GetLineCount(), 0);
333327

334328
clear_debug_marks();
335329

@@ -342,9 +336,9 @@ void ElasticTabstops_ConvertToSpaces(const Configuration *config) {
342336

343337
stretch_cells(grid, 0, max_tabs);
344338

345-
call_edit(SCI_BEGINUNDOACTION);
339+
editor.BeginUndoAction();
346340
for (size_t linenum = 0; linenum < grid.size(); ++linenum) {
347-
call_edit(SCI_CLEARTABSTOPS, linenum);
341+
editor.ClearTabStops((int) linenum);
348342

349343
int start_cell = 0;
350344

@@ -359,18 +353,18 @@ void ElasticTabstops_ConvertToSpaces(const Configuration *config) {
359353
}
360354

361355
// Iterate backwards since tabs are being removed, thus it wouldn't find the correct "nth" tab
362-
for (int end_cell = grid[linenum].size() - 1; end_cell >= start_cell; --end_cell) {
356+
for (int end_cell = (int)grid[linenum].size() - 1; end_cell >= start_cell; --end_cell) {
363357
int spaces = grid[linenum][end_cell].getTabLen() / char_width;
364-
replace_nth_tab(linenum, end_cell, std::string(spaces, ' ').c_str());
358+
replace_nth_tab((int)linenum, (int)end_cell, std::string(spaces, ' ').c_str());
365359
}
366360
}
367-
call_edit(SCI_ENDUNDOACTION);
361+
editor.EndUndoAction();
368362
}
369363

370364
void ElasticTabstops_OnReady(HWND sci) {
371365
#ifdef _DEBUG
372366
// Setup the markers for start/end of the computed block
373-
int mask = SendMessage(sci, SCI_GETMARGINMASKN, SC_MARGIN_SYBOL, 0);
367+
int mask = (int)SendMessage(sci, SCI_GETMARGINMASKN, SC_MARGIN_SYBOL, 0);
374368
SendMessage(sci, SCI_SETMARGINMASKN, SC_MARGIN_SYBOL, mask | (1 << MARK_UNDERLINE));
375369
SendMessage(sci, SCI_MARKERDEFINE, MARK_UNDERLINE, SC_MARK_UNDERLINE);
376370
SendMessage(sci, SCI_MARKERSETBACK, MARK_UNDERLINE, 0x77CC77);

0 commit comments

Comments
 (0)