From 2e4defe45917e78d56adb6f1e801a4867d00e3af Mon Sep 17 00:00:00 2001 From: dail8859 Date: Wed, 20 Jul 2016 22:46:30 -0400 Subject: [PATCH] Simplify word detection for SmartHighlighter Utilizes SCI_WORDSTARTPOSITION and SCI_WORDENDPOSITION from Scintilla. Fixes #2110 --- .../ScitillaComponent/SmartHighlighter.cpp | 81 ++++--------------- .../src/ScitillaComponent/SmartHighlighter.h | 3 - 2 files changed, 15 insertions(+), 69 deletions(-) diff --git a/PowerEditor/src/ScitillaComponent/SmartHighlighter.cpp b/PowerEditor/src/ScitillaComponent/SmartHighlighter.cpp index c2e8e9aa..081f6534 100644 --- a/PowerEditor/src/ScitillaComponent/SmartHighlighter.cpp +++ b/PowerEditor/src/ScitillaComponent/SmartHighlighter.cpp @@ -39,54 +39,25 @@ SmartHighlighter::SmartHighlighter(FindReplaceDlg * pFRDlg) void SmartHighlighter::highlightView(ScintillaEditView * pHighlightView) { - //Get selection - CharacterRange range = pHighlightView->getSelection(); - - //Clear marks + // Clear marks pHighlightView->clearIndicator(SCE_UNIVERSAL_FOUND_STYLE_SMART); - //If nothing selected, dont mark anything - if (range.cpMin == range.cpMax) - { + // If nothing selected, dont mark anything + if (pHighlightView->execute(SCI_GETSELECTIONEMPTY) == 1) + return; + + auto curPos = pHighlightView->execute(SCI_GETCURRENTPOS); + auto wordStart = pHighlightView->execute(SCI_WORDSTARTPOSITION, curPos, true); + auto wordEnd = pHighlightView->execute(SCI_WORDENDPOSITION, wordStart, true); + auto range = pHighlightView->getSelection(); + + // Make sure the "word" positions match the current selection + if (wordStart == wordEnd || wordStart != range.cpMin || wordEnd != range.cpMax) return; - } int textlen = range.cpMax - range.cpMin + 1; - char * text2Find = new char[textlen]; - pHighlightView->getSelectedText(text2Find, textlen, false); //do not expand selection (false) - - - //GETWORDCHARS for isQualifiedWord2() and isWordChar2() - auto listCharSize = pHighlightView->execute(SCI_GETWORDCHARS, 0, 0); - char *listChar = new char[listCharSize+1]; - pHighlightView->execute(SCI_GETWORDCHARS, 0, (LPARAM)listChar); - listChar[listCharSize] = '\0'; - - bool valid = true; - //The word has to consist if wordChars only, and the characters before and after something else - if (!isQualifiedWord(text2Find, listChar)) - valid = false; - else - { - UCHAR c = (UCHAR)pHighlightView->execute(SCI_GETCHARAT, range.cpMax); - if (c) - { - if (isWordChar(char(c), listChar)) - valid = false; - } - c = (UCHAR)pHighlightView->execute(SCI_GETCHARAT, range.cpMin-1); - if (c) - { - if (isWordChar(char(c), listChar)) - valid = false; - } - } - if (!valid) { - delete [] text2Find; - delete [] listChar; - return; - } + pHighlightView->getSelectedText(text2Find, textlen, false); //do not expand selection (false) // save target locations for other search functions int originalStartPos = (int)pHighlightView->execute(SCI_GETTARGETSTART); @@ -140,28 +111,6 @@ void SmartHighlighter::highlightView(ScintillaEditView * pHighlightView) // restore the original targets to avoid conflicts with the search/replace functions pHighlightView->execute(SCI_SETTARGETRANGE, originalStartPos, originalEndPos); - delete [] listChar; + + delete[] text2Find; } - -bool SmartHighlighter::isQualifiedWord(const char *str, char *listChar) const -{ - for (size_t i = 0, len = strlen(str) ; i < len ; ++i) - { - if (!isWordChar(str[i], listChar)) - return false; - } - return true; -}; - -bool SmartHighlighter::isWordChar(char ch, char listChar[]) const -{ - - for (size_t i = 0, len = strlen(listChar) ; i < len ; ++i) - { - if (ch == listChar[i]) - { - return true; - } - } - return false; -}; diff --git a/PowerEditor/src/ScitillaComponent/SmartHighlighter.h b/PowerEditor/src/ScitillaComponent/SmartHighlighter.h index 3a8d20e9..e2a3e7a9 100644 --- a/PowerEditor/src/ScitillaComponent/SmartHighlighter.h +++ b/PowerEditor/src/ScitillaComponent/SmartHighlighter.h @@ -37,9 +37,6 @@ public: void highlightView(ScintillaEditView * pHighlightView); private: FindReplaceDlg * _pFRDlg; - - bool isQualifiedWord(const char *str, char listChar[]) const; - bool isWordChar(char ch, char listChar[]) const; }; #endif //SMARTHIGHLIGHTER_H \ No newline at end of file