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)
This commit is contained in:
Don Ho 2015-10-06 18:07:30 +02:00
parent 52dda771d3
commit 35adb1910b

View File

@ -498,15 +498,26 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
int caretPos = _pEditView->execute(SCI_GETCURRENTPOS); int caretPos = _pEditView->execute(SCI_GETCURRENTPOS);
char *matchedChars = NULL; 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 // User defined matched pairs should be checked firstly
for (size_t i = 0, len = matchedPairs.size(); i < len; ++i) for (size_t i = 0, len = matchedPairs.size(); i < len; ++i)
{ {
if (int(matchedPairs[i].first) == character) if (int(matchedPairs[i].first) == character)
{ {
char userMatchedChar[2] = {'\0', '\0'}; if (isCharNextBlank)
userMatchedChar[0] = matchedPairs[i].second; {
_pEditView->execute(SCI_INSERTTEXT, caretPos, (LPARAM)userMatchedChar); char userMatchedChar[2] = { '\0', '\0' };
return; 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('('): case int('('):
if (matchedPairConf._doParentheses) if (matchedPairConf._doParentheses)
{ {
matchedChars = ")"; if (isCharNextBlank)
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); {
matchedChars = ")";
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
} }
break; break;
case int('['): case int('['):
if (matchedPairConf._doBrackets) if (matchedPairConf._doBrackets)
{ {
matchedChars = "]"; if (isCharNextBlank)
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); {
matchedChars = "]";
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
} }
break; break;
case int('{'): case int('{'):
if (matchedPairConf._doCurlyBrackets) if (matchedPairConf._doCurlyBrackets)
{ {
matchedChars = "}"; if (isCharNextBlank)
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); {
matchedChars = "}";
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
} }
break; break;
@ -555,8 +575,11 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
} }
} }
matchedChars = "\""; if (isCharPrevBlank && isCharNextBlank)
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); {
matchedChars = "\"";
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
} }
break; break;
case int('\''): case int('\''):
@ -572,8 +595,12 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
return; return;
} }
} }
matchedChars = "'";
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1)); if (isCharPrevBlank && isCharNextBlank)
{
matchedChars = "'";
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
} }
break; break;