From 35adb1910ba98897ad3a935fa7d712a986289bd3 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Tue, 6 Oct 2015 18:07:30 +0200 Subject: [PATCH] Enhance auto-insert Make auto-insert more usable - no auto-insert if the following (and previous character - in the case of quote or double quote) character is not blank character. (fixes #584, fixes #450) --- .../src/ScitillaComponent/AutoCompletion.cpp | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp b/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp index 57761cf69..7b73afe3b 100644 --- a/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp +++ b/PowerEditor/src/ScitillaComponent/AutoCompletion.cpp @@ -498,15 +498,26 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m int caretPos = _pEditView->execute(SCI_GETCURRENTPOS); char *matchedChars = NULL; + char charPrev = (char)_pEditView->execute(SCI_GETCHARAT, caretPos - 2); + char charNext = (char)_pEditView->execute(SCI_GETCHARAT, caretPos); + + bool isCharPrevBlank = (charPrev == ' ' || charPrev == '\t' || charPrev == '\n' || charPrev == '\r' || charPrev == '\0'); + int docLen = _pEditView->getCurrentDocLen(); + bool isCharNextBlank = (charNext == ' ' || charNext == '\t' || charNext == '\n' || charNext == '\r' || caretPos == docLen); + + // User defined matched pairs should be checked firstly for (size_t i = 0, len = matchedPairs.size(); i < len; ++i) { if (int(matchedPairs[i].first) == character) { - char userMatchedChar[2] = {'\0', '\0'}; - userMatchedChar[0] = matchedPairs[i].second; - _pEditView->execute(SCI_INSERTTEXT, caretPos, (LPARAM)userMatchedChar); - return; + if (isCharNextBlank) + { + char userMatchedChar[2] = { '\0', '\0' }; + userMatchedChar[0] = matchedPairs[i].second; + _pEditView->execute(SCI_INSERTTEXT, caretPos, (LPARAM)userMatchedChar); + return; + } } } @@ -520,24 +531,33 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m case int('('): if (matchedPairConf._doParentheses) { - matchedChars = ")"; - _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + if (isCharNextBlank) + { + matchedChars = ")"; + _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + } } break; case int('['): if (matchedPairConf._doBrackets) { - matchedChars = "]"; - _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + if (isCharNextBlank) + { + matchedChars = "]"; + _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + } } break; case int('{'): if (matchedPairConf._doCurlyBrackets) { - matchedChars = "}"; - _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + if (isCharNextBlank) + { + matchedChars = "}"; + _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + } } break; @@ -555,8 +575,11 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m } } - matchedChars = "\""; - _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + if (isCharPrevBlank && isCharNextBlank) + { + matchedChars = "\""; + _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + } } break; case int('\''): @@ -572,8 +595,12 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m return; } } - matchedChars = "'"; - _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + + if (isCharPrevBlank && isCharNextBlank) + { + matchedChars = "'"; + _insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); + } } break;