Skip to content

Commit 9452ca9

Browse files
committed
Edit Word misbehaves on adjacent regions delete #257
1 parent d306a46 commit 9452ca9

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

src/Extension/ExtSelection.c

+33-46
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ int n2e_HighlightWord(LPCSTR word)
205205
lrange = bHighlightAll
206206
? (bEditSelectionInit && bEditSelectionScope)
207207
? SciCall_GetLineCount()
208-
: min(SendMessage(hwndEdit, SCI_LINESONSCREEN, 0, 0), SciCall_GetLineCount())
208+
: min(SciCall_GetLinesOnScreen(), SciCall_GetLineCount())
209209
: 0;
210210

211211
ttf.chrg.cpMin = SendMessage(hwndEdit, SCI_POSITIONFROMLINE, lstart, 0);
@@ -224,7 +224,6 @@ int n2e_HighlightWord(LPCSTR word)
224224
int search_opt = bEditSelectionWholeWordMode ? SCFIND_WHOLEWORD : SCFIND_MATCHCASE;
225225
int wlen = strlen(word);
226226
int curr_indi = N2E_SELECT_INDICATOR_SINGLE;
227-
BOOL bPreviousMatchIsVisible = FALSE;
228227
if (bEditSelectionInit)
229228
{
230229
n2e_ClearEditSelections();
@@ -241,62 +240,45 @@ int n2e_HighlightWord(LPCSTR word)
241240
}
242241
strcpy(pEditSelectionOriginalWord, word);
243242
}
244-
// 2 first words
243+
244+
if (bEditSelectionInit)
245245
{
246-
ttf1.chrg.cpMin = max(0, ttf.chrg.cpMin - iMaxSearchDistance);
247-
ttf1.chrg.cpMax = min(len, ttf.chrg.cpMin + iMaxSearchDistance);
246+
curr_indi = N2E_SELECT_INDICATOR_EDIT;
247+
bEditSelection = TRUE;
248+
iOriginalSelectionLength = wlen;
248249
}
249-
ttf1.lpstrText = (LPSTR)word;
250-
res = SendMessage(hwndEdit, SCI_FINDTEXT, search_opt, (LPARAM)&ttf1);
251-
bPreviousMatchIsVisible = ttf1.chrgText.cpMin >= ttf.chrg.cpMin && ttf1.chrgText.cpMin < ttf.chrg.cpMax;
252-
while (1)
250+
else
253251
{
254-
ttf1.chrg.cpMin = ttf1.chrgText.cpMax;
255-
res = SendMessage(hwndEdit, SCI_FINDTEXT, search_opt, (LPARAM)&ttf1);
256-
if (-1 != res)
252+
ttf1.chrg.cpMin = max(0, ttf.chrg.cpMin - iMaxSearchDistance);
253+
ttf1.chrg.cpMax = min(len, ttf.chrg.cpMin + iMaxSearchDistance);
254+
ttf1.lpstrText = (LPSTR)word;
255+
SciCall_FindText(search_opt, &ttf1);
256+
const BOOL bMatchIsVisible = ttf1.chrgText.cpMin >= ttf.chrg.cpMin && ttf1.chrgText.cpMin < ttf.chrg.cpMax;
257+
if (bMatchIsVisible)
257258
{
258-
// current match is visible
259-
if (
260-
ttf1.chrgText.cpMin >= ttf.chrg.cpMin &&
261-
ttf1.chrgText.cpMin < ttf.chrg.cpMax
262-
)
263-
{
264-
if (bPreviousMatchIsVisible)
265-
{
266-
if (bEditSelectionInit)
267-
{
268-
curr_indi = N2E_SELECT_INDICATOR_EDIT;
269-
bEditSelection = TRUE;
270-
iOriginalSelectionLength = wlen;
271-
}
272-
else
273-
{
274-
curr_indi = N2E_SELECT_INDICATOR_PAGE;
275-
}
276-
break;
277-
}
278-
else
279-
{
280-
curr_indi = N2E_SELECT_INDICATOR;
281-
}
282-
bPreviousMatchIsVisible = TRUE;
283-
}
284-
if (ttf1.chrgText.cpMin >= ttf.chrg.cpMax && N2E_SELECT_INDICATOR == curr_indi)
259+
ttf1.chrg.cpMin = ttf1.chrgText.cpMax;
260+
const BOOL bSecondMatchIsVisible = (SciCall_FindText(search_opt, &ttf1) != -1)
261+
&& (ttf1.chrgText.cpMin >= ttf.chrg.cpMin && ttf1.chrgText.cpMin < ttf.chrg.cpMax);
262+
if (bHighlightAll)
285263
{
286-
if (bEditSelectionInit)
264+
if (SciCall_GetLinesOnScreen() < SciCall_GetLineCount())
287265
{
288-
bEditSelection = TRUE;
289-
iOriginalSelectionLength = wlen;
266+
ttf1.chrg.cpMin = SendMessage(hwndEdit, SCI_GETLINEENDPOSITION, lstart + SciCall_GetLinesOnScreen(), 0) + 1;
267+
ttf1.chrg.cpMax = SendMessage(hwndEdit, SCI_GETLINEENDPOSITION, SciCall_GetLineCount(), 0) + 1;
290268
}
291-
break;
292269
}
293-
ttf1.chrg.cpMin = ttf1.chrgText.cpMax;
270+
curr_indi = (SciCall_FindText(search_opt, &ttf1) == -1)
271+
? bSecondMatchIsVisible
272+
? N2E_SELECT_INDICATOR_PAGE
273+
: N2E_SELECT_INDICATOR_SINGLE
274+
: N2E_SELECT_INDICATOR;
294275
}
295276
else
296277
{
297-
break;
278+
curr_indi = N2E_SELECT_INDICATOR;
298279
}
299280
}
281+
300282
SendMessage(hwndEdit, SCI_SETINDICATORCURRENT, curr_indi, 0);
301283
if (bEditSelectionInit && !n2e_IsSelectionEditModeOn())
302284
{
@@ -437,7 +419,7 @@ void n2e_SelectionHighlightInit()
437419
}
438420

