From 5a7b30789b84d238031c9cff96ec7a284af0bb44 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Sun, 23 Nov 2014 16:46:44 +0000 Subject: [PATCH] [NEW_FEATURE] Enhance Auto-insert feature (in progress). git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1294 f5eea248-9336-0410-98b8-ebc06183d4e3 --- .../src/ScitillaComponent/AutoCompletion.cpp | 56 ++++++++++++++++++- .../src/ScitillaComponent/AutoCompletion.h | 4 ++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp b/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp index 8a5b908d..10f621fa 100644 --- a/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp +++ b/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp @@ -429,8 +429,37 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m { case int('('): if (matchedPairConf._doParentheses) - matchedChars = ")"; + { + matchedChars = ")"; + _doIgnoreParenthease = true; + _parenthesePos = caretPos - 1; + } + break; + case int(')') : + if (matchedPairConf._doParentheses && _doIgnoreParenthease) + { + matchedChars = ")"; + // if current pos is on the same line of _parenthesePos + // and current pos > _parenthesePos + if (_parenthesePos < caretPos) + { + // detect if ) is in between ( and ) + int pos = isInBetween(_parenthesePos, ')', caretPos); + if (pos != -1) + { + _pEditView->execute(SCI_DELETERANGE, pos, 1); + _pEditView->execute(SCI_GOTOPOS, pos); + } + } + + _doIgnoreParenthease = false; + _parenthesePos = -1; + return; + } + + break; + case int('['): if (matchedPairConf._doBrackets) matchedChars = "]"; @@ -463,6 +492,31 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m _pEditView->execute(SCI_INSERTTEXT, caretPos, (LPARAM)matchedChars); } +int AutoCompletion::isInBetween(int startPos, char endChar, int posToDetect) +{ + int posToDetectLine = _pEditView->execute(SCI_LINEFROMPOSITION, posToDetect); + int startPosLine = _pEditView->execute(SCI_LINEFROMPOSITION, startPos); + + if (startPosLine != posToDetectLine) + return -1; + + int endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, startPosLine); + + char startChar = (char)_pEditView->execute(SCI_GETCHARAT, startPos); + + + for (int i = posToDetect; i <= endPos; ++i) + { + char aChar = (char)_pEditView->execute(SCI_GETCHARAT, i); + if (aChar == startChar) + return -1; + if (aChar == endChar) + return i; + } + return -1; +} + + void AutoCompletion::update(int character) { const NppGUI & nppGUI = NppParameters::getInstance()->getNppGUI(); diff --git a/PowerEditor/src/ScitillaComponent/AutoCompletion.h b/PowerEditor/src/ScitillaComponent/AutoCompletion.h index 40dc122e..daff884d 100644 --- a/PowerEditor/src/ScitillaComponent/AutoCompletion.h +++ b/PowerEditor/src/ScitillaComponent/AutoCompletion.h @@ -79,6 +79,9 @@ private: TiXmlDocument *_pXmlFile; TiXmlElement *_pXmlKeyword; + bool _doIgnoreParenthease; + int _parenthesePos; + bool _ignoreCase; vector _keyWordArray; @@ -89,6 +92,7 @@ private: const TCHAR * getApiFileName(); void getWordArray(vector & wordArray, TCHAR *beginChars); + int isInBetween(int startPos, char endChar, int posToDetect); }; #endif //AUTOCOMPLETION_H