Skip to content

Commit

Permalink
Major: 0.2.5.17
Browse files Browse the repository at this point in the history
Bug Fixed:
1. Finally Fixed #5 Search bug.
2. Previously fixed #3 bug.
  • Loading branch information
Saki committed Dec 21, 2013
1 parent f478b58 commit fdf1c0f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 47 deletions.
4 changes: 4 additions & 0 deletions src/app/kccodeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ void KCCodeEditor::connectSearchWidgetWithEditor(KCSearchWidget *widget)
this, &KCCodeEditor::onShowNextSearchResult);
searcherConnections+=connect(widget, &KCSearchWidget::requireShowPreviousResult,
editor, &KCTextEditor::showPreviousSearchResult);
searcherConnections+=connect(editor, &KCTextEditor::matchedResult,
widget, &KCSearchWidget::setResultMatchStyle);
searcherConnections+=connect(editor, &KCTextEditor::nomatchedResult,
widget, &KCSearchWidget::setResultUnmatchStyle);
}
bool KCCodeEditor::getDebugging() const
{
Expand Down
10 changes: 10 additions & 0 deletions src/app/kcsearchwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ void KCSearchWidget::onMenuClicked()
onTextChanged(searchText->text());
}

void KCSearchWidget::setResultMatchStyle()
{
;
}

void KCSearchWidget::setResultUnmatchStyle()
{
;
}

