refactor: SynDocument and SynDocumentLine

This commit is contained in:
Roy Qu 2022-04-19 21:18:41 +08:00
parent 354f375f67
commit 0a1c2aea98
17 changed files with 655 additions and 616 deletions

View File

@ -202,15 +202,15 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
Editor editor(nullptr);
if (pMainWindow->editorList()->getContentFromOpenedEditor(
filename,buffer)){
editor.lines()->setContents(buffer);
editor.document()->setContents(buffer);
} else {
QByteArray encoding;
editor.lines()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
}
editor.setHighlighter(HighlighterManager().getCppHighlighter());
int posY = 0;
while (posY < editor.lines()->count()) {
QString line = editor.lines()->getString(posY);
while (posY < editor.document()->count()) {
QString line = editor.document()->getString(posY);
if (line.isEmpty()) {
posY++;
continue;
@ -220,7 +220,7 @@ PSearchResultTreeItem CppRefacter::findOccurenceInFile(
editor.highlighter()->resetState();
} else {
editor.highlighter()->setState(
editor.lines()->ranges(posY-1));
editor.document()->ranges(posY-1));
}
editor.highlighter()->setLine(line,posY);
while (!editor.highlighter()->eol()) {
@ -266,22 +266,22 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
Editor editor(nullptr);
if (pMainWindow->editorList()->getContentFromOpenedEditor(
filename,buffer)){
editor.lines()->setContents(buffer);
editor.document()->setContents(buffer);
} else {
QByteArray encoding;
editor.lines()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
editor.document()->loadFromFile(filename,ENCODING_AUTO_DETECT,encoding);
}
QStringList newContents;
editor.setHighlighter(HighlighterManager().getCppHighlighter());
int posY = 0;
while (posY < editor.lines()->count()) {
QString line = editor.lines()->getString(posY);
while (posY < editor.document()->count()) {
QString line = editor.document()->getString(posY);
if (posY == 0) {
editor.highlighter()->resetState();
} else {
editor.highlighter()->setState(
editor.lines()->ranges(posY-1));
editor.document()->ranges(posY-1));
}
editor.highlighter()->setLine(line,posY);
QString newLine;
@ -318,7 +318,7 @@ void CppRefacter::renameSymbolInFile(const QString &filename, const PStatement &
} else {
QByteArray realEncoding;
QFile file(filename);
editor.lines()->saveToFile(file,ENCODING_AUTO_DETECT,
editor.document()->saveToFile(file,ENCODING_AUTO_DETECT,
pSettings->editor().defaultEncoding(),
realEncoding);
}

View File

@ -201,10 +201,10 @@ Editor::~Editor() {
void Editor::loadFile(QString filename) {
if (filename.isEmpty()) {
this->lines()->loadFromFile(mFilename,mEncodingOption,mFileEncoding);
this->document()->loadFromFile(mFilename,mEncodingOption,mFileEncoding);
} else {
filename = QFileInfo(filename).absoluteFilePath();
this->lines()->loadFromFile(filename,mEncodingOption,mFileEncoding);
this->document()->loadFromFile(filename,mEncodingOption,mFileEncoding);
}
//this->setModified(false);
updateCaption();
@ -234,7 +234,7 @@ void Editor::saveFile(QString filename) {
QByteArray encoding = mFileEncoding;
if (mEncodingOption!=ENCODING_AUTO_DETECT || mFileEncoding==ENCODING_ASCII)
encoding = mEncodingOption;
this->lines()->saveToFile(file,encoding,
this->document()->saveToFile(file,encoding,
pSettings->editor().defaultEncoding(),
mFileEncoding);
emit fileSaved(filename, mInProject);
@ -642,16 +642,16 @@ void Editor::keyPressEvent(QKeyEvent *event)
} else if (highlighter()
&& caretY()>=2
&& highlighter()->isLastLineCommentNotFinished(
lines()->ranges(caretY()-2).state)) {
document()->ranges(caretY()-2).state)) {
s=trimLeft(lineText());
if (s.startsWith("* ")) {
handled = true;
int right = lines()->getString(caretY()-1).length()-caretX();
int right = document()->getString(caretY()-1).length()-caretX();
s=lineBreak()+"* ";
insertString(s,false);
BufferCoord p = caretXY();
p.Line++;
p.Char = lines()->getString(p.Line-1).length()+1;
p.Char = document()->getString(p.Line-1).length()+1;
if (right>0) {
p.Char -=right+1;
}
@ -924,7 +924,7 @@ void Editor::onPreparePaintHighlightToken(int line, int aChar, const QString &to
return;
if (mParser && mParser->enabled() && highlighter() && (attr == highlighter()->identifierAttribute())
&& !mParser->isIncludeLine(lines()->getString(line-1)) ) {
&& !mParser->isIncludeLine(document()->getString(line-1)) ) {
BufferCoord p{aChar,line};
// BufferCoord pBeginPos,pEndPos;
@ -943,8 +943,8 @@ void Editor::onPreparePaintHighlightToken(int line, int aChar, const QString &to
QString s= getWordAtPosition(this,p, pBeginPos,pEndPos, WordPurpose::wpInformation);
if ((pEndPos.Line>=1)
&& (pEndPos.Char>=0)
&& (pEndPos.Char+1 < lines()->getString(pEndPos.Line-1).length())
&& (lines()->getString(pEndPos.Line-1)[pEndPos.Char+1] == '(')) {
&& (pEndPos.Char+1 < document()->getString(pEndPos.Line-1).length())
&& (document()->getString(pEndPos.Line-1)[pEndPos.Char+1] == '(')) {
kind = StatementKind::skFunction;
} else {
kind = StatementKind::skVariable;
@ -1035,7 +1035,7 @@ bool Editor::event(QEvent *event)
switch (reason) {
case TipType::Preprocessor:
// When hovering above a preprocessor line, determine if we want to show an include or a identifier hint
s = lines()->getString(p.Line - 1);
s = document()->getString(p.Line - 1);
isIncludeLine = mParser->isIncludeLine(s);
if (!isIncludeLine)
s = wordAtRowCol(p);
@ -1165,7 +1165,7 @@ void Editor::mouseReleaseEvent(QMouseEvent *event)
BufferCoord p;
if (pointToCharLine(event->pos(),p)) {
QString s = lines()->getString(p.Line - 1);
QString s = document()->getString(p.Line - 1);
if (mParser->isIncludeLine(s)) {
QString filename = mParser->getHeaderFileName(mFilename,s);
Editor * e = pMainWindow->editorList()->getEditorByFilename(filename);
@ -1304,12 +1304,12 @@ void Editor::copyToClipboard()
void Editor::cutToClipboard()
{
if (pSettings->editor().copySizeLimit()) {
if (lines()->count() > pSettings->editor().copyLineLimits()) {
if (document()->count() > pSettings->editor().copyLineLimits()) {
QMessageBox::critical(pMainWindow,tr("Error"),
tr("The text to be cut exceeds count limit!"));
return;
}
if (lines()->getTextLength() > pSettings->editor().copyCharLimits() * 1000) {
if (document()->getTextLength() > pSettings->editor().copyCharLimits() * 1000) {
QMessageBox::critical(pMainWindow,tr("Error"),
tr("The text to be cut exceeds character limit!"));
return;
@ -1344,7 +1344,7 @@ void Editor::copyAsHTML()
));
exporter.setCreateHTMLFragment(true);
exporter.ExportRange(lines(),blockBegin(),blockEnd());
exporter.ExportRange(document(),blockBegin(),blockEnd());
QMimeData * mimeData = new QMimeData;
@ -1379,20 +1379,20 @@ void Editor::addSyntaxIssues(int line, int startChar, int endChar, CompileIssueT
int tokenKind,start;
PSynHighlighterAttribute attr;
PSyntaxIssueList lst;
if ((line<1) || (line>lines()->count()))
if ((line<1) || (line>document()->count()))
return;
pError = std::make_shared<SyntaxIssue>();
p.Char = startChar;
p.Line = line;
if (startChar >= lines()->getString(line-1).length()) {
if (startChar >= document()->getString(line-1).length()) {
start = 1;
token = lines()->getString(line-1);
token = document()->getString(line-1);
} else if (endChar < 1) {
if (!getHighlighterAttriAtRowColEx(p,token,tokenType,tokenKind,start,attr))
return;
} else {
start = startChar;
token = lines()->getString(line-1).mid(start-1,endChar-startChar);
token = document()->getString(line-1).mid(start-1,endChar-startChar);
}
pError->startChar = start;
pError->endChar = start + token.length();
@ -1490,8 +1490,8 @@ void Editor::onStatusChanged(SynStatusChanges changes)
{
if ((!changes.testFlag(SynStatusChange::scReadOnly)
&& !changes.testFlag(SynStatusChange::scInsertMode)
&& (lines()->count()!=mLineCount)
&& (lines()->count()!=0) && ((mLineCount>0) || (lines()->count()>1)))
&& (document()->count()!=mLineCount)
&& (document()->count()!=0) && ((mLineCount>0) || (document()->count()>1)))
||
(mCurrentLineModified
&& !changes.testFlag(SynStatusChange::scReadOnly)
@ -1506,7 +1506,7 @@ void Editor::onStatusChanged(SynStatusChanges changes)
checkSyntaxInBack();
reparseTodo();
}
mLineCount = lines()->count();
mLineCount = document()->count();
if (changes.testFlag(scModifyChanged)) {
updateCaption();
}
@ -1778,15 +1778,15 @@ QStringList Editor::getExpressionAtPosition(
if (!highlighter)
return result;
while (true) {
if (line>=lines()->count() || line<0)
if (line>=document()->count() || line<0)
break;
QStringList tokens;
if (line==0) {
highlighter->resetState();
} else {
highlighter->setState(lines()->ranges(line-1));
highlighter->setState(document()->ranges(line-1));
}
QString sLine = lines()->getString(line);
QString sLine = document()->getString(line);
highlighter->setLine(sLine,line-1);
while (!highlighter->eol()) {
int start = highlighter->getTokenPos();
@ -1979,7 +1979,7 @@ QStringList Editor::getExpressionAtPosition(
line--;
if (line>=0)
ch = lines()->getString(line).length()+1;
ch = document()->getString(line).length()+1;
}
return result;
}
@ -1989,7 +1989,7 @@ QString Editor::getWordForCompletionSearch(const BufferCoord &pos,bool permitTil
QString result = "";
QString s;
s = lines()->getString(pos.Line - 1);
s = document()->getString(pos.Line - 1);
int len = s.length();
int wordBegin = pos.Char - 1 - 1; //BufferCoord::Char starts with 1
@ -2026,9 +2026,9 @@ bool Editor::handleSymbolCompletion(QChar key)
if (highlighter()) {
if (caretX() <= 1) {
if (caretY()>1) {
if (highlighter()->isLastLineCommentNotFinished(lines()->ranges(caretY() - 2).state))
if (highlighter()->isLastLineCommentNotFinished(document()->ranges(caretY() - 2).state))
return false;
if (highlighter()->isLastLineStringNotFinished(lines()->ranges(caretY() - 2).state)
if (highlighter()->isLastLineStringNotFinished(document()->ranges(caretY() - 2).state)
&& (key!='\"') && (key!='\''))
return false;
}
@ -2172,10 +2172,10 @@ bool Editor::handleParentheseSkip()
if (status != QuoteStatus::NotQuote)
return false;
if (lines()->count()==0)
if (document()->count()==0)
return false;
if (highlighter()) {
SynRangeState lastLineState = lines()->ranges(lines()->count()-1);
SynRangeState lastLineState = document()->ranges(document()->count()-1);
if (lastLineState.parenthesisLevel==0) {
setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over
return true;
@ -2223,10 +2223,10 @@ bool Editor::handleBracketSkip()
if (getCurrentChar() != ']')
return false;
if (lines()->count()==0)
if (document()->count()==0)
return false;
if (highlighter()) {
SynRangeState lastLineState = lines()->ranges(lines()->count()-1);
SynRangeState lastLineState = document()->ranges(document()->count()-1);
if (lastLineState.bracketLevel==0) {
setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over
return true;
@ -2269,7 +2269,7 @@ bool Editor::handleBraceCompletion()
QString s = lineText().trimmed();
int i= caretY()-2;
while ((s.isEmpty()) && (i>=0)) {
s=lines()->getString(i);
s=document()->getString(i);
i--;
}
QString text=selText();
@ -2309,10 +2309,10 @@ bool Editor::handleBraceSkip()
if (getCurrentChar() != '}')
return false;
if (lines()->count()==0)
if (document()->count()==0)
return false;
if (highlighter()) {
SynRangeState lastLineState = lines()->ranges(lines()->count()-1);
SynRangeState lastLineState = document()->ranges(document()->count()-1);
if (lastLineState.braceLevel==0) {
bool oldInsertMode = insertMode();
setInsertMode(false); //set mode to overwrite
@ -2500,10 +2500,10 @@ Editor::QuoteStatus Editor::getQuoteStatus()
QuoteStatus Result = QuoteStatus::NotQuote;
if (!highlighter())
return Result;
if ((caretY()>1) && highlighter()->isLastLineStringNotFinished(lines()->ranges(caretY() - 2).state))
if ((caretY()>1) && highlighter()->isLastLineStringNotFinished(document()->ranges(caretY() - 2).state))
Result = QuoteStatus::DoubleQuote;
QString Line = lines()->getString(caretY()-1);
QString Line = document()->getString(caretY()-1);
int posX = caretX()-1;
if (posX >= Line.length()) {
posX = Line.length()-1;
@ -2794,7 +2794,7 @@ void Editor::exportAsRTF(const QString &rtfFilename)
std::placeholders::_4,
std::placeholders::_5
));
exporter.ExportAll(lines());
exporter.ExportAll(document());
exporter.SaveToFile(rtfFilename);
}
@ -2819,7 +2819,7 @@ void Editor::exportAsHTML(const QString &htmlFilename)
std::placeholders::_4,
std::placeholders::_5
));
exporter.ExportAll(lines());
exporter.ExportAll(document());
exporter.SaveToFile(htmlFilename);
}
@ -3009,7 +3009,7 @@ void Editor::showHeaderCompletion(bool autoComplete)
bool Editor::testInFunc(int x, int y)
{
bool result = false;
QString s = lines()->getString(y);
QString s = document()->getString(y);
int posY = y;
int posX = std::min(x,s.length()-1); // x is started from 1
int bracketLevel=0;
@ -3018,7 +3018,7 @@ bool Editor::testInFunc(int x, int y)
posY--;
if (posY < 0)
return false;
s = lines()->getString(posY);
s = document()->getString(posY);
posX = s.length()-1;
}
if (s[posX] == '>'
@ -3321,7 +3321,7 @@ Editor::TipType Editor::getTipType(QPoint point, BufferCoord& pos)
// do not allow when dragging selection
if (isPointInSelection(pos))
return TipType::Selection;
} else if (mParser && mParser->isIncludeLine(lines()->getString(pos.Line-1))) {
} else if (mParser && mParser->isIncludeLine(document()->getString(pos.Line-1))) {
return TipType::Preprocessor;
}else if (attr == highlighter()->identifierAttribute())
return TipType::Identifier;
@ -3474,10 +3474,10 @@ void Editor::updateFunctionTip(bool showTip)
int bracketLevel = 0;
int paramsCount = 1;
int currentParamPos = 1;
if (currentLine>=lines()->count())
if (currentLine>=document()->count())
return;
while (currentLine>=0) {
QString line = lines()->getString(currentLine);
QString line = document()->getString(currentLine);
if (currentLine!=caretPos.Line-1)
currentChar = line.length();
QStringList tokens;
@ -3486,7 +3486,7 @@ void Editor::updateFunctionTip(bool showTip)
highlighter()->resetState();
else
highlighter()->setState(
lines()->ranges(currentLine-1));
document()->ranges(currentLine-1));
highlighter()->setLine(line,currentLine);
while(!highlighter()->eol()) {
int start = highlighter()->getTokenPos();
@ -3578,7 +3578,7 @@ void Editor::updateFunctionTip(bool showTip)
QString s = getWordAtPosition(this, functionNamePos, pWordBegin,pWordEnd, WordPurpose::wpInformation);
int x = pWordBegin.Char-1-1;
QString line = lines()->getString(pWordBegin.Line-1);
QString line = document()->getString(pWordBegin.Line-1);
bool hasPreviousWord=false;
while (x>=0) {
QChar ch=line[x];
@ -3678,7 +3678,7 @@ void Editor::popUserCodeInTabStops()
tabStopBegin = mTabStopEnd + p->x;
tabStopEnd = mTabStopEnd + p->endX;
} else {
int n=countLeadingWhitespaceChars(lines()->getString(caretY()-1+p->y));
int n=countLeadingWhitespaceChars(document()->getString(caretY()-1+p->y));
// qDebug()<<line<<n<<p->x;
tabStopBegin = n+p->x+1;
tabStopEnd = n+p->endX+1;
@ -3721,8 +3721,8 @@ void Editor::onExportedFormatToken(PSynHighlighter syntaxHighlighter, int Line,
if (kind == StatementKind::skUnknown) {
if ((pEndPos.Line>=1)
&& (pEndPos.Char>=0)
&& (pEndPos.Char < lines()->getString(pEndPos.Line-1).length())
&& (lines()->getString(pEndPos.Line-1)[pEndPos.Char] == '(')) {
&& (pEndPos.Char < document()->getString(pEndPos.Line-1).length())
&& (document()->getString(pEndPos.Line-1)[pEndPos.Char] == '(')) {
kind = StatementKind::skFunction;
} else {
kind = StatementKind::skVariable;
@ -3908,13 +3908,13 @@ QString getWordAtPosition(SynEdit *editor, const BufferCoord &p, BufferCoord &pW
{
QString result = "";
QString s;
if ((p.Line<1) || (p.Line>editor->lines()->count())) {
if ((p.Line<1) || (p.Line>editor->document()->count())) {
pWordBegin = p;
pWordEnd = p;
return "";
}
s = editor->lines()->getString(p.Line - 1);
s = editor->document()->getString(p.Line - 1);
int len = s.length();
int wordBegin = p.Char - 1 - 1; //BufferCoord::Char starts with 1
@ -4069,7 +4069,7 @@ QString getWordAtPosition(SynEdit *editor, const BufferCoord &p, BufferCoord &pW
if (i<0) {
line--;
if (line>=1) {
s=editor->lines()->getString(line-1);
s=editor->document()->getString(line-1);
i=s.length();
continue;
} else
@ -4119,12 +4119,12 @@ QString getWordAtPosition(SynEdit *editor, const BufferCoord &p, BufferCoord &pW
QString Editor::getPreviousWordAtPositionForSuggestion(const BufferCoord &p)
{
QString result;
if ((p.Line<1) || (p.Line>lines()->count())) {
if ((p.Line<1) || (p.Line>document()->count())) {
return "";
}
bool inFunc = testInFunc(p.Char-1,p.Line-1);
QString s = lines()->getString(p.Line - 1);
QString s = document()->getString(p.Line - 1);
int wordBegin;
int wordEnd = p.Char-1;
if (wordEnd >= s.length())
@ -4190,7 +4190,7 @@ void Editor::reformat()
}
#endif
//we must remove all breakpoints and syntax issues
onLinesDeleted(1,lines()->count());
onLinesDeleted(1,document()->count());
QByteArray content = text().toUtf8();
QStringList args = pSettings->codeFormatter().getArguments();
#ifdef Q_OS_WIN

View File

@ -458,7 +458,7 @@ void MainWindow::updateEditorActions()
ui->actionCopy->setEnabled(e->selAvail());
ui->actionCut->setEnabled(e->selAvail());
ui->actionFoldAll->setEnabled(e->lines()->count()>0);
ui->actionFoldAll->setEnabled(e->document()->count()>0);
ui->actionIndent->setEnabled(!e->readOnly());
ui->actionPaste->setEnabled(!e->readOnly() && !QGuiApplication::clipboard()->text().isEmpty());
@ -469,15 +469,15 @@ void MainWindow::updateEditorActions()
ui->actionExport_As_HTML->setEnabled(true);
ui->actionExport_As_RTF->setEnabled(true);
ui->actionPrint->setEnabled(true);
ui->actionSelectAll->setEnabled(e->lines()->count()>0);
ui->actionToggleComment->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionUnIndent->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionUnfoldAll->setEnabled(e->lines()->count()>0);
ui->actionDelete_Line->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionDelete_Word->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionDuplicate_Line->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionDelete_to_BOL->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionDelete_to_EOL->setEnabled(!e->readOnly() && e->lines()->count()>0);
ui->actionSelectAll->setEnabled(e->document()->count()>0);
ui->actionToggleComment->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionUnIndent->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionUnfoldAll->setEnabled(e->document()->count()>0);
ui->actionDelete_Line->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionDelete_Word->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionDuplicate_Line->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionDelete_to_BOL->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionDelete_to_EOL->setEnabled(!e->readOnly() && e->document()->count()>0);
ui->actionFind->setEnabled(true);
ui->actionReplace->setEnabled(true);
@ -491,7 +491,7 @@ void MainWindow::updateEditorActions()
ui->actionClose_All->setEnabled(true);
int line = e->caretY();
ui->actionAdd_bookmark->setEnabled(e->lines()->count()>0 && !e->hasBookmark(line));
ui->actionAdd_bookmark->setEnabled(e->document()->count()>0 && !e->hasBookmark(line));
ui->actionRemove_Bookmark->setEnabled(e->hasBookmark(line));
ui->actionModify_Bookmark_Description->setEnabled(e->hasBookmark(line));
@ -1033,8 +1033,8 @@ void MainWindow::updateStatusbarForLineCol(bool clear)
.arg(e->caretY())
.arg(col)
.arg(e->selText().length())
.arg(e->lines()->count())
.arg(e->lines()->getTextLength());
.arg(e->document()->count())
.arg(e->document()->getTextLength());
mFileInfoStatus->setText(msg);
} else {
mFileInfoStatus->setText("");
@ -3941,7 +3941,7 @@ void MainWindow::onEditorContextMenu(const QPoint& pos)
ui->actionLocate_in_Files_View->setEnabled(!editor->isNew());
ui->actionBreakpoint_property->setEnabled(editor->hasBreakpoint(line));
ui->actionAdd_bookmark->setEnabled(
line>=0 && editor->lines()->count()>0
line>=0 && editor->document()->count()>0
&& !editor->hasBookmark(line)
);
ui->actionRemove_Bookmark->setEnabled(editor->hasBookmark(line));
@ -4544,11 +4544,11 @@ void MainWindow::onCompileIssue(PCompileIssue issue)
Editor* e = mEditorList->getOpenedEditorByFilename(issue->filename);
if (e!=nullptr && (issue->line>0)) {
int line = issue->line;
if (line > e->lines()->count())
if (line > e->document()->count())
return;
int col = std::min(issue->column,e->lines()->getString(line-1).length()+1);
int col = std::min(issue->column,e->document()->getString(line-1).length()+1);
if (col < 1)
col = e->lines()->getString(line-1).length()+1;
col = e->document()->getString(line-1).length()+1;
e->addSyntaxIssues(line,col,issue->endColumn,issue->type,issue->description);
}
}
@ -6529,11 +6529,11 @@ void MainWindow::on_actionAdd_bookmark_triggered()
Editor* editor = mEditorList->getEditor();
int line;
if (editor && editor->pointToLine(mEditorContextMenuPos,line)) {
if (editor->lines()->count()<=0)
if (editor->document()->count()<=0)
return;
QString desc = QInputDialog::getText(editor,tr("Bookmark Description"),
tr("Description:"),QLineEdit::Normal,
editor->lines()->getString(line-1).trimmed());
editor->document()->getString(line-1).trimmed());
desc = desc.trimmed();
editor->addBookmark(line,desc);
}

File diff suppressed because it is too large Load Diff

View File

@ -348,7 +348,7 @@ public:
QString lineText() const;
void setLineText(const QString s);
const PSynEditStringList& lines() const;
const PSynDocument& document() const;
bool empty();
SynSelectionMode selectionMode() const;
@ -361,7 +361,7 @@ public:
SynEditorOptions getOptions() const;
void setOptions(const SynEditorOptions &Value);
int tabWidth() const;
int tabWidth() const { return mDocument->tabWidth(); }
void setTabWidth(int tabWidth);
QColor caretColor() const;
@ -642,8 +642,8 @@ private:
bool mInserting;
bool mPainting;
PSynEditStringList mLines;
PSynEditStringList mOrigLines;
PSynDocument mDocument;
PSynDocument mOrigLines;
PSynEditUndoList mOrigUndoList;
PSynEditUndoList mOrigRedoList;
int mLinesInWindow;
@ -690,7 +690,6 @@ private:
bool mWantReturns;
bool mWantTabs;
SynGutter mGutter;
int mTabWidth;
QRect mInvalidateRect;
SynStateFlags mStateFlags;
SynEditorOptions mOptions;
@ -748,41 +747,41 @@ friend class SynEditTextPainter;
// QWidget interface
protected:
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void timerEvent(QTimerEvent *event) override;
bool event(QEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void inputMethodEvent(QInputMethodEvent *event) override;
void leaveEvent(QEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void timerEvent(QTimerEvent *event) override;
bool event(QEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void inputMethodEvent(QInputMethodEvent *event) override;
void leaveEvent(QEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
// QAbstractScrollArea interface
protected:
bool viewportEvent(QEvent * event) override;
bool viewportEvent(QEvent * event) override;
// QWidget interface
public:
QVariant inputMethodQuery(Qt::InputMethodQuery property) const override;
// QWidget interface
public:
QVariant inputMethodQuery(Qt::InputMethodQuery property) const override;
// QWidget interface
const QFont &fontForNonAscii() const;
void setFontForNonAscii(const QFont &newFontForNonAscii);
// QWidget interface
const QFont &fontForNonAscii() const;
void setFontForNonAscii(const QFont &newFontForNonAscii);
int mouseSelectionScrollSpeed() const;
void setMouseSelectionScrollSpeed(int newMouseSelectionScrollSpeed);
int mouseSelectionScrollSpeed() const;
void setMouseSelectionScrollSpeed(int newMouseSelectionScrollSpeed);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dragLeaveEvent(QDragLeaveEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dragLeaveEvent(QDragLeaveEvent *event) override;
};
#endif // SYNEDIT_H

View File

@ -25,16 +25,20 @@
#include "../utils.h"
#include "../platform.h"
#include <QMessageBox>
#include <cmath>
SynEditStringList::SynEditStringList(SynEdit *pEdit, QObject *parent):
SynDocument::SynDocument(const QFont& font, QObject *parent):
QObject(parent),
mEdit(pEdit),
mTabWidth(4),
mFontMetrics(font),
mMutex(QMutex::Recursive)
{
mAppendNewLineAtEOF = true;
mFileEndingType = FileEndingType::Windows;
mIndexOfLongestLine = -1;
mUpdateCount = 0;
mCharWidth = mFontMetrics.horizontalAdvance("M");
}
static void ListIndexOutOfBounds(int index) {
@ -43,29 +47,29 @@ static void ListIndexOutOfBounds(int index) {
int SynEditStringList::parenthesisLevels(int Index)
int SynDocument::parenthesisLevels(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.parenthesisLevel;
if (Index>=0 && Index < mLines.size()) {
return mLines[Index]->fRange.parenthesisLevel;
} else
return 0;
}
int SynEditStringList::bracketLevels(int Index)
int SynDocument::bracketLevels(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.bracketLevel;
if (Index>=0 && Index < mLines.size()) {
return mLines[Index]->fRange.bracketLevel;
} else
return 0;
}
int SynEditStringList::braceLevels(int Index)
int SynDocument::braceLevels(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.braceLevel;
if (Index>=0 && Index < mLines.size()) {
return mLines[Index]->fRange.braceLevel;
} else
return 0;
}
@ -81,43 +85,43 @@ int SynEditStringList::braceLevels(int Index)
// return QString();
//}
int SynEditStringList::lineColumns(int Index)
int SynDocument::lineColumns(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
if (mList[Index]->fColumns == -1) {
if (Index>=0 && Index < mLines.size()) {
if (mLines[Index]->fColumns == -1) {
return calculateLineColumns(Index);
} else
return mList[Index]->fColumns;
return mLines[Index]->fColumns;
} else
return 0;
}
int SynEditStringList::leftBraces(int Index)
int SynDocument::leftBraces(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.leftBraces;
if (Index>=0 && Index < mLines.size()) {
return mLines[Index]->fRange.leftBraces;
} else
return 0;
}
int SynEditStringList::rightBraces(int Index)
int SynDocument::rightBraces(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange.rightBraces;
if (Index>=0 && Index < mLines.size()) {
return mLines[Index]->fRange.rightBraces;
} else
return 0;
}
int SynEditStringList::lengthOfLongestLine() {
int SynDocument::lengthOfLongestLine() {
QMutexLocker locker(&mMutex);
if (mIndexOfLongestLine < 0) {
int MaxLen = -1;
mIndexOfLongestLine = -1;
if (mList.count() > 0 ) {
for (int i=0;i<mList.size();i++) {
if (mLines.count() > 0 ) {
for (int i=0;i<mLines.size();i++) {
int len = lineColumns(i);
if (len > MaxLen) {
MaxLen = len;
@ -127,12 +131,12 @@ int SynEditStringList::lengthOfLongestLine() {
}
}
if (mIndexOfLongestLine >= 0)
return mList[mIndexOfLongestLine]->fColumns;
return mLines[mIndexOfLongestLine]->fColumns;
else
return 0;
}
QString SynEditStringList::lineBreak() const
QString SynDocument::lineBreak() const
{
switch(mFileEndingType) {
case FileEndingType::Linux:
@ -145,97 +149,97 @@ QString SynEditStringList::lineBreak() const
return "\n";
}
SynRangeState SynEditStringList::ranges(int Index)
SynRangeState SynDocument::ranges(int Index)
{
QMutexLocker locker(&mMutex);
if (Index>=0 && Index < mList.size()) {
return mList[Index]->fRange;
if (Index>=0 && Index < mLines.size()) {
return mLines[Index]->fRange;
} else {
ListIndexOutOfBounds(Index);
}
return {0};
}
void SynEditStringList::insertItem(int Index, const QString &s)
void SynDocument::insertItem(int Index, const QString &s)
{
beginUpdate();
PSynEditStringRec line = std::make_shared<SynEditStringRec>();
PSynDocumentLine line = std::make_shared<SynDocumentLine>();
line->fString = s;
mIndexOfLongestLine = -1;
mList.insert(Index,line);
mLines.insert(Index,line);
endUpdate();
}
void SynEditStringList::addItem(const QString &s)
void SynDocument::addItem(const QString &s)
{
beginUpdate();
PSynEditStringRec line = std::make_shared<SynEditStringRec>();
PSynDocumentLine line = std::make_shared<SynDocumentLine>();
line->fString = s;
mIndexOfLongestLine = -1;
mList.append(line);
mLines.append(line);
endUpdate();
}
bool SynEditStringList::getAppendNewLineAtEOF()
bool SynDocument::getAppendNewLineAtEOF()
{
QMutexLocker locker(&mMutex);
return mAppendNewLineAtEOF;
}
void SynEditStringList::setAppendNewLineAtEOF(bool appendNewLineAtEOF)
void SynDocument::setAppendNewLineAtEOF(bool appendNewLineAtEOF)
{
QMutexLocker locker(&mMutex);
mAppendNewLineAtEOF = appendNewLineAtEOF;
}
void SynEditStringList::setRange(int Index, const SynRangeState& ARange)
void SynDocument::setRange(int Index, const SynRangeState& ARange)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) {
if (Index<0 || Index>=mLines.count()) {
ListIndexOutOfBounds(Index);
}
beginUpdate();
mList[Index]->fRange = ARange;
mLines[Index]->fRange = ARange;
endUpdate();
}
QString SynEditStringList::getString(int Index)
QString SynDocument::getString(int Index)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) {
if (Index<0 || Index>=mLines.count()) {
return QString();
}
return mList[Index]->fString;
return mLines[Index]->fString;
}
int SynEditStringList::count()
int SynDocument::count()
{
QMutexLocker locker(&mMutex);
return mList.count();
return mLines.count();
}
void *SynEditStringList::getObject(int Index)
void *SynDocument::getObject(int Index)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) {
if (Index<0 || Index>=mLines.count()) {
return nullptr;
}
return mList[Index]->fObject;
return mLines[Index]->fObject;
}
QString SynEditStringList::text()
QString SynDocument::text()
{
QMutexLocker locker(&mMutex);
return getTextStr();
}
void SynEditStringList::setText(const QString &text)
void SynDocument::setText(const QString &text)
{
QMutexLocker locker(&mMutex);
putTextStr(text);
}
void SynEditStringList::setContents(const QStringList &text)
void SynDocument::setContents(const QStringList &text)
{
QMutexLocker locker(&mMutex);
beginUpdate();
@ -245,7 +249,7 @@ void SynEditStringList::setContents(const QStringList &text)
internalClear();
if (text.count() > 0) {
mIndexOfLongestLine = -1;
int FirstAdded = mList.count();
int FirstAdded = mLines.count();
foreach (const QString& s,text) {
addItem(s);
@ -254,18 +258,18 @@ void SynEditStringList::setContents(const QStringList &text)
}
}
QStringList SynEditStringList::contents()
QStringList SynDocument::contents()
{
QMutexLocker locker(&mMutex);
QStringList Result;
SynEditStringRecList list = mList;
foreach (const PSynEditStringRec& line, list) {
SynDocumentLines list = mLines;
foreach (const PSynDocumentLine& line, list) {
Result.append(line->fString);
}
return Result;
}
void SynEditStringList::beginUpdate()
void SynDocument::beginUpdate()
{
if (mUpdateCount == 0) {
setUpdateState(true);
@ -273,7 +277,7 @@ void SynEditStringList::beginUpdate()
mUpdateCount++;
}
void SynEditStringList::endUpdate()
void SynDocument::endUpdate()
{
mUpdateCount--;
if (mUpdateCount == 0) {
@ -282,18 +286,18 @@ void SynEditStringList::endUpdate()
}
int SynEditStringList::add(const QString &s)
int SynDocument::add(const QString &s)
{
QMutexLocker locker(&mMutex);
beginUpdate();
int Result = mList.count();
int Result = mLines.count();
insertItem(Result, s);
emit inserted(Result,1);
endUpdate();
return Result;
}
void SynEditStringList::addStrings(const QStringList &Strings)
void SynDocument::addStrings(const QStringList &Strings)
{
QMutexLocker locker(&mMutex);
if (Strings.count() > 0) {
@ -302,7 +306,7 @@ void SynEditStringList::addStrings(const QStringList &Strings)
auto action = finally([this]{
endUpdate();
});
int FirstAdded = mList.count();
int FirstAdded = mLines.count();
for (const QString& s:Strings) {
addItem(s);
@ -311,11 +315,11 @@ void SynEditStringList::addStrings(const QStringList &Strings)
}
}
int SynEditStringList::getTextLength()
int SynDocument::getTextLength()
{
QMutexLocker locker(&mMutex);
int Result = 0;
foreach (const PSynEditStringRec& line, mList ) {
foreach (const PSynDocumentLine& line, mLines ) {
Result += line->fString.length();
if (mFileEndingType == FileEndingType::Windows) {
Result += 2;
@ -326,18 +330,18 @@ int SynEditStringList::getTextLength()
return Result;
}
void SynEditStringList::clear()
void SynDocument::clear()
{
QMutexLocker locker(&mMutex);
internalClear();
}
void SynEditStringList::deleteLines(int Index, int NumLines)
void SynDocument::deleteLines(int Index, int NumLines)
{
QMutexLocker locker(&mMutex);
if (NumLines<=0)
return;
if ((Index < 0) || (Index >= mList.count())) {
if ((Index < 0) || (Index >= mLines.count())) {
ListIndexOutOfBounds(Index);
}
beginUpdate();
@ -347,27 +351,27 @@ void SynEditStringList::deleteLines(int Index, int NumLines)
if (mIndexOfLongestLine>=Index && (mIndexOfLongestLine <Index+NumLines)) {
mIndexOfLongestLine = - 1;
}
int LinesAfter = mList.count() - (Index + NumLines);
int LinesAfter = mLines.count() - (Index + NumLines);
if (LinesAfter < 0) {
NumLines = mList.count() - Index;
NumLines = mLines.count() - Index;
}
mList.remove(Index,NumLines);
mLines.remove(Index,NumLines);
emit deleted(Index,NumLines);
}
void SynEditStringList::exchange(int Index1, int Index2)
void SynDocument::exchange(int Index1, int Index2)
{
QMutexLocker locker(&mMutex);
if ((Index1 < 0) || (Index1 >= mList.count())) {
if ((Index1 < 0) || (Index1 >= mLines.count())) {
ListIndexOutOfBounds(Index1);
}
if ((Index2 < 0) || (Index2 >= mList.count())) {
if ((Index2 < 0) || (Index2 >= mLines.count())) {
ListIndexOutOfBounds(Index2);
}
beginUpdate();
PSynEditStringRec temp = mList[Index1];
mList[Index1]=mList[Index2];
mList[Index2]=temp;
PSynDocumentLine temp = mLines[Index1];
mLines[Index1]=mLines[Index2];
mLines[Index2]=temp;
//mList.swapItemsAt(Index1,Index2);
if (mIndexOfLongestLine == Index1) {
mIndexOfLongestLine = Index2;
@ -377,10 +381,10 @@ void SynEditStringList::exchange(int Index1, int Index2)
endUpdate();
}
void SynEditStringList::insert(int Index, const QString &s)
void SynDocument::insert(int Index, const QString &s)
{
QMutexLocker locker(&mMutex);
if ((Index < 0) || (Index > mList.count())) {
if ((Index < 0) || (Index > mLines.count())) {
ListIndexOutOfBounds(Index);
}
beginUpdate();
@ -389,10 +393,10 @@ void SynEditStringList::insert(int Index, const QString &s)
endUpdate();
}
void SynEditStringList::deleteAt(int Index)
void SynDocument::deleteAt(int Index)
{
QMutexLocker locker(&mMutex);
if ((Index < 0) || (Index >= mList.count())) {
if ((Index < 0) || (Index >= mLines.count())) {
ListIndexOutOfBounds(Index);
}
beginUpdate();
@ -400,38 +404,38 @@ void SynEditStringList::deleteAt(int Index)
mIndexOfLongestLine = -1;
else if (mIndexOfLongestLine>Index)
mIndexOfLongestLine -= 1;
mList.removeAt(Index);
mLines.removeAt(Index);
emit deleted(Index,1);
endUpdate();
}
QString SynEditStringList::getTextStr() const
QString SynDocument::getTextStr() const
{
QString result;
for (int i=0;i<mList.count()-1;i++) {
const PSynEditStringRec& line = mList[i];
for (int i=0;i<mLines.count()-1;i++) {
const PSynDocumentLine& line = mLines[i];
result.append(line->fString);
result.append(lineBreak());
}
if (mList.length()>0) {
result.append(mList.back()->fString);
if (mLines.length()>0) {
result.append(mLines.back()->fString);
}
return result;
}
void SynEditStringList::putString(int Index, const QString &s, bool notify) {
void SynDocument::putString(int Index, const QString &s, bool notify) {
QMutexLocker locker(&mMutex);
if (Index == mList.count()) {
if (Index == mLines.count()) {
add(s);
} else {
if (Index<0 || Index>=mList.count()) {
if (Index<0 || Index>=mLines.count()) {
ListIndexOutOfBounds(Index);
}
beginUpdate();
int oldColumns = mList[Index]->fColumns;
mList[Index]->fString = s;
int oldColumns = mLines[Index]->fColumns;
mLines[Index]->fString = s;
calculateLineColumns(Index);
if (oldColumns>mList[Index]->fColumns)
if (oldColumns>mLines[Index]->fColumns)
mIndexOfLongestLine = -1;
if (notify)
emit putted(Index,1);
@ -439,18 +443,18 @@ void SynEditStringList::putString(int Index, const QString &s, bool notify) {
}
}
void SynEditStringList::putObject(int Index, void *AObject)
void SynDocument::putObject(int Index, void *AObject)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) {
if (Index<0 || Index>=mLines.count()) {
ListIndexOutOfBounds(Index);
}
beginUpdate();
mList[Index]->fObject = AObject;
mLines[Index]->fObject = AObject;
endUpdate();
}
void SynEditStringList::setUpdateState(bool Updating)
void SynDocument::setUpdateState(bool Updating)
{
if (Updating)
emit changing();
@ -458,18 +462,18 @@ void SynEditStringList::setUpdateState(bool Updating)
emit changed();
}
int SynEditStringList::calculateLineColumns(int Index)
int SynDocument::calculateLineColumns(int Index)
{
PSynEditStringRec line = mList[Index];
PSynDocumentLine line = mLines[Index];
line->fColumns = mEdit->stringColumns(line->fString,0);
line->fColumns = stringColumns(line->fString,0);
return line->fColumns;
}
void SynEditStringList::insertLines(int Index, int NumLines)
void SynDocument::insertLines(int Index, int NumLines)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>mList.count()) {
if (Index<0 || Index>mLines.count()) {
ListIndexOutOfBounds(Index);
}
if (NumLines<=0)
@ -478,19 +482,19 @@ void SynEditStringList::insertLines(int Index, int NumLines)
auto action = finally([this]{
endUpdate();
});
PSynEditStringRec line;
mList.insert(Index,NumLines,line);
PSynDocumentLine line;
mLines.insert(Index,NumLines,line);
for (int i=Index;i<Index+NumLines;i++) {
line = std::make_shared<SynEditStringRec>();
mList[i]=line;
line = std::make_shared<SynDocumentLine>();
mLines[i]=line;
}
emit inserted(Index,NumLines);
}
void SynEditStringList::insertStrings(int Index, const QStringList &NewStrings)
void SynDocument::insertStrings(int Index, const QStringList &NewStrings)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>mList.count()) {
if (Index<0 || Index>mLines.count()) {
ListIndexOutOfBounds(Index);
}
if (NewStrings.isEmpty())
@ -499,20 +503,20 @@ void SynEditStringList::insertStrings(int Index, const QStringList &NewStrings)
auto action = finally([this]{
endUpdate();
});
PSynEditStringRec line;
mList.insert(Index,NewStrings.length(),line);
PSynDocumentLine line;
mLines.insert(Index,NewStrings.length(),line);
for (int i=0;i<NewStrings.length();i++) {
line = std::make_shared<SynEditStringRec>();
line = std::make_shared<SynDocumentLine>();
line->fString = NewStrings[i];
mList[i+Index]=line;
mLines[i+Index]=line;
}
emit inserted(Index,NewStrings.length());
}
void SynEditStringList::insertText(int Index, const QString &NewText)
void SynDocument::insertText(int Index, const QString &NewText)
{
QMutexLocker locker(&mMutex);
if (Index<0 || Index>=mList.count()) {
if (Index<0 || Index>=mLines.count()) {
ListIndexOutOfBounds(Index);
}
if (NewText.isEmpty())
@ -521,7 +525,7 @@ void SynEditStringList::insertText(int Index, const QString &NewText)
insertStrings(Index,lines);
}
bool SynEditStringList::tryLoadFileByEncoding(QByteArray encodingName, QFile& file) {
bool SynDocument::tryLoadFileByEncoding(QByteArray encodingName, QFile& file) {
QTextCodec* codec = QTextCodec::codecForName(encodingName);
if (!codec)
return false;
@ -549,7 +553,26 @@ bool SynEditStringList::tryLoadFileByEncoding(QByteArray encodingName, QFile& fi
}
return true;
}
void SynEditStringList::loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding)
const QFontMetrics &SynDocument::fontMetrics() const
{
return mFontMetrics;
}
void SynDocument::setFontMetrics(const QFont &newFont)
{
mFontMetrics = QFontMetrics(newFont);
mCharWidth = mFontMetrics.horizontalAdvance("M");
}
void SynDocument::setTabWidth(int newTabWidth)
{
if (mTabWidth!=newTabWidth) {
mTabWidth = newTabWidth;
resetColumns();
}
}
void SynDocument::loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding)
{
QMutexLocker locker(&mMutex);
QFile file(filename);
@ -613,7 +636,7 @@ void SynEditStringList::loadFromFile(const QString& filename, const QByteArray&
}
line = file.readLine();
}
emit inserted(0,mList.count());
emit inserted(0,mLines.count());
if (!needReread) {
if (allAscii)
realEncoding = ENCODING_ASCII;
@ -623,7 +646,7 @@ void SynEditStringList::loadFromFile(const QString& filename, const QByteArray&
QList<PCharsetInfo> charsets = pCharsetInfoManager->findCharsetByLocale(pCharsetInfoManager->localeName());
if (!charsets.isEmpty()) {
if (tryLoadFileByEncoding(realEncoding,file)) {
emit inserted(0,mList.count());
emit inserted(0,mLines.count());
return;
}
@ -638,7 +661,7 @@ void SynEditStringList::loadFromFile(const QString& filename, const QByteArray&
if (tryLoadFileByEncoding(encodingName,file)) {
qDebug()<<encodingName;
realEncoding = encodingName;
emit inserted(0,mList.count());
emit inserted(0,mLines.count());
return;
}
}
@ -671,18 +694,18 @@ void SynEditStringList::loadFromFile(const QString& filename, const QByteArray&
}
addItem(line);
}
emit inserted(0,mList.count());
emit inserted(0,mLines.count());
}
void SynEditStringList::saveToFile(QFile &file, const QByteArray& encoding,
void SynDocument::saveToFile(QFile &file, const QByteArray& encoding,
const QByteArray& defaultEncoding, QByteArray& realEncoding)
{
QMutexLocker locker(&mMutex);
if (!file.open(QFile::WriteOnly | QFile::Truncate))
throw FileError(tr("Can't open file '%1' for save!").arg(file.fileName()));
if (mList.isEmpty())
if (mLines.isEmpty())
return;
bool allAscii = true;
@ -702,7 +725,7 @@ void SynEditStringList::saveToFile(QFile &file, const QByteArray& encoding,
} else {
codec = QTextCodec::codecForName(realEncoding);
}
for (PSynEditStringRec& line:mList) {
for (PSynDocumentLine& line:mLines) {
if (allAscii) {
allAscii = isTextAllAscii(line->fString);
}
@ -724,7 +747,31 @@ void SynEditStringList::saveToFile(QFile &file, const QByteArray& encoding,
}
}
void SynEditStringList::putTextStr(const QString &text)
int SynDocument::stringColumns(const QString &line, int colsBefore) const
{
int columns = std::max(0,colsBefore);
int charCols;
for (int i=0;i<line.length();i++) {
QChar ch = line[i];
if (ch == '\t') {
charCols = mTabWidth - columns % mTabWidth;
} else {
charCols = charColumns(ch);
}
columns+=charCols;
}
return columns-colsBefore;
}
int SynDocument::charColumns(QChar ch) const
{
if (ch.unicode()<=32)
return 1;
//return std::ceil((int)(fontMetrics().horizontalAdvance(ch) * dpiFactor()) / (double)mCharWidth);
return std::ceil((int)(fontMetrics().horizontalAdvance(ch)) / (double)mCharWidth);
}
void SynDocument::putTextStr(const QString &text)
{
beginUpdate();
auto action = finally([this]{
@ -751,57 +798,57 @@ void SynEditStringList::putTextStr(const QString &text)
}
}
void SynEditStringList::internalClear()
void SynDocument::internalClear()
{
if (!mList.isEmpty()) {
if (!mLines.isEmpty()) {
beginUpdate();
int oldCount = mList.count();
int oldCount = mLines.count();
mIndexOfLongestLine = -1;
mList.clear();
mLines.clear();
emit deleted(0,oldCount);
endUpdate();
}
}
FileEndingType SynEditStringList::getFileEndingType()
FileEndingType SynDocument::getFileEndingType()
{
QMutexLocker locker(&mMutex);
return mFileEndingType;
}
void SynEditStringList::setFileEndingType(const FileEndingType &fileEndingType)
void SynDocument::setFileEndingType(const FileEndingType &fileEndingType)
{
QMutexLocker locker(&mMutex);
mFileEndingType = fileEndingType;
}
bool SynEditStringList::empty()
bool SynDocument::empty()
{
QMutexLocker locker(&mMutex);
return mList.count()==0;
return mLines.count()==0;
}
void SynEditStringList::resetColumns()
void SynDocument::resetColumns()
{
QMutexLocker locker(&mMutex);
mIndexOfLongestLine = -1;
if (mList.count() > 0 ) {
for (int i=0;i<mList.size();i++) {
mList[i]->fColumns = -1;
if (mLines.count() > 0 ) {
for (int i=0;i<mLines.size();i++) {
mLines[i]->fColumns = -1;
}
}
}
void SynEditStringList::invalidAllLineColumns()
void SynDocument::invalidAllLineColumns()
{
QMutexLocker locker(&mMutex);
mIndexOfLongestLine = -1;
for (PSynEditStringRec& line:mList) {
for (PSynDocumentLine& line:mLines) {
line->fColumns = -1;
}
}
SynEditStringRec::SynEditStringRec():
SynDocumentLine::SynDocumentLine():
fString(),
fObject(nullptr),
fRange{0,0,0,0,0},

View File

@ -19,6 +19,7 @@
#include <QStringList>
#include "highlighter/base.h"
#include <QFontMetrics>
#include <QMutex>
#include <QVector>
#include <memory>
@ -34,36 +35,33 @@ enum SynEditStringFlag {
typedef int SynEditStringFlags;
struct SynEditStringRec {
struct SynDocumentLine {
QString fString;
void * fObject;
SynRangeState fRange;
int fColumns; //
public:
explicit SynEditStringRec();
explicit SynDocumentLine();
};
typedef std::shared_ptr<SynEditStringRec> PSynEditStringRec;
typedef std::shared_ptr<SynDocumentLine> PSynDocumentLine;
typedef QVector<PSynEditStringRec> SynEditStringRecList;
typedef QVector<PSynDocumentLine> SynDocumentLines;
typedef std::shared_ptr<SynEditStringRecList> PSynEditStringRecList;
typedef std::shared_ptr<SynDocumentLines> PSynDocumentLines;
class SynEditStringList;
class SynDocument;
typedef std::shared_ptr<SynEditStringList> PSynEditStringList;
using StringListChangeCallback = std::function<void(PSynEditStringList* object, int index, int count)>;
typedef std::shared_ptr<SynDocument> PSynDocument;
class QFile;
class SynEdit;
class SynEditStringList : public QObject
class SynDocument : public QObject
{
Q_OBJECT
public:
explicit SynEditStringList(SynEdit* pEdit,QObject* parent=nullptr);
explicit SynDocument(const QFont& font, QObject* parent=nullptr);
int parenthesisLevels(int Index);
int bracketLevels(int Index);
@ -104,6 +102,8 @@ public:
void loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding);
void saveToFile(QFile& file, const QByteArray& encoding,
const QByteArray& defaultEncoding, QByteArray& realEncoding);
int stringColumns(const QString& line, int colsBefore) const;
int charColumns(QChar ch) const;
bool getAppendNewLineAtEOF();
void setAppendNewLineAtEOF(bool appendNewLineAtEOF);
@ -114,6 +114,14 @@ public:
bool empty();
void resetColumns();
int tabWidth() const {
return mTabWidth;
}
void setTabWidth(int newTabWidth);
const QFontMetrics &fontMetrics() const;
void setFontMetrics(const QFont &newFont);
public slots:
void invalidAllLineColumns();
@ -135,9 +143,13 @@ private:
bool tryLoadFileByEncoding(QByteArray encodingName, QFile& file);
private:
SynEditStringRecList mList;
SynDocumentLines mLines;
SynEdit* mEdit;
//SynEdit* mEdit;
QFontMetrics mFontMetrics;
int mTabWidth;
int mCharWidth;
//int mCount;
//int mCapacity;
FileEndingType mFileEndingType;

View File

@ -128,7 +128,7 @@ void SynEditTextPainter::paintGutter(const QRect& clip)
BufferCoord selectionEnd = edit->blockEnd();
for (int cRow = aFirstRow; cRow <= aLastRow; cRow++) {
vLine = edit->rowToLine(cRow);
if ((vLine > edit->mLines->count()) && (edit->mLines->count() > 0 ))
if ((vLine > edit->mDocument->count()) && (edit->mDocument->count() > 0 ))
break;
if (edit->mGutter.activeLineTextColor().isValid()) {
if (
@ -164,7 +164,7 @@ void SynEditTextPainter::paintGutter(const QRect& clip)
if (edit->mUseCodeFolding) {
for (cRow = aLastRow; cRow>=aFirstRow; cRow--) {
vLine = edit->rowToLine(cRow);
if ((vLine > edit->mLines->count()) && (edit->mLines->count() != 0))
if ((vLine > edit->mDocument->count()) && (edit->mDocument->count() != 0))
continue;
// Form a rectangle for the square the user can click on
@ -229,7 +229,7 @@ void SynEditTextPainter::paintGutter(const QRect& clip)
for (cRow = aFirstRow; cRow <=aLastRow; cRow++) {
vLine = edit->rowToLine(cRow);
if ((vLine > edit->mLines->count()) && (edit->mLines->count() != 0))
if ((vLine > edit->mDocument->count()) && (edit->mDocument->count() != 0))
break;
edit->onGutterPaint(*painter,vLine, 0, (cRow - edit->mTopLine) * edit->mTextHeight);
}
@ -359,7 +359,7 @@ void SynEditTextPainter::PaintToken(const QString &Token, int TokenCols, int Col
int charCols=0;
QString textToPaint = Token[i];
if (Token[i] == SynTabChar) {
charCols = edit->mTabWidth - ((ColumnsBefore+tokenColLen) % edit->mTabWidth);
charCols = edit->tabWidth() - ((ColumnsBefore+tokenColLen) % edit->tabWidth());
} else {
charCols = edit->charColumns(Token[i]);
}
@ -675,7 +675,7 @@ void SynEditTextPainter::PaintFoldAttributes()
// Now loop through all the lines. The indices are valid for Lines.
for (cRow = aFirstRow; cRow<=aLastRow;cRow++) {
vLine = edit->rowToLine(cRow);
if (vLine > edit->mLines->count() && edit->mLines->count() > 0)
if (vLine > edit->mDocument->count() && edit->mDocument->count() > 0)
break;
// Set vertical coord
Y = (cRow - edit->mTopLine) * edit->mTextHeight; // limit inside clip rect
@ -684,15 +684,15 @@ void SynEditTextPainter::PaintFoldAttributes()
}
// Get next nonblank line
LastNonBlank = vLine - 1;
while (LastNonBlank + 1 < edit->mLines->count() && edit->mLines->getString(LastNonBlank).isEmpty())
while (LastNonBlank + 1 < edit->mDocument->count() && edit->mDocument->getString(LastNonBlank).isEmpty())
LastNonBlank++;
if (LastNonBlank>=edit->lines()->count())
if (LastNonBlank>=edit->document()->count())
continue;
LineIndent = edit->getLineIndent(edit->mLines->getString(LastNonBlank));
int braceLevel = edit->mLines->ranges(LastNonBlank).braceLevel;
LineIndent = edit->getLineIndent(edit->mDocument->getString(LastNonBlank));
int braceLevel = edit->mDocument->ranges(LastNonBlank).braceLevel;
int indentLevel = braceLevel ;
if (edit->mTabWidth>0)
indentLevel = LineIndent / edit->mTabWidth;
if (edit->tabWidth()>0)
indentLevel = LineIndent / edit->tabWidth();
int levelDiff = std::max(0,braceLevel - indentLevel);
// Step horizontal coord
//TabSteps = edit->mTabWidth;
@ -701,7 +701,7 @@ void SynEditTextPainter::PaintFoldAttributes()
while (TabSteps < LineIndent) {
X = TabSteps * edit->mCharWidth + edit->textOffset() - 2;
TabSteps+=edit->mTabWidth;
TabSteps+=edit->tabWidth();
indentLevel++ ;
if (edit->mHighlighter) {
if (edit->mCodeFolding.indentGuides) {
@ -815,11 +815,11 @@ void SynEditTextPainter::PaintLines()
BufferCoord selectionEnd= edit->blockEnd();
for (cRow = aFirstRow; cRow<=aLastRow; cRow++) {
vLine = edit->rowToLine(cRow);
if (vLine > edit->mLines->count() && edit->mLines->count() != 0)
if (vLine > edit->mDocument->count() && edit->mDocument->count() != 0)
break;
// Get the line.
sLine = edit->mLines->getString(vLine - 1);
sLine = edit->mDocument->getString(vLine - 1);
// determine whether will be painted with ActiveLineColor
if (edit->mActiveSelectionMode == SynSelectionMode::smColumn) {
bCurrentLine = (vLine >= selectionBegin.Line && vLine <= selectionEnd.Line);
@ -892,7 +892,7 @@ void SynEditTextPainter::PaintLines()
if (bCurrentLine) {
nTokenColumnLen = edit->stringColumns(sLine,0);
} else {
nTokenColumnLen = edit->mLines->lineColumns(vLine-1);
nTokenColumnLen = edit->mDocument->lineColumns(vLine-1);
}
if (edit->mOptions.testFlag(eoShowSpecialChars) && (!bLineSelected) && (!bSpecialLine) && (nTokenColumnLen < vLastChar)) {
sToken = sToken + SynLineBreakGlyph;
@ -932,7 +932,7 @@ void SynEditTextPainter::PaintLines()
edit->mHighlighter->resetState();
} else {
edit->mHighlighter->setState(
edit->mLines->ranges(vLine-2));
edit->mDocument->ranges(vLine-2));
}
edit->mHighlighter->setLine(sLine, vLine - 1);
// Try to concatenate as many tokens as possible to minimize the count
@ -1008,7 +1008,7 @@ void SynEditTextPainter::PaintLines()
// Don't assume HL.GetTokenPos is valid after HL.GetEOL == True.
//nTokenColumnsBefore += edit->stringColumns(sToken,nTokenColumnsBefore);
if (edit->mHighlighter->eol() && (nTokenColumnsBefore < vLastChar)) {
int lineColumns = edit->mLines->lineColumns(vLine-1);
int lineColumns = edit->mDocument->lineColumns(vLine-1);
// Draw text that couldn't be parsed by the highlighter, if any.
if (nTokenColumnsBefore < lineColumns) {
if (nTokenColumnsBefore + 1 < vFirstChar)
@ -1022,9 +1022,9 @@ void SynEditTextPainter::PaintLines()
}
// Draw LineBreak glyph.
if (edit->mOptions.testFlag(eoShowSpecialChars) && (!bLineSelected) &&
(!bSpecialLine) && (edit->mLines->lineColumns(vLine-1) < vLastChar)) {
(!bSpecialLine) && (edit->mDocument->lineColumns(vLine-1) < vLastChar)) {
AddHighlightToken(SynLineBreakGlyph,
edit->mLines->lineColumns(vLine-1) - (vFirstChar - FirstCol),
edit->mDocument->lineColumns(vLine-1) - (vFirstChar - FirstCol),
edit->charColumns(SynLineBreakGlyph),vLine, edit->mHighlighter->whitespaceAttribute());
}
}
@ -1033,10 +1033,10 @@ void SynEditTextPainter::PaintLines()
foldRange = edit->foldStartAtLine(vLine);
if ((foldRange) && foldRange->collapsed) {
sFold = " ... } ";
nFold = edit->stringColumns(sFold,edit->mLines->lineColumns(vLine-1));
nFold = edit->stringColumns(sFold,edit->mDocument->lineColumns(vLine-1));
attr = edit->mHighlighter->symbolAttribute();
GetBraceColorAttr(edit->mHighlighter->getRangeState().braceLevel,attr);
AddHighlightToken(sFold,edit->mLines->lineColumns(vLine-1)+1 - (vFirstChar - FirstCol)
AddHighlightToken(sFold,edit->mDocument->lineColumns(vLine-1)+1 - (vFirstChar - FirstCol)
, nFold, vLine, attr);
}

View File

@ -28,7 +28,7 @@ ContentsCoord::ContentsCoord(const SynEdit *edit, int ch, int line)
void ContentsCoord::normalize()
{
if (mEdit->lines()->count()==0) {
if (mEdit->document()->count()==0) {
mChar = 0;
mLine = 0;
return;
@ -36,9 +36,9 @@ void ContentsCoord::normalize()
int aLine = mLine;
int aChar = mChar;
int line = aLine-1;
int lineCount = mEdit->lines()->count();
int lineCount = mEdit->document()->count();
if (line>=lineCount) {
mChar = mEdit->lines()->getString(lineCount-1).length()+1;
mChar = mEdit->document()->getString(lineCount-1).length()+1;
mLine = lineCount;
return;
}
@ -55,7 +55,7 @@ void ContentsCoord::normalize()
mLine = 0;
return;
}
QString s = mEdit->lines()->getString(line);
QString s = mEdit->document()->getString(line);
int len = s.length();
aChar+=len+1;
if (aChar>=1) {
@ -64,7 +64,7 @@ void ContentsCoord::normalize()
}
} else {
while (true) {
QString s =mEdit->lines()->getString(line);
QString s =mEdit->document()->getString(line);
int len = s.length();
if (aChar<=len+1) {
break;
@ -101,7 +101,7 @@ bool ContentsCoord::atStart()
bool ContentsCoord::atEnd()
{
Q_ASSERT(mEdit!=nullptr);
return mLine>mEdit->lines()->count();
return mLine>mEdit->document()->count();
}
const SynEdit *ContentsCoord::edit() const
@ -164,13 +164,13 @@ size_t ContentsCoord::operator-(const ContentsCoord& coord) const
if (mLine == coord.mLine) {
return mChar - coord.mChar;
} else if (mLine > coord.mLine) {
size_t result = mEdit->lines()->getString(coord.mLine-1).length()+1-coord.mChar;
size_t result = mEdit->document()->getString(coord.mLine-1).length()+1-coord.mChar;
int line = coord.mLine+1;
while (line<=mLine-1) {
result += mEdit->lines()->getString(line-1).length()+1;
result += mEdit->document()->getString(line-1).length()+1;
line++;
}
if (mLine<=mEdit->lines()->count()) {
if (mLine<=mEdit->document()->count()) {
result += mChar;
}
return result;
@ -216,10 +216,10 @@ QChar ContentsCoord::operator*() const
if (mLine < 1) {
return QChar('\0');
}
if (mLine > mEdit->lines()->count()) {
if (mLine > mEdit->document()->count()) {
return QChar('\0');
}
QString s = mEdit->lines()->getString(mLine-1);
QString s = mEdit->document()->getString(mLine-1);
if (mChar >= s.length()+1 ) {
return QChar('\n');
}

View File

@ -53,12 +53,12 @@ void SynExporter::CopyToClipboard()
CopyToClipboardFormat(clipboardFormat());
}
void SynExporter::ExportAll(PSynEditStringList ALines)
void SynExporter::ExportAll(PSynDocument ALines)
{
ExportRange(ALines, BufferCoord{1, 1}, BufferCoord{INT_MAX, INT_MAX});
}
void SynExporter::ExportRange(PSynEditStringList ALines, BufferCoord Start, BufferCoord Stop)
void SynExporter::ExportRange(PSynDocument ALines, BufferCoord Start, BufferCoord Stop)
{
// abort if not all necessary conditions are met
if (!ALines || !mHighlighter || (ALines->count() == 0))

View File

@ -44,7 +44,7 @@ public:
* @brief Exports everything in the strings parameter to the output buffer.
* @param ALines
*/
void ExportAll(PSynEditStringList ALines);
void ExportAll(PSynDocument ALines);
/**
* @brief Exports the given range of the strings parameter to the output buffer.
@ -52,7 +52,7 @@ public:
* @param Start
* @param Stop
*/
void ExportRange(PSynEditStringList ALines,
void ExportRange(PSynDocument ALines,
BufferCoord Start, BufferCoord Stop);
/**
* @brief Saves the contents of the output buffer to a file.

View File

@ -63,7 +63,7 @@ EditorColorSchemeWidget::EditorColorSchemeWidget(const QString& name, const QStr
this, &EditorColorSchemeWidget::onItemSelectionChanged);
connect(this, &SettingsWidget::settingsChanged,this,
&EditorColorSchemeWidget::onSettingChanged);
ui->editDemo->lines()->setText(
ui->editDemo->document()->setText(
"#include <iostream>\n"
"#include <conio.h>\n"
"\n"

View File

@ -46,12 +46,12 @@ EditorSnippetWidget::EditorSnippetWidget(const QString& name, const QString& gro
QModelIndex index = ui->tblSnippets->currentIndex();
if (!index.isValid()) {
ui->editCode->setEnabled(false);
ui->editCode->lines()->clear();
ui->editCode->document()->clear();
} else {
mUpdatingCode = true;
ui->editCode->setEnabled(true);
PCodeSnippet snippet = mModel.snippets()[index.row()];
ui->editCode->lines()->setText(snippet->code);
ui->editCode->document()->setText(snippet->code);
mUpdatingCode = false;
}
});
@ -67,7 +67,7 @@ EditorSnippetWidget::~EditorSnippetWidget()
void EditorSnippetWidget::doLoad()
{
mModel.updateSnippets(pMainWindow->codeSnippetManager()->snippets());
ui->editFileTemplate->lines()->setText(pMainWindow->codeSnippetManager()->newFileTemplate());
ui->editFileTemplate->document()->setText(pMainWindow->codeSnippetManager()->newFileTemplate());
}
void EditorSnippetWidget::doSave()

View File

@ -325,7 +325,7 @@ void FormatterGeneralWidget::updateDemo()
pSettings->dirs().appDir(),
formatter.getArguments(),
content);
ui->editDemo->lines()->setText(newContent);
ui->editDemo->document()->setText(newContent);
}
void FormatterGeneralWidget::updateCodeFormatter(Settings::CodeFormatter &format)

View File

@ -114,13 +114,13 @@ void CPUDialog::setDisassembly(const QString& file, const QString& funcName,cons
{
ui->txtFunctionName->setText(QString("%1:%2").arg(file, funcName));
int activeLine = -1;
ui->txtCode->lines()->clear();
ui->txtCode->document()->clear();
for (int i=0;i<lines.size();i++) {
QString line = lines[i];
if (line.startsWith("=>")) {
activeLine = i;
}
ui->txtCode->lines()->add(line);
ui->txtCode->document()->add(line);
}
if (activeLine!=-1)
ui->txtCode->setCaretXYEx(true,BufferCoord{1,activeLine+1});

View File

@ -44,14 +44,14 @@ void FilePropertiesDialog::calcFile(Editor *editor,
int &codeLines,
int &includeLines)
{
totalLines = editor->lines()->count();
totalLines = editor->document()->count();
codeLines = 0;
commentLines = 0;
emptyLines = 0;
includeLines = 0;
// iterate through all lines of file
for (int i=0;i<editor->lines()->count();i++) {
QString line = editor->lines()->getString(i);
for (int i=0;i<editor->document()->count();i++) {
QString line = editor->document()->getString(i);
int j=0;
while (j<line.length() && (line[j]=='\t' || line[j]==' '))
j++;
@ -132,7 +132,7 @@ void FilePropertiesDialog::on_cbFiles_currentIndexChanged(int index)
ui->txtProject->setText("-");
ui->txtPath->setText(editor->filename());
ui->txtRelativeToProject->setText("_");
ui->txtLines->setText(QString("%1").arg(editor->lines()->count()));
ui->txtLines->setText(QString("%1").arg(editor->document()->count()));
int totalLines, codeLines,emptyLines,commentLines,includeLines;
calcFile(editor,totalLines,commentLines,emptyLines,codeLines,includeLines);

View File

@ -380,7 +380,7 @@ void SearchDialog::on_btnExecute_clicked()
} else if (fileExists(curFilename)) {
SynEdit editor;
QByteArray realEncoding;
editor.lines()->loadFromFile(curFilename,ENCODING_AUTO_DETECT, realEncoding);
editor.document()->loadFromFile(curFilename,ENCODING_AUTO_DETECT, realEncoding);
fileSearched++;
PSearchResultTreeItem parentItem = batchFindInEditor(
&editor,
@ -449,7 +449,7 @@ std::shared_ptr<SearchResultTreeItem> SearchDialog::batchFindInEditor(SynEdit *e
item->start = ch;
item->len = wordLen;
item->parent = parentItem.get();
item->text = e->lines()->getString(Line-1);
item->text = e->document()->getString(Line-1);
item->text.replace('\t',' ');
parentItem->results.append(item);
return SynSearchAction::Skip;