[NEW_FEATURE] Enhance Auto-insert feature (in progress).

git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository/trunk@1296 f5eea248-9336-0410-98b8-ebc06183d4e3
This commit is contained in:
Don Ho 2014-11-24 19:20:57 +00:00
parent cb07a7715d
commit 57940d83f3
2 changed files with 57 additions and 23 deletions

View File

@ -402,26 +402,55 @@ void AutoCompletion::getCloseTag(char *closeTag, size_t closeTagSize, size_t car
closeTag[foundTextLen+2] = '\0';
}
void InsertedMachedChars::add(MachedCharInserted mci)
void InsertedMatchedChars::removeInvalidElements(MatchedCharInserted mci)
{
_insertedMachedChars.push_back(mci);
if (mci._c == '\n' || mci._c == '\r') // create a new line, so all matched char are invalidated
{
_insertedMatchedChars.clear();
}
else
{
for (int i = _insertedMatchedChars.size() - 1; i >= 0; --i)
{
if (_insertedMatchedChars[i]._pos < mci._pos)
{
int posToDetectLine = _pEditView->execute(SCI_LINEFROMPOSITION, mci._pos);
int startPosLine = _pEditView->execute(SCI_LINEFROMPOSITION, _insertedMatchedChars[i]._pos);
if (posToDetectLine != startPosLine) //not in the same line
{
_insertedMatchedChars.erase(_insertedMatchedChars.begin() + i);
}
}
else // current position is before matchedStartSybol Pos
{
_insertedMatchedChars.erase(_insertedMatchedChars.begin() + i);
}
}
}
}
void InsertedMatchedChars::add(MatchedCharInserted mci)
{
removeInvalidElements(mci);
_insertedMatchedChars.push_back(mci);
}
// if current pos > matchedStartSybol Pos and current pos is on the same line of matchedStartSybolPos, it'll be checked then removed
// otherwise it is just removed
// return the pos of matchedEndSybol or -1 if matchedEndSybol not found
int InsertedMachedChars::search(char startChar, char endChar, int posToDetect)
int InsertedMatchedChars::search(char startChar, char endChar, int posToDetect)
{
if (isEmpty())
return -1;
int posToDetectLine = _pEditView->execute(SCI_LINEFROMPOSITION, posToDetect);
for (int i = _insertedMachedChars.size() - 1; i >= 0; --i)
for (int i = _insertedMatchedChars.size() - 1; i >= 0; --i)
{
if (_insertedMachedChars[i]._pos < posToDetect)
if (_insertedMatchedChars[i]._pos < posToDetect)
{
int startPosLine = _pEditView->execute(SCI_LINEFROMPOSITION, _insertedMachedChars[i]._pos);
int startPosLine = _pEditView->execute(SCI_LINEFROMPOSITION, _insertedMatchedChars[i]._pos);
if (posToDetectLine == startPosLine)
{
int endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, startPosLine);
@ -431,24 +460,24 @@ int InsertedMachedChars::search(char startChar, char endChar, int posToDetect)
char aChar = (char)_pEditView->execute(SCI_GETCHARAT, j);
if (aChar == startChar)
{
_insertedMachedChars.erase(_insertedMachedChars.begin() + i);
_insertedMatchedChars.erase(_insertedMatchedChars.begin() + i);
return -1;
}
if (aChar == endChar) // found it!!!
{
_insertedMachedChars.erase(_insertedMachedChars.begin() + i);
_insertedMatchedChars.erase(_insertedMatchedChars.begin() + i);
return j;
}
}
}
else // not in the same line
{
_insertedMachedChars.erase(_insertedMachedChars.begin() + i);
_insertedMatchedChars.erase(_insertedMatchedChars.begin() + i);
}
}
else // current position is before matchedStartSybol Pos
{
_insertedMachedChars.erase(_insertedMachedChars.begin() + i);
_insertedMatchedChars.erase(_insertedMatchedChars.begin() + i);
}
}
return -1;
@ -483,7 +512,7 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
if (matchedPairConf._doParentheses)
{
matchedChars = ")";
_insertedMachedChars.add(MachedCharInserted(char(character), caretPos - 1));
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
break;
@ -491,7 +520,7 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
if (matchedPairConf._doBrackets)
{
matchedChars = "]";
_insertedMachedChars.add(MachedCharInserted(char(character), caretPos - 1));
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
break;
@ -499,7 +528,7 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
if (matchedPairConf._doCurlyBrackets)
{
matchedChars = "}";
_insertedMachedChars.add(MachedCharInserted(char(character), caretPos - 1));
_insertedMatchedChars.add(MatchedCharInserted(char(character), caretPos - 1));
}
break;
@ -525,7 +554,7 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
case int(')') :
case int(']') :
case int('}') :
if (matchedPairConf._doParentheses && !_insertedMachedChars.isEmpty())
if (matchedPairConf._doParentheses && !_insertedMatchedChars.isEmpty())
{
char startChar;
if (character == int(')'))
@ -535,7 +564,7 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
else // if (character == int('}'))
startChar = '{';
int pos = _insertedMachedChars.search(startChar, char(character), caretPos);
int pos = _insertedMatchedChars.search(startChar, char(character), caretPos);
if (pos != -1)
{
_pEditView->execute(SCI_DELETERANGE, pos, 1);
@ -544,6 +573,10 @@ void AutoCompletion::insertMatchedChars(int character, const MatchedPairConf & m
return;
}
break;
default:
if (!_insertedMatchedChars.isEmpty())
_insertedMatchedChars.removeInvalidElements(MatchedCharInserted(char(character), caretPos - 1));
}
if (matchedChars)

View File

@ -39,21 +39,22 @@
class ScintillaEditView;
struct MachedCharInserted {
struct MatchedCharInserted {
char _c;
int _pos;
MachedCharInserted(char c, int pos) : _c(c), _pos(pos) {};
MatchedCharInserted(char c, int pos) : _c(c), _pos(pos) {};
};
class InsertedMachedChars {
class InsertedMatchedChars {
public:
void init(ScintillaEditView * pEditView) { _pEditView = pEditView; };
void add(MachedCharInserted mci);
bool isEmpty() const { return _insertedMachedChars.size() == 0; };
void removeInvalidElements(MatchedCharInserted mci);
void add(MatchedCharInserted mci);
bool isEmpty() const { return _insertedMatchedChars.size() == 0; };
int search(char startChar, char endChar, int posToDetect);
private:
std::vector<MachedCharInserted> _insertedMachedChars;
std::vector<MatchedCharInserted> _insertedMatchedChars;
ScintillaEditView * _pEditView;
};
@ -65,7 +66,7 @@ public:
_curLang(L_TEXT), _pXmlFile(NULL), _keyWordMaxLen(0),
_pXmlKeyword(NULL), _ignoreCase(true), _keyWords(TEXT("")) {
//Do not load any language yet
_insertedMachedChars.init(_pEditView);
_insertedMatchedChars.init(_pEditView);
};
~AutoCompletion(){
@ -98,7 +99,7 @@ private:
TiXmlDocument *_pXmlFile;
TiXmlElement *_pXmlKeyword;
InsertedMachedChars _insertedMachedChars;
InsertedMatchedChars _insertedMatchedChars;
bool _ignoreCase;