void KCSearchWidget::setText(const QString &text)
{
searchText->setText(text);
Expand Down
2 changes: 2 additions & 0 deletions src/app/kcsearchwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class KCSearchWidget : public QWidget
public slots:
void onTextChanged(const QString &text);
void onMenuClicked();
void setResultMatchStyle();
void setResultUnmatchStyle();

protected:
void resizeEvent(QResizeEvent *event);
Expand Down
1 change: 1 addition & 0 deletions src/app/kctextblockdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <climits>
#include <QDebug>

#include "kctextblockdata.h"

Expand Down
125 changes: 78 additions & 47 deletions src/app/kctexteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ void KCTextEditor::checkWhetherBlockSearchedAndDealWith(

bool KCTextEditor::showPreviousSearchResult()
{
return findString(false);
return findString(true);
}

bool KCTextEditor::showNextSearchResult()
{
return findString(true);
return findString(false);
}

void KCTextEditor::setTabWidth(int width)
Expand All @@ -136,33 +136,42 @@ bool KCTextEditor::findString(bool forward)
{
QTextCursor _textCursor;

//Simbol to match a string
bool hasMatch=false;
//Backup current cursor
_textCursor=textCursor();
if(!forward)
{
_textCursor.setPosition(_textCursor.selectionStart());
}

for(QTextBlock i=_textCursor.block();
i.isValid() && !hasMatch;
i=(forward==true)?i.next():i.previous())
{
KCTextBlockData *blockData=(KCTextBlockData *)i.userData();
//Check search position
int currentCursorPostion=forward?_textCursor.selectionStart():
_textCursor.selectionEnd();
_textCursor.setPosition(currentCursorPostion);
//Search from current place to next place
for(QTextBlock i=_textCursor.block(); //From current block
i.isValid() && !hasMatch; //to the destination block
i=(forward)?i.previous():i.next()) //If search forward, to previous
{ //otherwise to the next.
KCTextBlockData *blockData=(KCTextBlockData *) i.userData();

checkWhetherBlockSearchedAndDealWith(i);

blockData->beginUsingSearchDatas();
if(blockData->hasMatched())
if(blockData->hasMatched()) //If current have search result
{
//If position is forward
if(forward)
{
auto end=blockData->getEndMatchedTextPosition();
for(auto j=blockData->getFirstMatchedTextPosition();
j<end;
j++)
auto begin=blockData->getEndMatchedTextPosition();
auto end=blockData->getFirstMatchedTextPosition();
for(auto j=begin;
j>=end;
j--)
{
if(j->pos >= ((i.blockNumber() == _textCursor.blockNumber())?
_textCursor.positionInBlock():0))
/*
* If it is before the cursor in the current line.
* Or it is before the current block, then we find it.
*/
if((i.blockNumber() == _textCursor.blockNumber() &&
j->pos < currentCursorPostion) ||
i.blockNumber() != _textCursor.blockNumber())
{
hasMatch=true;
_textCursor.setPosition(i.position()+j->pos);
Expand All @@ -175,14 +184,14 @@ bool KCTextEditor::findString(bool forward)
}
else
{
auto end=blockData->getFirstMatchedTextPosition();
for(auto j=blockData->getEndMatchedTextPosition()-1;
j>=end;
j--)
auto begin=blockData->getFirstMatchedTextPosition();
auto end=blockData->getEndMatchedTextPosition();
for(auto j=begin;
j<end;
j++)
{
if((i.blockNumber() == _textCursor.blockNumber() &&
j->pos < _textCursor.positionInBlock()) ||
i.blockNumber() != _textCursor.blockNumber())
if(j->pos >= ((i.blockNumber()==_textCursor.blockNumber())?
_textCursor.positionInBlock():0))
{
hasMatch=true;
_textCursor.setPosition(i.position()+j->pos);
Expand All @@ -196,37 +205,42 @@ bool KCTextEditor::findString(bool forward)
}
blockData->endUsingSearchDatas();
}

//If there's not find, check loop
if(!hasMatch)
{
//Set the end block number, it will be the current block
int endBlockNumber=_textCursor.blockNumber();
for(QTextBlock i= (forward==true)?
document()->firstBlock():
document()->lastBlock().previous();

//If search forward, to the last block, else to the first
for(QTextBlock i=(forward)?
document()->lastBlock():
document()->firstBlock();
//Here, we should check to the end block
i.isValid() &&
(forward==true?
i.blockNumber()<endBlockNumber:
i.blockNumber()>endBlockNumber) &&
(forward?
i.blockNumber()>=endBlockNumber:
i.blockNumber()<=endBlockNumber) &&
!hasMatch;

i=(forward==true)?i.next():i.previous())
//Continue to the next one
i=forward?i.previous():i.next())
{

KCTextBlockData *blockData=(KCTextBlockData *)i.userData();
checkWhetherBlockSearchedAndDealWith(i);

blockData->beginUsingSearchDatas();
if(blockData->hasMatched())
{
//If we search to the forward, do the same things.
if(forward)
{
auto end=blockData->getEndMatchedTextPosition();
for(auto j=blockData->getFirstMatchedTextPosition();
j<end;
j++)
auto end=blockData->getFirstMatchedTextPosition();
for(auto j=blockData->getEndMatchedTextPosition()-1;
j>=end;
j--)
{
if(j->pos >= (i.blockNumber() == _textCursor.blockNumber())?
_textCursor.positionInBlock():0)
if((i.blockNumber() == _textCursor.blockNumber() &&
j->pos > _textCursor.positionInBlock()) ||
i.blockNumber() != _textCursor.blockNumber())
{
hasMatch=true;
_textCursor.setPosition(i.position()+j->pos);
Expand All @@ -239,10 +253,11 @@ bool KCTextEditor::findString(bool forward)
}
else
{
auto end=blockData->getFirstMatchedTextPosition();
for(auto j=blockData->getEndMatchedTextPosition()-1;
j>=end;
j--)
//search from end to the first
auto end=blockData->getEndMatchedTextPosition();
for(auto j=blockData->getFirstMatchedTextPosition();
j<end;
j++)
{
if((i.blockNumber() == _textCursor.blockNumber() &&
j->pos < _textCursor.positionInBlock()) ||
Expand Down Expand Up @@ -270,6 +285,22 @@ bool KCTextEditor::findString(bool forward)
return false;
}

bool KCTextEditor::findPreviousString(KCTextBlockData *blockData,
QTextCursor currentCursor,
bool forward)
{

}

bool KCTextEditor::findNextString(KCTextBlockData *blockData,
QTextCursor currentCursor,
bool forward)
{

}



void KCTextEditor::searchString(QString searchTextSets,
bool regularExpressionSets,
bool caseSensitivelySets,
Expand Down Expand Up @@ -302,7 +333,7 @@ void KCTextEditor::searchString(QString searchTextSets,
updateSearchResults();
searchOnOtherThread(searcherForNext,threadNext,firstVisibleBlock(),true);
searchOnOtherThread(searcherForPrev,threadPrev,firstVisibleBlock(),false);
findString(true);
findString(false);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/app/kctexteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class KCTextEditor : public QPlainTextEdit
void updated();
void searchStringChangedByShortCut(QString searchText);
void overwriteModeChanged(bool newValue);
void matchedResult();
void nomatchedResult();

public slots:
void updateHighlights();
Expand Down Expand Up @@ -89,6 +91,12 @@ private slots:
QTextCursor cursor);
QString parenthesesPair(const QString &parenthesesChar);
bool findString(bool forward);
bool findPreviousString(KCTextBlockData *blockData,
QTextCursor currentCursor,
bool forward);
bool findNextString(KCTextBlockData *blockData,
QTextCursor currentCursor,
bool forward);
void generalSearch(const QTextBlock &block,
const int &lines,
const bool forward);
Expand Down

0 comments on commit fdf1c0f

Please # to comment.