439421
sel_len = trEditSelection.chrg.cpMax - trEditSelection.chrg.cpMin;
440-
if (sel_len > 0)
422+
if (sel_len > 1)
441423
{
442424
trEditSelection.lpstrText = n2e_Alloc(sel_len + 1);
443425
SciCall_GetTextRange(0, &trEditSelection);
@@ -754,6 +736,11 @@ void n2e_SelectionNotificationHandler(const int code, const struct SCNotificatio
754736
}
755737
n2e_SelectionUpdate(SUM_UPDATE);
756738
}
739+
else if ((scn->updated & SC_UPDATE_SELECTION)
740+
&& n2e_IsSelectionEditModeOn())
741+
{
742+
n2e_SelectionUpdate(SUM_UPDATE);
743+
}
757744
else if ((scn->updated & (SC_UPDATE_V_SCROLL | SC_UPDATE_H_SCROLL))
758745
&& n2e_IsSelectionEditModeOn()
759746
&& (iEditSelectionFirstVisibleLine != SciCall_DocLineFromVisible(SciCall_GetFirstVisibleLine())))

src/Extension/SciCall.h

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ __forceinline LRESULT SciCall_##fn(type1 var1, type2 var2) { \
7575
//
7676
DeclareSciCallR0(GetCodePage, GETCODEPAGE, int);
7777
DeclareSciCallR0(GetLineCount, GETLINECOUNT, int);
78+
DeclareSciCallR0(GetLinesOnScreen, LINESONSCREEN, int);
7879
DeclareSciCallR0(GetLength, GETLENGTH, int);
7980
DeclareSciCallR0(GetSelectionMode, GETSELECTIONMODE, int);
8081
DeclareSciCallR0(GetSelStart, GETSELECTIONSTART, int);

0 commit comments

Comments
 (0)