2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-05-14 23:56:43 +08:00
|
|
|
#include "SynEdit.h"
|
2021-11-13 09:05:02 +08:00
|
|
|
#include "highlighter/cpp.h"
|
2021-05-14 23:56:43 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QPaintEvent>
|
2021-05-24 00:41:00 +08:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QTimerEvent>
|
2021-05-14 23:56:43 +08:00
|
|
|
#include "highlighter/base.h"
|
|
|
|
#include "Constants.h"
|
2021-05-24 00:41:00 +08:00
|
|
|
#include "TextPainter.h"
|
2021-06-05 23:43:45 +08:00
|
|
|
#include <QClipboard>
|
2021-05-24 00:41:00 +08:00
|
|
|
#include <QDebug>
|
2021-05-29 21:35:46 +08:00
|
|
|
#include <QGuiApplication>
|
2021-06-05 23:43:45 +08:00
|
|
|
#include <QInputMethodEvent>
|
2021-05-24 18:11:07 +08:00
|
|
|
#include <QPaintEvent>
|
2021-08-27 23:51:42 +08:00
|
|
|
#include <QResizeEvent>
|
2021-05-29 21:35:46 +08:00
|
|
|
#include <QStyleHints>
|
2021-09-26 22:39:26 +08:00
|
|
|
#include <QMessageBox>
|
2021-11-13 12:33:33 +08:00
|
|
|
#include <QDrag>
|
|
|
|
#include <QMimeData>
|
2021-12-16 19:14:14 +08:00
|
|
|
#include <QDesktopWidget>
|
2022-06-16 09:11:45 +08:00
|
|
|
#include <QTextEdit>
|
|
|
|
#include <QMimeData>
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2022-03-12 18:03:52 +08:00
|
|
|
SynEdit::SynEdit(QWidget *parent) : QAbstractScrollArea(parent),
|
|
|
|
mDropped(false)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-04-25 10:47:19 +08:00
|
|
|
mCharWidth=1;
|
2022-04-25 11:06:12 +08:00
|
|
|
mTextHeight = 1;
|
2021-10-09 09:09:05 +08:00
|
|
|
mLastKey = 0;
|
|
|
|
mLastKeyModifiers = Qt::NoModifier;
|
2021-09-18 11:08:30 +08:00
|
|
|
mModified = false;
|
2021-05-14 23:56:43 +08:00
|
|
|
mPaintLock = 0;
|
|
|
|
mPainterLock = 0;
|
|
|
|
mPainting = false;
|
|
|
|
#ifdef Q_OS_WIN
|
2021-05-24 18:11:07 +08:00
|
|
|
mFontDummy = QFont("Consolas",12);
|
2021-12-24 23:18:20 +08:00
|
|
|
#elif defined(Q_OS_LINUX)
|
2021-05-14 23:56:43 +08:00
|
|
|
mFontDummy = QFont("terminal",14);
|
2022-05-30 17:10:10 +08:00
|
|
|
#elif defined(Q_OS_MACOS)
|
|
|
|
mFontDummy = QFont("Menlo", 14);
|
2021-05-14 23:56:43 +08:00
|
|
|
#else
|
|
|
|
#error "Not supported!"
|
|
|
|
#endif
|
2022-04-25 11:06:12 +08:00
|
|
|
mFontDummy.setStyleStrategy(QFont::PreferAntialias);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument = std::make_shared<SynDocument>(mFontDummy, this);
|
|
|
|
//fPlugins := TList.Create;
|
|
|
|
mMouseMoved = false;
|
|
|
|
mUndoing = false;
|
|
|
|
mDocument->connect(mDocument.get(), &SynDocument::changed, this, &SynEdit::onLinesChanged);
|
|
|
|
mDocument->connect(mDocument.get(), &SynDocument::changing, this, &SynEdit::onLinesChanging);
|
|
|
|
mDocument->connect(mDocument.get(), &SynDocument::cleared, this, &SynEdit::onLinesCleared);
|
|
|
|
mDocument->connect(mDocument.get(), &SynDocument::deleted, this, &SynEdit::onLinesDeleted);
|
|
|
|
mDocument->connect(mDocument.get(), &SynDocument::inserted, this, &SynEdit::onLinesInserted);
|
|
|
|
mDocument->connect(mDocument.get(), &SynDocument::putted, this, &SynEdit::onLinesPutted);
|
|
|
|
|
2022-04-25 10:47:19 +08:00
|
|
|
mGutterWidth = 0;
|
|
|
|
mScrollBars = SynScrollStyle::ssBoth;
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2022-04-25 11:06:12 +08:00
|
|
|
|
2022-01-26 14:31:26 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
mUndoList = std::make_shared<SynEditUndoList>();
|
2021-09-02 12:14:02 +08:00
|
|
|
mUndoList->connect(mUndoList.get(), &SynEditUndoList::addedUndo, this, &SynEdit::onUndoAdded);
|
2021-05-14 23:56:43 +08:00
|
|
|
mRedoList = std::make_shared<SynEditUndoList>();
|
2021-09-02 12:14:02 +08:00
|
|
|
mRedoList->connect(mRedoList.get(), &SynEditUndoList::addedUndo, this, &SynEdit::onRedoAdded);
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-11-16 00:03:43 +08:00
|
|
|
mForegroundColor=palette().color(QPalette::Text);
|
|
|
|
mBackgroundColor=palette().color(QPalette::Base);
|
2022-01-04 16:50:54 +08:00
|
|
|
mCaretColor = Qt::red;
|
2021-09-15 14:35:37 +08:00
|
|
|
mCaretUseTextColor = false;
|
2022-01-04 16:50:54 +08:00
|
|
|
mActiveLineColor = Qt::blue;
|
2021-06-10 09:34:59 +08:00
|
|
|
mSelectedBackground = palette().color(QPalette::Highlight);
|
|
|
|
mSelectedForeground = palette().color(QPalette::HighlightedText);
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
mBookMarkOpt.connect(&mBookMarkOpt, &SynBookMarkOpt::changed, this, &SynEdit::onBookMarkOptionsChanged);
|
2021-05-14 23:56:43 +08:00
|
|
|
// fRightEdge has to be set before FontChanged is called for the first time
|
|
|
|
mRightEdge = 80;
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2022-03-04 19:04:45 +08:00
|
|
|
mMouseWheelScrollSpeed = 3;
|
|
|
|
mMouseSelectionScrollSpeed = 1;
|
2021-10-20 12:27:44 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
mGutter.setRightOffset(21);
|
2021-09-02 12:14:02 +08:00
|
|
|
mGutter.connect(&mGutter, &SynGutter::changed, this, &SynEdit::onGutterChanged);
|
2021-06-09 17:12:23 +08:00
|
|
|
mGutterWidth = mGutter.realGutterWidth(charWidth());
|
2021-05-14 23:56:43 +08:00
|
|
|
//ControlStyle := ControlStyle + [csOpaque, csSetCaption, csNeedsBorderPaint];
|
|
|
|
//Height := 150;
|
|
|
|
//Width := 200;
|
|
|
|
this->setCursor(Qt::CursorShape::IBeamCursor);
|
|
|
|
//TabStop := True;
|
|
|
|
mInserting = true;
|
2021-05-24 00:41:00 +08:00
|
|
|
mExtraLineSpacing = 0;
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
this->setFrameShape(QFrame::Panel);
|
|
|
|
this->setFrameShadow(QFrame::Sunken);
|
|
|
|
this->setLineWidth(1);
|
|
|
|
mInsertCaret = SynEditCaretType::ctVerticalLine;
|
|
|
|
mOverwriteCaret = SynEditCaretType::ctBlock;
|
2022-07-04 10:01:40 +08:00
|
|
|
mSelectionMode = SynSelectionMode::Normal;
|
|
|
|
mActiveSelectionMode = SynSelectionMode::Normal;
|
2021-06-03 23:18:51 +08:00
|
|
|
mReadOnly = false;
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
//stop qt to auto fill background
|
|
|
|
setAutoFillBackground(false);
|
|
|
|
//fFocusList := TList.Create;
|
|
|
|
//fKbdHandler := TSynEditKbdHandler.Create;
|
|
|
|
//fMarkList.OnChange := MarkListChange;
|
|
|
|
setDefaultKeystrokes();
|
2022-01-04 16:50:54 +08:00
|
|
|
mRightEdgeColor = Qt::lightGray;
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
mWantReturns = true;
|
|
|
|
mWantTabs = false;
|
|
|
|
mLeftChar = 1;
|
|
|
|
mTopLine = 1;
|
|
|
|
mCaretX = 1;
|
2021-06-03 23:18:51 +08:00
|
|
|
mLastCaretColumn = 1;
|
2021-05-14 23:56:43 +08:00
|
|
|
mCaretY = 1;
|
2022-07-04 10:01:40 +08:00
|
|
|
mBlockBegin.ch = 1;
|
|
|
|
mBlockBegin.line = 1;
|
2021-05-14 23:56:43 +08:00
|
|
|
mBlockEnd = mBlockBegin;
|
2021-10-29 15:15:43 +08:00
|
|
|
mOptions = eoAutoIndent
|
2021-06-05 23:43:45 +08:00
|
|
|
| eoDragDropEditing | eoEnhanceEndKey | eoTabIndent |
|
2021-11-19 07:52:36 +08:00
|
|
|
eoGroupUndo | eoKeepCaretX | eoSelectWordByDblClick
|
|
|
|
| eoHideShowScrollbars ;
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
mScrollTimer = new QTimer(this);
|
2021-11-26 19:04:04 +08:00
|
|
|
//mScrollTimer->setInterval(100);
|
2021-09-02 12:14:02 +08:00
|
|
|
connect(mScrollTimer, &QTimer::timeout,this, &SynEdit::onScrollTimeout);
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2022-01-04 16:50:54 +08:00
|
|
|
mScrollHintColor = Qt::yellow;
|
2021-05-14 23:56:43 +08:00
|
|
|
mScrollHintFormat = SynScrollHintFormat::shfTopLineOnly;
|
|
|
|
|
2022-04-02 18:22:29 +08:00
|
|
|
qreal dpr=devicePixelRatioF();
|
|
|
|
mContentImage = std::make_shared<QImage>(clientWidth()*dpr,clientHeight()*dpr,QImage::Format_ARGB32);
|
|
|
|
mContentImage->setDevicePixelRatio(dpr);
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-05-27 01:05:49 +08:00
|
|
|
mUseCodeFolding = true;
|
2021-05-24 00:41:00 +08:00
|
|
|
m_blinkTimerId = 0;
|
2021-05-24 18:11:07 +08:00
|
|
|
m_blinkStatus = 0;
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-10-20 12:11:36 +08:00
|
|
|
hideCaret();
|
2021-05-24 21:48:03 +08:00
|
|
|
|
|
|
|
connect(horizontalScrollBar(),&QScrollBar::valueChanged,
|
2021-09-02 12:14:02 +08:00
|
|
|
this, &SynEdit::onScrolled);
|
2021-05-24 21:48:03 +08:00
|
|
|
connect(verticalScrollBar(),&QScrollBar::valueChanged,
|
2021-09-02 12:14:02 +08:00
|
|
|
this, &SynEdit::onScrolled);
|
2021-06-05 23:43:45 +08:00
|
|
|
//enable input method
|
|
|
|
setAttribute(Qt::WA_InputMethodEnabled);
|
2021-06-07 11:02:03 +08:00
|
|
|
|
2021-07-03 13:57:22 +08:00
|
|
|
//setMouseTracking(true);
|
2021-11-13 12:33:33 +08:00
|
|
|
setAcceptDrops(true);
|
2021-12-16 19:14:14 +08:00
|
|
|
|
2022-04-25 11:06:12 +08:00
|
|
|
setFont(mFontDummy);
|
|
|
|
setFontForNonAscii(mFontDummy);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::displayLineCount() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument->empty()) {
|
2021-05-24 00:41:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
return lineToRow(mDocument->count());
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
DisplayCoord SynEdit::displayXY() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return bufferToDisplayPos(caretXY());
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::displayX() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return displayXY().Column;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::displayY() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return displayXY().Row;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
BufferCoord SynEdit::caretXY() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
BufferCoord result;
|
2022-07-04 10:01:40 +08:00
|
|
|
result.ch = caretX();
|
|
|
|
result.line = caretY();
|
2021-05-14 23:56:43 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::caretX() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return mCaretX;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::caretY() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return mCaretY;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setCaretX(int value)
|
|
|
|
{
|
|
|
|
setCaretXY({value,mCaretY});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setCaretY(int value)
|
|
|
|
{
|
|
|
|
setCaretXY({mCaretX,value});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setCaretXY(const BufferCoord &value)
|
|
|
|
{
|
2021-12-23 22:05:57 +08:00
|
|
|
setBlockBegin(value);
|
|
|
|
setBlockEnd(value);
|
2021-12-16 10:46:38 +08:00
|
|
|
setCaretXYEx(true,value);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:46:38 +08:00
|
|
|
void SynEdit::setCaretXYEx(bool CallEnsureCursorPosVisible, BufferCoord value)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
bool vTriggerPaint=true; //how to test it?
|
|
|
|
|
|
|
|
if (vTriggerPaint)
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2021-06-08 21:41:42 +08:00
|
|
|
int nMaxX;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (value.line > mDocument->count())
|
|
|
|
value.line = mDocument->count();
|
|
|
|
if (mActiveSelectionMode!=SynSelectionMode::Column) {
|
|
|
|
if (value.line < 1) {
|
2022-03-25 10:43:07 +08:00
|
|
|
// this is just to make sure if Lines stringlist should be empty
|
2022-07-04 10:01:40 +08:00
|
|
|
value.line = 1;
|
2022-03-25 10:43:07 +08:00
|
|
|
if (!mOptions.testFlag(SynEditorOption::eoScrollPastEol)) {
|
|
|
|
nMaxX = 1;
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
nMaxX = getDisplayStringAtLine(value.line).length()+1;
|
2022-03-25 10:43:07 +08:00
|
|
|
}
|
2021-11-03 23:51:47 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
nMaxX = getDisplayStringAtLine(value.line).length()+1;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
value.ch = std::min(value.ch,nMaxX);
|
2021-06-08 21:41:42 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
value.ch = std::max(value.ch,1);
|
2021-06-08 21:41:42 +08:00
|
|
|
// if ((value.Char > nMaxX) && (! (mOptions.testFlag(SynEditorOption::eoScrollPastEol)) ) )
|
|
|
|
// value.Char = nMaxX;
|
|
|
|
// if (value.Char < 1)
|
|
|
|
// value.Char = 1;
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((value.ch != mCaretX) || (value.line != mCaretY)) {
|
2021-05-14 23:56:43 +08:00
|
|
|
incPaintLock();
|
|
|
|
auto action = finally([this]{
|
|
|
|
decPaintLock();
|
|
|
|
});
|
|
|
|
// simply include the flags, fPaintLock is > 0
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mCaretX != value.ch) {
|
|
|
|
mCaretX = value.ch;
|
2021-05-14 23:56:43 +08:00
|
|
|
mStatusChanges.setFlag(SynStatusChange::scCaretX);
|
2021-05-27 21:39:18 +08:00
|
|
|
invalidateLine(mCaretY);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mCaretY != value.line) {
|
2021-05-27 20:33:25 +08:00
|
|
|
int oldCaretY = mCaretY;
|
2022-07-04 10:01:40 +08:00
|
|
|
mCaretY = value.line;
|
2022-01-24 16:20:16 +08:00
|
|
|
invalidateLine(mCaretY);
|
2022-03-20 20:56:21 +08:00
|
|
|
invalidateGutterLine(mCaretY);
|
2022-01-24 16:20:16 +08:00
|
|
|
invalidateLine(oldCaretY);
|
2022-03-20 20:56:21 +08:00
|
|
|
invalidateGutterLine(oldCaretY);
|
2021-05-14 23:56:43 +08:00
|
|
|
mStatusChanges.setFlag(SynStatusChange::scCaretY);
|
|
|
|
}
|
|
|
|
// Call UpdateLastCaretX before DecPaintLock because the event handler it
|
|
|
|
// calls could raise an exception, and we don't want fLastCaretX to be
|
|
|
|
// left in an undefined state if that happens.
|
|
|
|
updateLastCaretX();
|
2021-12-16 10:46:38 +08:00
|
|
|
if (CallEnsureCursorPosVisible)
|
2021-05-14 23:56:43 +08:00
|
|
|
ensureCursorPosVisible();
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfCaretChanged);
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfScrollbarChanged);
|
|
|
|
} else {
|
|
|
|
// Also call UpdateLastCaretX if the caret didn't move. Apps don't know
|
|
|
|
// anything about fLastCaretX and they shouldn't need to. So, to avoid any
|
|
|
|
// unwanted surprises, always update fLastCaretX whenever CaretXY is
|
|
|
|
// assigned to.
|
|
|
|
// Note to SynEdit developers: If this is undesirable in some obscure
|
|
|
|
// case, just save the value of fLastCaretX before assigning to CaretXY and
|
|
|
|
// restore it afterward as appropriate.
|
|
|
|
updateLastCaretX();
|
|
|
|
}
|
|
|
|
if (vTriggerPaint)
|
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:46:38 +08:00
|
|
|
void SynEdit::setCaretXYCentered(const BufferCoord &value)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
incPaintLock();
|
|
|
|
auto action = finally([this] {
|
|
|
|
decPaintLock();
|
|
|
|
});
|
|
|
|
mStatusChanges.setFlag(SynStatusChange::scSelection);
|
2021-12-16 10:46:38 +08:00
|
|
|
setCaretXYEx(false,value);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (selAvail())
|
|
|
|
invalidateSelection();
|
2022-07-04 10:01:40 +08:00
|
|
|
mBlockBegin.ch = mCaretX;
|
|
|
|
mBlockBegin.line = mCaretY;
|
2021-05-14 23:56:43 +08:00
|
|
|
mBlockEnd = mBlockBegin;
|
2021-12-16 10:46:38 +08:00
|
|
|
ensureCursorPosVisibleEx(true); // but here after block has been set
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:05:19 +08:00
|
|
|
void SynEdit::uncollapseAroundLine(int line)
|
|
|
|
{
|
|
|
|
while (true) { // Open up the closed folds around the focused line until we can see the line we're looking for
|
|
|
|
PSynEditFoldRange fold = foldHidesLine(line);
|
|
|
|
if (fold)
|
|
|
|
uncollapse(fold);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynEditFoldRange SynEdit::foldHidesLine(int line)
|
|
|
|
{
|
|
|
|
return foldAroundLineEx(line, true, false, true);
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
void SynEdit::setInsertMode(bool value)
|
|
|
|
{
|
|
|
|
if (mInserting != value) {
|
|
|
|
mInserting = value;
|
|
|
|
updateCaret();
|
2021-08-29 10:14:07 +08:00
|
|
|
emit statusChanged(scInsertMode);
|
2021-06-12 22:36:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SynEdit::insertMode() const
|
|
|
|
{
|
|
|
|
return mInserting;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SynEdit::canUndo() const
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
return !mReadOnly && mUndoList->canUndo();
|
2021-06-12 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SynEdit::canRedo() const
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
return !mReadOnly && mRedoList->canUndo();
|
2021-06-12 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::maxScrollWidth() const
|
|
|
|
{
|
2022-06-28 21:46:20 +08:00
|
|
|
int maxLen = mDocument->lengthOfLongestLine();
|
|
|
|
if (highlighter())
|
|
|
|
maxLen = maxLen+stringColumns(highlighter()->foldString(),maxLen);
|
2021-06-08 21:41:42 +08:00
|
|
|
if (mOptions.testFlag(eoScrollPastEol))
|
2022-06-28 21:46:20 +08:00
|
|
|
return std::max(maxLen ,1);
|
2021-06-08 21:41:42 +08:00
|
|
|
else
|
2022-06-28 21:46:20 +08:00
|
|
|
return std::max(maxLen-mCharsInWindow+1, 1);
|
2021-06-08 21:41:42 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::getHighlighterAttriAtRowCol(const BufferCoord &XY, QString &Token, PSynHighlighterAttribute &Attri)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
SynHighlighterTokenType TmpType;
|
|
|
|
int TmpKind, TmpStart;
|
2021-09-20 15:57:48 +08:00
|
|
|
return getHighlighterAttriAtRowColEx(XY, Token, TmpType, TmpKind,TmpStart, Attri);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::getHighlighterAttriAtRowCol(const BufferCoord &XY, QString &Token, bool &tokenFinished, SynHighlighterTokenType &TokenType, PSynHighlighterAttribute &Attri)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
int PosX, PosY, endPos, Start;
|
|
|
|
QString Line;
|
2022-07-04 10:01:40 +08:00
|
|
|
PosY = XY.line - 1;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mHighlighter && (PosY >= 0) && (PosY < mDocument->count())) {
|
|
|
|
Line = mDocument->getString(PosY);
|
2021-06-03 20:26:36 +08:00
|
|
|
if (PosY == 0) {
|
|
|
|
mHighlighter->resetState();
|
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setState(mDocument->ranges(PosY-1));
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
mHighlighter->setLine(Line, PosY);
|
2022-07-04 10:01:40 +08:00
|
|
|
PosX = XY.ch;
|
2021-06-03 20:26:36 +08:00
|
|
|
if ((PosX > 0) && (PosX <= Line.length())) {
|
|
|
|
while (!mHighlighter->eol()) {
|
|
|
|
Start = mHighlighter->getTokenPos() + 1;
|
|
|
|
Token = mHighlighter->getToken();
|
|
|
|
endPos = Start + Token.length()-1;
|
|
|
|
if ((PosX >= Start) && (PosX <= endPos)) {
|
|
|
|
Attri = mHighlighter->getTokenAttribute();
|
|
|
|
if (PosX == endPos)
|
|
|
|
tokenFinished = mHighlighter->getTokenFinished();
|
|
|
|
else
|
|
|
|
tokenFinished = false;
|
|
|
|
TokenType = mHighlighter->getTokenType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
mHighlighter->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Token = "";
|
|
|
|
Attri = PSynHighlighterAttribute();
|
|
|
|
tokenFinished = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::getHighlighterAttriAtRowColEx(const BufferCoord &XY, QString &Token, SynHighlighterTokenType &TokenType, SynTokenKind &TokenKind, int &Start, PSynHighlighterAttribute &Attri)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
int PosX, PosY, endPos;
|
|
|
|
QString Line;
|
2022-07-04 10:01:40 +08:00
|
|
|
PosY = XY.line - 1;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mHighlighter && (PosY >= 0) && (PosY < mDocument->count())) {
|
|
|
|
Line = mDocument->getString(PosY);
|
2021-06-03 20:26:36 +08:00
|
|
|
if (PosY == 0) {
|
|
|
|
mHighlighter->resetState();
|
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setState(mDocument->ranges(PosY-1));
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
mHighlighter->setLine(Line, PosY);
|
2022-07-04 10:01:40 +08:00
|
|
|
PosX = XY.ch;
|
2021-06-03 20:26:36 +08:00
|
|
|
if ((PosX > 0) && (PosX <= Line.length())) {
|
|
|
|
while (!mHighlighter->eol()) {
|
|
|
|
Start = mHighlighter->getTokenPos() + 1;
|
|
|
|
Token = mHighlighter->getToken();
|
|
|
|
endPos = Start + Token.length()-1;
|
|
|
|
if ((PosX >= Start) && (PosX <= endPos)) {
|
|
|
|
Attri = mHighlighter->getTokenAttribute();
|
|
|
|
TokenKind = mHighlighter->getTokenKind();
|
|
|
|
TokenType = mHighlighter->getTokenType();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
mHighlighter->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Token = "";
|
|
|
|
Attri = PSynHighlighterAttribute();
|
|
|
|
TokenKind = 0;
|
|
|
|
TokenType = SynHighlighterTokenType::Default;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-07 20:51:56 +08:00
|
|
|
void SynEdit::beginUndoBlock()
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-03-07 20:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::endUndoBlock()
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2022-03-07 20:51:56 +08:00
|
|
|
}
|
|
|
|
|
2022-06-12 22:54:34 +08:00
|
|
|
void SynEdit::addCaretToUndo()
|
|
|
|
{
|
|
|
|
BufferCoord p=caretXY();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Caret,p,p,QStringList(), mActiveSelectionMode);
|
2022-06-12 22:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::addLeftTopToUndo()
|
|
|
|
{
|
|
|
|
BufferCoord p;
|
2022-07-04 10:01:40 +08:00
|
|
|
p.ch = leftChar();
|
|
|
|
p.line = topLine();
|
|
|
|
mUndoList->addChange(SynChangeReason::LeftTop,p,p,QStringList(), mActiveSelectionMode);
|
2022-07-02 14:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::addSelectionToUndo()
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Selection,mBlockBegin,
|
2022-07-02 14:06:10 +08:00
|
|
|
mBlockEnd,QStringList(),mActiveSelectionMode);
|
2022-06-12 22:54:34 +08:00
|
|
|
}
|
|
|
|
|
2021-06-22 23:00:34 +08:00
|
|
|
void SynEdit::beginUpdate()
|
|
|
|
{
|
|
|
|
incPaintLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::endUpdate()
|
|
|
|
{
|
|
|
|
decPaintLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferCoord SynEdit::getMatchingBracket()
|
|
|
|
{
|
|
|
|
return getMatchingBracketEx(caretXY());
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferCoord SynEdit::getMatchingBracketEx(BufferCoord APoint)
|
|
|
|
{
|
|
|
|
QChar Brackets[] = {'(', ')', '[', ']', '{', '}', '<', '>'};
|
|
|
|
QString Line;
|
|
|
|
int i, PosX, PosY, Len;
|
|
|
|
QChar Test, BracketInc, BracketDec;
|
|
|
|
int NumBrackets;
|
|
|
|
QString vDummy;
|
|
|
|
PSynHighlighterAttribute attr;
|
|
|
|
BufferCoord p;
|
|
|
|
bool isCommentOrStringOrChar;
|
|
|
|
int nBrackets = sizeof(Brackets) / sizeof(QChar);
|
|
|
|
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument->count()<1)
|
2021-06-22 23:00:34 +08:00
|
|
|
return BufferCoord{0,0};
|
|
|
|
// get char at caret
|
2022-07-04 10:01:40 +08:00
|
|
|
PosX = std::max(APoint.ch,1);
|
|
|
|
PosY = std::max(APoint.line,1);
|
|
|
|
Line = mDocument->getString(APoint.line - 1);
|
2021-06-22 23:00:34 +08:00
|
|
|
if (Line.length() >= PosX ) {
|
|
|
|
Test = Line[PosX-1];
|
|
|
|
// is it one of the recognized brackets?
|
|
|
|
for (i = 0; i<nBrackets; i++) {
|
|
|
|
if (Test == Brackets[i]) {
|
|
|
|
// this is the bracket, get the matching one and the direction
|
|
|
|
BracketInc = Brackets[i];
|
2021-08-16 23:17:48 +08:00
|
|
|
BracketDec = Brackets[i ^ 1]; // 0 -> 1, 1 -> 0, ...
|
2021-06-22 23:00:34 +08:00
|
|
|
// search for the matching bracket (that is until NumBrackets = 0)
|
|
|
|
NumBrackets = 1;
|
|
|
|
if (i%2==1) {
|
2021-09-26 22:39:26 +08:00
|
|
|
while (true) {
|
2021-06-22 23:00:34 +08:00
|
|
|
// search until start of line
|
|
|
|
while (PosX > 1) {
|
|
|
|
PosX--;
|
|
|
|
Test = Line[PosX-1];
|
2022-07-04 10:01:40 +08:00
|
|
|
p.ch = PosX;
|
|
|
|
p.line = PosY;
|
2021-06-22 23:00:34 +08:00
|
|
|
if ((Test == BracketInc) || (Test == BracketDec)) {
|
2022-01-23 21:04:08 +08:00
|
|
|
isCommentOrStringOrChar = false;
|
2021-09-20 15:57:48 +08:00
|
|
|
if (getHighlighterAttriAtRowCol(p, vDummy, attr))
|
2021-06-22 23:00:34 +08:00
|
|
|
isCommentOrStringOrChar =
|
|
|
|
(attr == mHighlighter->stringAttribute()) ||
|
|
|
|
(attr == mHighlighter->commentAttribute()) ||
|
|
|
|
(attr->name() == SYNS_AttrCharacter);
|
|
|
|
if ((Test == BracketInc) && (!isCommentOrStringOrChar))
|
|
|
|
NumBrackets++;
|
|
|
|
else if ((Test == BracketDec) && (!isCommentOrStringOrChar)) {
|
|
|
|
NumBrackets--;
|
|
|
|
if (NumBrackets == 0) {
|
|
|
|
// matching bracket found, set caret and bail out
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// get previous line if possible
|
|
|
|
if (PosY == 1)
|
|
|
|
break;
|
|
|
|
PosY--;
|
2022-04-19 21:18:41 +08:00
|
|
|
Line = mDocument->getString(PosY - 1);
|
2021-06-22 23:00:34 +08:00
|
|
|
PosX = Line.length() + 1;
|
2021-09-26 22:39:26 +08:00
|
|
|
}
|
2021-06-22 23:00:34 +08:00
|
|
|
} else {
|
2021-09-26 22:39:26 +08:00
|
|
|
while (true) {
|
2021-06-22 23:00:34 +08:00
|
|
|
// search until end of line
|
|
|
|
Len = Line.length();
|
|
|
|
while (PosX < Len) {
|
|
|
|
PosX++;
|
|
|
|
Test = Line[PosX-1];
|
2022-07-04 10:01:40 +08:00
|
|
|
p.ch = PosX;
|
|
|
|
p.line = PosY;
|
2021-06-22 23:00:34 +08:00
|
|
|
if ((Test == BracketInc) || (Test == BracketDec)) {
|
2022-01-23 21:04:08 +08:00
|
|
|
isCommentOrStringOrChar = false;
|
2021-09-20 15:57:48 +08:00
|
|
|
if (getHighlighterAttriAtRowCol(p, vDummy, attr))
|
2021-06-22 23:00:34 +08:00
|
|
|
isCommentOrStringOrChar =
|
|
|
|
(attr == mHighlighter->stringAttribute()) ||
|
|
|
|
(attr == mHighlighter->commentAttribute()) ||
|
|
|
|
(attr->name() == SYNS_AttrCharacter);
|
|
|
|
else
|
|
|
|
isCommentOrStringOrChar = false;
|
|
|
|
if ((Test == BracketInc) && (!isCommentOrStringOrChar))
|
|
|
|
NumBrackets++;
|
|
|
|
else if ((Test == BracketDec) && (!isCommentOrStringOrChar)) {
|
|
|
|
NumBrackets--;
|
|
|
|
if (NumBrackets == 0) {
|
|
|
|
// matching bracket found, set caret and bail out
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// get next line if possible
|
2022-04-19 21:18:41 +08:00
|
|
|
if (PosY == mDocument->count())
|
2021-06-22 23:00:34 +08:00
|
|
|
break;
|
|
|
|
PosY++;
|
2022-04-19 21:18:41 +08:00
|
|
|
Line = mDocument->getString(PosY - 1);
|
2021-06-22 23:00:34 +08:00
|
|
|
PosX = 0;
|
2021-09-26 22:39:26 +08:00
|
|
|
}
|
2021-06-22 23:00:34 +08:00
|
|
|
}
|
|
|
|
// don't test the other brackets, we're done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BufferCoord{0,0};
|
|
|
|
}
|
|
|
|
|
2021-10-05 21:25:23 +08:00
|
|
|
QStringList SynEdit::contents()
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return document()->contents();
|
2021-10-05 21:25:23 +08:00
|
|
|
}
|
|
|
|
|
2021-10-07 07:52:20 +08:00
|
|
|
QString SynEdit::text()
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return document()->text();
|
2021-10-07 07:52:20 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::getPositionOfMouse(BufferCoord &aPos)
|
2021-08-29 17:23:40 +08:00
|
|
|
{
|
|
|
|
QPoint point = QCursor::pos();
|
|
|
|
point = mapFromGlobal(point);
|
2021-09-20 15:57:48 +08:00
|
|
|
return pointToCharLine(point,aPos);
|
2021-08-29 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::getLineOfMouse(int &line)
|
2021-08-29 17:23:40 +08:00
|
|
|
{
|
|
|
|
QPoint point = QCursor::pos();
|
|
|
|
point = mapFromGlobal(point);
|
2021-09-20 15:57:48 +08:00
|
|
|
return pointToLine(point,line);
|
2021-08-29 17:23:40 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::pointToCharLine(const QPoint &point, BufferCoord &coord)
|
2021-08-29 17:23:40 +08:00
|
|
|
{
|
|
|
|
// Make sure it fits within the SynEdit bounds (and on the gutter)
|
|
|
|
if ((point.x() < gutterWidth() + clientLeft())
|
|
|
|
|| (point.x()>clientWidth()+clientLeft())
|
|
|
|
|| (point.y() < clientTop())
|
|
|
|
|| (point.y() > clientTop()+clientHeight())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:36:46 +08:00
|
|
|
coord = displayToBufferPos(pixelsToNearestRowColumn(point.x(),point.y()));
|
2021-08-29 17:23:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::pointToLine(const QPoint &point, int &line)
|
2021-08-29 17:23:40 +08:00
|
|
|
{
|
|
|
|
// Make sure it fits within the SynEdit bounds
|
|
|
|
if ((point.x() < clientLeft())
|
|
|
|
|| (point.x()>clientWidth()+clientLeft())
|
|
|
|
|| (point.y() < clientTop())
|
|
|
|
|| (point.y() > clientTop()+clientHeight())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:36:46 +08:00
|
|
|
BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(point.x(),point.y()));
|
2022-07-04 10:01:40 +08:00
|
|
|
line = coord.line;
|
2021-08-29 17:23:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::invalidateGutter()
|
|
|
|
{
|
|
|
|
invalidateGutterLines(-1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::invalidateGutterLine(int aLine)
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if ((aLine < 1) || (aLine > mDocument->count()))
|
2021-05-14 23:56:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
invalidateGutterLines(aLine, aLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::invalidateGutterLines(int FirstLine, int LastLine)
|
|
|
|
{
|
|
|
|
QRect rcInval;
|
|
|
|
if (!isVisible())
|
|
|
|
return;
|
|
|
|
if (FirstLine == -1 && LastLine == -1) {
|
2021-05-21 23:33:53 +08:00
|
|
|
rcInval = QRect(0, 0, mGutterWidth, clientHeight());
|
2021-05-14 23:56:43 +08:00
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfLinesChanging))
|
|
|
|
mInvalidateRect = mInvalidateRect.united(rcInval);
|
|
|
|
else
|
|
|
|
invalidateRect(rcInval);
|
|
|
|
} else {
|
|
|
|
// find the visible lines first
|
|
|
|
if (LastLine < FirstLine)
|
|
|
|
std::swap(LastLine, FirstLine);
|
|
|
|
if (mUseCodeFolding) {
|
|
|
|
FirstLine = lineToRow(FirstLine);
|
2022-04-19 21:18:41 +08:00
|
|
|
if (LastLine <= mDocument->count())
|
2021-05-14 23:56:43 +08:00
|
|
|
LastLine = lineToRow(LastLine);
|
|
|
|
else
|
|
|
|
LastLine = INT_MAX;
|
|
|
|
}
|
|
|
|
FirstLine = std::max(FirstLine, mTopLine);
|
|
|
|
LastLine = std::min(LastLine, mTopLine + mLinesInWindow);
|
|
|
|
// any line visible?
|
|
|
|
if (LastLine >= FirstLine) {
|
2021-05-21 23:33:53 +08:00
|
|
|
rcInval = {0, mTextHeight * (FirstLine - mTopLine),
|
2021-05-14 23:56:43 +08:00
|
|
|
mGutterWidth, mTextHeight * (LastLine - mTopLine + 1)};
|
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfLinesChanging)) {
|
|
|
|
mInvalidateRect = mInvalidateRect.united(rcInval);
|
|
|
|
} else {
|
|
|
|
invalidateRect(rcInval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert point on the edit (x,y) to (row,column)
|
|
|
|
* @param aX
|
|
|
|
* @param aY
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2022-03-31 19:36:46 +08:00
|
|
|
DisplayCoord SynEdit::pixelsToNearestRowColumn(int aX, int aY) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return {
|
2022-03-07 21:53:52 +08:00
|
|
|
std::max(1, (int)(mLeftChar + round((aX - mGutterWidth - 2.0) / mCharWidth))),
|
2021-08-16 23:17:48 +08:00
|
|
|
std::max(1, mTopLine + (aY / mTextHeight))
|
2021-05-14 23:56:43 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:36:46 +08:00
|
|
|
DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY) const
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
std::max(1, (int)(mLeftChar + (aX - mGutterWidth - 2.0) / mCharWidth)),
|
|
|
|
std::max(1, mTopLine + (aY / mTextHeight))
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
QPoint SynEdit::rowColumnToPixels(const DisplayCoord &coord) const
|
2021-05-24 00:41:00 +08:00
|
|
|
{
|
|
|
|
QPoint result;
|
2021-05-24 21:48:03 +08:00
|
|
|
result.setX((coord.Column - 1) * mCharWidth + textOffset());
|
2021-05-24 00:41:00 +08:00
|
|
|
result.setY((coord.Row - mTopLine) * mTextHeight);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
/**
|
|
|
|
* @brief takes a position in the text and transforms it into
|
|
|
|
* the row and column it appears to be on the screen
|
|
|
|
* @param p
|
|
|
|
* @return
|
|
|
|
*/
|
2021-06-08 21:41:42 +08:00
|
|
|
DisplayCoord SynEdit::bufferToDisplayPos(const BufferCoord &p) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
DisplayCoord result {p.ch,p.line};
|
2021-05-18 15:49:58 +08:00
|
|
|
// Account for tabs and charColumns
|
2022-07-04 10:01:40 +08:00
|
|
|
if (p.line-1 <mDocument->count())
|
|
|
|
result.Column = charToColumn(p.line,p.ch);
|
2021-05-14 23:56:43 +08:00
|
|
|
// Account for code folding
|
|
|
|
if (mUseCodeFolding)
|
|
|
|
result.Row = foldLineToRow(result.Row);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief takes a position on screen and transfrom it into position of text
|
|
|
|
* @param p
|
|
|
|
* @return
|
|
|
|
*/
|
2021-06-08 21:41:42 +08:00
|
|
|
BufferCoord SynEdit::displayToBufferPos(const DisplayCoord &p) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
BufferCoord Result{p.Column,p.Row};
|
|
|
|
// Account for code folding
|
|
|
|
if (mUseCodeFolding)
|
2022-07-04 10:01:40 +08:00
|
|
|
Result.line = foldRowToLine(p.Row);
|
2021-05-14 23:56:43 +08:00
|
|
|
// Account for tabs
|
2022-07-04 10:01:40 +08:00
|
|
|
if (Result.line <= mDocument->count() ) {
|
|
|
|
Result.ch = columnToChar(Result.line,p.Column);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
//ContentsCoord SynEdit::fromBufferCoord(const BufferCoord &p) const
|
|
|
|
//{
|
|
|
|
// return createNormalizedBufferCoord(p.Char,p.Line);
|
|
|
|
//}
|
2021-09-20 15:57:48 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
//ContentsCoord SynEdit::createNormalizedBufferCoord(int aChar, int aLine) const
|
|
|
|
//{
|
|
|
|
// return ContentsCoord(this,aChar,aLine);
|
|
|
|
//}
|
2021-09-20 15:57:48 +08:00
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
//QStringList SynEdit::getContents(const ContentsCoord &pStart, const ContentsCoord &pEnd)
|
|
|
|
//{
|
|
|
|
// QStringList result;
|
|
|
|
// if (mDocument->count()==0)
|
|
|
|
// return result;
|
|
|
|
// if (pStart.line()>0) {
|
|
|
|
// QString s = mDocument->getString(pStart.line()-1);
|
|
|
|
// result += s.mid(pStart.ch()-1);
|
|
|
|
// }
|
|
|
|
// int endLine = std::min(pEnd.line(),mDocument->count());
|
|
|
|
// for (int i=pStart.line();i<endLine-1;i++) {
|
|
|
|
// result += mDocument->getString(i);
|
|
|
|
// }
|
|
|
|
// if (pEnd.line()<=mDocument->count()) {
|
|
|
|
// result += mDocument->getString(pEnd.line()-1).mid(0,pEnd.ch()-1);
|
|
|
|
// }
|
|
|
|
// return result;
|
|
|
|
//}
|
2021-09-20 23:33:53 +08:00
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
//QString SynEdit::getJoinedContents(const ContentsCoord &pStart, const ContentsCoord &pEnd, const QString &joinStr)
|
|
|
|
//{
|
|
|
|
// return getContents(pStart,pEnd).join(joinStr);
|
|
|
|
//}
|
2021-09-20 23:33:53 +08:00
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::leftSpaces(const QString &line) const
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
if (mOptions.testFlag(eoAutoIndent)) {
|
|
|
|
for (QChar ch:line) {
|
|
|
|
if (ch == '\t') {
|
2022-04-19 21:18:41 +08:00
|
|
|
result += tabWidth() - (result % tabWidth());
|
2021-06-04 21:27:51 +08:00
|
|
|
} else if (ch == ' ') {
|
2021-05-29 21:35:46 +08:00
|
|
|
result ++;
|
2021-06-04 21:27:51 +08:00
|
|
|
} else {
|
|
|
|
break;
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
2021-05-24 18:11:07 +08:00
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
return result;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
QString SynEdit::GetLeftSpacing(int charCount, bool wantTabs) const
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (wantTabs && !mOptions.testFlag(eoTabsToSpaces) && tabWidth()>0) {
|
|
|
|
return QString(charCount / tabWidth(),'\t') + QString(charCount % tabWidth(),' ');
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
2021-06-04 21:27:51 +08:00
|
|
|
return QString(charCount,' ');
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::charToColumn(int aLine, int aChar) const
|
2021-05-18 15:49:58 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (aLine>=1 && aLine <= mDocument->count()) {
|
2022-06-28 21:46:20 +08:00
|
|
|
QString s = getDisplayStringAtLine(aLine);
|
2021-06-03 20:26:36 +08:00
|
|
|
return charToColumn(s,aChar);
|
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
return aChar;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::charToColumn(const QString &s, int aChar) const
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
int len = std::min(aChar-1,s.length());
|
|
|
|
for (int i=0;i<len;i++) {
|
|
|
|
if (s[i] == '\t')
|
2022-04-19 21:18:41 +08:00
|
|
|
x+=tabWidth() - (x % tabWidth());
|
2021-06-03 20:26:36 +08:00
|
|
|
else
|
|
|
|
x+=charColumns(s[i]);
|
2021-05-18 15:49:58 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
return x+1;
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::columnToChar(int aLine, int aColumn) const
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
Q_ASSERT( (aLine <= mDocument->count()) && (aLine >= 1));
|
|
|
|
if (aLine <= mDocument->count()) {
|
2022-06-28 21:46:20 +08:00
|
|
|
QString s = getDisplayStringAtLine(aLine);
|
2021-05-29 21:35:46 +08:00
|
|
|
int x = 0;
|
|
|
|
int len = s.length();
|
|
|
|
int i;
|
|
|
|
for (i=0;i<len;i++) {
|
|
|
|
if (s[i] == '\t')
|
2022-04-19 21:18:41 +08:00
|
|
|
x+=tabWidth() - (x % tabWidth());
|
2021-05-29 21:35:46 +08:00
|
|
|
else
|
|
|
|
x+=charColumns(s[i]);
|
|
|
|
if (x>=aColumn) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 21:27:51 +08:00
|
|
|
return i+1;
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
return aColumn;
|
2021-05-18 15:49:58 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::stringColumns(const QString &line, int colsBefore) const
|
2021-05-18 15:49:58 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return mDocument->stringColumns(line,colsBefore);
|
2021-05-21 23:33:53 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::getLineIndent(const QString &line) const
|
2021-05-21 23:33:53 +08:00
|
|
|
{
|
|
|
|
int indents = 0;
|
|
|
|
for (QChar ch:line) {
|
2021-10-12 19:39:24 +08:00
|
|
|
switch(ch.unicode()) {
|
2021-05-21 23:33:53 +08:00
|
|
|
case '\t':
|
2022-04-19 21:18:41 +08:00
|
|
|
indents+=tabWidth();
|
2021-05-21 23:33:53 +08:00
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
indents+=1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return indents;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indents;
|
2021-05-18 15:49:58 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::rowToLine(int aRow) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-24 18:11:07 +08:00
|
|
|
if (mUseCodeFolding)
|
|
|
|
return foldRowToLine(aRow);
|
|
|
|
else
|
|
|
|
return aRow;
|
|
|
|
//return displayToBufferPos({1, aRow}).Line;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::lineToRow(int aLine) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
return bufferToDisplayPos({1, aLine}).Row;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::foldRowToLine(int Row) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
int result = Row;
|
|
|
|
for (int i=0;i<mAllFoldRanges.count();i++) {
|
|
|
|
PSynEditFoldRange range = mAllFoldRanges[i];
|
|
|
|
if (range->collapsed && !range->parentCollapsed() && range->fromLine < result) {
|
|
|
|
result += range->linesCollapsed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::foldLineToRow(int Line) const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
int result = Line;
|
|
|
|
for (int i=mAllFoldRanges.count()-1;i>=0;i--) {
|
|
|
|
PSynEditFoldRange range =mAllFoldRanges[i];
|
|
|
|
if (range->collapsed && !range->parentCollapsed()) {
|
|
|
|
// Line is found after fold
|
|
|
|
if (range->toLine < Line)
|
|
|
|
result -= range->linesCollapsed;
|
|
|
|
// Inside fold
|
|
|
|
else if (range->fromLine < Line && Line <= range->toLine)
|
|
|
|
result -= Line - range->fromLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setDefaultKeystrokes()
|
|
|
|
{
|
|
|
|
mKeyStrokes.resetDefaults();
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:14:47 +08:00
|
|
|
void SynEdit::setExtraKeystrokes()
|
|
|
|
{
|
|
|
|
mKeyStrokes.setExtraKeyStrokes();
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::invalidateLine(int Line)
|
|
|
|
{
|
|
|
|
QRect rcInval;
|
|
|
|
if (mPainterLock >0)
|
|
|
|
return;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (Line<1 || (Line>mDocument->count() &&
|
2021-10-05 21:25:23 +08:00
|
|
|
Line!=1) || !isVisible())
|
2021-05-14 23:56:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// invalidate text area of this line
|
|
|
|
if (mUseCodeFolding)
|
|
|
|
Line = foldLineToRow(Line);
|
|
|
|
if (Line >= mTopLine && Line <= mTopLine + mLinesInWindow) {
|
2021-05-27 20:33:25 +08:00
|
|
|
rcInval = { mGutterWidth,
|
|
|
|
mTextHeight * (Line - mTopLine),
|
2021-05-14 23:56:43 +08:00
|
|
|
clientWidth(),
|
|
|
|
mTextHeight};
|
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfLinesChanging))
|
|
|
|
mInvalidateRect = mInvalidateRect.united(rcInval);
|
|
|
|
else
|
|
|
|
invalidateRect(rcInval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::invalidateLines(int FirstLine, int LastLine)
|
|
|
|
{
|
|
|
|
if (mPainterLock>0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!isVisible())
|
|
|
|
return;
|
|
|
|
if (FirstLine == -1 && LastLine == -1) {
|
|
|
|
QRect rcInval = clientRect();
|
|
|
|
rcInval.setLeft(rcInval.left()+mGutterWidth);
|
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfLinesChanging)) {
|
|
|
|
mInvalidateRect = mInvalidateRect.united(rcInval);
|
|
|
|
} else {
|
|
|
|
invalidateRect(rcInval);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FirstLine = std::max(FirstLine, 1);
|
|
|
|
LastLine = std::max(LastLine, 1);
|
|
|
|
// find the visible lines first
|
|
|
|
if (LastLine < FirstLine)
|
|
|
|
std::swap(LastLine, FirstLine);
|
|
|
|
|
2022-04-19 21:18:41 +08:00
|
|
|
if (LastLine >= mDocument->count())
|
2021-05-14 23:56:43 +08:00
|
|
|
LastLine = INT_MAX; // paint empty space beyond last line
|
|
|
|
|
|
|
|
if (mUseCodeFolding) {
|
|
|
|
FirstLine = lineToRow(FirstLine);
|
|
|
|
// Could avoid this conversion if (First = Last) and
|
|
|
|
// (Length < CharsInWindow) but the dependency isn't worth IMO.
|
2022-04-19 21:18:41 +08:00
|
|
|
if (LastLine < mDocument->count())
|
2021-05-14 23:56:43 +08:00
|
|
|
LastLine = lineToRow(LastLine + 1) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mTopLine is in display coordinates, so FirstLine and LastLine must be
|
|
|
|
// converted previously.
|
|
|
|
FirstLine = std::max(FirstLine, mTopLine);
|
|
|
|
LastLine = std::min(LastLine, mTopLine + mLinesInWindow);
|
|
|
|
|
|
|
|
// any line visible?
|
|
|
|
if (LastLine >= FirstLine) {
|
|
|
|
QRect rcInval = {
|
|
|
|
clientLeft()+mGutterWidth,
|
|
|
|
mTextHeight * (FirstLine - mTopLine),
|
|
|
|
clientWidth(), mTextHeight * (LastLine - mTopLine + 1)
|
|
|
|
};
|
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfLinesChanging))
|
|
|
|
mInvalidateRect = mInvalidateRect.united(rcInval);
|
|
|
|
else
|
|
|
|
invalidateRect(rcInval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::invalidateSelection()
|
|
|
|
{
|
|
|
|
if (mPainterLock>0)
|
|
|
|
return;
|
2022-07-04 10:01:40 +08:00
|
|
|
invalidateLines(blockBegin().line, blockEnd().line);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::invalidateRect(const QRect &rect)
|
|
|
|
{
|
|
|
|
if (mPainterLock>0)
|
|
|
|
return;
|
|
|
|
viewport()->update(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::invalidate()
|
|
|
|
{
|
|
|
|
if (mPainterLock>0)
|
|
|
|
return;
|
|
|
|
viewport()->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::lockPainter()
|
|
|
|
{
|
|
|
|
mPainterLock++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::unlockPainter()
|
|
|
|
{
|
|
|
|
Q_ASSERT(mPainterLock>0);
|
|
|
|
mPainterLock--;
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
bool SynEdit::selAvail() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mBlockBegin.ch == mBlockEnd.ch && mBlockBegin.line == mBlockEnd.line)
|
2022-05-18 23:17:06 +08:00
|
|
|
return false;
|
|
|
|
// start line != end line or start char != end char
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
|
|
|
if (mBlockBegin.line != mBlockEnd.line) {
|
2022-05-18 23:17:06 +08:00
|
|
|
DisplayCoord coordBegin = bufferToDisplayPos(mBlockBegin);
|
|
|
|
DisplayCoord coordEnd = bufferToDisplayPos(mBlockEnd);
|
|
|
|
return coordBegin.Column!=coordEnd.Column;
|
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2022-03-25 12:59:53 +08:00
|
|
|
bool SynEdit::colSelAvail() const
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode != SynSelectionMode::Column)
|
2022-05-18 23:17:06 +08:00
|
|
|
return false;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mBlockBegin.ch == mBlockEnd.ch && mBlockBegin.line == mBlockEnd.line)
|
2022-05-18 23:17:06 +08:00
|
|
|
return false;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mBlockBegin.line == mBlockEnd.line && mBlockBegin.ch!=mBlockBegin.ch)
|
2022-05-18 23:17:06 +08:00
|
|
|
return true;
|
|
|
|
DisplayCoord coordBegin = bufferToDisplayPos(mBlockBegin);
|
|
|
|
DisplayCoord coordEnd = bufferToDisplayPos(mBlockEnd);
|
|
|
|
return coordBegin.Column!=coordEnd.Column;
|
2022-03-25 12:59:53 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
QString SynEdit::wordAtCursor()
|
2021-08-01 01:06:43 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
return wordAtRowCol(caretXY());
|
2021-08-01 01:06:43 +08:00
|
|
|
}
|
|
|
|
|
2022-01-12 18:07:52 +08:00
|
|
|
QString SynEdit::wordAtRowCol(const BufferCoord &pos)
|
2021-08-01 01:06:43 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((pos.line >= 1) && (pos.line <= mDocument->count())) {
|
|
|
|
QString line = mDocument->getString(pos.line - 1);
|
2022-01-12 18:07:52 +08:00
|
|
|
int len = line.length();
|
|
|
|
if (len == 0)
|
2021-08-01 01:06:43 +08:00
|
|
|
return "";
|
2022-07-04 10:01:40 +08:00
|
|
|
if (pos.ch<1 || pos.ch>len)
|
2021-08-01 01:06:43 +08:00
|
|
|
return "";
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
int start = pos.ch - 1;
|
2021-08-01 01:06:43 +08:00
|
|
|
if ((start> 0) && !isIdentChar(line[start]))
|
|
|
|
start--;
|
|
|
|
|
|
|
|
if (isIdentChar(line[start])) {
|
|
|
|
int stop = start;
|
2022-01-12 18:07:52 +08:00
|
|
|
while ((stop < len) && isIdentChar(line[stop]))
|
2021-08-01 01:06:43 +08:00
|
|
|
stop++;
|
|
|
|
while ((start-1 >=0) && isIdentChar(line[start - 1]))
|
|
|
|
start--;
|
|
|
|
if (stop > start)
|
|
|
|
return line.mid(start,stop-start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-01-12 18:07:52 +08:00
|
|
|
QChar SynEdit::charAt(const BufferCoord &pos)
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((pos.line >= 1) && (pos.line <= mDocument->count())) {
|
|
|
|
QString line = mDocument->getString(pos.line-1);
|
2022-01-12 18:07:52 +08:00
|
|
|
int len = line.length();
|
|
|
|
if (len == 0)
|
|
|
|
return QChar(0);
|
2022-07-04 10:01:40 +08:00
|
|
|
if (pos.ch<1 || pos.ch>len)
|
2022-01-12 18:07:52 +08:00
|
|
|
return QChar(0);
|
2022-07-04 10:01:40 +08:00
|
|
|
return line[pos.ch-1];
|
2022-01-12 18:07:52 +08:00
|
|
|
}
|
|
|
|
return QChar(0);
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:55:39 +08:00
|
|
|
QChar SynEdit::nextNonSpaceChar(int line, int ch)
|
2022-04-13 16:42:23 +08:00
|
|
|
{
|
|
|
|
if (ch<0)
|
|
|
|
return QChar();
|
2022-04-19 21:18:41 +08:00
|
|
|
QString s = mDocument->getString(line);
|
2022-04-13 16:42:23 +08:00
|
|
|
if (s.isEmpty())
|
|
|
|
return QChar();
|
|
|
|
int x=ch;
|
|
|
|
while (x<s.length()) {
|
|
|
|
QChar ch = s[x];
|
|
|
|
if (!ch.isSpace())
|
|
|
|
return ch;
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
return QChar();
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:55:39 +08:00
|
|
|
QChar SynEdit::lastNonSpaceChar(int line, int ch)
|
|
|
|
{
|
|
|
|
if (line>=mDocument->count())
|
|
|
|
return QChar();
|
|
|
|
QString s = mDocument->getString(line);
|
|
|
|
int x = std::min(ch-1,s.length()-1);
|
|
|
|
while (line>=0) {
|
|
|
|
while (x>=0) {
|
|
|
|
QChar c = s[x];
|
|
|
|
if (!c.isSpace())
|
|
|
|
return c;
|
|
|
|
x--;
|
|
|
|
}
|
|
|
|
line--;
|
|
|
|
if (line>=0) {
|
|
|
|
s = mDocument->getString(line);
|
|
|
|
x = s.length()-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QChar();
|
|
|
|
}
|
|
|
|
|
2022-05-04 00:08:04 +08:00
|
|
|
void SynEdit::setCaretAndSelection(const BufferCoord &ptCaret, const BufferCoord &ptSelBegin, const BufferCoord &ptSelEnd)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
incPaintLock();
|
|
|
|
internalSetCaretXY(ptCaret);
|
2022-05-04 00:08:04 +08:00
|
|
|
setBlockBegin(ptSelBegin);
|
|
|
|
setBlockEnd(ptSelEnd);
|
2022-07-04 10:01:40 +08:00
|
|
|
decPaintLock();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2022-03-10 12:15:44 +08:00
|
|
|
bool SynEdit::inputMethodOn()
|
|
|
|
{
|
|
|
|
return !mInputPreeditString.isEmpty();
|
|
|
|
}
|
|
|
|
|
2021-11-13 17:41:16 +08:00
|
|
|
void SynEdit::collapseAll()
|
|
|
|
{
|
2022-04-18 16:17:06 +08:00
|
|
|
incPaintLock();
|
2021-11-13 17:41:16 +08:00
|
|
|
for (int i = mAllFoldRanges.count()-1;i>=0;i--){
|
|
|
|
collapse(mAllFoldRanges[i]);
|
|
|
|
}
|
2022-04-18 16:17:06 +08:00
|
|
|
decPaintLock();
|
2021-11-13 17:41:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::unCollpaseAll()
|
|
|
|
{
|
2022-04-18 16:17:06 +08:00
|
|
|
incPaintLock();
|
2021-11-13 17:41:16 +08:00
|
|
|
for (int i = mAllFoldRanges.count()-1;i>=0;i--){
|
|
|
|
uncollapse(mAllFoldRanges[i]);
|
|
|
|
}
|
2022-04-18 16:17:06 +08:00
|
|
|
decPaintLock();
|
2021-11-13 17:41:16 +08:00
|
|
|
}
|
|
|
|
|
2021-06-09 17:12:23 +08:00
|
|
|
void SynEdit::processGutterClick(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
int X = event->pos().x();
|
|
|
|
int Y = event->pos().y();
|
2022-03-31 19:36:46 +08:00
|
|
|
DisplayCoord RowColumn = pixelsToNearestRowColumn(X, Y);
|
2021-06-09 17:12:23 +08:00
|
|
|
int Line = rowToLine(RowColumn.Row);
|
|
|
|
|
|
|
|
// Check if we clicked on a folding thing
|
|
|
|
if (mUseCodeFolding) {
|
2022-06-28 21:46:20 +08:00
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(Line);
|
|
|
|
if (foldRange) {
|
2021-06-09 17:12:23 +08:00
|
|
|
// See if we actually clicked on the rectangle...
|
|
|
|
//rect.Left := Gutter.RealGutterWidth(CharWidth) - Gutter.RightOffset;
|
|
|
|
QRect rect;
|
|
|
|
rect.setLeft(mGutterWidth - mGutter.rightOffset());
|
|
|
|
rect.setRight(rect.left() + mGutter.rightOffset() - 4);
|
|
|
|
rect.setTop((RowColumn.Row - mTopLine) * mTextHeight);
|
2021-11-03 18:09:12 +08:00
|
|
|
rect.setBottom(rect.top() + mTextHeight - 1);
|
2021-06-09 17:12:23 +08:00
|
|
|
if (rect.contains(QPoint(X, Y))) {
|
2022-06-28 21:46:20 +08:00
|
|
|
if (foldRange->collapsed)
|
|
|
|
uncollapse(foldRange);
|
2021-06-09 17:12:23 +08:00
|
|
|
else
|
2022-06-28 21:46:20 +08:00
|
|
|
collapse(foldRange);
|
2021-06-09 17:12:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, check gutter marks
|
2022-04-19 21:18:41 +08:00
|
|
|
if (Line>=1 && Line <= mDocument->count()) {
|
2021-06-09 17:12:23 +08:00
|
|
|
emit gutterClicked(event->button(),X,Y,Line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::clearUndo()
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->clear();
|
|
|
|
mRedoList->clear();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-10-30 18:28:40 +08:00
|
|
|
int SynEdit::findIndentsStartLine(int line, QVector<int> indents)
|
|
|
|
{
|
|
|
|
line--;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (line<0 || line>=mDocument->count())
|
2021-10-30 18:28:40 +08:00
|
|
|
return -1;
|
|
|
|
while (line>=1) {
|
2022-04-19 21:18:41 +08:00
|
|
|
SynRangeState range = mDocument->ranges(line);
|
2021-10-30 18:28:40 +08:00
|
|
|
QVector<int> newIndents = range.indents.mid(range.firstIndentThisLine);
|
|
|
|
int i = 0;
|
|
|
|
int len = indents.length();
|
|
|
|
while (i<len && !newIndents.isEmpty()) {
|
|
|
|
int indent = indents[i];
|
|
|
|
int idx = newIndents.lastIndexOf(indent);
|
|
|
|
if (idx >=0) {
|
|
|
|
newIndents.remove(idx,newIndents.size());
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i>=len) {
|
|
|
|
return line+1;
|
|
|
|
} else {
|
|
|
|
indents = range.matchingIndents + indents.mid(i);
|
|
|
|
}
|
|
|
|
line--;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferCoord SynEdit::getPreviousLeftBrace(int x, int y)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
QChar Test;
|
|
|
|
QString vDummy;
|
|
|
|
PSynHighlighterAttribute attr;
|
|
|
|
BufferCoord p;
|
|
|
|
bool isCommentOrStringOrChar;
|
|
|
|
BufferCoord Result{0,0};
|
|
|
|
// get char at caret
|
|
|
|
int PosX = x-1;
|
|
|
|
int PosY = y;
|
|
|
|
if (PosX<1)
|
|
|
|
PosY--;
|
|
|
|
if (PosY<1 )
|
|
|
|
return Result;
|
2022-04-19 21:18:41 +08:00
|
|
|
QString Line = mDocument->getString(PosY - 1);
|
2021-06-03 23:18:51 +08:00
|
|
|
if ((PosX > Line.length()) || (PosX<1))
|
|
|
|
PosX = Line.length();
|
|
|
|
int numBrackets = 1;
|
|
|
|
while (true) {
|
|
|
|
if (Line.isEmpty()){
|
|
|
|
PosY--;
|
|
|
|
if (PosY<1)
|
|
|
|
return Result;
|
2022-04-19 21:18:41 +08:00
|
|
|
Line = mDocument->getString(PosY - 1);
|
2021-06-03 23:18:51 +08:00
|
|
|
PosX = Line.length();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Test = Line[PosX-1];
|
2022-07-04 10:01:40 +08:00
|
|
|
p.ch = PosX;
|
|
|
|
p.line = PosY;
|
2021-06-03 23:18:51 +08:00
|
|
|
if (Test=='{' || Test == '}') {
|
2021-09-20 15:57:48 +08:00
|
|
|
if (getHighlighterAttriAtRowCol(p, vDummy, attr)) {
|
2021-06-03 23:18:51 +08:00
|
|
|
isCommentOrStringOrChar =
|
|
|
|
(attr == mHighlighter->stringAttribute()) ||
|
|
|
|
(attr == mHighlighter->commentAttribute()) ||
|
|
|
|
(attr->name() == SYNS_AttrCharacter);
|
|
|
|
} else
|
|
|
|
isCommentOrStringOrChar = false;
|
|
|
|
if ((Test == '{') && (! isCommentOrStringOrChar))
|
|
|
|
numBrackets--;
|
|
|
|
else if ((Test == '}') && (!isCommentOrStringOrChar))
|
|
|
|
numBrackets++;
|
|
|
|
if (numBrackets == 0) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PosX--;
|
|
|
|
if (PosX<1) {
|
|
|
|
PosY--;
|
|
|
|
if (PosY<1)
|
|
|
|
return Result;
|
2022-04-19 21:18:41 +08:00
|
|
|
Line = mDocument->getString(PosY - 1);
|
2021-06-03 23:18:51 +08:00
|
|
|
PosX = Line.length();
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::charColumns(QChar ch) const
|
2021-05-16 20:36:00 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return mDocument->charColumns(ch);
|
2021-05-16 20:36:00 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
void SynEdit::showCaret()
|
|
|
|
{
|
2021-05-24 18:11:07 +08:00
|
|
|
if (m_blinkTimerId==0)
|
|
|
|
m_blinkTimerId = startTimer(500);
|
2021-11-13 12:33:33 +08:00
|
|
|
m_blinkStatus = 1;
|
|
|
|
updateCaret();
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::hideCaret()
|
|
|
|
{
|
|
|
|
if (m_blinkTimerId!=0) {
|
|
|
|
killTimer(m_blinkTimerId);
|
|
|
|
m_blinkTimerId = 0;
|
2021-05-27 23:45:22 +08:00
|
|
|
m_blinkStatus = 0;
|
|
|
|
updateCaret();
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::isPointInSelection(const BufferCoord &Value) const
|
2021-05-27 23:45:22 +08:00
|
|
|
{
|
2021-06-03 20:26:36 +08:00
|
|
|
BufferCoord ptBegin = blockBegin();
|
|
|
|
BufferCoord ptEnd = blockEnd();
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((Value.line >= ptBegin.line) && (Value.line <= ptEnd.line) &&
|
|
|
|
((ptBegin.line != ptEnd.line) || (ptBegin.ch != ptEnd.ch))) {
|
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Line)
|
2021-05-27 23:45:22 +08:00
|
|
|
return true;
|
2022-07-04 10:01:40 +08:00
|
|
|
else if (mActiveSelectionMode == SynSelectionMode::Column) {
|
|
|
|
if (ptBegin.ch > ptEnd.ch)
|
|
|
|
return (Value.ch >= ptEnd.ch) && (Value.ch < ptBegin.ch);
|
|
|
|
else if (ptBegin.ch < ptEnd.ch)
|
|
|
|
return (Value.ch >= ptBegin.ch) && (Value.ch < ptEnd.ch);
|
2021-05-27 23:45:22 +08:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
} else
|
2022-07-04 10:01:40 +08:00
|
|
|
return ((Value.line > ptBegin.line) || (Value.ch >= ptBegin.ch)) &&
|
|
|
|
((Value.line < ptEnd.line) || (Value.ch < ptEnd.ch));
|
2021-05-27 23:45:22 +08:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::nextWordPos()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
return nextWordPosEx(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::nextWordPosEx(const BufferCoord &XY)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
int CX = XY.ch;
|
|
|
|
int CY = XY.line;
|
2021-05-29 21:35:46 +08:00
|
|
|
// valid line?
|
2022-04-19 21:18:41 +08:00
|
|
|
if ((CY >= 1) && (CY <= mDocument->count())) {
|
|
|
|
QString Line = mDocument->getString(CY - 1);
|
2021-05-29 21:35:46 +08:00
|
|
|
int LineLen = Line.length();
|
|
|
|
if (CX >= LineLen) {
|
|
|
|
// find first IdentChar or multibyte char in the next line
|
2022-04-19 21:18:41 +08:00
|
|
|
if (CY < mDocument->count()) {
|
|
|
|
Line = mDocument->getString(CY);
|
2021-05-29 21:35:46 +08:00
|
|
|
CY++;
|
|
|
|
CX=StrScanForWordChar(Line,1);
|
|
|
|
if (CX==0)
|
|
|
|
CX=1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// find next "whitespace" if current char is an IdentChar
|
|
|
|
if (!Line[CX-1].isSpace())
|
|
|
|
CX = StrScanForNonWordChar(Line,CX);
|
|
|
|
// if "whitespace" found, find the next IdentChar
|
|
|
|
if (CX > 0)
|
|
|
|
CX = StrScanForWordChar(Line, CX);
|
|
|
|
// if one of those failed position at the begin of next line
|
|
|
|
if (CX == 0) {
|
2022-04-19 21:18:41 +08:00
|
|
|
if (CY < mDocument->count()) {
|
|
|
|
Line = mDocument->getString(CY);
|
2021-05-29 21:35:46 +08:00
|
|
|
CY++;
|
|
|
|
CX=StrScanForWordChar(Line,1);
|
|
|
|
if (CX==0)
|
|
|
|
CX=1;
|
|
|
|
} else {
|
|
|
|
CX=Line.length()+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BufferCoord{CX,CY};
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::wordStart()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
return wordStartEx(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::wordStartEx(const BufferCoord &XY)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
int CX = XY.ch;
|
|
|
|
int CY = XY.line;
|
2021-05-29 21:35:46 +08:00
|
|
|
// valid line?
|
2022-04-19 21:18:41 +08:00
|
|
|
if ((CY >= 1) && (CY <= mDocument->count())) {
|
|
|
|
QString Line = mDocument->getString(CY - 1);
|
2021-08-31 14:41:48 +08:00
|
|
|
CX = std::min(CX, Line.length()+1);
|
|
|
|
if (CX > 1) {
|
2021-09-01 17:44:28 +08:00
|
|
|
if (isWordChar(Line[CX - 2]))
|
2021-05-29 21:35:46 +08:00
|
|
|
CX = StrRScanForNonWordChar(Line, CX - 1) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BufferCoord{CX,CY};
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::wordEnd()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
return wordEndEx(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::wordEndEx(const BufferCoord &XY)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
int CX = XY.ch;
|
|
|
|
int CY = XY.line;
|
2021-05-29 21:35:46 +08:00
|
|
|
// valid line?
|
2022-04-19 21:18:41 +08:00
|
|
|
if ((CY >= 1) && (CY <= mDocument->count())) {
|
|
|
|
QString Line = mDocument->getString(CY - 1);
|
2022-01-24 11:23:30 +08:00
|
|
|
if (CX <= Line.length() && CX-1>=0) {
|
|
|
|
if (isWordChar(Line[CX - 1]))
|
2021-05-29 21:35:46 +08:00
|
|
|
CX = StrScanForNonWordChar(Line, CX);
|
|
|
|
if (CX == 0)
|
|
|
|
CX = Line.length() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BufferCoord{CX,CY};
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::prevWordPos()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
return prevWordPosEx(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord SynEdit::prevWordPosEx(const BufferCoord &XY)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
int CX = XY.ch;
|
|
|
|
int CY = XY.line;
|
2021-05-29 21:35:46 +08:00
|
|
|
// valid line?
|
2022-04-19 21:18:41 +08:00
|
|
|
if ((CY >= 1) && (CY <= mDocument->count())) {
|
|
|
|
QString Line = mDocument->getString(CY - 1);
|
2021-05-29 21:35:46 +08:00
|
|
|
CX = std::min(CX, Line.length());
|
|
|
|
if (CX <= 1) {
|
|
|
|
// find last IdentChar in the previous line
|
|
|
|
if (CY > 1) {
|
|
|
|
CY -- ;
|
2022-04-19 21:18:41 +08:00
|
|
|
Line = mDocument->getString(CY - 1);
|
2021-05-29 21:35:46 +08:00
|
|
|
CX = StrRScanForWordChar(Line, Line.length())+1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if previous char is a "whitespace" search for the last IdentChar
|
2021-11-22 15:50:28 +08:00
|
|
|
if (!isWordChar(Line[CX - 2]))
|
2021-05-29 21:35:46 +08:00
|
|
|
CX = StrRScanForWordChar(Line, CX - 1);
|
|
|
|
if (CX > 0) // search for the first IdentChar of this "word"
|
|
|
|
CX = StrRScanForNonWordChar(Line, CX - 1)+1;
|
|
|
|
if (CX == 0) {
|
|
|
|
// find last IdentChar in the previous line
|
|
|
|
if (CY > 1) {
|
|
|
|
CY -- ;
|
2022-04-19 21:18:41 +08:00
|
|
|
Line = mDocument->getString(CY - 1);
|
2021-05-29 21:35:46 +08:00
|
|
|
CX = StrRScanForWordChar(Line, Line.length())+1;
|
|
|
|
} else {
|
|
|
|
CX = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BufferCoord{CX,CY};
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::setSelWord()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
setWordBlock(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::setWordBlock(BufferCoord Value)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-06-08 21:41:42 +08:00
|
|
|
// if (mOptions.testFlag(eoScrollPastEol))
|
|
|
|
// Value.Char =
|
|
|
|
// else
|
|
|
|
// Value.Char = std::max(Value.Char, 1);
|
2022-07-04 10:01:40 +08:00
|
|
|
Value.line = minMax(Value.line, 1, mDocument->count());
|
|
|
|
Value.ch = std::max(Value.ch, 1);
|
|
|
|
QString TempString = mDocument->getString(Value.line - 1); //needed for CaretX = LineLength +1
|
|
|
|
if (Value.ch > TempString.length()) {
|
|
|
|
internalSetCaretXY(BufferCoord{TempString.length()+1, Value.line});
|
2021-05-29 21:35:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord v_WordStart = wordStartEx(Value);
|
|
|
|
BufferCoord v_WordEnd = wordEndEx(Value);
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((v_WordStart.line == v_WordEnd.line) && (v_WordStart.ch < v_WordEnd.ch))
|
2021-05-29 21:35:46 +08:00
|
|
|
setCaretAndSelection(v_WordEnd, v_WordStart, v_WordEnd);
|
|
|
|
}
|
|
|
|
|
2021-11-07 11:39:52 +08:00
|
|
|
int SynEdit::findCommentStartLine(int searchStartLine)
|
|
|
|
{
|
|
|
|
int commentStartLine = searchStartLine;
|
|
|
|
SynRangeState range;
|
|
|
|
while (commentStartLine>=1) {
|
2022-04-19 21:18:41 +08:00
|
|
|
range = mDocument->ranges(commentStartLine-1);
|
2021-11-07 11:39:52 +08:00
|
|
|
if (!mHighlighter->isLastLineCommentNotFinished(range.state)){
|
|
|
|
commentStartLine++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!range.matchingIndents.isEmpty()
|
|
|
|
|| range.firstIndentThisLine<range.indents.length())
|
|
|
|
break;
|
|
|
|
commentStartLine--;
|
|
|
|
}
|
|
|
|
if (commentStartLine<1)
|
|
|
|
commentStartLine = 1;
|
|
|
|
return commentStartLine;
|
|
|
|
}
|
|
|
|
|
2021-10-28 09:20:58 +08:00
|
|
|
int SynEdit::calcIndentSpaces(int line, const QString& lineText, bool addIndent)
|
2021-10-28 08:56:13 +08:00
|
|
|
{
|
|
|
|
if (!mHighlighter)
|
|
|
|
return 0;
|
2022-04-19 21:18:41 +08:00
|
|
|
line = std::min(line, mDocument->count()+1);
|
2021-10-28 08:56:13 +08:00
|
|
|
if (line<=1)
|
|
|
|
return 0;
|
2021-10-29 12:25:04 +08:00
|
|
|
// find the first non-empty preceeding line
|
2021-10-28 08:56:13 +08:00
|
|
|
int startLine = line-1;
|
2021-12-20 08:18:43 +08:00
|
|
|
QString startLineText;
|
2021-10-28 08:56:13 +08:00
|
|
|
while (startLine>=1) {
|
2022-04-19 21:18:41 +08:00
|
|
|
startLineText = mDocument->getString(startLine-1);
|
2021-12-20 08:18:43 +08:00
|
|
|
if (!startLineText.startsWith('#') && !startLineText.trimmed().isEmpty()) {
|
2021-10-28 08:56:13 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
startLine -- ;
|
|
|
|
}
|
2021-10-29 12:25:04 +08:00
|
|
|
int indentSpaces = 0;
|
2021-10-28 08:56:13 +08:00
|
|
|
if (startLine>=1) {
|
2021-12-20 08:45:16 +08:00
|
|
|
//calculate the indents of last statement;
|
2021-12-20 08:18:43 +08:00
|
|
|
indentSpaces = leftSpaces(startLineText);
|
2022-04-19 21:18:41 +08:00
|
|
|
SynRangeState rangePreceeding = mDocument->ranges(startLine-1);
|
2021-11-06 15:23:28 +08:00
|
|
|
mHighlighter->setState(rangePreceeding);
|
2021-11-07 01:07:41 +08:00
|
|
|
if (addIndent) {
|
2021-12-20 08:18:43 +08:00
|
|
|
// QString trimmedS = s.trimmed();
|
2021-11-07 10:48:09 +08:00
|
|
|
QString trimmedLineText = lineText.trimmed();
|
|
|
|
mHighlighter->setLine(trimmedLineText,line-1);
|
2021-11-07 11:39:52 +08:00
|
|
|
int statePrePre;
|
|
|
|
if (startLine>1) {
|
2022-04-19 21:18:41 +08:00
|
|
|
statePrePre = mDocument->ranges(startLine-2).state;
|
2021-11-07 11:39:52 +08:00
|
|
|
} else {
|
|
|
|
statePrePre = 0;
|
|
|
|
}
|
2021-10-30 18:28:40 +08:00
|
|
|
SynRangeState rangeAfterFirstToken = mHighlighter->getRangeState();
|
|
|
|
QString firstToken = mHighlighter->getToken();
|
|
|
|
PSynHighlighterAttribute attr = mHighlighter->getTokenAttribute();
|
|
|
|
if (attr == mHighlighter->keywordAttribute()
|
|
|
|
&& lineText.endsWith(':')
|
|
|
|
&& (
|
|
|
|
firstToken == "public" || firstToken == "private"
|
|
|
|
|| firstToken == "protected" || firstToken == "case")) {
|
2021-11-07 01:07:41 +08:00
|
|
|
// public: private: protecte: case: should indents like it's parent statement
|
2021-10-30 18:28:40 +08:00
|
|
|
mHighlighter->setState(rangePreceeding);
|
|
|
|
mHighlighter->setLine("}",line-1);
|
|
|
|
rangeAfterFirstToken = mHighlighter->getRangeState();
|
|
|
|
firstToken = mHighlighter->getToken();
|
|
|
|
attr = mHighlighter->getTokenAttribute();
|
|
|
|
}
|
2021-12-20 08:45:16 +08:00
|
|
|
bool indentAdded = false;
|
2021-11-07 10:48:09 +08:00
|
|
|
int additionIndent = 0;
|
2021-10-30 18:28:40 +08:00
|
|
|
QVector<int> matchingIndents;
|
|
|
|
int l;
|
|
|
|
if (attr == mHighlighter->symbolAttribute()
|
2021-12-20 08:45:16 +08:00
|
|
|
&& (firstToken == '}')) {
|
|
|
|
// current line starts with '}', we should consider it to calc indents
|
2021-10-30 18:28:40 +08:00
|
|
|
matchingIndents = rangeAfterFirstToken.matchingIndents;
|
2021-12-20 08:45:16 +08:00
|
|
|
indentAdded = true;
|
|
|
|
l = startLine;
|
|
|
|
} else if (attr == mHighlighter->symbolAttribute()
|
|
|
|
&& (firstToken == '{')
|
|
|
|
&& (rangePreceeding.getLastIndent()==sitStatement)) {
|
|
|
|
// current line starts with '{' and last statement not finished, we should consider it to calc indents
|
|
|
|
matchingIndents = rangeAfterFirstToken.matchingIndents;
|
|
|
|
indentAdded = true;
|
2021-10-30 18:28:40 +08:00
|
|
|
l = startLine;
|
2021-11-13 09:05:02 +08:00
|
|
|
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
|
|
|
|
&& trimmedLineText.startsWith('#')
|
|
|
|
&& attr == ((SynEditCppHighlighter *)mHighlighter.get())->preprocessorAttribute()) {
|
2021-12-20 08:45:16 +08:00
|
|
|
indentAdded = true;
|
2021-11-13 09:05:02 +08:00
|
|
|
indentSpaces=0;
|
|
|
|
l=0;
|
|
|
|
} else if (mHighlighter->getClass() == SynHighlighterClass::CppHighlighter
|
|
|
|
&& mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)
|
2021-11-07 01:07:41 +08:00
|
|
|
) {
|
2022-06-28 09:37:18 +08:00
|
|
|
// last line is a not finished comment,
|
|
|
|
if (trimmedLineText.startsWith("*")) {
|
|
|
|
// this line start with "* "
|
|
|
|
// it means this line is a docstring, should indents according to
|
|
|
|
// the line the comment beginning , and add 1 additional space
|
|
|
|
additionIndent = 1;
|
|
|
|
int commentStartLine = findCommentStartLine(startLine-1);
|
|
|
|
SynRangeState range;
|
|
|
|
indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1));
|
|
|
|
range = mDocument->ranges(commentStartLine-1);
|
|
|
|
matchingIndents = range.matchingIndents;
|
|
|
|
indentAdded = true;
|
|
|
|
l = commentStartLine;
|
|
|
|
} else {
|
2022-06-28 12:22:40 +08:00
|
|
|
//indents according to the beginning of the comment and 2 additional space
|
2022-06-28 09:37:18 +08:00
|
|
|
additionIndent = 0;
|
2022-06-28 12:22:40 +08:00
|
|
|
int commentStartLine = findCommentStartLine(startLine-1);
|
2022-06-28 09:37:18 +08:00
|
|
|
SynRangeState range;
|
2022-06-28 12:22:40 +08:00
|
|
|
indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1))+2;
|
|
|
|
range = mDocument->ranges(commentStartLine-1);
|
2022-06-28 09:37:18 +08:00
|
|
|
matchingIndents = range.matchingIndents;
|
|
|
|
indentAdded = true;
|
|
|
|
l = startLine;
|
|
|
|
}
|
2021-11-07 11:39:52 +08:00
|
|
|
} else if ( mHighlighter->isLastLineCommentNotFinished(statePrePre)
|
|
|
|
&& rangePreceeding.matchingIndents.isEmpty()
|
|
|
|
&& rangePreceeding.firstIndentThisLine>=rangePreceeding.indents.length()
|
|
|
|
&& !mHighlighter->isLastLineCommentNotFinished(rangePreceeding.state)) {
|
|
|
|
// the preceeding line is the end of comment
|
|
|
|
// we should use the indents of the start line of the comment
|
|
|
|
int commentStartLine = findCommentStartLine(startLine-2);
|
2021-11-07 01:07:41 +08:00
|
|
|
SynRangeState range;
|
2022-04-19 21:18:41 +08:00
|
|
|
indentSpaces = leftSpaces(mDocument->getString(commentStartLine-1));
|
|
|
|
range = mDocument->ranges(commentStartLine-1);
|
2021-11-07 01:07:41 +08:00
|
|
|
matchingIndents = range.matchingIndents;
|
2021-12-20 08:45:16 +08:00
|
|
|
indentAdded = true;
|
2021-11-07 01:07:41 +08:00
|
|
|
l = commentStartLine;
|
2021-10-30 18:28:40 +08:00
|
|
|
} else {
|
2021-11-07 01:07:41 +08:00
|
|
|
// we just use infos till preceeding line's end to calc indents
|
2021-10-30 18:28:40 +08:00
|
|
|
matchingIndents = rangePreceeding.matchingIndents;
|
|
|
|
l = startLine-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchingIndents.isEmpty()
|
|
|
|
) {
|
2021-10-29 12:25:04 +08:00
|
|
|
// find the indent's start line, and use it's indent as the default indent;
|
|
|
|
while (l>=1) {
|
2022-04-19 21:18:41 +08:00
|
|
|
SynRangeState range = mDocument->ranges(l-1);
|
2021-10-30 18:28:40 +08:00
|
|
|
QVector<int> newIndents = range.indents.mid(range.firstIndentThisLine);
|
2021-10-29 17:55:05 +08:00
|
|
|
int i = 0;
|
|
|
|
int len = matchingIndents.length();
|
2021-10-29 15:15:43 +08:00
|
|
|
while (i<len && !newIndents.isEmpty()) {
|
2021-10-30 18:28:40 +08:00
|
|
|
int indent = matchingIndents[i];
|
2021-10-29 15:15:43 +08:00
|
|
|
int idx = newIndents.lastIndexOf(indent);
|
|
|
|
if (idx >=0) {
|
2021-10-30 18:28:40 +08:00
|
|
|
newIndents.remove(idx,newIndents.length()-idx);
|
2021-10-29 15:15:43 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (i>=len) {
|
2021-10-31 00:08:20 +08:00
|
|
|
// we found the where the indent started
|
|
|
|
if (len>0 && !range.matchingIndents.isEmpty()
|
|
|
|
&&
|
|
|
|
( matchingIndents.back()== sitBrace
|
|
|
|
|| matchingIndents.back() == sitStatement
|
|
|
|
) ) {
|
|
|
|
// but it's not a complete statement
|
|
|
|
matchingIndents = range.matchingIndents;
|
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
indentSpaces = leftSpaces(mDocument->getString(l-1));
|
2021-10-31 00:08:20 +08:00
|
|
|
if (newIndents.length()>0)
|
2022-04-19 21:18:41 +08:00
|
|
|
indentSpaces+=tabWidth();
|
2021-10-31 00:08:20 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-10-29 17:55:05 +08:00
|
|
|
} else {
|
|
|
|
matchingIndents = range.matchingIndents + matchingIndents.mid(i);
|
2021-10-29 12:25:04 +08:00
|
|
|
}
|
|
|
|
l--;
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 08:45:16 +08:00
|
|
|
if (!indentAdded) {
|
2021-11-06 15:23:28 +08:00
|
|
|
if (rangePreceeding.firstIndentThisLine < rangePreceeding.indents.length()) {
|
2022-04-19 21:18:41 +08:00
|
|
|
indentSpaces += tabWidth();
|
2021-12-20 08:45:16 +08:00
|
|
|
indentAdded = true;
|
2021-11-06 15:23:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 08:45:16 +08:00
|
|
|
if (!indentAdded && !startLineText.isEmpty()) {
|
2021-11-06 15:23:28 +08:00
|
|
|
BufferCoord coord;
|
|
|
|
QString token;
|
|
|
|
PSynHighlighterAttribute attr;
|
2022-07-04 10:01:40 +08:00
|
|
|
coord.line = startLine;
|
|
|
|
coord.ch = document()->getString(startLine-1).length();
|
2021-11-06 15:23:28 +08:00
|
|
|
if (getHighlighterAttriAtRowCol(coord,token,attr)
|
|
|
|
&& attr == mHighlighter->symbolAttribute()
|
|
|
|
&& token == ":") {
|
2022-04-19 21:18:41 +08:00
|
|
|
indentSpaces += tabWidth();
|
2021-12-20 08:45:16 +08:00
|
|
|
indentAdded = true;
|
2021-11-06 15:23:28 +08:00
|
|
|
}
|
2021-10-28 23:46:54 +08:00
|
|
|
}
|
2021-11-07 10:48:09 +08:00
|
|
|
indentSpaces += additionIndent;
|
2021-10-28 09:20:58 +08:00
|
|
|
}
|
2021-10-28 08:56:13 +08:00
|
|
|
}
|
|
|
|
return std::max(0,indentSpaces);
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doSelectAll()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
|
|
|
BufferCoord LastPt;
|
2022-07-04 10:01:40 +08:00
|
|
|
LastPt.ch = 1;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument->empty()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
LastPt.line = 1;
|
2021-05-29 21:35:46 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
LastPt.line = mDocument->count();
|
|
|
|
LastPt.ch = mDocument->getString(LastPt.line-1).length()+1;
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
setCaretAndSelection(caretXY(), BufferCoord{1, 1}, LastPt);
|
|
|
|
// Selection should have changed...
|
2021-08-27 23:51:42 +08:00
|
|
|
emit statusChanged(SynStatusChange::scSelection);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:26 +08:00
|
|
|
void SynEdit::doComment()
|
|
|
|
{
|
|
|
|
BufferCoord origBlockBegin, origBlockEnd, origCaret;
|
|
|
|
int endLine;
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2021-06-21 11:21:26 +08:00
|
|
|
auto action = finally([this]{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-21 11:21:26 +08:00
|
|
|
});
|
|
|
|
origBlockBegin = blockBegin();
|
|
|
|
origBlockEnd = blockEnd();
|
|
|
|
origCaret = caretXY();
|
|
|
|
// Ignore the last line the cursor is placed on
|
2022-07-04 10:01:40 +08:00
|
|
|
if (origBlockEnd.ch == 1)
|
|
|
|
endLine = std::max(origBlockBegin.line - 1, origBlockEnd.line - 2);
|
2021-06-21 11:21:26 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
endLine = origBlockEnd.line - 1;
|
|
|
|
for (int i = origBlockBegin.line - 1; i<=endLine; i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(i, "//" + mDocument->getString(i));
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Insert,
|
2021-06-21 11:21:26 +08:00
|
|
|
BufferCoord{1, i + 1},
|
|
|
|
BufferCoord{3, i + 1},
|
2022-07-04 10:01:40 +08:00
|
|
|
QStringList(), SynSelectionMode::Normal);
|
2021-06-21 11:21:26 +08:00
|
|
|
}
|
|
|
|
// When grouping similar commands, process one comment action per undo/redo
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addGroupBreak();
|
2021-06-21 11:21:26 +08:00
|
|
|
// Move begin of selection
|
2022-07-04 10:01:40 +08:00
|
|
|
if (origBlockBegin.ch > 1)
|
|
|
|
origBlockBegin.ch+=2;
|
2021-06-21 11:21:26 +08:00
|
|
|
// Move end of selection
|
2022-07-04 10:01:40 +08:00
|
|
|
if (origBlockEnd.ch > 1)
|
|
|
|
origBlockEnd.ch+=2;
|
2021-06-21 11:21:26 +08:00
|
|
|
// Move caret
|
2022-07-04 10:01:40 +08:00
|
|
|
if (origCaret.ch > 1)
|
|
|
|
origCaret.ch+=2;
|
2021-06-21 11:21:26 +08:00
|
|
|
setCaretAndSelection(origCaret, origBlockBegin, origBlockEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doUncomment()
|
|
|
|
{
|
|
|
|
BufferCoord origBlockBegin, origBlockEnd, origCaret;
|
|
|
|
int endLine;
|
|
|
|
QString s;
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList changeText;
|
|
|
|
changeText.append("//");
|
2021-06-21 11:21:26 +08:00
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2021-06-21 11:21:26 +08:00
|
|
|
auto action = finally([this]{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-21 11:21:26 +08:00
|
|
|
});
|
|
|
|
origBlockBegin = blockBegin();
|
|
|
|
origBlockEnd = blockEnd();
|
|
|
|
origCaret = caretXY();
|
|
|
|
// Ignore the last line the cursor is placed on
|
2022-07-04 10:01:40 +08:00
|
|
|
if (origBlockEnd.ch == 1)
|
|
|
|
endLine = std::max(origBlockBegin.line - 1, origBlockEnd.line - 2);
|
2021-06-21 11:21:26 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
endLine = origBlockEnd.line - 1;
|
|
|
|
for (int i = origBlockBegin.line - 1; i<= endLine; i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
s = mDocument->getString(i);
|
2021-06-21 11:21:26 +08:00
|
|
|
// Find // after blanks only
|
|
|
|
int j = 0;
|
|
|
|
while ((j+1 < s.length()) && (s[j] == '\n' || s[j] == '\t'))
|
|
|
|
j++;
|
|
|
|
if ((j + 1 < s.length()) && (s[j] == '/') && (s[j + 1] == '/')) {
|
|
|
|
s.remove(j,2);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(i,s);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Delete,
|
2021-06-21 11:21:26 +08:00
|
|
|
BufferCoord{j+1, i + 1},
|
|
|
|
BufferCoord{j + 3, i + 1},
|
2022-07-04 10:01:40 +08:00
|
|
|
changeText, SynSelectionMode::Normal);
|
2021-06-21 11:21:26 +08:00
|
|
|
// Move begin of selection
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((i == origBlockBegin.line - 1) && (origBlockBegin.ch > 1))
|
|
|
|
origBlockBegin.ch-=2;
|
2021-06-21 11:21:26 +08:00
|
|
|
// Move end of selection
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((i == origBlockEnd.line - 1) && (origBlockEnd.ch > 1))
|
|
|
|
origBlockEnd.ch-=2;
|
2021-06-21 11:21:26 +08:00
|
|
|
// Move caret
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((i == origCaret.line - 1) && (origCaret.ch > 1))
|
|
|
|
origCaret.ch-=2;
|
2021-06-21 11:21:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// When grouping similar commands, process one uncomment action per undo/redo
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addGroupBreak();
|
2021-06-21 11:21:26 +08:00
|
|
|
setCaretAndSelection(origCaret,origBlockBegin,origBlockEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doToggleComment()
|
|
|
|
{
|
|
|
|
BufferCoord origBlockBegin, origBlockEnd, origCaret;
|
|
|
|
int endLine;
|
|
|
|
QString s;
|
|
|
|
bool allCommented = true;
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2021-06-21 11:21:26 +08:00
|
|
|
auto action = finally([this]{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-21 11:21:26 +08:00
|
|
|
});
|
|
|
|
origBlockBegin = blockBegin();
|
|
|
|
origBlockEnd = blockEnd();
|
|
|
|
origCaret = caretXY();
|
|
|
|
// Ignore the last line the cursor is placed on
|
2022-07-04 10:01:40 +08:00
|
|
|
if (origBlockEnd.ch == 1)
|
|
|
|
endLine = std::max(origBlockBegin.line - 1, origBlockEnd.line - 2);
|
2021-06-21 11:21:26 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
endLine = origBlockEnd.line - 1;
|
|
|
|
for (int i = origBlockBegin.line - 1; i<= endLine; i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
s = mDocument->getString(i);
|
2021-06-21 11:21:26 +08:00
|
|
|
// Find // after blanks only
|
|
|
|
int j = 0;
|
|
|
|
while ((j < s.length()) && (s[j] == '\n' || s[j] == '\t'))
|
|
|
|
j++;
|
|
|
|
if (j>= s.length())
|
|
|
|
continue;
|
|
|
|
if (s[j] != '/'){
|
|
|
|
allCommented = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j+1>=s.length()) {
|
|
|
|
allCommented = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (s[j + 1] != '/') {
|
|
|
|
allCommented = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allCommented)
|
|
|
|
doUncomment();
|
|
|
|
else
|
|
|
|
doComment();
|
|
|
|
}
|
|
|
|
|
2022-03-12 18:03:52 +08:00
|
|
|
void SynEdit::doToggleBlockComment()
|
|
|
|
{
|
|
|
|
QString s;
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
|
|
|
|
|
|
|
QString text=selText().trimmed();
|
|
|
|
if (text.length()>4 && text.startsWith("/*") && text.endsWith("*/")) {
|
|
|
|
QString newText=selText();
|
|
|
|
int pos = newText.indexOf("/*");
|
|
|
|
if (pos>=0) {
|
|
|
|
newText.remove(pos,2);
|
|
|
|
}
|
|
|
|
pos = newText.lastIndexOf("*/");
|
|
|
|
if (pos>=0) {
|
|
|
|
newText.remove(pos,2);
|
|
|
|
}
|
|
|
|
setSelText(newText);
|
|
|
|
} else {
|
|
|
|
QString newText="/*"+selText()+"*/";
|
|
|
|
setSelText(newText);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-26 19:04:04 +08:00
|
|
|
void SynEdit::doMouseScroll(bool isDragging)
|
|
|
|
{
|
2022-03-12 18:03:52 +08:00
|
|
|
if (mDropped) {
|
|
|
|
mDropped=false;
|
|
|
|
return;
|
|
|
|
}
|
2022-03-25 10:43:07 +08:00
|
|
|
if (!hasFocus())
|
|
|
|
return;
|
2022-03-24 10:55:17 +08:00
|
|
|
Qt::MouseButtons buttons = qApp->mouseButtons();
|
|
|
|
if (!buttons.testFlag(Qt::LeftButton))
|
|
|
|
return;
|
2021-11-26 19:04:04 +08:00
|
|
|
QPoint iMousePos;
|
|
|
|
DisplayCoord C;
|
|
|
|
int X, Y;
|
|
|
|
|
|
|
|
iMousePos = QCursor::pos();
|
|
|
|
iMousePos = mapFromGlobal(iMousePos);
|
2022-03-31 19:36:46 +08:00
|
|
|
C = pixelsToNearestRowColumn(iMousePos.x(), iMousePos.y());
|
2021-11-26 19:04:04 +08:00
|
|
|
C.Row = minMax(C.Row, 1, displayLineCount());
|
|
|
|
if (mScrollDeltaX != 0) {
|
2022-03-04 19:04:45 +08:00
|
|
|
setLeftChar(leftChar() + mScrollDeltaX * mMouseSelectionScrollSpeed);
|
2021-11-26 19:04:04 +08:00
|
|
|
X = leftChar();
|
|
|
|
if (mScrollDeltaX > 0) // scrolling right?
|
|
|
|
X+=charsInWindow();
|
|
|
|
C.Column = X;
|
|
|
|
}
|
|
|
|
if (mScrollDeltaY != 0) {
|
2022-03-04 19:04:45 +08:00
|
|
|
//qDebug()<<mScrollDeltaY;
|
2021-11-26 19:04:04 +08:00
|
|
|
if (QApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier))
|
|
|
|
setTopLine(mTopLine + mScrollDeltaY * mLinesInWindow);
|
|
|
|
else
|
2022-03-04 19:04:45 +08:00
|
|
|
setTopLine(mTopLine + mScrollDeltaY * mMouseSelectionScrollSpeed);
|
2021-11-26 19:04:04 +08:00
|
|
|
Y = mTopLine;
|
|
|
|
if (mScrollDeltaY > 0) // scrolling down?
|
|
|
|
Y+=mLinesInWindow - 1;
|
|
|
|
C.Row = minMax(Y, 1, displayLineCount());
|
|
|
|
}
|
|
|
|
BufferCoord vCaret = displayToBufferPos(C);
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((caretX() != vCaret.ch) || (caretY() != vCaret.line)) {
|
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column) {
|
|
|
|
int startLine=std::min(mBlockBegin.line,mBlockEnd.line);
|
|
|
|
startLine = std::min(startLine,vCaret.line);
|
|
|
|
int endLine=std::max(mBlockBegin.line,mBlockEnd.line);
|
|
|
|
endLine = std::max(endLine,vCaret.line);
|
2022-07-02 20:19:37 +08:00
|
|
|
|
|
|
|
int currentCol=displayXY().Column;
|
|
|
|
for (int i=startLine;i<=endLine;i++) {
|
|
|
|
QString s = mDocument->getString(i-1);
|
|
|
|
int cols = stringColumns(s,0);
|
|
|
|
if (cols+1<currentCol) {
|
|
|
|
computeScroll(isDragging);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-11-26 19:04:04 +08:00
|
|
|
// changes to line / column in one go
|
|
|
|
incPaintLock();
|
|
|
|
auto action = finally([this]{
|
|
|
|
decPaintLock();
|
|
|
|
});
|
|
|
|
internalSetCaretXY(vCaret);
|
|
|
|
|
|
|
|
// if MouseCapture is True we're changing selection. otherwise we're dragging
|
|
|
|
if (isDragging) {
|
|
|
|
setBlockBegin(mDragSelBeginSave);
|
|
|
|
setBlockEnd(mDragSelEndSave);
|
|
|
|
} else
|
|
|
|
setBlockEnd(caretXY());
|
2022-07-04 10:01:40 +08:00
|
|
|
|
|
|
|
if (mOptions.testFlag(SynEditorOption::eoGroupUndo))
|
|
|
|
mUndoList->addGroupBreak();
|
2021-11-26 19:04:04 +08:00
|
|
|
}
|
2022-03-07 20:30:45 +08:00
|
|
|
computeScroll(isDragging);
|
2021-11-26 19:04:04 +08:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:46:20 +08:00
|
|
|
QString SynEdit::getDisplayStringAtLine(int line) const
|
|
|
|
{
|
|
|
|
QString s = mDocument->getString(line-1);
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(line);
|
|
|
|
if ((foldRange) && foldRange->collapsed) {
|
|
|
|
return s+highlighter()->foldString();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDeleteLastChar()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-10-19 09:05:11 +08:00
|
|
|
if (mReadOnly)
|
|
|
|
return ;
|
2021-05-29 21:35:46 +08:00
|
|
|
doOnPaintTransientEx(SynTransientType::ttBefore, true);
|
|
|
|
auto action = finally([this]{
|
|
|
|
ensureCursorPosVisible();
|
|
|
|
doOnPaintTransientEx(SynTransientType::ttAfter, true);
|
|
|
|
});
|
2021-10-19 09:05:11 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
2022-03-25 22:13:00 +08:00
|
|
|
BufferCoord start=blockBegin();
|
|
|
|
BufferCoord end=blockEnd();
|
|
|
|
if (!selAvail()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
start.ch--;
|
2022-03-25 22:13:00 +08:00
|
|
|
setBlockBegin(start);
|
|
|
|
setBlockEnd(end);
|
|
|
|
}
|
|
|
|
setSelectedTextEmpty();
|
|
|
|
return;
|
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
if (selAvail()) {
|
2021-09-20 15:57:48 +08:00
|
|
|
setSelectedTextEmpty();
|
2021-05-29 21:35:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-07-02 17:59:07 +08:00
|
|
|
bool shouldAddGroupBreak=false;
|
2021-05-29 21:35:46 +08:00
|
|
|
QString Temp = lineText();
|
|
|
|
int Len = Temp.length();
|
|
|
|
BufferCoord Caret = caretXY();
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList helper;
|
2021-05-29 21:35:46 +08:00
|
|
|
if (mCaretX > Len + 1) {
|
2022-07-02 10:39:31 +08:00
|
|
|
// only move caret one column
|
2022-07-02 20:19:37 +08:00
|
|
|
return;
|
2021-05-29 21:35:46 +08:00
|
|
|
} else if (mCaretX == 1) {
|
|
|
|
// join this line with the last line if possible
|
|
|
|
if (mCaretY > 1) {
|
|
|
|
internalSetCaretY(mCaretY - 1);
|
2022-04-19 21:18:41 +08:00
|
|
|
internalSetCaretX(mDocument->getString(mCaretY - 1).length() + 1);
|
|
|
|
mDocument->deleteAt(mCaretY);
|
2021-09-02 12:14:02 +08:00
|
|
|
doLinesDeleted(mCaretY+1, 1);
|
2021-05-29 21:35:46 +08:00
|
|
|
if (mOptions.testFlag(eoTrimTrailingSpaces))
|
2021-11-24 17:53:25 +08:00
|
|
|
Temp = trimRight(Temp);
|
2021-05-29 21:35:46 +08:00
|
|
|
setLineText(lineText() + Temp);
|
2022-07-02 10:39:31 +08:00
|
|
|
helper.append("");
|
|
|
|
helper.append("");
|
2022-07-02 17:59:07 +08:00
|
|
|
shouldAddGroupBreak=true;
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// delete text before the caret
|
|
|
|
int caretColumn = charToColumn(mCaretY,mCaretX);
|
|
|
|
int SpaceCount1 = leftSpaces(Temp);
|
|
|
|
int SpaceCount2 = 0;
|
|
|
|
int newCaretX;
|
|
|
|
|
|
|
|
if (SpaceCount1 == caretColumn - 1) {
|
|
|
|
//how much till the next tab column
|
2022-04-19 21:18:41 +08:00
|
|
|
int BackCounter = (caretColumn - 1) % tabWidth();
|
2021-05-29 21:35:46 +08:00
|
|
|
if (BackCounter == 0)
|
2022-04-19 21:18:41 +08:00
|
|
|
BackCounter = tabWidth();
|
|
|
|
SpaceCount2 = std::max(0,SpaceCount1 - tabWidth());
|
2021-05-29 21:35:46 +08:00
|
|
|
newCaretX = columnToChar(mCaretY,SpaceCount2+1);
|
2022-07-02 10:39:31 +08:00
|
|
|
helper.append(Temp.mid(newCaretX - 1, mCaretX - newCaretX));
|
2021-05-29 21:35:46 +08:00
|
|
|
Temp.remove(newCaretX-1,mCaretX - newCaretX);
|
2021-09-20 15:57:48 +08:00
|
|
|
properSetLine(mCaretY - 1, Temp);
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretX(newCaretX);
|
2021-05-29 21:35:46 +08:00
|
|
|
} else {
|
|
|
|
// delete char
|
|
|
|
internalSetCaretX(mCaretX - 1);
|
2022-07-02 17:59:07 +08:00
|
|
|
QChar ch=Temp[mCaretX-1];
|
|
|
|
if (ch==' ' || ch=='\t')
|
|
|
|
shouldAddGroupBreak=true;
|
|
|
|
helper.append(QString(ch));
|
2021-05-29 21:35:46 +08:00
|
|
|
Temp.remove(mCaretX-1,1);
|
2021-09-20 15:57:48 +08:00
|
|
|
properSetLine(mCaretY - 1, Temp);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((Caret.ch != mCaretX) || (Caret.line != mCaretY)) {
|
|
|
|
mUndoList->addChange(SynChangeReason::Delete, caretXY(), Caret, helper,
|
2022-07-02 17:59:07 +08:00
|
|
|
mActiveSelectionMode);
|
|
|
|
if (shouldAddGroupBreak)
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addGroupBreak();
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDeleteCurrentChar()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList helper;
|
2021-06-03 20:26:36 +08:00
|
|
|
BufferCoord Caret;
|
2022-07-02 17:59:07 +08:00
|
|
|
if (mReadOnly) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
|
|
|
auto action = finally([this]{
|
|
|
|
ensureCursorPosVisible();
|
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
});
|
2022-03-25 22:13:00 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
2022-07-02 17:59:07 +08:00
|
|
|
BufferCoord start=blockBegin();
|
|
|
|
BufferCoord end=blockEnd();
|
|
|
|
if (!selAvail()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
end.ch++;
|
2022-07-02 17:59:07 +08:00
|
|
|
setBlockBegin(start);
|
|
|
|
setBlockEnd(end);
|
|
|
|
}
|
|
|
|
setSelectedTextEmpty();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (selAvail())
|
|
|
|
setSelectedTextEmpty();
|
|
|
|
else {
|
|
|
|
bool shouldAddGroupBreak=false;
|
|
|
|
// Call UpdateLastCaretX. Even though the caret doesn't move, the
|
|
|
|
// current caret position should "stick" whenever text is modified.
|
|
|
|
updateLastCaretX();
|
|
|
|
QString Temp = lineText();
|
|
|
|
int Len = Temp.length();
|
2022-07-02 20:19:37 +08:00
|
|
|
if (mCaretX>Len+1) {
|
|
|
|
return;
|
|
|
|
} else if (mCaretX <= Len) {
|
2022-07-02 17:59:07 +08:00
|
|
|
QChar ch = Temp[mCaretX-1];
|
|
|
|
if (ch==' ' || ch=='\t')
|
|
|
|
shouldAddGroupBreak=true;
|
|
|
|
// delete char
|
|
|
|
helper.append(QString(ch));
|
2022-07-04 10:01:40 +08:00
|
|
|
Caret.ch = mCaretX + 1;
|
|
|
|
Caret.line = mCaretY;
|
2022-07-02 17:59:07 +08:00
|
|
|
Temp.remove(mCaretX-1, 1);
|
|
|
|
properSetLine(mCaretY - 1, Temp);
|
2022-03-25 22:13:00 +08:00
|
|
|
} else {
|
2022-07-02 17:59:07 +08:00
|
|
|
// join line with the line after
|
|
|
|
if (mCaretY < mDocument->count()) {
|
|
|
|
shouldAddGroupBreak=true;
|
|
|
|
properSetLine(mCaretY - 1, Temp + mDocument->getString(mCaretY));
|
2022-07-04 10:01:40 +08:00
|
|
|
Caret.ch = 1;
|
|
|
|
Caret.line = mCaretY + 1;
|
2022-07-02 17:59:07 +08:00
|
|
|
helper.append("");
|
|
|
|
helper.append("");
|
|
|
|
mDocument->deleteAt(mCaretY);
|
|
|
|
if (mCaretX==1)
|
|
|
|
doLinesDeleted(mCaretY, 1);
|
|
|
|
else
|
|
|
|
doLinesDeleted(mCaretY + 1, 1);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((Caret.ch != mCaretX) || (Caret.line != mCaretY)) {
|
|
|
|
mUndoList->addChange(SynChangeReason::Delete, caretXY(), Caret,
|
2022-07-02 17:59:07 +08:00
|
|
|
helper, mActiveSelectionMode);
|
|
|
|
if (shouldAddGroupBreak)
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addGroupBreak();
|
2022-07-02 17:59:07 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDeleteWord()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-07-02 20:19:37 +08:00
|
|
|
if (mCaretX>lineText().length()+1)
|
|
|
|
return;
|
2021-06-03 20:26:36 +08:00
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord start = wordStart();
|
|
|
|
BufferCoord end = wordEnd();
|
|
|
|
deleteFromTo(start,end);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDeleteToEOL()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-07-02 20:19:37 +08:00
|
|
|
if (mCaretX>lineText().length()+1)
|
|
|
|
return;
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
deleteFromTo(caretXY(),BufferCoord{lineText().length()+1,mCaretY});
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 11:23:30 +08:00
|
|
|
void SynEdit::doDeleteToWordStart()
|
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-07-02 20:19:37 +08:00
|
|
|
if (mCaretX>lineText().length()+1)
|
|
|
|
return;
|
|
|
|
|
2022-01-24 11:23:30 +08:00
|
|
|
BufferCoord start = wordStart();
|
|
|
|
BufferCoord end = caretXY();
|
|
|
|
if (start==end) {
|
|
|
|
start = prevWordPos();
|
|
|
|
}
|
|
|
|
deleteFromTo(start,end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doDeleteToWordEnd()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-07-02 20:19:37 +08:00
|
|
|
if (mCaretX>lineText().length()+1)
|
|
|
|
return;
|
|
|
|
|
2022-01-24 11:23:30 +08:00
|
|
|
BufferCoord start = caretXY();
|
|
|
|
BufferCoord end = wordEnd();
|
|
|
|
if (start == end) {
|
|
|
|
end = wordEndEx(nextWordPos());
|
|
|
|
}
|
2021-09-20 15:57:48 +08:00
|
|
|
deleteFromTo(start,end);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDeleteFromBOL()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-07-02 20:19:37 +08:00
|
|
|
if (mCaretX>lineText().length()+1)
|
|
|
|
return;
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
deleteFromTo(BufferCoord{1,mCaretY},caretXY());
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDeleteLine()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-07-02 14:06:10 +08:00
|
|
|
if (!mReadOnly && (mDocument->count() > 0)) {
|
2022-07-02 20:19:37 +08:00
|
|
|
PSynEditFoldRange foldRange=foldStartAtLine(mCaretY);
|
|
|
|
if (foldRange && foldRange->collapsed)
|
|
|
|
return;
|
2021-06-03 20:26:36 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
2021-06-03 20:26:36 +08:00
|
|
|
if (selAvail())
|
|
|
|
setBlockBegin(caretXY());
|
2022-07-02 14:06:10 +08:00
|
|
|
QStringList helper(lineText());
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY == mDocument->count()) {
|
2022-07-02 14:06:10 +08:00
|
|
|
if (mDocument->count()==1) {
|
|
|
|
mDocument->putString(mCaretY - 1,"");
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Delete,
|
2022-07-02 14:06:10 +08:00
|
|
|
BufferCoord{1, mCaretY},
|
|
|
|
BufferCoord{helper.length() + 1, mCaretY},
|
2022-07-04 10:01:40 +08:00
|
|
|
helper, SynSelectionMode::Normal);
|
2022-07-02 14:06:10 +08:00
|
|
|
} else {
|
|
|
|
QString s = mDocument->getString(mCaretY-2);
|
|
|
|
mDocument->deleteAt(mCaretY - 1);
|
|
|
|
helper.insert(0,"");
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Delete,
|
2022-07-02 14:06:10 +08:00
|
|
|
BufferCoord{s.length()+1, mCaretY-1},
|
|
|
|
BufferCoord{helper.length() + 1, mCaretY},
|
2022-07-04 10:01:40 +08:00
|
|
|
helper, SynSelectionMode::Normal);
|
2022-07-02 14:06:10 +08:00
|
|
|
doLinesDeleted(mCaretY, 1);
|
|
|
|
mCaretY--;
|
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->deleteAt(mCaretY - 1);
|
2022-07-02 10:39:31 +08:00
|
|
|
helper.append("");
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Delete,
|
2021-06-03 20:26:36 +08:00
|
|
|
BufferCoord{1, mCaretY},
|
|
|
|
BufferCoord{helper.length() + 1, mCaretY},
|
2022-07-04 10:01:40 +08:00
|
|
|
helper, SynSelectionMode::Normal);
|
2021-09-02 12:14:02 +08:00
|
|
|
doLinesDeleted(mCaretY, 1);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
internalSetCaretXY(BufferCoord{1, mCaretY}); // like seen in the Delphi editor
|
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:37:00 +08:00
|
|
|
void SynEdit::doSelecteLine()
|
|
|
|
{
|
|
|
|
setBlockBegin(BufferCoord{1,mCaretY});
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY==mDocument->count())
|
2022-03-14 23:55:02 +08:00
|
|
|
setBlockEnd(BufferCoord{lineText().length()+1,mCaretY});
|
|
|
|
else
|
|
|
|
setBlockEnd(BufferCoord{1,mCaretY+1});
|
2022-01-10 18:37:00 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doDuplicateLine()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (!mReadOnly && (mDocument->count() > 0)) {
|
2022-07-02 20:19:37 +08:00
|
|
|
PSynEditFoldRange foldRange=foldStartAtLine(mCaretY);
|
|
|
|
if (foldRange && foldRange->collapsed)
|
|
|
|
return;
|
2022-07-02 10:39:31 +08:00
|
|
|
QString s = lineText();
|
2021-06-03 20:26:36 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->insert(mCaretY, lineText());
|
2021-09-02 12:14:02 +08:00
|
|
|
doLinesInserted(mCaretY + 1, 1);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
mUndoList->addChange(SynChangeReason::LineBreak,
|
2022-07-02 14:06:10 +08:00
|
|
|
BufferCoord{s.length()+1,mCaretY},
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord{s.length()+1,mCaretY}, QStringList(), SynSelectionMode::Normal);
|
|
|
|
mUndoList->addChange(SynChangeReason::Insert,
|
2022-07-02 14:06:10 +08:00
|
|
|
BufferCoord{1,mCaretY+1},
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord{s.length()+1,mCaretY+1}, QStringList(), SynSelectionMode::Normal);
|
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
internalSetCaretXY(BufferCoord{1, mCaretY}); // like seen in the Delphi editor
|
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
void SynEdit::doMoveSelUp()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column)
|
2022-07-02 20:19:37 +08:00
|
|
|
return;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (!mReadOnly && (mDocument->count() > 0) && (blockBegin().line > 1)) {
|
|
|
|
if (!mUndoing) {
|
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
|
|
|
}
|
2022-07-02 20:19:37 +08:00
|
|
|
BufferCoord origBlockBegin = blockBegin();
|
|
|
|
BufferCoord origBlockEnd = blockEnd();
|
2022-07-04 10:01:40 +08:00
|
|
|
PSynEditFoldRange foldRange=foldStartAtLine(origBlockEnd.line);
|
2022-07-02 20:19:37 +08:00
|
|
|
if (foldRange && foldRange->collapsed)
|
|
|
|
return;
|
|
|
|
// for (int line=origBlockBegin.Line;line<=origBlockEnd.Line;line++) {
|
|
|
|
// PSynEditFoldRange foldRange=foldStartAtLine(line);
|
|
|
|
// if (foldRange && foldRange->collapsed)
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
|
|
|
|
|
|
|
// Delete line above selection
|
2022-07-04 10:01:40 +08:00
|
|
|
QString s = mDocument->getString(origBlockBegin.line - 2); // before start, 0 based
|
|
|
|
mDocument->deleteAt(origBlockBegin.line - 2); // before start, 0 based
|
|
|
|
doLinesDeleted(origBlockBegin.line - 1, 1); // before start, 1 based
|
2021-06-03 20:26:36 +08:00
|
|
|
|
|
|
|
// Insert line below selection
|
2022-07-04 10:01:40 +08:00
|
|
|
mDocument->insert(origBlockEnd.line - 1, s);
|
|
|
|
doLinesInserted(origBlockEnd.line, 1);
|
2021-06-03 20:26:36 +08:00
|
|
|
// Restore caret and selection
|
|
|
|
setCaretAndSelection(
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord{mCaretX, origBlockBegin.line - 1},
|
|
|
|
BufferCoord{origBlockBegin.ch, origBlockBegin.line - 1},
|
|
|
|
BufferCoord{origBlockEnd.ch, origBlockEnd.line - 1}
|
2021-06-03 20:26:36 +08:00
|
|
|
);
|
2022-07-04 10:01:40 +08:00
|
|
|
if (!mUndoing) {
|
|
|
|
mUndoList->addChange(SynChangeReason::MoveSelectionUp,
|
|
|
|
origBlockBegin,
|
|
|
|
origBlockEnd,
|
|
|
|
QStringList(),
|
|
|
|
SynSelectionMode::Normal);
|
|
|
|
mUndoList->endBlock();
|
|
|
|
}
|
2022-04-01 23:10:38 +08:00
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
void SynEdit::doMoveSelDown()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column)
|
2022-07-02 20:19:37 +08:00
|
|
|
return;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (!mReadOnly && (mDocument->count() > 0) && (blockEnd().line < mDocument->count())) {
|
|
|
|
if (!mUndoing) {
|
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
|
|
|
}
|
2022-04-01 23:10:38 +08:00
|
|
|
BufferCoord origBlockBegin = blockBegin();
|
|
|
|
BufferCoord origBlockEnd = blockEnd();
|
2022-07-04 10:01:40 +08:00
|
|
|
|
|
|
|
PSynEditFoldRange foldRange=foldStartAtLine(origBlockEnd.line);
|
2022-07-02 20:19:37 +08:00
|
|
|
if (foldRange && foldRange->collapsed)
|
|
|
|
return;
|
2022-07-04 10:01:40 +08:00
|
|
|
|
2022-07-02 20:19:37 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2021-06-03 20:26:36 +08:00
|
|
|
|
|
|
|
// Delete line below selection
|
2022-07-04 10:01:40 +08:00
|
|
|
QString s = mDocument->getString(origBlockEnd.line); // after end, 0 based
|
|
|
|
mDocument->deleteAt(origBlockEnd.line); // after end, 0 based
|
|
|
|
doLinesDeleted(origBlockEnd.line, 1); // before start, 1 based
|
2021-06-03 20:26:36 +08:00
|
|
|
|
|
|
|
// Insert line above selection
|
2022-07-04 10:01:40 +08:00
|
|
|
mDocument->insert(origBlockBegin.line - 1, s);
|
|
|
|
doLinesInserted(origBlockBegin.line, 1);
|
2021-06-03 20:26:36 +08:00
|
|
|
|
|
|
|
// Restore caret and selection
|
|
|
|
setCaretAndSelection(
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord{mCaretX, origBlockEnd.line + 1},
|
|
|
|
BufferCoord{origBlockBegin.ch, origBlockBegin.line + 1},
|
|
|
|
BufferCoord{origBlockEnd.ch, origBlockEnd.line + 1}
|
2021-06-03 20:26:36 +08:00
|
|
|
);
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (!mUndoing) {
|
|
|
|
mUndoList->addChange(SynChangeReason::MoveSelectionDown,
|
|
|
|
origBlockBegin,
|
|
|
|
origBlockEnd,
|
|
|
|
QStringList(),
|
|
|
|
SynSelectionMode::Normal);
|
|
|
|
mUndoList->endBlock();
|
|
|
|
}
|
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::clearAll()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->clear();
|
2021-06-03 20:26:36 +08:00
|
|
|
mMarkList.clear();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->clear();
|
|
|
|
mRedoList->clear();
|
2021-06-03 20:26:36 +08:00
|
|
|
setModified(false);
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::insertLine(bool moveCaret)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
int nLinesInserted=0;
|
2022-07-03 13:09:42 +08:00
|
|
|
if (!mUndoing)
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
auto action = finally([this] {
|
2022-07-03 13:09:42 +08:00
|
|
|
if (!mUndoing)
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
});
|
|
|
|
QString helper;
|
|
|
|
if (selAvail()) {
|
|
|
|
helper = selText();
|
2022-07-02 10:39:31 +08:00
|
|
|
setSelectedTextEmpty();
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2021-09-01 16:17:31 +08:00
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
QString Temp = lineText();
|
2022-07-02 20:19:37 +08:00
|
|
|
|
2022-06-28 21:46:20 +08:00
|
|
|
if (mCaretX>lineText().length()+1) {
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(mCaretY);
|
|
|
|
if ((foldRange) && foldRange->collapsed) {
|
|
|
|
QString s = Temp+highlighter()->foldString();
|
|
|
|
if (mCaretX > s.length()) {
|
2022-07-02 20:19:37 +08:00
|
|
|
if (!mUndoing) {
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
|
|
|
}
|
2022-06-28 21:46:20 +08:00
|
|
|
mCaretY=foldRange->toLine;
|
|
|
|
if (mCaretY>mDocument->count()) {
|
|
|
|
mCaretY=mDocument->count();
|
|
|
|
}
|
|
|
|
Temp = lineText();
|
|
|
|
mCaretX=Temp.length()+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 20:19:37 +08:00
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
QString Temp2 = Temp;
|
|
|
|
QString Temp3;
|
|
|
|
PSynHighlighterAttribute Attr;
|
|
|
|
|
|
|
|
// This is sloppy, but the Right Thing would be to track the column of markers
|
|
|
|
// too, so they could be moved depending on whether they are after the caret...
|
|
|
|
int InsDelta = (mCaretX == 1)?1:0;
|
2021-10-28 09:20:58 +08:00
|
|
|
QString leftLineText = lineText().mid(0, mCaretX - 1);
|
|
|
|
QString rightLineText = lineText().mid(mCaretX-1);
|
2022-07-04 10:01:40 +08:00
|
|
|
if (!mUndoing)
|
|
|
|
mUndoList->addChange(SynChangeReason::LineBreak, caretXY(), caretXY(), QStringList(rightLineText),
|
|
|
|
SynSelectionMode::Normal);
|
2021-10-28 09:20:58 +08:00
|
|
|
bool notInComment=true;
|
|
|
|
properSetLine(mCaretY-1,leftLineText);
|
|
|
|
//update range stated for line mCaretY
|
|
|
|
if (mHighlighter) {
|
|
|
|
if (mCaretY==1) {
|
|
|
|
mHighlighter->resetState();
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setState(mDocument->ranges(mCaretY-2));
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2021-10-28 09:20:58 +08:00
|
|
|
mHighlighter->setLine(leftLineText, mCaretY-1);
|
|
|
|
mHighlighter->nextToEol();
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->setRange(mCaretY-1,mHighlighter->getRangeState());
|
2021-10-28 23:46:54 +08:00
|
|
|
notInComment = !mHighlighter->isLastLineCommentNotFinished(
|
|
|
|
mHighlighter->getRangeState().state)
|
|
|
|
&& !mHighlighter->isLastLineStringNotFinished(
|
|
|
|
mHighlighter->getRangeState().state);
|
2021-10-28 09:20:58 +08:00
|
|
|
}
|
2022-03-02 11:31:03 +08:00
|
|
|
int indentSpaces = 0;
|
2021-10-28 09:20:58 +08:00
|
|
|
if (mOptions.testFlag(eoAutoIndent)) {
|
2021-11-24 17:53:25 +08:00
|
|
|
rightLineText=trimLeft(rightLineText);
|
2022-03-02 11:31:03 +08:00
|
|
|
indentSpaces = calcIndentSpaces(mCaretY+1,
|
|
|
|
rightLineText,mOptions.testFlag(eoAutoIndent)
|
2022-06-28 09:37:18 +08:00
|
|
|
);
|
2021-10-28 09:20:58 +08:00
|
|
|
}
|
|
|
|
QString indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->insert(mCaretY, indentSpacesForRightLineText+rightLineText);
|
2021-10-28 09:20:58 +08:00
|
|
|
nLinesInserted++;
|
2022-01-25 12:29:44 +08:00
|
|
|
|
2022-03-02 11:31:03 +08:00
|
|
|
if (!mUndoing) {
|
|
|
|
//insert new line in middle of "/*" and "*/"
|
|
|
|
if (!notInComment &&
|
|
|
|
( leftLineText.endsWith("/*") && rightLineText.startsWith("*/")
|
|
|
|
)) {
|
2022-06-28 09:37:18 +08:00
|
|
|
indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent));
|
2022-03-02 11:31:03 +08:00
|
|
|
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->insert(mCaretY, indentSpacesForRightLineText);
|
2022-03-02 11:31:03 +08:00
|
|
|
nLinesInserted++;
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::LineBreak, caretXY(), caretXY(), QStringList(),
|
|
|
|
SynSelectionMode::Normal);
|
2022-03-02 11:31:03 +08:00
|
|
|
}
|
|
|
|
//insert new line in middle of "{" and "}"
|
|
|
|
if (notInComment &&
|
|
|
|
( leftLineText.endsWith('{') && rightLineText.startsWith('}')
|
|
|
|
)) {
|
|
|
|
indentSpaces = calcIndentSpaces(mCaretY+1, "" , mOptions.testFlag(eoAutoIndent)
|
|
|
|
&& notInComment);
|
|
|
|
indentSpacesForRightLineText = GetLeftSpacing(indentSpaces,true);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->insert(mCaretY, indentSpacesForRightLineText);
|
2022-03-02 11:31:03 +08:00
|
|
|
nLinesInserted++;
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::LineBreak, caretXY(), caretXY(), QStringList(),
|
|
|
|
SynSelectionMode::Normal);
|
2022-03-02 11:31:03 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2021-10-28 09:20:58 +08:00
|
|
|
if (moveCaret)
|
|
|
|
internalSetCaretXY(BufferCoord{indentSpacesForRightLineText.length()+1,mCaretY + 1});
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
doLinesInserted(mCaretY - InsDelta, nLinesInserted);
|
2021-06-03 20:26:36 +08:00
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
|
|
|
ensureCursorPosVisible();
|
|
|
|
updateLastCaretX();
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doTabKey()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column) {
|
2022-03-26 09:54:06 +08:00
|
|
|
doAddChar('\t');
|
|
|
|
return;
|
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
// Provide Visual Studio like block indenting
|
2021-09-20 15:57:48 +08:00
|
|
|
if (mOptions.testFlag(eoTabIndent) && canDoBlockIndent()) {
|
2021-06-03 20:26:36 +08:00
|
|
|
doBlockIndent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
auto action = finally([this]{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
});
|
|
|
|
if (selAvail()) {
|
2022-07-02 10:39:31 +08:00
|
|
|
setSelectedTextEmpty();
|
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
QString Spaces;
|
|
|
|
int NewCaretX = 0;
|
|
|
|
if (mOptions.testFlag(eoTabsToSpaces)) {
|
|
|
|
int cols = charToColumn(mCaretY,mCaretX);
|
2022-04-19 21:18:41 +08:00
|
|
|
i = tabWidth() - (cols) % tabWidth();
|
2021-06-04 21:27:51 +08:00
|
|
|
Spaces = QString(i,' ');
|
2021-06-03 20:26:36 +08:00
|
|
|
NewCaretX = mCaretX + i;
|
|
|
|
} else {
|
|
|
|
Spaces = '\t';
|
2021-06-05 23:43:45 +08:00
|
|
|
NewCaretX = mCaretX + 1;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
setSelTextPrimitive(QStringList(Spaces));
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
ensureCursorPosVisible();
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doShiftTabKey()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
// Provide Visual Studio like block indenting
|
2021-09-20 15:57:48 +08:00
|
|
|
if (mOptions.testFlag(eoTabIndent) && canDoBlockIndent()) {
|
2021-06-03 23:18:51 +08:00
|
|
|
doBlockUnindent();
|
2021-06-03 20:26:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Don't un-tab if caret is not on line or is beyond line end
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY > mDocument->count() || mCaretX > lineText().length()+1)
|
2021-06-03 20:26:36 +08:00
|
|
|
return;
|
|
|
|
//Don't un-tab if no chars before the Caret
|
|
|
|
if (mCaretX==1)
|
|
|
|
return;
|
|
|
|
QString s = lineText().mid(0,mCaretX-1);
|
|
|
|
//Only un-tab if caret is at the begin of the line
|
|
|
|
if (!s.trimmed().isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int NewX = 0;
|
|
|
|
if (s[s.length()-1] == '\t') {
|
|
|
|
NewX= mCaretX-1;
|
|
|
|
} else {
|
|
|
|
int colsBefore = charToColumn(mCaretY,mCaretX)-1;
|
2022-04-19 21:18:41 +08:00
|
|
|
int spacesToRemove = colsBefore % tabWidth();
|
2021-06-03 20:26:36 +08:00
|
|
|
if (spacesToRemove == 0)
|
2022-04-19 21:18:41 +08:00
|
|
|
spacesToRemove = tabWidth();
|
2021-06-03 20:26:36 +08:00
|
|
|
if (spacesToRemove > colsBefore )
|
|
|
|
spacesToRemove = colsBefore;
|
|
|
|
NewX = mCaretX;
|
|
|
|
while (spacesToRemove > 0 && s[NewX-2] == ' ' ) {
|
|
|
|
NewX--;
|
|
|
|
spacesToRemove--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// perform un-tab
|
|
|
|
|
|
|
|
if (NewX != mCaretX) {
|
2022-07-02 10:39:31 +08:00
|
|
|
doDeleteText(BufferCoord{NewX, mCaretY},caretXY(),mActiveSelectionMode);
|
2021-06-03 20:26:36 +08:00
|
|
|
internalSetCaretX(NewX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
bool SynEdit::canDoBlockIndent()
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
BufferCoord BB;
|
|
|
|
BufferCoord BE;
|
|
|
|
|
|
|
|
if (selAvail()) {
|
2021-11-30 20:43:58 +08:00
|
|
|
// BB = blockBegin();
|
|
|
|
// BE = blockEnd();
|
|
|
|
return true;
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
|
|
|
BB = caretXY();
|
|
|
|
BE = caretXY();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (BB.line > mDocument->count() || BE.line > mDocument->count()) {
|
2021-06-03 20:26:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Normal) {
|
|
|
|
QString s = mDocument->getString(BB.line-1).mid(0,BB.ch-1);
|
2021-06-03 20:26:36 +08:00
|
|
|
if (!s.trimmed().isEmpty())
|
|
|
|
return false;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (BE.ch>1) {
|
|
|
|
QString s1=mDocument->getString(BE.line-1).mid(BE.ch-1);
|
|
|
|
QString s2=mDocument->getString(BE.line-1).mid(0,BE.ch-1);
|
2021-06-03 20:26:36 +08:00
|
|
|
if (!s1.trimmed().isEmpty() && !s2.trimmed().isEmpty())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column) {
|
|
|
|
int startCol = charToColumn(BB.line,BB.ch);
|
|
|
|
int endCol = charToColumn(BE.line,BE.ch);
|
|
|
|
for (int i = BB.line; i<=BE.line;i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString line = mDocument->getString(i-1);
|
2021-06-03 20:26:36 +08:00
|
|
|
int startChar = columnToChar(i,startCol);
|
|
|
|
QString s = line.mid(0,startChar-1);
|
|
|
|
if (!s.trimmed().isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int endChar = columnToChar(i,endCol);
|
|
|
|
s=line.mid(endChar-1);
|
|
|
|
if (!s.trimmed().isEmpty())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:19:48 +08:00
|
|
|
QRect SynEdit::calculateCaretRect() const
|
2021-10-05 00:42:35 +08:00
|
|
|
{
|
|
|
|
DisplayCoord coord = displayXY();
|
|
|
|
if (!mInputPreeditString.isEmpty()) {
|
|
|
|
QString sLine = lineText().left(mCaretX-1)
|
|
|
|
+ mInputPreeditString
|
|
|
|
+ lineText().mid(mCaretX-1);
|
|
|
|
coord.Column = charToColumn(sLine,mCaretX+mInputPreeditString.length());
|
|
|
|
}
|
2022-05-19 12:32:33 +08:00
|
|
|
int rows=1;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column) {
|
|
|
|
int startRow = lineToRow(std::min(blockBegin().line, blockEnd().line));
|
|
|
|
int endRow = lineToRow(std::max(blockBegin().line, blockEnd().line));
|
2022-05-19 12:32:33 +08:00
|
|
|
coord.Row = startRow;
|
|
|
|
rows = endRow-startRow+1;
|
2022-03-25 22:13:00 +08:00
|
|
|
}
|
2021-10-05 00:42:35 +08:00
|
|
|
QPoint caretPos = rowColumnToPixels(coord);
|
|
|
|
int caretWidth=mCharWidth;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY <= mDocument->count() && mCaretX <= mDocument->getString(mCaretY-1).length()) {
|
2022-06-28 21:46:20 +08:00
|
|
|
caretWidth = charColumns(getDisplayStringAtLine(mCaretY)[mCaretX-1])*mCharWidth;
|
2021-10-05 00:42:35 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column) {
|
2022-03-25 22:13:00 +08:00
|
|
|
return QRect(caretPos.x(),caretPos.y(),caretWidth,
|
2022-05-19 12:32:33 +08:00
|
|
|
mTextHeight*(rows));
|
2022-03-25 22:13:00 +08:00
|
|
|
} else {
|
|
|
|
return QRect(caretPos.x(),caretPos.y(),caretWidth,
|
|
|
|
mTextHeight);
|
|
|
|
}
|
2021-11-08 21:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QRect SynEdit::calculateInputCaretRect() const
|
|
|
|
{
|
|
|
|
DisplayCoord coord = displayXY();
|
|
|
|
QPoint caretPos = rowColumnToPixels(coord);
|
|
|
|
int caretWidth=mCharWidth;
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY <= mDocument->count() && mCaretX <= mDocument->getString(mCaretY-1).length()) {
|
|
|
|
caretWidth = charColumns(mDocument->getString(mCaretY-1)[mCaretX-1])*mCharWidth;
|
2021-11-08 21:19:48 +08:00
|
|
|
}
|
|
|
|
return QRect(caretPos.x(),caretPos.y(),caretWidth,
|
|
|
|
mTextHeight);
|
2021-10-05 00:42:35 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::clearAreaList(SynEditingAreaList areaList)
|
|
|
|
{
|
|
|
|
areaList.clear();
|
|
|
|
}
|
|
|
|
|
2022-03-07 20:30:45 +08:00
|
|
|
void SynEdit::computeCaret()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-03-07 20:30:45 +08:00
|
|
|
QPoint iMousePos = QCursor::pos();
|
|
|
|
iMousePos = mapFromGlobal(iMousePos);
|
|
|
|
int X=iMousePos.x();
|
|
|
|
int Y=iMousePos.y();
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord oldCaret = caretXY();
|
2022-03-31 19:36:46 +08:00
|
|
|
DisplayCoord vCaretNearestPos = pixelsToNearestRowColumn(X, Y);
|
2021-09-11 18:42:49 +08:00
|
|
|
vCaretNearestPos.Row = minMax(vCaretNearestPos.Row, 1, displayLineCount());
|
2021-05-14 23:56:43 +08:00
|
|
|
setInternalDisplayXY(vCaretNearestPos);
|
|
|
|
}
|
|
|
|
|
2022-03-07 20:30:45 +08:00
|
|
|
void SynEdit::computeScroll(bool isDragging)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-03-07 20:30:45 +08:00
|
|
|
QPoint iMousePos = QCursor::pos();
|
|
|
|
iMousePos = mapFromGlobal(iMousePos);
|
|
|
|
int X=iMousePos.x();
|
|
|
|
int Y=iMousePos.y();
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
QRect iScrollBounds; // relative to the client area
|
2021-11-26 19:04:04 +08:00
|
|
|
int dispX=2,dispY = 2;
|
2022-03-04 19:04:45 +08:00
|
|
|
// if (isDragging) {
|
|
|
|
// dispX = mCharWidth / 2 -1;
|
|
|
|
// dispY = mTextHeight/ 2 -1;
|
|
|
|
// }
|
2021-11-26 19:04:04 +08:00
|
|
|
int left = mGutterWidth+frameWidth()+dispX;
|
|
|
|
int top = frameWidth()+dispY;
|
|
|
|
iScrollBounds = QRect(left,
|
|
|
|
top,
|
|
|
|
clientWidth()-left-dispX,
|
|
|
|
clientHeight()-top-dispY);
|
2021-05-14 23:56:43 +08:00
|
|
|
|
|
|
|
if (X < iScrollBounds.left())
|
2021-05-24 00:41:00 +08:00
|
|
|
mScrollDeltaX = (X - iScrollBounds.left()) / mCharWidth - 1;
|
2021-05-14 23:56:43 +08:00
|
|
|
else if (X >= iScrollBounds.right())
|
2021-05-24 00:41:00 +08:00
|
|
|
mScrollDeltaX = (X - iScrollBounds.right()) / mCharWidth + 1;
|
2021-05-14 23:56:43 +08:00
|
|
|
else
|
|
|
|
mScrollDeltaX = 0;
|
|
|
|
|
2022-03-04 19:10:09 +08:00
|
|
|
// if (isDragging && (X<0 || X>clientRect().width())) {
|
|
|
|
// mScrollDeltaX = 0;
|
|
|
|
// }
|
2021-11-26 19:04:04 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
if (Y < iScrollBounds.top())
|
2021-05-24 00:41:00 +08:00
|
|
|
mScrollDeltaY = (Y - iScrollBounds.top()) / mTextHeight - 1;
|
2021-05-14 23:56:43 +08:00
|
|
|
else if (Y >= iScrollBounds.bottom())
|
2021-05-24 00:41:00 +08:00
|
|
|
mScrollDeltaY = (Y - iScrollBounds.bottom()) / mTextHeight + 1;
|
2021-10-19 22:41:38 +08:00
|
|
|
else
|
2021-05-14 23:56:43 +08:00
|
|
|
mScrollDeltaY = 0;
|
|
|
|
|
2022-03-04 19:10:09 +08:00
|
|
|
// if (isDragging && (Y<0 || Y>clientRect().height())) {
|
|
|
|
// mScrollDeltaY = 0;
|
|
|
|
// }
|
2021-11-26 19:04:04 +08:00
|
|
|
|
2022-03-07 20:30:45 +08:00
|
|
|
|
2022-03-04 19:04:45 +08:00
|
|
|
// if (mScrollDeltaX!=0 || mScrollDeltaY!=0) {
|
|
|
|
if (isDragging) {
|
|
|
|
mScrollTimer->singleShot(100,this,&SynEdit::onDraggingScrollTimeout);
|
|
|
|
} else {
|
|
|
|
mScrollTimer->singleShot(100,this,&SynEdit::onScrollTimeout);
|
2021-11-26 19:04:04 +08:00
|
|
|
}
|
2022-03-04 19:04:45 +08:00
|
|
|
// }
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doBlockIndent()
|
|
|
|
{
|
2021-11-30 20:43:58 +08:00
|
|
|
BufferCoord oldCaretPos;
|
2021-05-14 23:56:43 +08:00
|
|
|
BufferCoord BB, BE;
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList strToInsert;
|
2021-06-03 23:18:51 +08:00
|
|
|
int e,x,i;
|
2021-11-30 21:11:21 +08:00
|
|
|
QString spaces;
|
|
|
|
BufferCoord insertionPos;
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-11-30 20:43:58 +08:00
|
|
|
oldCaretPos = caretXY();
|
2021-06-03 20:26:36 +08:00
|
|
|
|
|
|
|
// keep current selection detail
|
2021-05-14 23:56:43 +08:00
|
|
|
if (selAvail()) {
|
2021-06-03 20:26:36 +08:00
|
|
|
BB = blockBegin();
|
|
|
|
BE = blockEnd();
|
|
|
|
} else {
|
|
|
|
BB = caretXY();
|
|
|
|
BE = caretXY();
|
|
|
|
}
|
|
|
|
// build text to insert
|
2022-07-04 10:01:40 +08:00
|
|
|
if (BE.ch == 1 && BE.line != BB.line) {
|
|
|
|
e = BE.line - 1;
|
2021-06-03 20:26:36 +08:00
|
|
|
x = 1;
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
e = BE.line;
|
2021-06-03 20:26:36 +08:00
|
|
|
if (mOptions.testFlag(SynEditorOption::eoTabsToSpaces))
|
2022-04-19 21:18:41 +08:00
|
|
|
x = caretX() + tabWidth();
|
2021-06-03 20:26:36 +08:00
|
|
|
else
|
|
|
|
x = caretX() + 1;
|
|
|
|
}
|
|
|
|
if (mOptions.testFlag(eoTabsToSpaces)) {
|
2022-04-19 21:18:41 +08:00
|
|
|
spaces = QString(tabWidth(),' ') ;
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
2021-11-30 21:11:21 +08:00
|
|
|
spaces = "\t";
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
for (i = BB.line; i<e;i++) {
|
2022-07-02 10:39:31 +08:00
|
|
|
strToInsert.append(spaces);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
strToInsert.append(spaces);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
|
|
|
mUndoList->addChange(SynChangeReason::Caret, oldCaretPos, oldCaretPos,QStringList(), activeSelectionMode());
|
|
|
|
mUndoList->addChange(SynChangeReason::Selection,mBlockBegin,mBlockEnd,QStringList(), activeSelectionMode());
|
|
|
|
insertionPos.line = BB.line;
|
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column)
|
|
|
|
insertionPos.ch = std::min(BB.ch, BE.ch);
|
2022-07-02 10:39:31 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
insertionPos.ch = 1;
|
2022-07-02 10:39:31 +08:00
|
|
|
insertBlock(insertionPos, insertionPos, strToInsert);
|
|
|
|
//adjust caret and selection
|
2022-07-04 10:01:40 +08:00
|
|
|
oldCaretPos.ch = x;
|
|
|
|
if (BB.ch > 1)
|
|
|
|
BB.ch += spaces.length();
|
|
|
|
if (BE.ch > 1)
|
|
|
|
BE.ch+=spaces.length();
|
2022-07-02 10:39:31 +08:00
|
|
|
setCaretAndSelection(oldCaretPos,
|
|
|
|
BB, BE);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
void SynEdit::doBlockUnindent()
|
|
|
|
{
|
2021-11-30 21:11:21 +08:00
|
|
|
int lastIndent = 0;
|
|
|
|
int firstIndent = 0;
|
2021-06-03 20:26:36 +08:00
|
|
|
|
|
|
|
BufferCoord BB,BE;
|
|
|
|
// keep current selection detail
|
|
|
|
if (selAvail()) {
|
|
|
|
BB = blockBegin();
|
|
|
|
BE = blockEnd();
|
|
|
|
} else {
|
|
|
|
BB = caretXY();
|
|
|
|
BE = caretXY();
|
|
|
|
}
|
2021-11-30 20:43:58 +08:00
|
|
|
BufferCoord oldCaretPos = caretXY();
|
2021-06-03 20:26:36 +08:00
|
|
|
int x = 0;
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
|
|
|
mUndoList->addChange(SynChangeReason::Caret, oldCaretPos, oldCaretPos,QStringList(), activeSelectionMode());
|
|
|
|
mUndoList->addChange(SynChangeReason::Selection,mBlockBegin,mBlockEnd,QStringList(), activeSelectionMode());
|
2021-06-03 20:26:36 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
int e = BE.line;
|
2021-06-03 20:26:36 +08:00
|
|
|
// convert selection to complete lines
|
2022-07-04 10:01:40 +08:00
|
|
|
if (BE.ch == 1)
|
|
|
|
e = BE.line - 1;
|
2021-06-03 20:26:36 +08:00
|
|
|
// build string to delete
|
2022-07-04 10:01:40 +08:00
|
|
|
for (int i = BB.line; i<= e;i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString line = mDocument->getString(i - 1);
|
2021-11-30 21:11:21 +08:00
|
|
|
if (line.isEmpty())
|
2021-06-03 20:26:36 +08:00
|
|
|
continue;
|
2021-11-30 21:11:21 +08:00
|
|
|
if (line[0]!=' ' && line[0]!='\t')
|
2021-06-03 20:26:36 +08:00
|
|
|
continue;
|
|
|
|
int charsToDelete = 0;
|
2022-04-19 21:18:41 +08:00
|
|
|
while (charsToDelete < tabWidth() &&
|
2021-11-30 21:11:21 +08:00
|
|
|
charsToDelete < line.length() &&
|
|
|
|
line[charsToDelete] == ' ')
|
2021-06-03 20:26:36 +08:00
|
|
|
charsToDelete++;
|
|
|
|
if (charsToDelete == 0)
|
|
|
|
charsToDelete = 1;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (i==BB.line)
|
2021-11-30 21:11:21 +08:00
|
|
|
firstIndent = charsToDelete;
|
2021-06-05 23:43:45 +08:00
|
|
|
if (i==e)
|
2021-11-30 21:11:21 +08:00
|
|
|
lastIndent = charsToDelete;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (i==oldCaretPos.line)
|
2021-06-03 20:26:36 +08:00
|
|
|
x = charsToDelete;
|
2021-11-30 21:11:21 +08:00
|
|
|
QString tempString = line.mid(charsToDelete);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(i-1,tempString);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Delete,
|
2022-07-02 10:39:31 +08:00
|
|
|
BufferCoord{1,i},
|
|
|
|
BufferCoord{charsToDelete+1,i},
|
|
|
|
QStringList(line.left(charsToDelete)),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
// restore selection
|
|
|
|
//adjust the x position of orgcaretpos appropriately
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
oldCaretPos.ch -= x;
|
|
|
|
BB.ch -= firstIndent;
|
|
|
|
BE.ch -= lastIndent;
|
2021-11-30 20:43:58 +08:00
|
|
|
setCaretAndSelection(oldCaretPos, BB, BE);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doAddChar(QChar AChar)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-03-26 09:54:06 +08:00
|
|
|
if (!AChar.isPrint() && AChar!='\t')
|
2021-06-03 20:26:36 +08:00
|
|
|
return;
|
|
|
|
//DoOnPaintTransient(ttBefore);
|
2021-09-01 17:34:39 +08:00
|
|
|
//mCaretX will change after setSelLength;
|
2022-05-18 23:17:06 +08:00
|
|
|
if (mInserting == false && !selAvail()) {
|
|
|
|
switch(mActiveSelectionMode) {
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Column: {
|
2022-05-18 23:17:06 +08:00
|
|
|
//we can't use blockBegin()/blockEnd()
|
|
|
|
BufferCoord start=mBlockBegin;
|
|
|
|
BufferCoord end=mBlockEnd;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (start.line > end.line )
|
2022-05-18 23:17:06 +08:00
|
|
|
std::swap(start,end);
|
2022-07-04 10:01:40 +08:00
|
|
|
start.ch++; // make sure we select a whole char in the start line
|
2022-03-25 22:13:00 +08:00
|
|
|
setBlockBegin(start);
|
|
|
|
setBlockEnd(end);
|
2022-05-18 23:17:06 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Line:
|
2022-05-18 23:17:06 +08:00
|
|
|
//do nothing;
|
|
|
|
break;
|
|
|
|
default:
|
2022-03-25 22:13:00 +08:00
|
|
|
setSelLength(1);
|
2022-05-18 23:17:06 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2022-03-01 22:03:54 +08:00
|
|
|
if (isIdentChar(AChar)) {
|
|
|
|
doSetSelText(AChar);
|
2022-03-01 23:35:01 +08:00
|
|
|
} else if (AChar.isSpace()) {
|
|
|
|
// break group undo chain
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addGroupBreak();
|
2022-03-01 23:35:01 +08:00
|
|
|
doSetSelText(AChar);
|
|
|
|
// break group undo chain
|
2022-03-26 11:49:41 +08:00
|
|
|
// if (mActiveSelectionMode!=SynSelectionMode::smColumn)
|
|
|
|
// mUndoList->AddChange(SynChangeReason::crNothing,
|
|
|
|
// BufferCoord{0, 0},
|
|
|
|
// BufferCoord{0, 0},
|
|
|
|
// "", SynSelectionMode::smNormal);
|
2022-05-18 23:17:06 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-03-01 22:03:54 +08:00
|
|
|
doSetSelText(AChar);
|
|
|
|
int oldCaretX=mCaretX-1;
|
|
|
|
int oldCaretY=mCaretY;
|
|
|
|
// auto
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Normal
|
2022-03-25 22:13:00 +08:00
|
|
|
&& mOptions.testFlag(eoAutoIndent)
|
2022-03-01 22:03:54 +08:00
|
|
|
&& mHighlighter
|
|
|
|
&& mHighlighter->getClass()==SynHighlighterClass::CppHighlighter
|
2022-04-19 21:18:41 +08:00
|
|
|
&& (oldCaretY<=mDocument->count()) ) {
|
2022-03-01 22:03:54 +08:00
|
|
|
|
|
|
|
//unindent if ':' at end of the line
|
|
|
|
if (AChar == ':') {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString line = mDocument->getString(oldCaretY-1);
|
2022-03-01 22:03:54 +08:00
|
|
|
if (line.length() <= oldCaretX) {
|
|
|
|
int indentSpaces = calcIndentSpaces(oldCaretY,line+":", true);
|
|
|
|
if (indentSpaces != leftSpaces(line)) {
|
|
|
|
QString newLine = GetLeftSpacing(indentSpaces,true) + trimLeft(line);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(oldCaretY-1,newLine);
|
2022-03-01 22:03:54 +08:00
|
|
|
internalSetCaretXY(BufferCoord{newLine.length()+2,oldCaretY});
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Delete,
|
2022-03-01 22:03:54 +08:00
|
|
|
BufferCoord{1, oldCaretY},
|
|
|
|
BufferCoord{line.length()+1, oldCaretY},
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(line),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal
|
2022-03-01 22:03:54 +08:00
|
|
|
);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Insert,
|
2022-03-01 22:03:54 +08:00
|
|
|
BufferCoord{1, oldCaretY},
|
|
|
|
BufferCoord{newLine.length()+1, oldCaretY},
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal
|
2022-03-01 22:03:54 +08:00
|
|
|
);
|
|
|
|
}
|
2021-10-29 21:22:41 +08:00
|
|
|
}
|
2022-06-28 09:37:18 +08:00
|
|
|
} else if (AChar == '*') {
|
|
|
|
QString line = mDocument->getString(oldCaretY-1);
|
|
|
|
if (line.length() <= oldCaretX) {
|
|
|
|
int indentSpaces = calcIndentSpaces(oldCaretY,line+"*", true);
|
|
|
|
if (indentSpaces != leftSpaces(line)) {
|
|
|
|
QString newLine = GetLeftSpacing(indentSpaces,true) + trimLeft(line);
|
|
|
|
mDocument->putString(oldCaretY-1,newLine);
|
|
|
|
internalSetCaretXY(BufferCoord{newLine.length()+2,oldCaretY});
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Delete,
|
2022-06-28 09:37:18 +08:00
|
|
|
BufferCoord{1, oldCaretY},
|
|
|
|
BufferCoord{line.length()+1, oldCaretY},
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(line),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal
|
2022-06-28 09:37:18 +08:00
|
|
|
);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Insert,
|
2022-06-28 09:37:18 +08:00
|
|
|
BufferCoord{1, oldCaretY},
|
|
|
|
BufferCoord{newLine.length()+1, oldCaretY},
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal
|
2022-06-28 09:37:18 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 22:03:54 +08:00
|
|
|
} else if (AChar == '{' || AChar == '}' || AChar == '#') {
|
|
|
|
//Reindent line when add '{' '}' and '#' at the beginning
|
2022-04-19 21:18:41 +08:00
|
|
|
QString left = mDocument->getString(oldCaretY-1).mid(0,oldCaretX-1);
|
2022-03-01 22:03:54 +08:00
|
|
|
// and the first nonblank char is this new {
|
|
|
|
if (left.trimmed().isEmpty()) {
|
|
|
|
int indentSpaces = calcIndentSpaces(oldCaretY,AChar, true);
|
|
|
|
if (indentSpaces != leftSpaces(left)) {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString right = mDocument->getString(oldCaretY-1).mid(oldCaretX-1);
|
2022-03-01 22:03:54 +08:00
|
|
|
QString newLeft = GetLeftSpacing(indentSpaces,true);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(oldCaretY-1,newLeft+right);
|
2022-03-01 22:03:54 +08:00
|
|
|
BufferCoord newCaretPos = BufferCoord{newLeft.length()+2,oldCaretY};
|
|
|
|
internalSetCaretXY(newCaretPos);
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Delete,
|
2022-03-01 22:03:54 +08:00
|
|
|
BufferCoord{1, oldCaretY},
|
|
|
|
BufferCoord{left.length()+1, oldCaretY},
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(left),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal
|
2022-03-01 22:03:54 +08:00
|
|
|
);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Insert,
|
2022-03-01 22:03:54 +08:00
|
|
|
BufferCoord{1, oldCaretY},
|
|
|
|
BufferCoord{newLeft.length()+1, oldCaretY},
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(""),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal
|
2022-03-01 22:03:54 +08:00
|
|
|
);
|
2021-12-19 20:48:23 +08:00
|
|
|
|
2022-03-01 22:03:54 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
//DoOnPaintTransient(ttAfter);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doCutToClipboard()
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
2022-01-10 18:37:00 +08:00
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
2022-03-15 00:15:35 +08:00
|
|
|
if (!selAvail()) {
|
|
|
|
doSelecteLine();
|
|
|
|
}
|
2021-06-07 11:02:03 +08:00
|
|
|
internalDoCopyToClipboard(selText());
|
2022-07-02 10:39:31 +08:00
|
|
|
setSelectedTextEmpty();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
|
|
|
mUndoList->addGroupBreak();
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doCopyToClipboard()
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
2022-03-14 23:55:02 +08:00
|
|
|
bool selected=selAvail();
|
|
|
|
if (!selected)
|
2022-01-10 18:37:00 +08:00
|
|
|
doSelecteLine();
|
2022-07-04 10:01:40 +08:00
|
|
|
bool ChangeTrim = (mActiveSelectionMode == SynSelectionMode::Column) &&
|
2021-06-05 23:43:45 +08:00
|
|
|
mOptions.testFlag(eoTrimTrailingSpaces);
|
|
|
|
QString sText;
|
|
|
|
{
|
|
|
|
auto action = finally([&,this] {
|
|
|
|
if (ChangeTrim)
|
|
|
|
mOptions.setFlag(eoTrimTrailingSpaces);
|
|
|
|
});
|
|
|
|
if (ChangeTrim)
|
|
|
|
mOptions.setFlag(eoTrimTrailingSpaces,false);
|
|
|
|
sText = selText();
|
|
|
|
}
|
2021-06-07 11:02:03 +08:00
|
|
|
internalDoCopyToClipboard(sText);
|
2022-03-14 23:55:02 +08:00
|
|
|
if (!selected) {
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
|
|
|
}
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::internalDoCopyToClipboard(const QString &s)
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
|
|
|
QClipboard* clipboard=QGuiApplication::clipboard();
|
|
|
|
clipboard->clear();
|
|
|
|
clipboard->setText(s);
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doPasteFromClipboard()
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
QClipboard* clipboard = QGuiApplication::clipboard();
|
2022-06-16 09:11:45 +08:00
|
|
|
QString text = clipboard->text();
|
|
|
|
if (text.isEmpty())
|
2021-06-05 23:43:45 +08:00
|
|
|
return;
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-07-02 06:44:01 +08:00
|
|
|
// if (selAvail()) {
|
|
|
|
// mUndoList->AddChange(
|
|
|
|
// SynChangeReason::crDelete,
|
|
|
|
// mBlockBegin,
|
|
|
|
// mBlockEnd,
|
|
|
|
// selText(),
|
|
|
|
// mActiveSelectionMode);
|
|
|
|
// }
|
2022-05-18 23:17:06 +08:00
|
|
|
// } else if (!colSelAvail())
|
|
|
|
// setActiveSelectionMode(selectionMode());
|
2022-06-16 09:11:45 +08:00
|
|
|
BufferCoord vStartOfBlock = blockBegin();
|
|
|
|
BufferCoord vEndOfBlock = blockEnd();
|
|
|
|
mBlockBegin = vStartOfBlock;
|
|
|
|
mBlockEnd = vEndOfBlock;
|
2022-07-02 14:06:10 +08:00
|
|
|
qDebug()<<textToLines(text);
|
|
|
|
setSelTextPrimitive(splitStrings(text));
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::incPaintLock()
|
|
|
|
{
|
2021-10-04 11:07:35 +08:00
|
|
|
if (mPaintLock==0) {
|
|
|
|
onBeginFirstPaintLock();
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
mPaintLock ++ ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::decPaintLock()
|
|
|
|
{
|
|
|
|
Q_ASSERT(mPaintLock > 0);
|
|
|
|
mPaintLock--;
|
|
|
|
if (mPaintLock == 0 ) {
|
2021-06-07 21:34:48 +08:00
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfScrollbarChanged)) {
|
2021-05-14 23:56:43 +08:00
|
|
|
updateScrollbars();
|
2021-06-07 21:34:48 +08:00
|
|
|
ensureCursorPosVisible();
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfCaretChanged))
|
|
|
|
updateCaret();
|
|
|
|
if (mStatusChanges!=0)
|
|
|
|
doOnStatusChange(mStatusChanges);
|
2021-10-04 11:07:35 +08:00
|
|
|
onEndFirstPaintLock();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::clientWidth()
|
|
|
|
{
|
|
|
|
return viewport()->size().width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::clientHeight()
|
|
|
|
{
|
|
|
|
return viewport()->size().height();
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::clientTop()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::clientLeft()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect SynEdit::clientRect()
|
|
|
|
{
|
2021-05-21 23:33:53 +08:00
|
|
|
return QRect(0,0, clientWidth(), clientHeight());
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::synFontChanged()
|
|
|
|
{
|
|
|
|
recalcCharExtent();
|
2021-09-02 12:14:02 +08:00
|
|
|
onSizeOrFontChanged(true);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doOnPaintTransient(SynTransientType TransientType)
|
|
|
|
{
|
|
|
|
doOnPaintTransientEx(TransientType, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::updateLastCaretX()
|
|
|
|
{
|
2021-06-03 23:18:51 +08:00
|
|
|
mLastCaretColumn = displayX();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::ensureCursorPosVisible()
|
|
|
|
{
|
|
|
|
ensureCursorPosVisibleEx(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::ensureCursorPosVisibleEx(bool ForceToMiddle)
|
|
|
|
{
|
|
|
|
incPaintLock();
|
|
|
|
auto action = finally([this]{
|
|
|
|
decPaintLock();
|
|
|
|
});
|
|
|
|
// Make sure X is visible
|
|
|
|
int VisibleX = displayX();
|
|
|
|
if (VisibleX < leftChar())
|
|
|
|
setLeftChar(VisibleX);
|
|
|
|
else if (VisibleX >= mCharsInWindow + leftChar() && mCharsInWindow > 0)
|
|
|
|
setLeftChar(VisibleX - mCharsInWindow + 1);
|
|
|
|
else
|
|
|
|
setLeftChar(leftChar());
|
|
|
|
// Make sure Y is visible
|
|
|
|
int vCaretRow = displayY();
|
|
|
|
if (ForceToMiddle) {
|
|
|
|
if (vCaretRow < mTopLine || vCaretRow>(mTopLine + (mLinesInWindow - 1)))
|
|
|
|
setTopLine( vCaretRow - (mLinesInWindow - 1) / 2);
|
|
|
|
} else {
|
|
|
|
if (vCaretRow < mTopLine)
|
|
|
|
setTopLine(vCaretRow);
|
|
|
|
else if (vCaretRow > mTopLine + (mLinesInWindow - 1) && mLinesInWindow > 0)
|
|
|
|
setTopLine(vCaretRow - (mLinesInWindow - 1));
|
|
|
|
else
|
|
|
|
setTopLine(mTopLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 07:39:56 +08:00
|
|
|
void SynEdit::scrollWindow(int dx, int dy)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-01-13 07:39:56 +08:00
|
|
|
int nx = horizontalScrollBar()->value()+dx;
|
|
|
|
int ny = verticalScrollBar()->value()+dy;
|
|
|
|
horizontalScrollBar()->setValue(nx);
|
|
|
|
verticalScrollBar()->setValue(ny);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setInternalDisplayXY(const DisplayCoord &aPos)
|
|
|
|
{
|
|
|
|
incPaintLock();
|
|
|
|
internalSetCaretXY(displayToBufferPos(aPos));
|
|
|
|
decPaintLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::internalSetCaretXY(const BufferCoord &Value)
|
|
|
|
{
|
|
|
|
setCaretXYEx(true, Value);
|
|
|
|
}
|
|
|
|
|
2021-05-29 21:35:46 +08:00
|
|
|
void SynEdit::internalSetCaretX(int Value)
|
|
|
|
{
|
|
|
|
internalSetCaretXY(BufferCoord{Value, mCaretY});
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::internalSetCaretY(int Value)
|
|
|
|
{
|
|
|
|
internalSetCaretXY(BufferCoord{mCaretX,Value});
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::setStatusChanged(SynStatusChanges changes)
|
|
|
|
{
|
|
|
|
mStatusChanges = mStatusChanges | changes;
|
|
|
|
if (mPaintLock == 0)
|
|
|
|
doOnStatusChange(mStatusChanges);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doOnStatusChange(SynStatusChanges)
|
|
|
|
{
|
2021-11-08 21:19:48 +08:00
|
|
|
if (mStatusChanges.testFlag(SynStatusChange::scCaretX)
|
|
|
|
|| mStatusChanges.testFlag(SynStatusChange::scCaretY)) {
|
|
|
|
qApp->inputMethod()->update(Qt::ImCursorPosition);
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
emit statusChanged(mStatusChanges);
|
|
|
|
mStatusChanges = SynStatusChange::scNone;
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
void SynEdit::insertBlock(const BufferCoord& startPos, const BufferCoord& endPos, const QStringList& blockText)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-07-02 10:39:31 +08:00
|
|
|
setCaretAndSelection(startPos, startPos, startPos);
|
2022-07-04 10:01:40 +08:00
|
|
|
setSelTextPrimitiveEx(SynSelectionMode::Column, blockText);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::updateScrollbars()
|
|
|
|
{
|
|
|
|
int nMaxScroll;
|
|
|
|
int nMin,nMax,nPage,nPos;
|
|
|
|
if (mPaintLock!=0) {
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfScrollbarChanged);
|
|
|
|
} else {
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfScrollbarChanged,false);
|
|
|
|
if (mScrollBars != SynScrollStyle::ssNone) {
|
|
|
|
if (mOptions.testFlag(eoHideShowScrollbars)) {
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
|
|
|
|
} else {
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
|
|
|
|
}
|
|
|
|
if (mScrollBars == SynScrollStyle::ssBoth || mScrollBars == SynScrollStyle::ssHorizontal) {
|
2021-06-08 21:41:42 +08:00
|
|
|
nMaxScroll = maxScrollWidth();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (nMaxScroll <= MAX_SCROLL) {
|
|
|
|
nMin = 1;
|
|
|
|
nMax = nMaxScroll;
|
|
|
|
nPage = mCharsInWindow;
|
|
|
|
nPos = mLeftChar;
|
|
|
|
} else {
|
|
|
|
nMin = 0;
|
|
|
|
nMax = MAX_SCROLL;
|
2021-09-11 18:42:49 +08:00
|
|
|
nPage = mulDiv(MAX_SCROLL, mCharsInWindow, nMaxScroll);
|
|
|
|
nPos = mulDiv(MAX_SCROLL, mLeftChar, nMaxScroll);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
horizontalScrollBar()->setMinimum(nMin);
|
|
|
|
horizontalScrollBar()->setMaximum(nMax);
|
|
|
|
horizontalScrollBar()->setPageStep(nPage);
|
|
|
|
horizontalScrollBar()->setValue(nPos);
|
|
|
|
horizontalScrollBar()->setSingleStep(1);
|
|
|
|
} else
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
|
|
|
|
|
|
|
|
if (mScrollBars == SynScrollStyle::ssBoth || mScrollBars == SynScrollStyle::ssVertical) {
|
2021-06-08 21:41:42 +08:00
|
|
|
nMaxScroll = maxScrollHeight();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (nMaxScroll <= MAX_SCROLL) {
|
|
|
|
nMin = 1;
|
|
|
|
nMax = std::max(1, nMaxScroll);
|
|
|
|
nPage = mLinesInWindow;
|
|
|
|
nPos = mTopLine;
|
|
|
|
} else {
|
|
|
|
nMin = 0;
|
|
|
|
nMax = MAX_SCROLL;
|
2021-09-11 18:42:49 +08:00
|
|
|
nPage = mulDiv(MAX_SCROLL, mLinesInWindow, nMaxScroll);
|
|
|
|
nPos = mulDiv(MAX_SCROLL, mTopLine, nMaxScroll);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
verticalScrollBar()->setMinimum(nMin);
|
|
|
|
verticalScrollBar()->setMaximum(nMax);
|
|
|
|
verticalScrollBar()->setPageStep(nPage);
|
|
|
|
verticalScrollBar()->setValue(nPos);
|
|
|
|
verticalScrollBar()->setSingleStep(1);
|
|
|
|
} else
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
|
|
|
|
} else {
|
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::updateCaret()
|
|
|
|
{
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfCaretChanged,false);
|
2021-10-05 00:42:35 +08:00
|
|
|
invalidateRect(calculateCaretRect());
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::recalcCharExtent()
|
|
|
|
{
|
|
|
|
SynFontStyle styles[] = {SynFontStyle::fsBold, SynFontStyle::fsItalic, SynFontStyle::fsStrikeOut, SynFontStyle::fsUnderline};
|
|
|
|
bool hasStyles[] = {false,false,false,false};
|
|
|
|
int size = 4;
|
|
|
|
if (mHighlighter && mHighlighter->attributes().count()>0) {
|
2021-08-29 10:14:07 +08:00
|
|
|
for (const PSynHighlighterAttribute& attribute: mHighlighter->attributes()) {
|
2021-05-14 23:56:43 +08:00
|
|
|
for (int i=0;i<size;i++) {
|
|
|
|
if (attribute->styles().testFlag(styles[i]))
|
|
|
|
hasStyles[i] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hasStyles[0] = font().bold();
|
|
|
|
hasStyles[1] = font().italic();
|
|
|
|
hasStyles[2] = font().strikeOut();
|
|
|
|
hasStyles[3] = font().underline();
|
|
|
|
}
|
|
|
|
|
|
|
|
mTextHeight = 0;
|
|
|
|
mCharWidth = 0;
|
2021-12-13 16:38:22 +08:00
|
|
|
QFontMetrics fm(font());
|
2022-01-26 14:31:26 +08:00
|
|
|
QFontMetrics fm2(font());
|
|
|
|
mTextHeight = std::max(fm.lineSpacing(),fm2.lineSpacing());
|
2021-12-13 16:38:22 +08:00
|
|
|
mCharWidth = fm.horizontalAdvance("M");
|
2022-01-26 14:31:26 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
if (hasStyles[0]) { // has bold font
|
|
|
|
QFont f = font();
|
|
|
|
f.setBold(true);
|
|
|
|
QFontMetrics fm(f);
|
2022-01-26 14:31:26 +08:00
|
|
|
QFont f2 = font();
|
|
|
|
f2.setBold(true);
|
|
|
|
QFontMetrics fm2(f);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm.lineSpacing();
|
2022-01-26 14:31:26 +08:00
|
|
|
if (fm2.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm2.lineSpacing();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.horizontalAdvance("M")>mCharWidth)
|
|
|
|
mCharWidth = fm.horizontalAdvance("M");
|
|
|
|
}
|
|
|
|
if (hasStyles[1]) { // has strike out font
|
|
|
|
QFont f = font();
|
|
|
|
f.setItalic(true);
|
|
|
|
QFontMetrics fm(f);
|
2022-01-26 14:31:26 +08:00
|
|
|
QFont f2 = font();
|
|
|
|
f2.setItalic(true);
|
|
|
|
QFontMetrics fm2(f);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm.lineSpacing();
|
2022-01-26 14:31:26 +08:00
|
|
|
if (fm2.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm2.lineSpacing();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.horizontalAdvance("M")>mCharWidth)
|
|
|
|
mCharWidth = fm.horizontalAdvance("M");
|
|
|
|
}
|
|
|
|
if (hasStyles[2]) { // has strikeout
|
|
|
|
QFont f = font();
|
|
|
|
f.setStrikeOut(true);
|
|
|
|
QFontMetrics fm(f);
|
2022-01-26 14:31:26 +08:00
|
|
|
QFont f2 = font();
|
|
|
|
f2.setStrikeOut(true);
|
|
|
|
QFontMetrics fm2(f);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm.lineSpacing();
|
2022-01-26 14:31:26 +08:00
|
|
|
if (fm2.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm2.lineSpacing();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.horizontalAdvance("M")>mCharWidth)
|
|
|
|
mCharWidth = fm.horizontalAdvance("M");
|
|
|
|
}
|
|
|
|
if (hasStyles[3]) { // has underline
|
|
|
|
QFont f = font();
|
|
|
|
f.setUnderline(true);
|
|
|
|
QFontMetrics fm(f);
|
2022-01-26 14:31:26 +08:00
|
|
|
QFont f2 = font();
|
|
|
|
f2.setUnderline(true);
|
|
|
|
QFontMetrics fm2(f);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm.lineSpacing();
|
2022-01-26 14:31:26 +08:00
|
|
|
if (fm2.lineSpacing()>mTextHeight)
|
|
|
|
mTextHeight=fm2.lineSpacing();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (fm.horizontalAdvance("M")>mCharWidth)
|
|
|
|
mCharWidth = fm.horizontalAdvance("M");
|
|
|
|
}
|
|
|
|
mTextHeight += mExtraLineSpacing;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString SynEdit::expandAtWideGlyphs(const QString &S)
|
|
|
|
{
|
|
|
|
QString Result(S.length()*2); // speed improvement
|
|
|
|
int j = 0;
|
|
|
|
for (int i=0;i<S.length();i++) {
|
|
|
|
int CountOfAvgGlyphs = ceil(fontMetrics().horizontalAdvance(S[i])/(double)mCharWidth);
|
|
|
|
if (j+CountOfAvgGlyphs>=Result.length())
|
|
|
|
Result.resize(Result.length()+128);
|
|
|
|
// insert CountOfAvgGlyphs filling chars
|
|
|
|
while (CountOfAvgGlyphs>1) {
|
|
|
|
Result[j]=QChar(0xE000);
|
|
|
|
j++;
|
|
|
|
CountOfAvgGlyphs--;
|
|
|
|
}
|
|
|
|
Result[j]=S[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
Result.resize(j);
|
2021-06-21 22:01:35 +08:00
|
|
|
return Result;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::updateModifiedStatus()
|
|
|
|
{
|
|
|
|
setModified(!mUndoList->initialState());
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:06:26 +08:00
|
|
|
int SynEdit::scanFrom(int Index, int canStopIndex)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
SynRangeState iRange;
|
2021-10-04 20:56:57 +08:00
|
|
|
int Result = std::max(0,Index);
|
2022-04-19 21:18:41 +08:00
|
|
|
if (Result >= mDocument->count())
|
2021-05-14 23:56:43 +08:00
|
|
|
return Result;
|
|
|
|
|
|
|
|
if (Result == 0) {
|
|
|
|
mHighlighter->resetState();
|
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setState(mDocument->ranges(Result-1));
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
do {
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setLine(mDocument->getString(Result), Result);
|
2021-05-14 23:56:43 +08:00
|
|
|
mHighlighter->nextToEol();
|
|
|
|
iRange = mHighlighter->getRangeState();
|
2021-09-23 12:06:26 +08:00
|
|
|
if (Result > canStopIndex){
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument->ranges(Result).state == iRange.state
|
|
|
|
&& mDocument->ranges(Result).braceLevel == iRange.braceLevel
|
|
|
|
&& mDocument->ranges(Result).parenthesisLevel == iRange.parenthesisLevel
|
|
|
|
&& mDocument->ranges(Result).bracketLevel == iRange.bracketLevel
|
2021-10-09 23:09:08 +08:00
|
|
|
) {
|
|
|
|
if (mUseCodeFolding)
|
2021-10-29 15:15:43 +08:00
|
|
|
rescanFolds();
|
2021-05-14 23:56:43 +08:00
|
|
|
return Result;// avoid the final Decrement
|
2021-10-09 23:09:08 +08:00
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->setRange(Result,iRange);
|
2021-05-14 23:56:43 +08:00
|
|
|
Result ++ ;
|
2022-04-19 21:18:41 +08:00
|
|
|
} while (Result < mDocument->count());
|
2021-05-14 23:56:43 +08:00
|
|
|
Result--;
|
2021-09-26 22:39:26 +08:00
|
|
|
if (mUseCodeFolding)
|
2021-10-29 15:15:43 +08:00
|
|
|
rescanFolds();
|
2021-05-14 23:56:43 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:15:43 +08:00
|
|
|
void SynEdit::rescanRange(int line)
|
|
|
|
{
|
2021-11-03 18:09:12 +08:00
|
|
|
if (!mHighlighter)
|
|
|
|
return;
|
2021-10-29 15:15:43 +08:00
|
|
|
line--;
|
|
|
|
line = std::max(0,line);
|
2022-04-19 21:18:41 +08:00
|
|
|
if (line >= mDocument->count())
|
2021-10-29 15:15:43 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (line == 0) {
|
|
|
|
mHighlighter->resetState();
|
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setState(mDocument->ranges(line-1));
|
2021-10-29 15:15:43 +08:00
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setLine(mDocument->getString(line), line);
|
2021-10-29 15:15:43 +08:00
|
|
|
mHighlighter->nextToEol();
|
|
|
|
SynRangeState iRange = mHighlighter->getRangeState();
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->setRange(line,iRange);
|
2021-10-29 15:15:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::rescanRanges()
|
2021-05-26 00:04:20 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mHighlighter && !mDocument->empty()) {
|
2021-05-26 00:04:20 +08:00
|
|
|
mHighlighter->resetState();
|
2022-04-19 21:18:41 +08:00
|
|
|
for (int i =0;i<mDocument->count();i++) {
|
|
|
|
mHighlighter->setLine(mDocument->getString(i), i);
|
2021-05-26 00:04:20 +08:00
|
|
|
mHighlighter->nextToEol();
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->setRange(i, mHighlighter->getRangeState());
|
2021-05-26 00:04:20 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 22:39:26 +08:00
|
|
|
if (mUseCodeFolding)
|
2021-10-29 15:15:43 +08:00
|
|
|
rescanFolds();
|
2021-05-26 00:04:20 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::uncollapse(PSynEditFoldRange FoldRange)
|
|
|
|
{
|
|
|
|
FoldRange->linesCollapsed = 0;
|
|
|
|
FoldRange->collapsed = false;
|
|
|
|
|
|
|
|
// Redraw the collapsed line
|
|
|
|
invalidateLines(FoldRange->fromLine, INT_MAX);
|
|
|
|
|
|
|
|
// Redraw fold mark
|
|
|
|
invalidateGutterLines(FoldRange->fromLine, INT_MAX);
|
2022-04-18 16:17:06 +08:00
|
|
|
updateScrollbars();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-09 17:12:23 +08:00
|
|
|
void SynEdit::collapse(PSynEditFoldRange FoldRange)
|
|
|
|
{
|
|
|
|
FoldRange->linesCollapsed = FoldRange->toLine - FoldRange->fromLine;
|
|
|
|
FoldRange->collapsed = true;
|
|
|
|
|
|
|
|
// Extract caret from fold
|
|
|
|
if ((mCaretY > FoldRange->fromLine) && (mCaretY <= FoldRange->toLine)) {
|
2022-04-19 21:18:41 +08:00
|
|
|
setCaretXY(BufferCoord{mDocument->getString(FoldRange->fromLine - 1).length() + 1,
|
2021-06-09 17:12:23 +08:00
|
|
|
FoldRange->fromLine});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redraw the collapsed line
|
|
|
|
invalidateLines(FoldRange->fromLine, INT_MAX);
|
|
|
|
|
|
|
|
// Redraw fold mark
|
|
|
|
invalidateGutterLines(FoldRange->fromLine, INT_MAX);
|
2022-04-18 16:17:06 +08:00
|
|
|
|
|
|
|
updateScrollbars();
|
2021-06-09 17:12:23 +08:00
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::foldOnListInserted(int Line, int Count)
|
|
|
|
{
|
|
|
|
// Delete collapsed inside selection
|
|
|
|
for (int i = mAllFoldRanges.count()-1;i>=0;i--) {
|
|
|
|
PSynEditFoldRange range = mAllFoldRanges[i];
|
2022-07-03 12:00:05 +08:00
|
|
|
if (range->fromLine == Line - 1) {// insertion starts at fold line
|
|
|
|
if (range->collapsed)
|
2021-05-14 23:56:43 +08:00
|
|
|
uncollapse(range);
|
2022-07-03 12:00:05 +08:00
|
|
|
} else if (range->fromLine >= Line) // insertion of count lines above FromLine
|
|
|
|
range->move(Count);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::foldOnListDeleted(int Line, int Count)
|
|
|
|
{
|
|
|
|
// Delete collapsed inside selection
|
|
|
|
for (int i = mAllFoldRanges.count()-1;i>=0;i--) {
|
|
|
|
PSynEditFoldRange range = mAllFoldRanges[i];
|
2022-07-03 12:00:05 +08:00
|
|
|
if (range->fromLine == Line && Count == 1) {// open up because we are messing with the starting line
|
|
|
|
if (range->collapsed)
|
2021-05-14 23:56:43 +08:00
|
|
|
uncollapse(range);
|
2022-07-03 12:00:05 +08:00
|
|
|
} else if (range->fromLine >= Line - 1 && range->fromLine < Line + Count) // delete inside affectec area
|
|
|
|
mAllFoldRanges.remove(i);
|
|
|
|
else if (range->fromLine >= Line + Count) // Move after affected area
|
|
|
|
range->move(-Count);
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::foldOnListCleared()
|
|
|
|
{
|
|
|
|
mAllFoldRanges.clear();
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:15:43 +08:00
|
|
|
void SynEdit::rescanFolds()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
if (!mUseCodeFolding)
|
|
|
|
return;
|
|
|
|
rescanForFoldRanges();
|
|
|
|
invalidateGutter();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_deleter(SynEditFoldRanges *) {}
|
|
|
|
|
|
|
|
void SynEdit::rescanForFoldRanges()
|
|
|
|
{
|
|
|
|
// Delete all uncollapsed folds
|
2022-07-03 12:00:05 +08:00
|
|
|
// for (int i=mAllFoldRanges.count()-1;i>=0;i--) {
|
|
|
|
// PSynEditFoldRange range =mAllFoldRanges[i];
|
|
|
|
// if (!range->collapsed && !range->parentCollapsed())
|
|
|
|
// mAllFoldRanges.remove(i);
|
|
|
|
// }
|
2021-05-14 23:56:43 +08:00
|
|
|
|
|
|
|
// Did we leave any collapsed folds and are we viewing a code file?
|
|
|
|
if (mAllFoldRanges.count() > 0) {
|
2022-07-03 11:22:10 +08:00
|
|
|
SynEditFoldRanges ranges=mAllFoldRanges;
|
|
|
|
mAllFoldRanges.clear();
|
2021-05-14 23:56:43 +08:00
|
|
|
// Add folds to a separate list
|
|
|
|
PSynEditFoldRanges TemporaryAllFoldRanges = std::make_shared<SynEditFoldRanges>();
|
|
|
|
scanForFoldRanges(TemporaryAllFoldRanges);
|
2022-07-03 11:22:10 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
// Combine new with old folds, preserve parent order
|
|
|
|
for (int i = 0; i< TemporaryAllFoldRanges->count();i++) {
|
2022-07-03 12:00:05 +08:00
|
|
|
PSynEditFoldRange tempFoldRange=TemporaryAllFoldRanges->range(i);
|
2022-01-27 18:34:18 +08:00
|
|
|
int j=0;
|
2022-06-28 21:46:20 +08:00
|
|
|
while (j <ranges.count()) {
|
2022-07-03 12:00:05 +08:00
|
|
|
PSynEditFoldRange foldRange = ranges[j];
|
|
|
|
//qDebug()<<TemporaryAllFoldRanges->range(i)->fromLine<<ranges[j]->fromLine;
|
|
|
|
if (tempFoldRange->fromLine == foldRange->fromLine
|
|
|
|
&& tempFoldRange->toLine == foldRange->toLine) {
|
|
|
|
//qDebug()<<"-"<<foldRange->fromLine;
|
|
|
|
mAllFoldRanges.add(foldRange);
|
2021-05-14 23:56:43 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-01-27 18:34:18 +08:00
|
|
|
j++;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2022-07-03 12:00:05 +08:00
|
|
|
if (j>=ranges.count()) {
|
|
|
|
//qDebug()<<"--"<<tempFoldRange->fromLine;
|
|
|
|
mAllFoldRanges.add(tempFoldRange);
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-09-26 22:39:26 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
// We ended up with no folds after deleting, just pass standard data...
|
|
|
|
PSynEditFoldRanges temp(&mAllFoldRanges, null_deleter);
|
|
|
|
scanForFoldRanges(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::scanForFoldRanges(PSynEditFoldRanges TopFoldRanges)
|
|
|
|
{
|
2021-05-27 01:05:49 +08:00
|
|
|
PSynEditFoldRanges parentFoldRanges = TopFoldRanges;
|
2021-09-26 22:39:26 +08:00
|
|
|
// Recursively scan for folds (all types)
|
|
|
|
for (int i= 0 ; i< mCodeFolding.foldRegions.count() ; i++ ) {
|
|
|
|
findSubFoldRange(TopFoldRanges, i,parentFoldRanges,PSynEditFoldRange());
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//this func should only be used in findSubFoldRange
|
|
|
|
int SynEdit::lineHasChar(int Line, int startChar, QChar character, const QString& highlighterAttrName) {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString CurLine = mDocument->getString(Line);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (!mHighlighter){
|
|
|
|
for (int i=startChar; i<CurLine.length();i++) {
|
|
|
|
if (CurLine[i]==character) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
mHighlighter->setState(mLines->ranges(Line),
|
|
|
|
mLines->braceLevels(Line),
|
|
|
|
mLines->bracketLevels(Line),
|
|
|
|
mLines->parenthesisLevels(Line));
|
|
|
|
mHighlighter->setLine(CurLine,Line);
|
|
|
|
*/
|
|
|
|
QString token;
|
|
|
|
while (!mHighlighter->eol()) {
|
|
|
|
token = mHighlighter->getToken();
|
|
|
|
PSynHighlighterAttribute attr = mHighlighter->getTokenAttribute();
|
|
|
|
if (token == character && attr->name()==highlighterAttrName)
|
|
|
|
return mHighlighter->getTokenPos();
|
|
|
|
mHighlighter->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-27 01:05:49 +08:00
|
|
|
void SynEdit::findSubFoldRange(PSynEditFoldRanges TopFoldRanges, int FoldIndex,PSynEditFoldRanges& parentFoldRanges, PSynEditFoldRange Parent)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
PSynEditFoldRange CollapsedFold;
|
|
|
|
int Line = 0;
|
|
|
|
QString CurLine;
|
2021-09-26 22:39:26 +08:00
|
|
|
if (!mHighlighter)
|
|
|
|
return;
|
2021-09-19 14:28:30 +08:00
|
|
|
bool useBraces = ( mCodeFolding.foldRegions.get(FoldIndex)->openSymbol == "{"
|
|
|
|
&& mCodeFolding.foldRegions.get(FoldIndex)->closeSymbol == "}");
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2022-04-19 21:18:41 +08:00
|
|
|
while (Line < mDocument->count()) { // index is valid for LinesToScan and fLines
|
2021-05-14 23:56:43 +08:00
|
|
|
// If there is a collapsed fold over here, skip it
|
|
|
|
CollapsedFold = collapsedFoldStartAtLine(Line + 1); // only collapsed folds remain
|
|
|
|
if (CollapsedFold) {
|
|
|
|
Line = CollapsedFold->toLine;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-19 14:28:30 +08:00
|
|
|
//we just use braceLevel
|
|
|
|
if (useBraces) {
|
2021-09-19 15:00:53 +08:00
|
|
|
// Find an opening character on this line
|
2022-04-19 21:18:41 +08:00
|
|
|
CurLine = mDocument->getString(Line);
|
|
|
|
if (mDocument->rightBraces(Line)>0) {
|
|
|
|
for (int i=0; i<mDocument->rightBraces(Line);i++) {
|
2021-09-19 15:00:53 +08:00
|
|
|
// Stop the recursion if we find a closing char, and return to our parent
|
|
|
|
if (Parent) {
|
|
|
|
Parent->toLine = Line + 1;
|
|
|
|
Parent = Parent->parent;
|
|
|
|
if (!Parent) {
|
|
|
|
parentFoldRanges = TopFoldRanges;
|
|
|
|
} else {
|
|
|
|
parentFoldRanges = Parent->subFoldRanges;
|
|
|
|
}
|
|
|
|
}
|
2021-09-19 14:28:30 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument->leftBraces(Line)>0) {
|
|
|
|
for (int i=0; i<mDocument->leftBraces(Line);i++) {
|
2021-09-23 12:06:26 +08:00
|
|
|
// Add it to the top list of folds
|
|
|
|
Parent = parentFoldRanges->addByParts(
|
|
|
|
Parent,
|
|
|
|
TopFoldRanges,
|
|
|
|
Line + 1,
|
|
|
|
mCodeFolding.foldRegions.get(FoldIndex),
|
|
|
|
Line + 1);
|
|
|
|
parentFoldRanges = Parent->subFoldRanges;
|
|
|
|
}
|
|
|
|
}
|
2021-09-19 14:28:30 +08:00
|
|
|
} else {
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-09-19 14:28:30 +08:00
|
|
|
// Find an opening character on this line
|
2022-04-19 21:18:41 +08:00
|
|
|
CurLine = mDocument->getString(Line);
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2022-04-19 21:18:41 +08:00
|
|
|
mHighlighter->setState(mDocument->ranges(Line));
|
2021-09-19 14:28:30 +08:00
|
|
|
mHighlighter->setLine(CurLine,Line);
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-09-19 14:28:30 +08:00
|
|
|
QString token;
|
|
|
|
int pos;
|
|
|
|
while (!mHighlighter->eol()) {
|
|
|
|
token = mHighlighter->getToken();
|
|
|
|
pos = mHighlighter->getTokenPos()+token.length();
|
|
|
|
PSynHighlighterAttribute attr = mHighlighter->getTokenAttribute();
|
|
|
|
// We've found a starting character and it have proper highlighting (ignore stuff inside comments...)
|
|
|
|
if (token == mCodeFolding.foldRegions.get(FoldIndex)->openSymbol && attr->name()==mCodeFolding.foldRegions.get(FoldIndex)->highlight) {
|
|
|
|
// And ignore lines with both opening and closing chars in them
|
|
|
|
if (lineHasChar(Line,pos,mCodeFolding.foldRegions.get(FoldIndex)->closeSymbol,
|
|
|
|
mCodeFolding.foldRegions.get(FoldIndex)->highlight)<0) {
|
|
|
|
// Add it to the top list of folds
|
|
|
|
Parent = parentFoldRanges->addByParts(
|
|
|
|
Parent,
|
|
|
|
TopFoldRanges,
|
|
|
|
Line + 1,
|
|
|
|
mCodeFolding.foldRegions.get(FoldIndex),
|
|
|
|
Line + 1);
|
|
|
|
parentFoldRanges = Parent->subFoldRanges;
|
|
|
|
|
|
|
|
// Skip until a newline
|
|
|
|
break;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-09-19 14:28:30 +08:00
|
|
|
} else if (token == mCodeFolding.foldRegions.get(FoldIndex)->closeSymbol && attr->name()==mCodeFolding.foldRegions.get(FoldIndex)->highlight) {
|
|
|
|
// And ignore lines with both opening and closing chars in them
|
|
|
|
if (lineHasChar(Line,pos,mCodeFolding.foldRegions.get(FoldIndex)->openSymbol,
|
|
|
|
mCodeFolding.foldRegions.get(FoldIndex)->highlight)<0) {
|
|
|
|
// Stop the recursion if we find a closing char, and return to our parent
|
|
|
|
if (Parent) {
|
|
|
|
Parent->toLine = Line + 1;
|
|
|
|
Parent = Parent->parent;
|
|
|
|
if (!Parent) {
|
|
|
|
parentFoldRanges = TopFoldRanges;
|
|
|
|
} else {
|
|
|
|
parentFoldRanges = Parent->subFoldRanges;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip until a newline
|
|
|
|
break;
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2021-09-19 14:28:30 +08:00
|
|
|
mHighlighter->next();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Line++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynEditFoldRange SynEdit::collapsedFoldStartAtLine(int Line)
|
|
|
|
{
|
|
|
|
for (int i = 0; i< mAllFoldRanges.count() - 1; i++ ) {
|
|
|
|
if (mAllFoldRanges[i]->fromLine == Line && mAllFoldRanges[i]->collapsed) {
|
|
|
|
return mAllFoldRanges[i];
|
|
|
|
} else if (mAllFoldRanges[i]->fromLine > Line) {
|
|
|
|
break; // sorted by line. don't bother scanning further
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PSynEditFoldRange();
|
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
void SynEdit::doOnPaintTransientEx(SynTransientType , bool )
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
//todo: we can't draw to canvas outside paintEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::initializeCaret()
|
|
|
|
{
|
2021-05-24 18:11:07 +08:00
|
|
|
//showCaret();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:46:20 +08:00
|
|
|
PSynEditFoldRange SynEdit::foldStartAtLine(int Line) const
|
2021-05-21 23:33:53 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i<mAllFoldRanges.count();i++) {
|
|
|
|
PSynEditFoldRange range = mAllFoldRanges[i];
|
|
|
|
if (range->fromLine == Line ){
|
|
|
|
return range;
|
|
|
|
} else if (range->fromLine>Line)
|
|
|
|
break; // sorted by line. don't bother scanning further
|
|
|
|
}
|
2021-05-24 18:11:07 +08:00
|
|
|
return PSynEditFoldRange();
|
2021-05-21 23:33:53 +08:00
|
|
|
}
|
|
|
|
|
2022-06-28 21:46:20 +08:00
|
|
|
bool SynEdit::foldCollapsedBetween(int startLine, int endLine) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i<mAllFoldRanges.count();i++) {
|
|
|
|
PSynEditFoldRange range = mAllFoldRanges[i];
|
|
|
|
if (startLine >=range->fromLine && range->fromLine<=endLine
|
|
|
|
&& (range->collapsed || range->parentCollapsed())){
|
|
|
|
return true;
|
|
|
|
} else if (range->fromLine>endLine)
|
|
|
|
break; // sorted by line. don't bother scanning further
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-21 23:33:53 +08:00
|
|
|
QString SynEdit::substringByColumns(const QString &s, int startColumn, int &colLen)
|
|
|
|
{
|
|
|
|
|
|
|
|
int len = s.length();
|
|
|
|
int columns = 0;
|
|
|
|
int i = 0;
|
2021-11-03 23:51:47 +08:00
|
|
|
int oldColumns=0;
|
2021-05-21 23:33:53 +08:00
|
|
|
while (columns < startColumn) {
|
|
|
|
oldColumns = columns;
|
2021-06-04 21:27:51 +08:00
|
|
|
if (i>=len)
|
|
|
|
break;
|
|
|
|
if (s[i] == '\t')
|
2022-04-19 21:18:41 +08:00
|
|
|
columns += tabWidth() - (columns % tabWidth());
|
2021-05-21 23:33:53 +08:00
|
|
|
else
|
|
|
|
columns += charColumns(s[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
QString result;
|
|
|
|
if (i>=len) {
|
|
|
|
colLen = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (colLen>result.capacity()) {
|
|
|
|
result.resize(colLen);
|
|
|
|
}
|
|
|
|
int j=0;
|
|
|
|
if (i>0) {
|
|
|
|
result[0]=s[i-1];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
while (i<len && columns<startColumn+colLen) {
|
|
|
|
result[j]=s[i];
|
|
|
|
if (i < len && s[i] == '\t')
|
2022-04-19 21:18:41 +08:00
|
|
|
columns += tabWidth() - (columns % tabWidth());
|
2021-05-21 23:33:53 +08:00
|
|
|
else
|
|
|
|
columns += charColumns(s[i]);
|
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
result.resize(j);
|
|
|
|
colLen = columns-oldColumns;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynEditFoldRange SynEdit::foldAroundLine(int Line)
|
|
|
|
{
|
|
|
|
return foldAroundLineEx(Line,false,false,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
PSynEditFoldRange SynEdit::foldAroundLineEx(int Line, bool WantCollapsed, bool AcceptFromLine, bool AcceptToLine)
|
|
|
|
{
|
2021-05-24 00:41:00 +08:00
|
|
|
// Check global list
|
|
|
|
PSynEditFoldRange Result = checkFoldRange(&mAllFoldRanges, Line, WantCollapsed, AcceptFromLine, AcceptToLine);
|
2021-05-21 23:33:53 +08:00
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
// Found an item in the top level list?
|
|
|
|
if (Result) {
|
|
|
|
while (true) {
|
|
|
|
PSynEditFoldRange ResultChild = checkFoldRange(Result->subFoldRanges.get(), Line, WantCollapsed, AcceptFromLine, AcceptToLine);
|
|
|
|
if (!ResultChild)
|
|
|
|
break;
|
|
|
|
Result = ResultChild; // repeat for this one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
2021-05-21 23:33:53 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
PSynEditFoldRange SynEdit::checkFoldRange(SynEditFoldRanges *FoldRangeToCheck, int Line, bool WantCollapsed, bool AcceptFromLine, bool AcceptToLine)
|
2021-05-21 23:33:53 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i< FoldRangeToCheck->count(); i++) {
|
|
|
|
PSynEditFoldRange range = (*FoldRangeToCheck)[i];
|
|
|
|
if (((range->fromLine < Line) || ((range->fromLine <= Line) && AcceptFromLine)) &&
|
|
|
|
((range->toLine > Line) || ((range->toLine >= Line) && AcceptToLine))) {
|
|
|
|
if (range->collapsed == WantCollapsed) {
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PSynEditFoldRange();
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
PSynEditFoldRange SynEdit::foldEndAtLine(int Line)
|
|
|
|
{
|
|
|
|
for (int i = 0; i<mAllFoldRanges.count();i++) {
|
|
|
|
PSynEditFoldRange range = mAllFoldRanges[i];
|
|
|
|
if (range->toLine == Line ){
|
|
|
|
return range;
|
|
|
|
} else if (range->fromLine>Line)
|
|
|
|
break; // sorted by line. don't bother scanning further
|
|
|
|
}
|
|
|
|
return PSynEditFoldRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::paintCaret(QPainter &painter, const QRect rcClip)
|
|
|
|
{
|
2021-05-24 18:11:07 +08:00
|
|
|
if (m_blinkStatus!=1)
|
2021-05-24 00:41:00 +08:00
|
|
|
return;
|
2021-05-24 18:11:07 +08:00
|
|
|
painter.setClipRect(rcClip);
|
2021-05-24 00:41:00 +08:00
|
|
|
SynEditCaretType ct;
|
|
|
|
if (this->mInserting) {
|
|
|
|
ct = mInsertCaret;
|
|
|
|
} else {
|
|
|
|
ct =mOverwriteCaret;
|
|
|
|
}
|
2021-11-13 12:33:33 +08:00
|
|
|
QColor caretColor;
|
2021-09-15 14:35:37 +08:00
|
|
|
if (mCaretUseTextColor) {
|
2021-11-13 12:33:33 +08:00
|
|
|
caretColor = mForegroundColor;
|
2021-09-15 14:35:37 +08:00
|
|
|
} else {
|
2021-11-13 12:33:33 +08:00
|
|
|
caretColor = mCaretColor;
|
2021-09-15 14:35:37 +08:00
|
|
|
}
|
2021-05-24 00:41:00 +08:00
|
|
|
switch(ct) {
|
2021-11-13 12:33:33 +08:00
|
|
|
case SynEditCaretType::ctVerticalLine: {
|
|
|
|
QRect caretRC;
|
2022-03-25 22:13:00 +08:00
|
|
|
int size = std::max(1, mTextHeight/15);
|
2021-11-13 12:33:33 +08:00
|
|
|
caretRC.setLeft(rcClip.left()+1);
|
|
|
|
caretRC.setTop(rcClip.top());
|
|
|
|
caretRC.setBottom(rcClip.bottom());
|
|
|
|
caretRC.setRight(rcClip.left()+1+size);
|
|
|
|
painter.fillRect(caretRC,caretColor);
|
2021-05-24 00:41:00 +08:00
|
|
|
break;
|
2021-11-13 12:33:33 +08:00
|
|
|
}
|
|
|
|
case SynEditCaretType::ctHorizontalLine: {
|
|
|
|
QRect caretRC;
|
2022-03-25 22:13:00 +08:00
|
|
|
int size = std::max(1,mTextHeight/15);
|
2021-11-13 12:33:33 +08:00
|
|
|
caretRC.setLeft(rcClip.left());
|
|
|
|
caretRC.setTop(rcClip.bottom()-1+size);
|
|
|
|
caretRC.setBottom(rcClip.bottom()-1);
|
|
|
|
caretRC.setRight(rcClip.right());
|
|
|
|
painter.fillRect(caretRC,caretColor);
|
2021-05-24 00:41:00 +08:00
|
|
|
break;
|
2021-11-13 12:33:33 +08:00
|
|
|
}
|
2021-05-24 00:41:00 +08:00
|
|
|
case SynEditCaretType::ctBlock:
|
2021-11-13 12:33:33 +08:00
|
|
|
painter.fillRect(rcClip, caretColor);
|
2021-05-24 00:41:00 +08:00
|
|
|
break;
|
|
|
|
case SynEditCaretType::ctHalfBlock:
|
|
|
|
QRect rc=rcClip;
|
|
|
|
rc.setTop(rcClip.top()+rcClip.height() / 2);
|
2021-11-13 12:33:33 +08:00
|
|
|
painter.fillRect(rcClip, caretColor);
|
2021-05-24 18:11:07 +08:00
|
|
|
break;
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::textOffset() const
|
2021-05-24 21:48:03 +08:00
|
|
|
{
|
|
|
|
return mGutterWidth + 2 - (mLeftChar-1)*mCharWidth;
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:33:25 +08:00
|
|
|
SynEditorCommand SynEdit::TranslateKeyCode(int key, Qt::KeyboardModifiers modifiers)
|
|
|
|
{
|
|
|
|
PSynEditKeyStroke keyStroke = mKeyStrokes.findKeycode2(mLastKey,mLastKeyModifiers,
|
|
|
|
key, modifiers);
|
|
|
|
SynEditorCommand cmd=SynEditorCommand::ecNone;
|
|
|
|
if (keyStroke)
|
|
|
|
cmd = keyStroke->command();
|
|
|
|
else {
|
|
|
|
keyStroke = mKeyStrokes.findKeycode(key,modifiers);
|
|
|
|
if (keyStroke)
|
|
|
|
cmd = keyStroke->command();
|
|
|
|
}
|
|
|
|
if (cmd == SynEditorCommand::ecNone) {
|
|
|
|
mLastKey = key;
|
|
|
|
mLastKeyModifiers = modifiers;
|
|
|
|
} else {
|
|
|
|
mLastKey = 0;
|
|
|
|
mLastKeyModifiers = Qt::NoModifier;
|
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onSizeOrFontChanged(bool bFont)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
if (mCharWidth != 0) {
|
|
|
|
mCharsInWindow = std::max(clientWidth() - mGutterWidth - 2, 0) / mCharWidth;
|
|
|
|
mLinesInWindow = clientHeight() / mTextHeight;
|
2021-10-29 19:20:55 +08:00
|
|
|
bool scrollBarChangedSettings = mStateFlags.testFlag(SynStateFlag::sfScrollbarChanged);
|
2021-05-14 23:56:43 +08:00
|
|
|
if (bFont) {
|
|
|
|
if (mGutter.showLineNumbers())
|
2021-09-02 12:14:02 +08:00
|
|
|
onGutterChanged();
|
2021-05-14 23:56:43 +08:00
|
|
|
else
|
|
|
|
updateScrollbars();
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfCaretChanged,false);
|
|
|
|
invalidate();
|
|
|
|
} else
|
|
|
|
updateScrollbars();
|
2021-10-29 19:20:55 +08:00
|
|
|
mStateFlags.setFlag(SynStateFlag::sfScrollbarChanged,scrollBarChangedSettings);
|
2021-06-08 21:41:42 +08:00
|
|
|
//if (!mOptions.testFlag(SynEditorOption::eoScrollPastEol))
|
|
|
|
setLeftChar(mLeftChar);
|
|
|
|
//if (!mOptions.testFlag(SynEditorOption::eoScrollPastEof))
|
|
|
|
setTopLine(mTopLine);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onChanged()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
emit changed();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onScrolled(int)
|
2021-05-24 21:48:03 +08:00
|
|
|
{
|
|
|
|
mLeftChar = horizontalScrollBar()->value();
|
|
|
|
mTopLine = verticalScrollBar()->value();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2022-03-04 19:04:45 +08:00
|
|
|
int SynEdit::mouseSelectionScrollSpeed() const
|
|
|
|
{
|
|
|
|
return mMouseSelectionScrollSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setMouseSelectionScrollSpeed(int newMouseSelectionScrollSpeed)
|
|
|
|
{
|
|
|
|
mMouseSelectionScrollSpeed = newMouseSelectionScrollSpeed;
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:31:26 +08:00
|
|
|
const QFont &SynEdit::fontForNonAscii() const
|
|
|
|
{
|
|
|
|
return mFontForNonAscii;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setFontForNonAscii(const QFont &newFontForNonAscii)
|
|
|
|
{
|
|
|
|
mFontForNonAscii = newFontForNonAscii;
|
|
|
|
mFontForNonAscii.setStyleStrategy(QFont::PreferAntialias);
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:09:12 +08:00
|
|
|
const QColor &SynEdit::backgroundColor() const
|
|
|
|
{
|
|
|
|
return mBackgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setBackgroundColor(const QColor &newBackgroundColor)
|
|
|
|
{
|
|
|
|
mBackgroundColor = newBackgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QColor &SynEdit::foregroundColor() const
|
|
|
|
{
|
|
|
|
return mForegroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setForegroundColor(const QColor &newForegroundColor)
|
|
|
|
{
|
|
|
|
mForegroundColor = newForegroundColor;
|
|
|
|
}
|
|
|
|
|
2021-10-20 12:27:44 +08:00
|
|
|
int SynEdit::mouseWheelScrollSpeed() const
|
|
|
|
{
|
|
|
|
return mMouseWheelScrollSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setMouseWheelScrollSpeed(int newMouseWheelScrollSpeed)
|
|
|
|
{
|
|
|
|
mMouseWheelScrollSpeed = newMouseWheelScrollSpeed;
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:59:03 +08:00
|
|
|
const PSynHighlighterAttribute &SynEdit::rainbowAttr3() const
|
|
|
|
{
|
|
|
|
return mRainbowAttr3;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PSynHighlighterAttribute &SynEdit::rainbowAttr2() const
|
|
|
|
{
|
|
|
|
return mRainbowAttr2;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PSynHighlighterAttribute &SynEdit::rainbowAttr1() const
|
|
|
|
{
|
|
|
|
return mRainbowAttr1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PSynHighlighterAttribute &SynEdit::rainbowAttr0() const
|
|
|
|
{
|
|
|
|
return mRainbowAttr0;
|
|
|
|
}
|
|
|
|
|
2021-09-15 14:35:37 +08:00
|
|
|
bool SynEdit::caretUseTextColor() const
|
|
|
|
{
|
|
|
|
return mCaretUseTextColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setCaretUseTextColor(bool newCaretUseTextColor)
|
|
|
|
{
|
|
|
|
mCaretUseTextColor = newCaretUseTextColor;
|
|
|
|
}
|
|
|
|
|
2021-09-04 00:13:42 +08:00
|
|
|
const QColor &SynEdit::rightEdgeColor() const
|
|
|
|
{
|
|
|
|
return mRightEdgeColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setRightEdgeColor(const QColor &newRightEdgeColor)
|
|
|
|
{
|
|
|
|
if (newRightEdgeColor!=mRightEdgeColor) {
|
|
|
|
mRightEdgeColor = newRightEdgeColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::rightEdge() const
|
|
|
|
{
|
|
|
|
return mRightEdge;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setRightEdge(int newRightEdge)
|
|
|
|
{
|
|
|
|
if (mRightEdge != newRightEdge) {
|
|
|
|
mRightEdge = newRightEdge;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 09:01:40 +08:00
|
|
|
const QColor &SynEdit::selectedBackground() const
|
|
|
|
{
|
|
|
|
return mSelectedBackground;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setSelectedBackground(const QColor &newSelectedBackground)
|
|
|
|
{
|
|
|
|
mSelectedBackground = newSelectedBackground;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QColor &SynEdit::selectedForeground() const
|
|
|
|
{
|
|
|
|
return mSelectedForeground;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setSelectedForeground(const QColor &newSelectedForeground)
|
|
|
|
{
|
|
|
|
mSelectedForeground = newSelectedForeground;
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:05:19 +08:00
|
|
|
int SynEdit::textHeight() const
|
|
|
|
{
|
|
|
|
return mTextHeight;
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
bool SynEdit::readOnly() const
|
|
|
|
{
|
|
|
|
return mReadOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setReadOnly(bool readOnly)
|
|
|
|
{
|
|
|
|
if (mReadOnly != readOnly) {
|
|
|
|
mReadOnly = readOnly;
|
2021-08-29 10:14:07 +08:00
|
|
|
emit statusChanged(scReadOnly);
|
2021-06-12 22:36:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:12:23 +08:00
|
|
|
SynGutter& SynEdit::gutter()
|
|
|
|
{
|
|
|
|
return mGutter;
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
SynEditCaretType SynEdit::insertCaret() const
|
2021-06-07 11:02:03 +08:00
|
|
|
{
|
|
|
|
return mInsertCaret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setInsertCaret(const SynEditCaretType &insertCaret)
|
|
|
|
{
|
|
|
|
mInsertCaret = insertCaret;
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
SynEditCaretType SynEdit::overwriteCaret() const
|
2021-06-07 11:02:03 +08:00
|
|
|
{
|
|
|
|
return mOverwriteCaret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setOverwriteCaret(const SynEditCaretType &overwriteCaret)
|
|
|
|
{
|
|
|
|
mOverwriteCaret = overwriteCaret;
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
QColor SynEdit::activeLineColor() const
|
2021-06-07 11:02:03 +08:00
|
|
|
{
|
|
|
|
return mActiveLineColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setActiveLineColor(const QColor &activeLineColor)
|
|
|
|
{
|
|
|
|
if (mActiveLineColor!=activeLineColor) {
|
|
|
|
mActiveLineColor = activeLineColor;
|
|
|
|
invalidateLine(mCaretY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 22:36:23 +08:00
|
|
|
QColor SynEdit::caretColor() const
|
2021-06-07 11:02:03 +08:00
|
|
|
{
|
|
|
|
return mCaretColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setCaretColor(const QColor &caretColor)
|
|
|
|
{
|
|
|
|
mCaretColor = caretColor;
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:18:41 +08:00
|
|
|
void SynEdit::setTabWidth(int newTabWidth)
|
2021-06-07 11:02:03 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (newTabWidth!=tabWidth()) {
|
|
|
|
mDocument->setTabWidth(newTabWidth);
|
2021-06-07 11:02:03 +08:00
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SynEditorOptions SynEdit::getOptions() const
|
|
|
|
{
|
|
|
|
return mOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setOptions(const SynEditorOptions &Value)
|
|
|
|
{
|
|
|
|
if (Value != mOptions) {
|
2021-10-21 19:33:11 +08:00
|
|
|
//bool bSetDrag = mOptions.testFlag(eoDropFiles) != Value.testFlag(eoDropFiles);
|
2021-06-08 21:41:42 +08:00
|
|
|
//if (!mOptions.testFlag(eoScrollPastEol))
|
|
|
|
setLeftChar(mLeftChar);
|
|
|
|
//if (!mOptions.testFlag(eoScrollPastEof))
|
|
|
|
setTopLine(mTopLine);
|
2021-06-07 11:02:03 +08:00
|
|
|
|
|
|
|
bool bUpdateAll = Value.testFlag(eoShowSpecialChars) != mOptions.testFlag(eoShowSpecialChars);
|
|
|
|
if (!bUpdateAll)
|
|
|
|
bUpdateAll = Value.testFlag(eoShowRainbowColor) != mOptions.testFlag(eoShowRainbowColor);
|
|
|
|
//bool bUpdateScroll = (Options * ScrollOptions)<>(Value * ScrollOptions);
|
|
|
|
bool bUpdateScroll = true;
|
|
|
|
mOptions = Value;
|
|
|
|
|
|
|
|
// constrain caret position to MaxScrollWidth if eoScrollPastEol is enabled
|
|
|
|
internalSetCaretXY(caretXY());
|
|
|
|
if (mOptions.testFlag(eoScrollPastEol)) {
|
|
|
|
BufferCoord vTempBlockBegin = blockBegin();
|
|
|
|
BufferCoord vTempBlockEnd = blockEnd();
|
|
|
|
setBlockBegin(vTempBlockBegin);
|
|
|
|
setBlockEnd(vTempBlockEnd);
|
|
|
|
}
|
|
|
|
updateScrollbars();
|
|
|
|
// (un)register HWND as drop target
|
|
|
|
// if bSetDrag and not (csDesigning in ComponentState) and HandleAllocated then
|
|
|
|
// DragAcceptFiles(Handle, (eoDropFiles in fOptions));
|
|
|
|
if (bUpdateAll)
|
|
|
|
invalidate();
|
|
|
|
if (bUpdateScroll)
|
|
|
|
updateScrollbars();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 23:43:45 +08:00
|
|
|
void SynEdit::doAddStr(const QString &s)
|
|
|
|
{
|
2022-05-18 23:17:06 +08:00
|
|
|
if (mInserting == false && !selAvail()) {
|
|
|
|
switch(mActiveSelectionMode) {
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Column: {
|
2022-05-18 23:17:06 +08:00
|
|
|
//we can't use blockBegin()/blockEnd()
|
2022-03-25 22:13:00 +08:00
|
|
|
BufferCoord start=blockBegin();
|
|
|
|
BufferCoord end=blockEnd();
|
2022-07-04 10:01:40 +08:00
|
|
|
if (start.line > end.line )
|
2022-05-18 23:17:06 +08:00
|
|
|
std::swap(start,end);
|
2022-07-04 10:01:40 +08:00
|
|
|
start.ch+=s.length(); // make sure we select a whole char in the start line
|
2022-05-18 23:17:06 +08:00
|
|
|
setBlockBegin(start);
|
|
|
|
setBlockEnd(end);
|
|
|
|
}
|
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Line:
|
2022-05-18 23:17:06 +08:00
|
|
|
//do nothing;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
setSelLength(s.length());
|
2022-03-25 22:13:00 +08:00
|
|
|
}
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
2021-10-06 23:35:45 +08:00
|
|
|
doSetSelText(s);
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doUndo()
|
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//Remove Group Break;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mUndoList->lastChangeReason() == SynChangeReason::GroupBreak) {
|
2021-06-05 23:43:45 +08:00
|
|
|
int OldBlockNumber = mRedoList->blockChangeNumber();
|
|
|
|
auto action = finally([&,this]{
|
|
|
|
mRedoList->setBlockChangeNumber(OldBlockNumber);
|
|
|
|
});
|
2022-07-04 10:01:40 +08:00
|
|
|
PSynEditUndoItem item = mUndoList->popItem();
|
|
|
|
mRedoList->setBlockChangeNumber(item->changeNumber());
|
|
|
|
mRedoList->addGroupBreak();
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
PSynEditUndoItem item = mUndoList->peekItem();
|
|
|
|
if (item) {
|
|
|
|
int oldChangeNumber = item->changeNumber();
|
|
|
|
int saveChangeNumber = mRedoList->blockChangeNumber();
|
|
|
|
mRedoList->setBlockChangeNumber(item->changeNumber());
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
|
|
|
auto action = finally([&,this] {
|
2022-07-04 10:01:40 +08:00
|
|
|
mRedoList->setBlockChangeNumber(saveChangeNumber);
|
2021-06-05 23:43:45 +08:00
|
|
|
});
|
2022-03-01 23:35:01 +08:00
|
|
|
//skip group chain breakers
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mUndoList->lastChangeReason()==SynChangeReason::GroupBreak) {
|
|
|
|
while (!mUndoList->isEmpty() && mUndoList->lastChangeReason()==SynChangeReason::GroupBreak) {
|
2022-03-01 23:35:01 +08:00
|
|
|
doUndoItem();
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
SynChangeReason lastChange = mUndoList->lastChangeReason();
|
|
|
|
bool keepGoing;
|
2021-06-05 23:43:45 +08:00
|
|
|
do {
|
|
|
|
doUndoItem();
|
2022-07-04 10:01:40 +08:00
|
|
|
item = mUndoList->peekItem();
|
|
|
|
if (!item)
|
|
|
|
keepGoing = false;
|
2021-06-05 23:43:45 +08:00
|
|
|
else {
|
2022-07-04 10:01:40 +08:00
|
|
|
if (item->changeNumber() == oldChangeNumber)
|
|
|
|
keepGoing = true;
|
2021-06-05 23:43:45 +08:00
|
|
|
else {
|
2022-07-04 10:01:40 +08:00
|
|
|
keepGoing = (mOptions.testFlag(eoGroupUndo) &&
|
|
|
|
(lastChange == item->changeReason()) );
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
oldChangeNumber=item->changeNumber();
|
|
|
|
lastChange = item->changeReason();
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
} while (keepGoing);
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doUndoItem()
|
|
|
|
{
|
|
|
|
mUndoing = true;
|
|
|
|
bool ChangeScrollPastEol = ! mOptions.testFlag(eoScrollPastEol);
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
PSynEditUndoItem item = mUndoList->popItem();
|
|
|
|
if (item) {
|
|
|
|
setActiveSelectionMode(item->changeSelMode());
|
2021-06-05 23:43:45 +08:00
|
|
|
incPaintLock();
|
|
|
|
auto action = finally([&,this]{
|
|
|
|
mUndoing = false;
|
|
|
|
if (ChangeScrollPastEol)
|
|
|
|
mOptions.setFlag(eoScrollPastEol,false);
|
|
|
|
decPaintLock();
|
|
|
|
});
|
|
|
|
mOptions.setFlag(eoScrollPastEol);
|
2022-07-04 10:01:40 +08:00
|
|
|
switch(item->changeReason()) {
|
|
|
|
case SynChangeReason::Caret:
|
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
2021-06-05 23:43:45 +08:00
|
|
|
caretXY(),
|
2022-07-02 10:39:31 +08:00
|
|
|
caretXY(), QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeSelMode());
|
|
|
|
internalSetCaretXY(item->changeStartPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::LeftTop:
|
2022-06-12 22:54:34 +08:00
|
|
|
BufferCoord p;
|
2022-07-04 10:01:40 +08:00
|
|
|
p.ch = leftChar();
|
|
|
|
p.line = topLine();
|
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
2022-06-12 22:54:34 +08:00
|
|
|
p,
|
2022-07-02 10:39:31 +08:00
|
|
|
p, QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeSelMode());
|
|
|
|
setLeftChar(item->changeStartPos().ch);
|
|
|
|
setTopLine(item->changeStartPos().line);
|
2022-06-12 22:54:34 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::Selection:
|
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
2022-07-02 10:39:31 +08:00
|
|
|
mBlockBegin,
|
|
|
|
mBlockEnd,
|
|
|
|
QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeSelMode());
|
|
|
|
setCaretAndSelection(caretXY(), item->changeStartPos(), item->changeEndPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::Insert: {
|
|
|
|
QStringList tmpText = getContent(item->changeStartPos(),item->changeEndPos(),item->changeSelMode());
|
|
|
|
doDeleteText(item->changeStartPos(),item->changeEndPos(),item->changeSelMode());
|
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
2022-07-02 10:39:31 +08:00
|
|
|
tmpText,
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeSelMode());
|
|
|
|
internalSetCaretXY(item->changeStartPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::MoveSelectionUp:
|
|
|
|
setBlockBegin(BufferCoord{item->changeStartPos().ch, item->changeStartPos().line-1});
|
|
|
|
setBlockEnd(BufferCoord{item->changeEndPos().ch, item->changeEndPos().line-1});
|
2022-07-02 10:39:31 +08:00
|
|
|
doMoveSelDown();
|
2022-07-04 10:01:40 +08:00
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
|
|
|
item->changeText(),
|
|
|
|
item->changeSelMode());
|
2022-04-01 23:10:38 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::MoveSelectionDown:
|
|
|
|
setBlockBegin(BufferCoord{item->changeStartPos().ch, item->changeStartPos().line+1});
|
|
|
|
setBlockEnd(BufferCoord{item->changeEndPos().ch, item->changeEndPos().line+1});
|
2022-07-02 10:39:31 +08:00
|
|
|
doMoveSelUp();
|
2022-07-04 10:01:40 +08:00
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
|
|
|
item->changeText(),
|
|
|
|
item->changeSelMode());
|
2022-04-01 23:10:38 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::Delete: {
|
2021-06-05 23:43:45 +08:00
|
|
|
// If there's no selection, we have to set
|
|
|
|
// the Caret's position manualy.
|
2022-07-02 17:59:07 +08:00
|
|
|
// qDebug()<<"undo delete";
|
|
|
|
// qDebug()<<Item->changeText();
|
|
|
|
// qDebug()<<Item->changeStartPos().Line<<Item->changeStartPos().Char;
|
2022-07-04 10:01:40 +08:00
|
|
|
doInsertText(item->changeStartPos(),item->changeText(),item->changeSelMode(),
|
|
|
|
item->changeStartPos().line,
|
|
|
|
item->changeEndPos().line);
|
|
|
|
internalSetCaretXY(item->changeEndPos());
|
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
|
|
|
item->changeText(),
|
|
|
|
item->changeSelMode());
|
2021-06-05 23:43:45 +08:00
|
|
|
ensureCursorPosVisible();
|
|
|
|
break;
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::LineBreak:{
|
2022-07-02 10:39:31 +08:00
|
|
|
QString s;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (!item->changeText().isEmpty()) {
|
|
|
|
s=item->changeText()[0];
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
2021-06-05 23:43:45 +08:00
|
|
|
// If there's no selection, we have to set
|
|
|
|
// the Caret's position manualy.
|
2022-07-04 10:01:40 +08:00
|
|
|
internalSetCaretXY(item->changeStartPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
if (mCaretY > 0) {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString TmpStr = mDocument->getString(mCaretY - 1);
|
2022-07-02 10:39:31 +08:00
|
|
|
if ( (mCaretX > TmpStr.length() + 1) && (leftSpaces(s) == 0))
|
2021-06-05 23:43:45 +08:00
|
|
|
TmpStr = TmpStr + QString(mCaretX - 1 - TmpStr.length(), ' ');
|
2022-07-02 10:39:31 +08:00
|
|
|
properSetLine(mCaretY - 1, TmpStr + s);
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->deleteAt(mCaretY);
|
2021-09-02 12:14:02 +08:00
|
|
|
doLinesDeleted(mCaretY, 1);
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
mRedoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
|
|
|
item->changeText(),
|
|
|
|
item->changeSelMode());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
2021-10-21 19:33:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doRedo()
|
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
PSynEditUndoItem item = mRedoList->peekItem();
|
|
|
|
if (!item)
|
2021-06-05 23:43:45 +08:00
|
|
|
return;
|
2022-07-04 10:01:40 +08:00
|
|
|
int oldChangeNumber = item->changeNumber();
|
|
|
|
int saveChangeNumber = mUndoList->blockChangeNumber();
|
|
|
|
mUndoList->setBlockChangeNumber(item->changeNumber());
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
|
|
|
auto action = finally([&,this]{
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->setBlockChangeNumber(saveChangeNumber);
|
2021-06-05 23:43:45 +08:00
|
|
|
});
|
2022-03-02 11:31:03 +08:00
|
|
|
//skip group chain breakers
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mRedoList->lastChangeReason()==SynChangeReason::GroupBreak) {
|
|
|
|
while (!mRedoList->isEmpty() && mRedoList->lastChangeReason()==SynChangeReason::GroupBreak) {
|
2022-03-02 11:31:03 +08:00
|
|
|
doRedoItem();
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
SynChangeReason lastChange = mRedoList->lastChangeReason();
|
|
|
|
bool keepGoing;
|
2021-06-05 23:43:45 +08:00
|
|
|
do {
|
|
|
|
doRedoItem();
|
2022-07-04 10:01:40 +08:00
|
|
|
item = mRedoList->peekItem();
|
|
|
|
if (!item)
|
|
|
|
keepGoing = false;
|
2021-06-05 23:43:45 +08:00
|
|
|
else {
|
2022-07-04 10:01:40 +08:00
|
|
|
if (item->changeNumber() == oldChangeNumber)
|
|
|
|
keepGoing = true;
|
2021-06-05 23:43:45 +08:00
|
|
|
else {
|
2022-07-04 10:01:40 +08:00
|
|
|
keepGoing = (mOptions.testFlag(eoGroupUndo) &&
|
|
|
|
(lastChange == item->changeReason()));
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
oldChangeNumber=item->changeNumber();
|
|
|
|
lastChange = item->changeReason();
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
} while (keepGoing);
|
2021-06-05 23:43:45 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
//Remove Group Break
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mRedoList->lastChangeReason() == SynChangeReason::GroupBreak) {
|
2021-06-05 23:43:45 +08:00
|
|
|
int OldBlockNumber = mUndoList->blockChangeNumber();
|
2022-07-04 10:01:40 +08:00
|
|
|
item = mRedoList->popItem();
|
2021-06-05 23:43:45 +08:00
|
|
|
{
|
|
|
|
auto action2=finally([&,this]{
|
|
|
|
mUndoList->setBlockChangeNumber(OldBlockNumber);
|
|
|
|
});
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->setBlockChangeNumber(item->changeNumber());
|
|
|
|
mUndoList->addGroupBreak();
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
updateModifiedStatus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doRedoItem()
|
|
|
|
{
|
2022-03-02 11:31:03 +08:00
|
|
|
mUndoing = true;
|
2021-06-05 23:43:45 +08:00
|
|
|
bool ChangeScrollPastEol = ! mOptions.testFlag(eoScrollPastEol);
|
2022-07-04 10:01:40 +08:00
|
|
|
PSynEditUndoItem item = mRedoList->popItem();
|
|
|
|
if (item) {
|
|
|
|
setActiveSelectionMode(item->changeSelMode());
|
2021-06-05 23:43:45 +08:00
|
|
|
incPaintLock();
|
|
|
|
mOptions.setFlag(eoScrollPastEol);
|
|
|
|
mUndoList->setInsideRedo(true);
|
|
|
|
auto action = finally([&,this]{
|
2022-03-02 11:31:03 +08:00
|
|
|
mUndoing = false;
|
2021-06-05 23:43:45 +08:00
|
|
|
mUndoList->setInsideRedo(false);
|
|
|
|
if (ChangeScrollPastEol)
|
|
|
|
mOptions.setFlag(eoScrollPastEol,false);
|
|
|
|
decPaintLock();
|
|
|
|
});
|
2022-07-04 10:01:40 +08:00
|
|
|
switch(item->changeReason()) {
|
|
|
|
case SynChangeReason::Caret:
|
|
|
|
mUndoList->addChange(
|
|
|
|
item->changeReason(),
|
2021-06-05 23:43:45 +08:00
|
|
|
caretXY(),
|
|
|
|
caretXY(),
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(),
|
2021-06-05 23:43:45 +08:00
|
|
|
mActiveSelectionMode);
|
2022-07-04 10:01:40 +08:00
|
|
|
internalSetCaretXY(item->changeStartPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::LeftTop:
|
2022-06-12 22:54:34 +08:00
|
|
|
BufferCoord p;
|
2022-07-04 10:01:40 +08:00
|
|
|
p.ch = leftChar();
|
|
|
|
p.line = topLine();
|
|
|
|
mUndoList->addChange(
|
|
|
|
item->changeReason(),
|
2022-06-12 22:54:34 +08:00
|
|
|
p,
|
2022-07-02 10:39:31 +08:00
|
|
|
p, QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeSelMode());
|
|
|
|
setLeftChar(item->changeStartPos().ch);
|
|
|
|
setTopLine(item->changeStartPos().line);
|
2022-06-12 22:54:34 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::Selection:
|
|
|
|
mUndoList->addChange(
|
|
|
|
item->changeReason(),
|
2022-07-02 10:39:31 +08:00
|
|
|
mBlockBegin,
|
|
|
|
mBlockEnd,
|
|
|
|
QStringList(),
|
2021-06-05 23:43:45 +08:00
|
|
|
mActiveSelectionMode);
|
|
|
|
setCaretAndSelection(
|
|
|
|
caretXY(),
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::MoveSelectionUp:
|
|
|
|
setBlockBegin(BufferCoord{item->changeStartPos().ch, item->changeStartPos().line});
|
|
|
|
setBlockEnd(BufferCoord{item->changeEndPos().ch, item->changeEndPos().line});
|
2022-07-02 10:39:31 +08:00
|
|
|
doMoveSelUp();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
|
|
|
item->changeText(),
|
|
|
|
item->changeSelMode());
|
2022-04-01 23:10:38 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::MoveSelectionDown:
|
|
|
|
setBlockBegin(BufferCoord{item->changeStartPos().ch, item->changeStartPos().line});
|
|
|
|
setBlockEnd(BufferCoord{item->changeEndPos().ch, item->changeEndPos().line});
|
2022-07-02 10:39:31 +08:00
|
|
|
doMoveSelDown();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
|
|
|
item->changeText(),
|
|
|
|
item->changeSelMode());
|
2022-04-01 23:10:38 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::Insert:
|
2021-06-05 23:43:45 +08:00
|
|
|
setCaretAndSelection(
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeStartPos());
|
|
|
|
doInsertText(item->changeStartPos(),item->changeText(), item->changeSelMode(),
|
|
|
|
item->changeStartPos().line,
|
|
|
|
item->changeEndPos().line);
|
|
|
|
internalSetCaretXY(item->changeEndPos());
|
|
|
|
mUndoList->addChange(item->changeReason(),
|
|
|
|
item->changeStartPos(),
|
|
|
|
item->changeEndPos(),
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
item->changeSelMode());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::Delete: {
|
|
|
|
doDeleteText(item->changeStartPos(),item->changeEndPos(),item->changeSelMode());
|
|
|
|
mUndoList->addChange(item->changeReason(), item->changeStartPos(),
|
|
|
|
item->changeEndPos(),item->changeText(),
|
|
|
|
item->changeSelMode());
|
|
|
|
internalSetCaretXY(item->changeStartPos());
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
|
|
|
};
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynChangeReason::LineBreak: {
|
|
|
|
BufferCoord CaretPt = item->changeStartPos();
|
|
|
|
mUndoList->addChange(item->changeReason(), item->changeStartPos(),
|
|
|
|
item->changeEndPos(),item->changeText(),
|
|
|
|
item->changeSelMode());
|
2021-06-05 23:43:45 +08:00
|
|
|
setCaretAndSelection(CaretPt, CaretPt, CaretPt);
|
2021-09-20 15:57:48 +08:00
|
|
|
commandProcessor(SynEditorCommand::ecLineBreak);
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-10-21 19:33:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-06-05 23:43:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:02:03 +08:00
|
|
|
void SynEdit::doZoomIn()
|
|
|
|
{
|
|
|
|
QFont newFont = font();
|
2021-12-16 19:14:14 +08:00
|
|
|
int size = newFont.pixelSize();
|
2021-06-07 11:02:03 +08:00
|
|
|
size++;
|
2021-12-16 19:14:14 +08:00
|
|
|
newFont.setPixelSize(size);
|
2021-06-07 11:02:03 +08:00
|
|
|
setFont(newFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::doZoomOut()
|
|
|
|
{
|
|
|
|
QFont newFont = font();
|
2021-12-16 19:14:14 +08:00
|
|
|
int size = newFont.pixelSize();
|
2021-06-07 11:02:03 +08:00
|
|
|
size--;
|
|
|
|
if (size<2)
|
|
|
|
size = 2;
|
2021-12-16 19:14:14 +08:00
|
|
|
newFont.setPixelSize(size);
|
2021-06-07 11:02:03 +08:00
|
|
|
setFont(newFont);
|
|
|
|
}
|
|
|
|
|
2021-05-27 23:45:22 +08:00
|
|
|
SynSelectionMode SynEdit::selectionMode() const
|
|
|
|
{
|
|
|
|
return mSelectionMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setSelectionMode(SynSelectionMode value)
|
|
|
|
{
|
|
|
|
if (mSelectionMode!=value) {
|
|
|
|
mSelectionMode = value;
|
|
|
|
setActiveSelectionMode(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 21:35:46 +08:00
|
|
|
QString SynEdit::selText()
|
|
|
|
{
|
|
|
|
if (!selAvail()) {
|
|
|
|
return "";
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
int ColFrom = blockBegin().ch;
|
|
|
|
int First = blockBegin().line - 1;
|
2021-05-29 21:35:46 +08:00
|
|
|
//
|
2022-07-04 10:01:40 +08:00
|
|
|
int ColTo = blockEnd().ch;
|
|
|
|
int Last = blockEnd().line - 1;
|
2022-06-28 21:46:20 +08:00
|
|
|
|
2021-05-29 21:35:46 +08:00
|
|
|
switch(mActiveSelectionMode) {
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Normal:{
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(blockEnd().line);
|
2022-06-28 21:46:20 +08:00
|
|
|
QString s = mDocument->getString(Last);
|
|
|
|
if ((foldRange) && foldRange->collapsed && ColTo>s.length()) {
|
|
|
|
s=s+highlighter()->foldString();
|
|
|
|
if (ColTo>s.length()) {
|
|
|
|
Last = foldRange->toLine-1;
|
|
|
|
ColTo = mDocument->getString(Last).length()+1;
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
if (First == Last)
|
2022-04-19 21:18:41 +08:00
|
|
|
return mDocument->getString(First).mid(ColFrom-1, ColTo - ColFrom);
|
2021-05-29 21:35:46 +08:00
|
|
|
else {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString result = mDocument->getString(First).mid(ColFrom-1);
|
2021-05-29 21:35:46 +08:00
|
|
|
result+= lineBreak();
|
|
|
|
for (int i = First + 1; i<=Last - 1; i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
result += mDocument->getString(i);
|
2021-05-29 21:35:46 +08:00
|
|
|
result+=lineBreak();
|
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
result += mDocument->getString(Last).leftRef(ColTo-1);
|
2021-05-29 21:35:46 +08:00
|
|
|
return result;
|
|
|
|
}
|
2022-06-28 21:46:20 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Column:
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
First = blockBegin().line;
|
|
|
|
ColFrom = charToColumn(blockBegin().line, blockBegin().ch);
|
|
|
|
Last = blockEnd().line;
|
|
|
|
ColTo = charToColumn(blockEnd().line, blockEnd().ch);
|
2021-05-29 21:35:46 +08:00
|
|
|
if (ColFrom > ColTo)
|
|
|
|
std::swap(ColFrom, ColTo);
|
2022-05-19 12:32:33 +08:00
|
|
|
if (First>Last)
|
|
|
|
std::swap(First,Last);
|
2021-05-29 21:35:46 +08:00
|
|
|
QString result;
|
|
|
|
for (int i = First; i <= Last; i++) {
|
2022-05-19 12:32:33 +08:00
|
|
|
int l = columnToChar(i,ColFrom);
|
|
|
|
int r = columnToChar(i,ColTo-1)+1;
|
|
|
|
QString s = mDocument->getString(i-1);
|
2021-06-06 09:45:08 +08:00
|
|
|
result += s.mid(l-1,r-l);
|
2021-11-30 20:43:58 +08:00
|
|
|
if (i<Last)
|
|
|
|
result+=lineBreak();
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Line:
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
// If block selection includes LastLine,
|
|
|
|
// line break code(s) of the last line will not be added.
|
|
|
|
for (int i= First; i<=Last - 1;i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
result += mDocument->getString(i);
|
2021-05-29 21:35:46 +08:00
|
|
|
result+=lineBreak();
|
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
result += mDocument->getString(Last);
|
|
|
|
if (Last < mDocument->count() - 1)
|
2021-05-29 21:35:46 +08:00
|
|
|
result+=lineBreak();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 23:51:42 +08:00
|
|
|
return "";
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList SynEdit::getContent(BufferCoord startPos, BufferCoord endPos, SynSelectionMode mode) const
|
|
|
|
{
|
|
|
|
QStringList result;
|
|
|
|
if (startPos==endPos) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (startPos>endPos) {
|
|
|
|
std::swap(startPos,endPos);
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
int ColFrom = startPos.ch;
|
|
|
|
int First = startPos.line - 1;
|
2022-07-02 10:39:31 +08:00
|
|
|
//
|
2022-07-04 10:01:40 +08:00
|
|
|
int ColTo = endPos.ch;
|
|
|
|
int Last = endPos.line - 1;
|
2022-07-02 10:39:31 +08:00
|
|
|
|
|
|
|
switch(mode) {
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Normal:{
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(endPos.line);
|
2022-07-02 10:39:31 +08:00
|
|
|
QString s = mDocument->getString(Last);
|
|
|
|
if ((foldRange) && foldRange->collapsed && ColTo>s.length()) {
|
|
|
|
s=s+highlighter()->foldString();
|
|
|
|
if (ColTo>s.length()) {
|
|
|
|
Last = foldRange->toLine-1;
|
|
|
|
ColTo = mDocument->getString(Last).length()+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (First == Last) {
|
|
|
|
result.append(mDocument->getString(First).mid(ColFrom-1, ColTo - ColFrom));
|
|
|
|
} else {
|
|
|
|
result.append(mDocument->getString(First).mid(ColFrom-1));
|
|
|
|
for (int i = First + 1; i<=Last - 1; i++) {
|
|
|
|
result.append(mDocument->getString(i));
|
|
|
|
}
|
|
|
|
result.append(mDocument->getString(Last).left(ColTo-1));
|
|
|
|
}
|
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Column:
|
|
|
|
First = blockBegin().line;
|
|
|
|
ColFrom = charToColumn(blockBegin().line, blockBegin().ch);
|
|
|
|
Last = blockEnd().line;
|
|
|
|
ColTo = charToColumn(blockEnd().line, blockEnd().ch);
|
2022-07-02 10:39:31 +08:00
|
|
|
if (ColFrom > ColTo)
|
|
|
|
std::swap(ColFrom, ColTo);
|
|
|
|
if (First>Last)
|
|
|
|
std::swap(First,Last);
|
|
|
|
for (int i = First; i <= Last; i++) {
|
|
|
|
int l = columnToChar(i,ColFrom);
|
|
|
|
int r = columnToChar(i,ColTo-1)+1;
|
|
|
|
QString s = mDocument->getString(i-1);
|
|
|
|
result.append(s.mid(l-1,r-l));
|
|
|
|
}
|
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Line:
|
2022-07-02 10:39:31 +08:00
|
|
|
// If block selection includes LastLine,
|
|
|
|
// line break code(s) of the last line will not be added.
|
|
|
|
for (int i= First; i<=Last - 1;i++) {
|
|
|
|
result.append(mDocument->getString(i));
|
|
|
|
}
|
|
|
|
result.append(mDocument->getString(Last));
|
|
|
|
if (Last < mDocument->count() - 1)
|
|
|
|
result.append("");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-29 21:35:46 +08:00
|
|
|
QString SynEdit::lineBreak()
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return mDocument->lineBreak();
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2021-05-27 01:05:49 +08:00
|
|
|
bool SynEdit::useCodeFolding() const
|
|
|
|
{
|
|
|
|
return mUseCodeFolding;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setUseCodeFolding(bool value)
|
|
|
|
{
|
|
|
|
if (mUseCodeFolding!=value) {
|
|
|
|
mUseCodeFolding = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:03:24 +08:00
|
|
|
SynEditCodeFolding &SynEdit::codeFolding()
|
2021-06-24 22:33:57 +08:00
|
|
|
{
|
|
|
|
return mCodeFolding;
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:46:20 +08:00
|
|
|
QString SynEdit::displayLineText()
|
|
|
|
{
|
|
|
|
if (mCaretY >= 1 && mCaretY <= mDocument->count()) {
|
|
|
|
QString s= mDocument->getString(mCaretY - 1);
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(mCaretY);
|
|
|
|
if ((foldRange) && foldRange->collapsed) {
|
|
|
|
return s+highlighter()->foldString();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
QString SynEdit::lineText() const
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY >= 1 && mCaretY <= mDocument->count())
|
|
|
|
return mDocument->getString(mCaretY - 1);
|
2021-05-27 20:33:25 +08:00
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setLineText(const QString s)
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mCaretY >= 1 && mCaretY <= mDocument->count())
|
|
|
|
mDocument->putString(mCaretY-1,s);
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 00:04:20 +08:00
|
|
|
PSynHighlighter SynEdit::highlighter() const
|
|
|
|
{
|
|
|
|
return mHighlighter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setHighlighter(const PSynHighlighter &highlighter)
|
|
|
|
{
|
|
|
|
PSynHighlighter oldHighlighter= mHighlighter;
|
|
|
|
mHighlighter = highlighter;
|
|
|
|
if (oldHighlighter && mHighlighter &&
|
|
|
|
oldHighlighter->language() == highlighter->language()) {
|
|
|
|
} else {
|
|
|
|
recalcCharExtent();
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->beginUpdate();
|
2021-05-26 00:04:20 +08:00
|
|
|
auto action=finally([this]{
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->endUpdate();
|
2021-05-26 00:04:20 +08:00
|
|
|
});
|
2021-10-29 15:15:43 +08:00
|
|
|
rescanRanges();
|
2021-05-26 00:04:20 +08:00
|
|
|
}
|
2021-09-02 12:14:02 +08:00
|
|
|
onSizeOrFontChanged(true);
|
2021-05-26 00:04:20 +08:00
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:18:41 +08:00
|
|
|
const PSynDocument& SynEdit::document() const
|
2021-05-24 00:41:00 +08:00
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return mDocument;
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SynEdit::empty()
|
|
|
|
{
|
2022-04-19 21:18:41 +08:00
|
|
|
return mDocument->empty();
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::commandProcessor(SynEditorCommand Command, QChar AChar, void *pData)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
|
|
|
// first the program event handler gets a chance to process the command
|
|
|
|
onProcessCommand(Command, AChar, pData);
|
|
|
|
if (Command != SynEditorCommand::ecNone)
|
|
|
|
ExecuteCommand(Command, AChar, pData);
|
|
|
|
onCommandProcessed(Command, AChar, pData);
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::moveCaretHorz(int DX, bool isSelection)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
|
|
|
BufferCoord ptO = caretXY();
|
|
|
|
BufferCoord ptDst = ptO;
|
2022-06-28 21:46:20 +08:00
|
|
|
QString s = displayLineText();
|
2021-05-27 20:33:25 +08:00
|
|
|
int nLineLen = s.length();
|
|
|
|
// only moving or selecting one char can change the line
|
2021-06-08 21:41:42 +08:00
|
|
|
//bool bChangeY = !mOptions.testFlag(SynEditorOption::eoScrollPastEol);
|
|
|
|
bool bChangeY=true;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (bChangeY && (DX == -1) && (ptO.ch == 1) && (ptO.line > 1)) {
|
2021-05-27 20:33:25 +08:00
|
|
|
// end of previous line
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
2022-07-02 17:59:07 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
int row = lineToRow(ptDst.line);
|
2022-06-28 21:46:20 +08:00
|
|
|
row--;
|
|
|
|
int line = rowToLine(row);
|
2022-07-04 10:01:40 +08:00
|
|
|
if (line!=ptDst.line && line>=1) {
|
|
|
|
ptDst.line = line;
|
|
|
|
ptDst.ch = getDisplayStringAtLine(ptDst.line).length() + 1;
|
2022-06-28 21:46:20 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
} else if (bChangeY && (DX == 1) && (ptO.ch > nLineLen) && (ptO.line < mDocument->count())) {
|
2021-05-27 20:33:25 +08:00
|
|
|
// start of next line
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
2022-07-02 17:59:07 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
int row = lineToRow(ptDst.line);
|
2022-06-28 21:46:20 +08:00
|
|
|
row++;
|
|
|
|
int line = rowToLine(row);
|
2022-07-02 20:19:37 +08:00
|
|
|
// qDebug()<<line<<ptDst.Line;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (line!=ptDst.line && line<=mDocument->count()) {
|
|
|
|
ptDst.line = line;
|
|
|
|
ptDst.ch = 1;
|
2022-06-28 21:46:20 +08:00
|
|
|
}
|
2021-05-27 20:33:25 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
ptDst.ch = std::max(1, ptDst.ch + DX);
|
2021-05-27 20:33:25 +08:00
|
|
|
// don't go past last char when ScrollPastEol option not set
|
|
|
|
if ((DX > 0) && bChangeY)
|
2022-07-04 10:01:40 +08:00
|
|
|
ptDst.ch = std::min(ptDst.ch, nLineLen + 1);
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
|
|
|
// set caret and block begin / end
|
2022-03-25 10:43:07 +08:00
|
|
|
incPaintLock();
|
|
|
|
if (mOptions.testFlag(eoAltSetsColumnMode) &&
|
2022-07-04 10:01:40 +08:00
|
|
|
(mActiveSelectionMode != SynSelectionMode::Line)) {
|
2022-03-25 10:43:07 +08:00
|
|
|
if (qApp->keyboardModifiers().testFlag(Qt::AltModifier)) {
|
2022-07-04 10:01:40 +08:00
|
|
|
setActiveSelectionMode(SynSelectionMode::Column);
|
2022-03-25 10:43:07 +08:00
|
|
|
} else
|
|
|
|
setActiveSelectionMode(selectionMode());
|
|
|
|
}
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretAndSelection(mBlockBegin, ptDst, isSelection);
|
2022-03-25 10:43:07 +08:00
|
|
|
decPaintLock();
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::moveCaretVert(int DY, bool isSelection)
|
2021-05-27 21:39:18 +08:00
|
|
|
{
|
|
|
|
DisplayCoord ptO = displayXY();
|
|
|
|
DisplayCoord ptDst = ptO;
|
|
|
|
|
2022-07-02 17:59:07 +08:00
|
|
|
|
2021-05-27 21:39:18 +08:00
|
|
|
ptDst.Row+=DY;
|
|
|
|
if (DY >= 0) {
|
2022-04-19 21:18:41 +08:00
|
|
|
if (rowToLine(ptDst.Row) > mDocument->count())
|
2021-05-27 21:39:18 +08:00
|
|
|
ptDst.Row = std::max(1, displayLineCount());
|
|
|
|
} else {
|
|
|
|
if (ptDst.Row < 1)
|
|
|
|
ptDst.Row = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptO.Row != ptDst.Row) {
|
|
|
|
if (mOptions.testFlag(eoKeepCaretX))
|
2021-06-03 23:18:51 +08:00
|
|
|
ptDst.Column = mLastCaretColumn;
|
2021-05-27 21:39:18 +08:00
|
|
|
}
|
|
|
|
BufferCoord vDstLineChar = displayToBufferPos(ptDst);
|
2022-07-02 17:59:07 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
|
|
|
QString s=mDocument->getString(vDstLineChar.line-1);
|
2022-07-02 17:59:07 +08:00
|
|
|
int cols=stringColumns(s,0);
|
|
|
|
if (cols+1<ptO.Column)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-03 23:18:51 +08:00
|
|
|
int SaveLastCaretX = mLastCaretColumn;
|
2021-05-27 21:39:18 +08:00
|
|
|
|
|
|
|
// set caret and block begin / end
|
|
|
|
incPaintLock();
|
2022-03-25 10:43:07 +08:00
|
|
|
if (mOptions.testFlag(eoAltSetsColumnMode) &&
|
2022-07-04 10:01:40 +08:00
|
|
|
(mActiveSelectionMode != SynSelectionMode::Line)) {
|
2022-03-25 10:43:07 +08:00
|
|
|
if (qApp->keyboardModifiers().testFlag(Qt::AltModifier))
|
2022-07-04 10:01:40 +08:00
|
|
|
setActiveSelectionMode(SynSelectionMode::Column);
|
2022-03-25 10:43:07 +08:00
|
|
|
else
|
2022-03-25 12:59:53 +08:00
|
|
|
setActiveSelectionMode(selectionMode());
|
2022-03-25 10:43:07 +08:00
|
|
|
}
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretAndSelection(mBlockBegin, vDstLineChar, isSelection);
|
2021-05-27 21:39:18 +08:00
|
|
|
decPaintLock();
|
|
|
|
|
2022-03-10 12:15:44 +08:00
|
|
|
// Restore fLastCaretX after moving caret, since
|
2021-05-27 21:39:18 +08:00
|
|
|
// UpdateLastCaretX, called by SetCaretXYEx, changes them. This is the one
|
|
|
|
// case where we don't want that.
|
2021-06-03 23:18:51 +08:00
|
|
|
mLastCaretColumn = SaveLastCaretX;
|
2021-05-27 21:39:18 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::moveCaretAndSelection(const BufferCoord &ptBefore, const BufferCoord &ptAfter, bool isSelection)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mOptions.testFlag(SynEditorOption::eoGroupUndo)) {
|
|
|
|
mUndoList->addGroupBreak();
|
|
|
|
}
|
2021-05-27 20:33:25 +08:00
|
|
|
|
|
|
|
incPaintLock();
|
|
|
|
if (isSelection) {
|
2022-03-25 10:43:07 +08:00
|
|
|
if (!selAvail())
|
|
|
|
setBlockBegin(ptBefore);
|
|
|
|
setBlockEnd(ptAfter);
|
2021-05-27 20:33:25 +08:00
|
|
|
} else
|
2022-03-25 10:43:07 +08:00
|
|
|
setBlockBegin(ptAfter);
|
2021-05-27 20:33:25 +08:00
|
|
|
internalSetCaretXY(ptAfter);
|
|
|
|
decPaintLock();
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::moveCaretToLineStart(bool isSelection)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
|
|
|
int newX;
|
|
|
|
// home key enhancement
|
2021-11-20 08:12:10 +08:00
|
|
|
if (mOptions.testFlag(SynEditorOption::eoEnhanceHomeKey)) {
|
2022-04-19 21:18:41 +08:00
|
|
|
QString s = mDocument->getString(mCaretY - 1);
|
2021-05-27 20:33:25 +08:00
|
|
|
|
|
|
|
int first_nonblank = 0;
|
|
|
|
int vMaxX = s.length();
|
|
|
|
while ((first_nonblank < vMaxX) && (s[first_nonblank] == ' ' || s[first_nonblank] == '\t')) {
|
|
|
|
first_nonblank++;
|
|
|
|
}
|
|
|
|
newX = mCaretX;
|
|
|
|
|
2021-11-30 21:45:48 +08:00
|
|
|
if ((newX > first_nonblank+1)
|
|
|
|
|| (newX == 1))
|
2021-11-20 08:12:10 +08:00
|
|
|
newX = first_nonblank+1;
|
2021-05-27 20:33:25 +08:00
|
|
|
else
|
|
|
|
newX = 1;
|
|
|
|
} else
|
|
|
|
newX = 1;
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretAndSelection(caretXY(), BufferCoord{newX, mCaretY}, isSelection);
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::moveCaretToLineEnd(bool isSelection)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
|
|
|
int vNewX;
|
|
|
|
if (mOptions.testFlag(SynEditorOption::eoEnhanceEndKey)) {
|
2022-06-28 21:46:20 +08:00
|
|
|
QString vText = displayLineText();
|
2021-09-01 16:17:31 +08:00
|
|
|
int vLastNonBlank = vText.length()-1;
|
2021-05-27 20:33:25 +08:00
|
|
|
int vMinX = 0;
|
2021-09-01 16:17:31 +08:00
|
|
|
while ((vLastNonBlank >= vMinX) && (vText[vLastNonBlank] == ' ' || vText[vLastNonBlank] =='\t'))
|
2021-05-27 20:33:25 +08:00
|
|
|
vLastNonBlank--;
|
2021-09-01 16:17:31 +08:00
|
|
|
vLastNonBlank++;
|
2021-05-27 20:33:25 +08:00
|
|
|
vNewX = mCaretX;
|
2021-11-30 21:45:48 +08:00
|
|
|
if ((vNewX <= vLastNonBlank) || (vNewX == vText.length() + 1))
|
2021-05-27 20:33:25 +08:00
|
|
|
vNewX = vLastNonBlank + 1;
|
2021-11-30 21:45:48 +08:00
|
|
|
else
|
|
|
|
vNewX = vText.length() + 1;
|
2021-05-27 20:33:25 +08:00
|
|
|
} else
|
2022-06-28 21:46:20 +08:00
|
|
|
vNewX = displayLineText().length() + 1;
|
2021-05-27 20:33:25 +08:00
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretAndSelection(caretXY(), BufferCoord{vNewX, mCaretY}, isSelection);
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::setSelectedTextEmpty()
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-02 10:39:31 +08:00
|
|
|
BufferCoord startPos=blockBegin();
|
|
|
|
BufferCoord endPos=blockEnd();
|
|
|
|
doDeleteText(startPos,endPos,mActiveSelectionMode);
|
|
|
|
internalSetCaretXY(startPos);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
void SynEdit::setSelTextPrimitive(const QStringList &text)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-02 10:39:31 +08:00
|
|
|
setSelTextPrimitiveEx(mActiveSelectionMode, text);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
void SynEdit::setSelTextPrimitiveEx(SynSelectionMode mode, const QStringList &text)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
|
|
|
incPaintLock();
|
2022-07-02 17:59:07 +08:00
|
|
|
bool groupUndo=false;
|
2022-07-02 10:39:31 +08:00
|
|
|
BufferCoord startPos = blockBegin();
|
|
|
|
BufferCoord endPos = blockEnd();
|
2021-05-29 21:35:46 +08:00
|
|
|
if (selAvail()) {
|
2022-07-02 10:39:31 +08:00
|
|
|
if (!mUndoing && !text.isEmpty()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-07-02 10:39:31 +08:00
|
|
|
groupUndo=true;
|
|
|
|
}
|
|
|
|
doDeleteText(startPos,endPos,activeSelectionMode());
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mode == SynSelectionMode::Column) {
|
|
|
|
int colBegin = charToColumn(startPos.line,startPos.ch);
|
|
|
|
int colEnd = charToColumn(endPos.line,endPos.ch);
|
2022-05-18 23:17:06 +08:00
|
|
|
if (colBegin<colEnd)
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretXY(startPos);
|
2022-05-18 23:17:06 +08:00
|
|
|
else
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretXY(endPos);
|
2022-05-18 23:17:06 +08:00
|
|
|
} else
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretXY(startPos);
|
|
|
|
}
|
|
|
|
if (!text.isEmpty()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
doInsertText(caretXY(),text,mode,mBlockBegin.line,mBlockEnd.line);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
if (groupUndo) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
decPaintLock();
|
|
|
|
setStatusChanged(SynStatusChange::scSelection);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
void SynEdit::doSetSelText(const QString &value)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2022-03-01 22:03:54 +08:00
|
|
|
bool blockBeginned = false;
|
|
|
|
auto action = finally([this, &blockBeginned]{
|
|
|
|
if (blockBeginned)
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
});
|
|
|
|
if (selAvail()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-03-02 11:31:03 +08:00
|
|
|
blockBeginned = true;
|
2022-07-02 06:44:01 +08:00
|
|
|
// mUndoList->AddChange(
|
|
|
|
// SynChangeReason::crDelete, mBlockBegin, mBlockEnd,
|
|
|
|
// selText(), mActiveSelectionMode);
|
2022-05-18 23:17:06 +08:00
|
|
|
}
|
|
|
|
// } else if (!colSelAvail())
|
|
|
|
// setActiveSelectionMode(selectionMode());
|
2021-06-03 20:26:36 +08:00
|
|
|
BufferCoord StartOfBlock = blockBegin();
|
|
|
|
BufferCoord EndOfBlock = blockEnd();
|
|
|
|
mBlockBegin = StartOfBlock;
|
|
|
|
mBlockEnd = EndOfBlock;
|
2022-07-02 14:06:10 +08:00
|
|
|
setSelTextPrimitive(splitStrings(value));
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-08-04 09:13:41 +08:00
|
|
|
int SynEdit::searchReplace(const QString &sSearch, const QString &sReplace, SynSearchOptions sOptions, PSynSearchBase searchEngine,
|
2022-01-24 18:03:35 +08:00
|
|
|
SynSearchMathedProc matchedCallback, SynSearchConfirmAroundProc confirmAroundCallback)
|
2021-08-02 17:24:11 +08:00
|
|
|
{
|
2021-08-03 23:55:57 +08:00
|
|
|
if (!searchEngine)
|
2021-08-02 17:24:11 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// can't search for or replace an empty string
|
2021-08-04 09:13:41 +08:00
|
|
|
if (sSearch.isEmpty()) {
|
2021-08-02 17:24:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-08-02 21:58:39 +08:00
|
|
|
int result = 0;
|
2021-08-02 17:24:11 +08:00
|
|
|
// get the text range to search in, ignore the "Search in selection only"
|
|
|
|
// option if nothing is selected
|
2021-08-04 09:13:41 +08:00
|
|
|
bool bBackward = sOptions.testFlag(ssoBackwards);
|
|
|
|
bool bFromCursor = !sOptions.testFlag(ssoEntireScope);
|
2021-08-02 17:24:11 +08:00
|
|
|
BufferCoord ptCurrent;
|
|
|
|
BufferCoord ptStart;
|
|
|
|
BufferCoord ptEnd;
|
|
|
|
if (!selAvail())
|
2021-08-04 09:13:41 +08:00
|
|
|
sOptions.setFlag(ssoSelectedOnly,false);
|
|
|
|
if (sOptions.testFlag(ssoSelectedOnly)) {
|
2021-08-02 17:24:11 +08:00
|
|
|
ptStart = blockBegin();
|
|
|
|
ptEnd = blockEnd();
|
|
|
|
// search the whole line in the line selection mode
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Line) {
|
|
|
|
ptStart.ch = 1;
|
|
|
|
ptEnd.ch = mDocument->getString(ptEnd.line - 1).length();
|
|
|
|
} else if (mActiveSelectionMode == SynSelectionMode::Column) {
|
2021-08-02 17:24:11 +08:00
|
|
|
// make sure the start column is smaller than the end column
|
2022-07-04 10:01:40 +08:00
|
|
|
if (ptStart.ch > ptEnd.ch)
|
|
|
|
std::swap(ptStart.ch,ptEnd.ch);
|
2021-08-02 17:24:11 +08:00
|
|
|
}
|
|
|
|
// ignore the cursor position when searching in the selection
|
|
|
|
if (bBackward) {
|
|
|
|
ptCurrent = ptEnd;
|
|
|
|
} else {
|
|
|
|
ptCurrent = ptStart;
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
ptStart.ch = 1;
|
|
|
|
ptStart.line = 1;
|
|
|
|
ptEnd.line = mDocument->count();
|
|
|
|
ptEnd.ch = mDocument->getString(ptEnd.line - 1).length();
|
2021-08-02 17:24:11 +08:00
|
|
|
if (bFromCursor) {
|
|
|
|
if (bBackward)
|
|
|
|
ptEnd = caretXY();
|
|
|
|
else
|
|
|
|
ptStart = caretXY();
|
|
|
|
}
|
|
|
|
if (bBackward)
|
|
|
|
ptCurrent = ptEnd;
|
|
|
|
else
|
|
|
|
ptCurrent = ptStart;
|
|
|
|
}
|
2022-01-13 12:55:55 +08:00
|
|
|
BufferCoord originCaretXY=caretXY();
|
2021-08-02 17:24:11 +08:00
|
|
|
// initialize the search engine
|
2021-08-04 09:13:41 +08:00
|
|
|
searchEngine->setOptions(sOptions);
|
|
|
|
searchEngine->setPattern(sSearch);
|
2021-08-02 17:24:11 +08:00
|
|
|
// search while the current search position is inside of the search range
|
2021-08-04 09:13:41 +08:00
|
|
|
bool dobatchReplace = false;
|
2021-08-02 17:24:11 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
|
|
|
{
|
|
|
|
auto action = finally([&,this]{
|
2021-08-04 09:13:41 +08:00
|
|
|
if (dobatchReplace) {
|
|
|
|
decPaintLock();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-08-04 09:13:41 +08:00
|
|
|
}
|
2021-08-02 17:24:11 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
});
|
2021-08-03 23:55:57 +08:00
|
|
|
int i;
|
2021-11-03 18:09:12 +08:00
|
|
|
// If it's a search only we can leave the procedure now.
|
|
|
|
SynSearchAction searchAction = SynSearchAction::Exit;
|
2022-07-04 10:01:40 +08:00
|
|
|
while ((ptCurrent.line >= ptStart.line) && (ptCurrent.line <= ptEnd.line)) {
|
|
|
|
int nInLine = searchEngine->findAll(mDocument->getString(ptCurrent.line - 1));
|
2021-08-02 21:58:39 +08:00
|
|
|
int iResultOffset = 0;
|
|
|
|
if (bBackward)
|
2021-08-03 23:55:57 +08:00
|
|
|
i = searchEngine->resultCount()-1;
|
2021-08-02 21:58:39 +08:00
|
|
|
else
|
2021-08-03 23:55:57 +08:00
|
|
|
i = 0;
|
2021-08-02 21:58:39 +08:00
|
|
|
// Operate on all results in this line.
|
|
|
|
while (nInLine > 0) {
|
|
|
|
// An occurrence may have been replaced with a text of different length
|
2021-08-03 23:55:57 +08:00
|
|
|
int nFound = searchEngine->result(i) + 1 + iResultOffset;
|
|
|
|
int nSearchLen = searchEngine->length(i);
|
2021-08-04 09:13:41 +08:00
|
|
|
int nReplaceLen = 0;
|
2021-08-02 21:58:39 +08:00
|
|
|
if (bBackward)
|
2021-08-03 23:55:57 +08:00
|
|
|
i--;
|
2021-08-02 21:58:39 +08:00
|
|
|
else
|
2021-08-03 23:55:57 +08:00
|
|
|
i++;
|
2021-08-02 21:58:39 +08:00
|
|
|
nInLine--;
|
|
|
|
// Is the search result entirely in the search range?
|
|
|
|
bool isInValidSearchRange = true;
|
|
|
|
int first = nFound;
|
|
|
|
int last = nFound + nSearchLen;
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((mActiveSelectionMode == SynSelectionMode::Normal)
|
2021-08-04 09:13:41 +08:00
|
|
|
|| !sOptions.testFlag(ssoSelectedOnly)) {
|
2022-07-04 10:01:40 +08:00
|
|
|
if (((ptCurrent.line == ptStart.line) && (first < ptStart.ch)) ||
|
|
|
|
((ptCurrent.line == ptEnd.line) && (last > ptEnd.ch)))
|
2021-08-02 21:58:39 +08:00
|
|
|
isInValidSearchRange = false;
|
2022-07-04 10:01:40 +08:00
|
|
|
} else if (mActiveSelectionMode == SynSelectionMode::Column) {
|
2021-08-02 21:58:39 +08:00
|
|
|
// solves bug in search/replace when smColumn mode active and no selection
|
2022-07-04 10:01:40 +08:00
|
|
|
isInValidSearchRange = ((first >= ptStart.ch) && (last <= ptEnd.ch))
|
|
|
|
|| (ptEnd.ch - ptStart.ch < 1);
|
2021-08-02 21:58:39 +08:00
|
|
|
}
|
|
|
|
if (!isInValidSearchRange)
|
|
|
|
continue;
|
|
|
|
result++;
|
|
|
|
// Select the text, so the user can see it in the OnReplaceText event
|
|
|
|
// handler or as the search result.
|
2022-07-04 10:01:40 +08:00
|
|
|
ptCurrent.ch = nFound;
|
2021-08-02 21:58:39 +08:00
|
|
|
setBlockBegin(ptCurrent);
|
|
|
|
|
|
|
|
//Be sure to use the Ex version of CursorPos so that it appears in the middle if necessary
|
2022-07-04 10:01:40 +08:00
|
|
|
setCaretXYEx(false, BufferCoord{1, ptCurrent.line});
|
2021-08-02 21:58:39 +08:00
|
|
|
ensureCursorPosVisibleEx(true);
|
2022-07-04 10:01:40 +08:00
|
|
|
ptCurrent.ch += nSearchLen;
|
2021-08-02 21:58:39 +08:00
|
|
|
setBlockEnd(ptCurrent);
|
|
|
|
//internalSetCaretXY(ptCurrent);
|
|
|
|
if (bBackward)
|
|
|
|
internalSetCaretXY(blockBegin());
|
|
|
|
else
|
|
|
|
internalSetCaretXY(ptCurrent);
|
2021-11-03 18:09:12 +08:00
|
|
|
|
2021-08-04 09:13:41 +08:00
|
|
|
QString replaceText = searchEngine->replace(selText(), sReplace);
|
|
|
|
if (matchedCallback && !dobatchReplace) {
|
2022-07-04 10:01:40 +08:00
|
|
|
searchAction = matchedCallback(sSearch,replaceText,ptCurrent.line,
|
2021-08-03 23:55:57 +08:00
|
|
|
nFound,nSearchLen);
|
2021-08-02 21:58:39 +08:00
|
|
|
}
|
2021-08-04 09:13:41 +08:00
|
|
|
if (searchAction==SynSearchAction::Exit) {
|
2021-08-02 21:58:39 +08:00
|
|
|
return result;
|
2021-08-04 09:13:41 +08:00
|
|
|
} else if (searchAction == SynSearchAction::Skip) {
|
|
|
|
continue;
|
|
|
|
} else if (searchAction == SynSearchAction::Replace
|
|
|
|
|| searchAction == SynSearchAction::ReplaceAll) {
|
|
|
|
if (!dobatchReplace &&
|
|
|
|
(searchAction == SynSearchAction::ReplaceAll) ){
|
|
|
|
incPaintLock();
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2021-08-04 09:13:41 +08:00
|
|
|
dobatchReplace = true;
|
|
|
|
}
|
2021-11-13 10:38:48 +08:00
|
|
|
bool oldAutoIndent = mOptions.testFlag(SynEditorOption::eoAutoIndent);
|
|
|
|
mOptions.setFlag(SynEditorOption::eoAutoIndent,false);
|
2021-10-06 23:35:45 +08:00
|
|
|
doSetSelText(replaceText);
|
2021-08-04 09:13:41 +08:00
|
|
|
nReplaceLen = caretX() - nFound;
|
|
|
|
// fix the caret position and the remaining results
|
|
|
|
if (!bBackward) {
|
|
|
|
internalSetCaretX(nFound + nReplaceLen);
|
|
|
|
if ((nSearchLen != nReplaceLen)) {
|
|
|
|
iResultOffset += nReplaceLen - nSearchLen;
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((mActiveSelectionMode != SynSelectionMode::Column) && (caretY() == ptEnd.line)) {
|
|
|
|
ptEnd.ch+=nReplaceLen - nSearchLen;
|
2021-08-04 09:13:41 +08:00
|
|
|
setBlockEnd(ptEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 10:38:48 +08:00
|
|
|
mOptions.setFlag(SynEditorOption::eoAutoIndent,oldAutoIndent);
|
2021-08-04 09:13:41 +08:00
|
|
|
}
|
2021-08-02 21:58:39 +08:00
|
|
|
}
|
|
|
|
// search next / previous line
|
|
|
|
if (bBackward)
|
2022-07-04 10:01:40 +08:00
|
|
|
ptCurrent.line--;
|
2021-08-02 21:58:39 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
ptCurrent.line++;
|
|
|
|
if (((ptCurrent.line < ptStart.line) || (ptCurrent.line > ptEnd.line))
|
2022-01-24 18:03:35 +08:00
|
|
|
&& bFromCursor && sOptions.testFlag(ssoWrapAround)){
|
|
|
|
if (confirmAroundCallback && !confirmAroundCallback())
|
|
|
|
break;
|
2022-01-13 12:55:55 +08:00
|
|
|
//search start from cursor, search has finished but no result founds
|
|
|
|
bFromCursor = false;
|
2022-07-04 10:01:40 +08:00
|
|
|
ptStart.ch = 1;
|
|
|
|
ptStart.line = 1;
|
|
|
|
ptEnd.line = mDocument->count();
|
|
|
|
ptEnd.ch = mDocument->getString(ptEnd.line - 1).length();
|
2022-01-13 12:55:55 +08:00
|
|
|
if (bBackward) {
|
|
|
|
ptStart = originCaretXY;
|
|
|
|
ptCurrent = ptEnd;
|
|
|
|
} else {
|
|
|
|
ptEnd= originCaretXY;
|
|
|
|
ptCurrent = ptStart;
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 21:58:39 +08:00
|
|
|
}
|
2021-08-02 17:24:11 +08:00
|
|
|
}
|
2021-08-03 23:55:57 +08:00
|
|
|
return result;
|
2021-08-02 17:24:11 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::doLinesDeleted(int firstLine, int count)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-09-02 12:14:02 +08:00
|
|
|
emit linesDeleted(firstLine, count);
|
2021-05-29 21:35:46 +08:00
|
|
|
// // gutter marks
|
|
|
|
// for i := 0 to Marks.Count - 1 do begin
|
|
|
|
// if Marks[i].Line >= FirstLine + Count then
|
|
|
|
// Marks[i].Line := Marks[i].Line - Count
|
|
|
|
// else if Marks[i].Line > FirstLine then
|
|
|
|
// Marks[i].Line := FirstLine;
|
|
|
|
// end;
|
|
|
|
// // plugins
|
|
|
|
// if fPlugins <> nil then begin
|
|
|
|
// for i := 0 to fPlugins.Count - 1 do
|
|
|
|
// TSynEditPlugin(fPlugins[i]).LinesDeleted(FirstLine, Count);
|
|
|
|
// end;
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::doLinesInserted(int firstLine, int count)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
2021-09-02 12:14:02 +08:00
|
|
|
emit linesInserted(firstLine, count);
|
2021-06-03 20:26:36 +08:00
|
|
|
// // gutter marks
|
|
|
|
// for i := 0 to Marks.Count - 1 do begin
|
|
|
|
// if Marks[i].Line >= FirstLine then
|
|
|
|
// Marks[i].Line := Marks[i].Line + Count;
|
|
|
|
// end;
|
|
|
|
// // plugins
|
|
|
|
// if fPlugins <> nil then begin
|
|
|
|
// for i := 0 to fPlugins.Count - 1 do
|
|
|
|
// TSynEditPlugin(fPlugins[i]).LinesInserted(FirstLine, Count);
|
|
|
|
// end;
|
|
|
|
}
|
|
|
|
|
2021-11-13 13:03:42 +08:00
|
|
|
void SynEdit::properSetLine(int ALine, const QString &ALineText, bool notify)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2021-11-13 13:03:42 +08:00
|
|
|
if (mOptions.testFlag(eoTrimTrailingSpaces)) {
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(ALine,trimRight(ALineText),notify);
|
2021-11-13 13:03:42 +08:00
|
|
|
} else {
|
2022-04-19 21:18:41 +08:00
|
|
|
mDocument->putString(ALine,ALineText,notify);
|
2021-11-13 13:03:42 +08:00
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 20:19:37 +08:00
|
|
|
void SynEdit::doDeleteText(BufferCoord startPos, BufferCoord endPos, SynSelectionMode mode)
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
|
|
|
bool UpdateMarks = false;
|
|
|
|
int MarkOffset = 0;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mode == SynSelectionMode::Normal) {
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(endPos.line);
|
|
|
|
QString s = mDocument->getString(endPos.line-1);
|
|
|
|
if ((foldRange) && foldRange->collapsed && endPos.ch>s.length()) {
|
2022-07-02 20:19:37 +08:00
|
|
|
QString newS=s+highlighter()->foldString();
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((startPos.ch<=s.length() || startPos.line<endPos.line)
|
|
|
|
&& endPos.ch>newS.length() ) {
|
2022-07-02 20:19:37 +08:00
|
|
|
//selection has whole block
|
2022-07-04 10:01:40 +08:00
|
|
|
endPos.line = foldRange->toLine;
|
|
|
|
endPos.ch = mDocument->getString(endPos.line-1).length()+1;
|
2022-07-02 20:19:37 +08:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
QStringList deleted=getContent(startPos,endPos,mode);
|
|
|
|
switch(mode) {
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Normal:
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument->count() > 0) {
|
2021-05-29 21:35:46 +08:00
|
|
|
// Create a string that contains everything on the first line up
|
|
|
|
// to the selection mark, and everything on the last line after
|
|
|
|
// the selection mark.
|
2022-07-04 10:01:40 +08:00
|
|
|
QString TempString = mDocument->getString(startPos.line - 1).mid(0, startPos.ch - 1)
|
|
|
|
+ mDocument->getString(endPos.line - 1).mid(endPos.ch-1);
|
2022-06-28 21:46:20 +08:00
|
|
|
// bool collapsed=foldCollapsedBetween(BB.Line,BE.Line);
|
2021-05-29 21:35:46 +08:00
|
|
|
// Delete all lines in the selection range.
|
2022-07-04 10:01:40 +08:00
|
|
|
mDocument->deleteLines(startPos.line, endPos.line - startPos.line);
|
|
|
|
properSetLine(startPos.line-1,TempString);
|
2021-05-29 21:35:46 +08:00
|
|
|
UpdateMarks = true;
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretXY(startPos);
|
2022-06-28 21:46:20 +08:00
|
|
|
// if (collapsed) {
|
|
|
|
// PSynEditFoldRange foldRange = foldStartAtLine(BB.Line);
|
|
|
|
// if (!foldRange
|
|
|
|
// || (!foldRange->collapsed))
|
|
|
|
// uncollapseAroundLine(BB.Line);
|
|
|
|
// }
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Column:
|
2021-05-29 21:35:46 +08:00
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
int First = startPos.line - 1;
|
|
|
|
int ColFrom = charToColumn(startPos.line, startPos.ch);
|
|
|
|
int Last = endPos.line - 1;
|
|
|
|
int ColTo = charToColumn(endPos.line, endPos.ch);
|
2021-05-29 21:35:46 +08:00
|
|
|
if (ColFrom > ColTo)
|
|
|
|
std::swap(ColFrom, ColTo);
|
2022-05-18 23:17:06 +08:00
|
|
|
if (First > Last)
|
|
|
|
std::swap(First,Last);
|
2021-05-29 21:35:46 +08:00
|
|
|
QString result;
|
|
|
|
for (int i = First; i <= Last; i++) {
|
2021-11-30 20:43:58 +08:00
|
|
|
int l = columnToChar(i+1,ColFrom);
|
|
|
|
int r = columnToChar(i+1,ColTo-1)+1;
|
2022-04-19 21:18:41 +08:00
|
|
|
QString s = mDocument->getString(i);
|
2021-06-06 09:45:08 +08:00
|
|
|
s.remove(l-1,r-l);
|
2021-09-20 15:57:48 +08:00
|
|
|
properSetLine(i,s);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
// Lines never get deleted completely, so keep caret at end.
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretXY(startPos);
|
2021-05-29 21:35:46 +08:00
|
|
|
// Column deletion never removes a line entirely, so no mark
|
|
|
|
// updating is needed here.
|
|
|
|
break;
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Line:
|
|
|
|
if (endPos.line == mDocument->count()) {
|
|
|
|
mDocument->putString(endPos.line - 1,"");
|
|
|
|
mDocument->deleteLines(startPos.line-1,endPos.line-startPos.line);
|
2021-05-29 21:35:46 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
mDocument->deleteLines(startPos.line-1,endPos.line-startPos.line+1);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
// smLine deletion always resets to first column.
|
2022-07-04 10:01:40 +08:00
|
|
|
internalSetCaretXY(BufferCoord{1, startPos.line});
|
2021-05-29 21:35:46 +08:00
|
|
|
UpdateMarks = true;
|
|
|
|
MarkOffset = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Update marks
|
|
|
|
if (UpdateMarks)
|
2022-07-04 10:01:40 +08:00
|
|
|
doLinesDeleted(startPos.line, endPos.line - startPos.line + MarkOffset);
|
2022-07-02 10:39:31 +08:00
|
|
|
if (!mUndoing) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(SynChangeReason::Delete,
|
2022-07-02 10:39:31 +08:00
|
|
|
startPos,
|
|
|
|
endPos,
|
|
|
|
deleted,
|
|
|
|
mode);
|
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 20:19:37 +08:00
|
|
|
void SynEdit::doInsertText(const BufferCoord& pos,
|
|
|
|
const QStringList& text,
|
|
|
|
SynSelectionMode mode, int startLine, int endLine) {
|
2022-07-02 10:39:31 +08:00
|
|
|
if (text.isEmpty())
|
2021-05-29 21:35:46 +08:00
|
|
|
return;
|
2022-07-02 17:59:07 +08:00
|
|
|
if (startLine>endLine)
|
|
|
|
std::swap(startLine,endLine);
|
2021-05-29 21:35:46 +08:00
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mode == SynSelectionMode::Normal) {
|
|
|
|
PSynEditFoldRange foldRange = foldStartAtLine(pos.line);
|
|
|
|
QString s = mDocument->getString(pos.line-1);
|
|
|
|
if ((foldRange) && foldRange->collapsed && pos.ch>s.length()+1)
|
2022-07-02 20:19:37 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
int insertedLines = 0;
|
|
|
|
BufferCoord newPos;
|
|
|
|
switch(mode){
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Normal:
|
2022-07-02 10:39:31 +08:00
|
|
|
insertedLines = doInsertTextByNormalMode(pos,text, newPos);
|
2022-07-04 10:01:40 +08:00
|
|
|
doLinesInserted(pos.line+1, insertedLines);
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Column:
|
2022-07-02 17:59:07 +08:00
|
|
|
insertedLines = doInsertTextByColumnMode(pos,text, newPos, startLine,endLine);
|
|
|
|
doLinesInserted(endLine-insertedLines+1,insertedLines);
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
2022-07-04 10:01:40 +08:00
|
|
|
case SynSelectionMode::Line:
|
2022-07-02 10:39:31 +08:00
|
|
|
insertedLines = doInsertTextByLineMode(pos,text, newPos);
|
2022-07-04 10:01:40 +08:00
|
|
|
doLinesInserted(pos.line, insertedLines);
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
internalSetCaretXY(newPos);
|
2021-05-29 21:35:46 +08:00
|
|
|
ensureCursorPosVisible();
|
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
int SynEdit::doInsertTextByNormalMode(const BufferCoord& pos, const QStringList& text, BufferCoord &newPos)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
QString sLeftSide;
|
|
|
|
QString sRightSide;
|
2022-07-02 14:06:10 +08:00
|
|
|
QString str;
|
2021-06-03 20:26:36 +08:00
|
|
|
bool bChangeScroll;
|
2021-10-29 15:15:43 +08:00
|
|
|
// int SpaceCount;
|
2022-07-02 10:39:31 +08:00
|
|
|
int result = 0;
|
2022-07-04 10:01:40 +08:00
|
|
|
int startLine = pos.line;
|
|
|
|
QString line=mDocument->getString(pos.line-1);
|
|
|
|
sLeftSide = line.mid(0, pos.ch - 1);
|
|
|
|
if (pos.ch - 1 > sLeftSide.length()) {
|
2021-11-24 17:53:25 +08:00
|
|
|
if (stringIsBlank(sLeftSide))
|
2021-06-03 20:26:36 +08:00
|
|
|
sLeftSide = GetLeftSpacing(displayX() - 1, true);
|
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
sLeftSide += QString(pos.ch - 1 - sLeftSide.length(),' ');
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
sRightSide = line.mid(pos.ch - 1);
|
2021-10-29 15:15:43 +08:00
|
|
|
// if (mUndoing) {
|
|
|
|
// SpaceCount = 0;
|
|
|
|
// } else {
|
|
|
|
// SpaceCount = leftSpaces(sLeftSide);
|
|
|
|
// }
|
2022-07-04 10:01:40 +08:00
|
|
|
int caretY=pos.line;
|
2021-06-03 20:26:36 +08:00
|
|
|
// step1: insert the first line of Value into current line
|
2022-07-02 10:39:31 +08:00
|
|
|
if (text.length()>1) {
|
2022-07-02 14:06:10 +08:00
|
|
|
if (!mUndoing && mHighlighter && mOptions.testFlag(eoAutoIndent)) {
|
2022-07-02 10:39:31 +08:00
|
|
|
QString s = trimLeft(text[0]);
|
2022-01-23 21:28:27 +08:00
|
|
|
if (sLeftSide.isEmpty()) {
|
|
|
|
sLeftSide = GetLeftSpacing(calcIndentSpaces(caretY,s,true),true);
|
|
|
|
}
|
2022-07-02 14:06:10 +08:00
|
|
|
str = sLeftSide + s;
|
2022-01-24 11:23:30 +08:00
|
|
|
} else
|
2022-07-02 14:06:10 +08:00
|
|
|
str = sLeftSide + text[0];
|
|
|
|
properSetLine(caretY - 1, str);
|
2022-07-02 10:39:31 +08:00
|
|
|
mDocument->insertLines(caretY, text.length()-1);
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
2022-07-02 14:06:10 +08:00
|
|
|
str = sLeftSide + text[0] + sRightSide;
|
|
|
|
properSetLine(caretY - 1, str);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2021-10-29 17:55:05 +08:00
|
|
|
rescanRange(caretY);
|
2021-06-03 20:26:36 +08:00
|
|
|
// step2: insert remaining lines of Value
|
2022-07-02 10:39:31 +08:00
|
|
|
for (int i=1;i<text.length();i++) {
|
2022-03-01 18:11:15 +08:00
|
|
|
bool notInComment = true;
|
2022-06-28 09:37:18 +08:00
|
|
|
// if (mHighlighter) {
|
|
|
|
// notInComment = !mHighlighter->isLastLineCommentNotFinished(
|
|
|
|
// mHighlighter->getRangeState().state)
|
|
|
|
// && !mHighlighter->isLastLineStringNotFinished(
|
|
|
|
// mHighlighter->getRangeState().state);
|
|
|
|
// }
|
2022-07-04 10:01:40 +08:00
|
|
|
caretY=pos.line+i;
|
2022-07-02 10:39:31 +08:00
|
|
|
// mStatusChanges.setFlag(SynStatusChange::scCaretY);
|
|
|
|
if (text[i].isEmpty()) {
|
|
|
|
if (i==text.length()-1) {
|
2022-07-02 14:06:10 +08:00
|
|
|
str = sRightSide;
|
2022-07-02 10:39:31 +08:00
|
|
|
} else {
|
2022-07-02 14:06:10 +08:00
|
|
|
if (!mUndoing && mHighlighter && mOptions.testFlag(eoAutoIndent) && notInComment) {
|
|
|
|
str = GetLeftSpacing(calcIndentSpaces(caretY,"",true),true);
|
2022-01-25 12:29:44 +08:00
|
|
|
} else {
|
2022-07-02 14:06:10 +08:00
|
|
|
str = "";
|
2022-01-25 12:29:44 +08:00
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
2021-06-03 20:26:36 +08:00
|
|
|
} else {
|
2022-07-02 14:06:10 +08:00
|
|
|
str = text[i];
|
2022-07-02 10:39:31 +08:00
|
|
|
if (i==text.length()-1)
|
2022-07-02 14:06:10 +08:00
|
|
|
str += sRightSide;
|
|
|
|
if (!mUndoing && mHighlighter && mOptions.testFlag(eoAutoIndent) && notInComment) {
|
|
|
|
int indentSpaces = calcIndentSpaces(caretY,str,true);
|
|
|
|
str = GetLeftSpacing(indentSpaces,true)+trimLeft(str);
|
2021-11-04 21:14:58 +08:00
|
|
|
}
|
2021-10-29 15:15:43 +08:00
|
|
|
}
|
2022-07-02 14:06:10 +08:00
|
|
|
properSetLine(caretY - 1, str,false);
|
2021-10-29 17:55:05 +08:00
|
|
|
rescanRange(caretY);
|
2022-07-02 10:39:31 +08:00
|
|
|
result++;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
bChangeScroll = !mOptions.testFlag(eoScrollPastEol);
|
|
|
|
mOptions.setFlag(eoScrollPastEol);
|
|
|
|
auto action = finally([&,this]{
|
|
|
|
if (bChangeScroll)
|
|
|
|
mOptions.setFlag(eoScrollPastEol,false);
|
|
|
|
});
|
|
|
|
if (mOptions.testFlag(eoTrimTrailingSpaces) && (sRightSide == "")) {
|
2022-07-02 14:06:10 +08:00
|
|
|
newPos=BufferCoord{mDocument->getString(caretY-1).length()+1,caretY};
|
2021-06-03 20:26:36 +08:00
|
|
|
} else
|
2022-07-02 14:06:10 +08:00
|
|
|
newPos=BufferCoord{str.length() - sRightSide.length()+1,caretY};
|
|
|
|
onLinesPutted(startLine-1,result+1);
|
2022-07-02 10:39:31 +08:00
|
|
|
if (!mUndoing) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Insert,
|
2022-07-02 10:39:31 +08:00
|
|
|
pos,newPos,
|
2022-07-04 10:01:40 +08:00
|
|
|
QStringList(),SynSelectionMode::Normal);
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
|
|
|
return result;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 17:59:07 +08:00
|
|
|
int SynEdit::doInsertTextByColumnMode(const BufferCoord& pos, const QStringList& text, BufferCoord &newPos, int startLine, int endLine)
|
2022-03-15 15:34:34 +08:00
|
|
|
{
|
|
|
|
QString str;
|
|
|
|
QString tempString;
|
2022-05-18 23:17:06 +08:00
|
|
|
int line;
|
2022-07-02 10:39:31 +08:00
|
|
|
int len;
|
2022-03-15 15:34:34 +08:00
|
|
|
BufferCoord lineBreakPos;
|
|
|
|
int result = 0;
|
2022-05-18 23:17:06 +08:00
|
|
|
DisplayCoord insertCoord = bufferToDisplayPos(caretXY());
|
2022-07-02 17:59:07 +08:00
|
|
|
int insertCol = insertCoord.Column;
|
2022-05-18 23:17:06 +08:00
|
|
|
line = startLine;
|
2022-07-02 10:39:31 +08:00
|
|
|
if (!mUndoing) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
2022-07-02 17:59:07 +08:00
|
|
|
int i=0;
|
|
|
|
while(line<=endLine) {
|
2022-07-02 10:39:31 +08:00
|
|
|
str = text[i];
|
|
|
|
int insertPos = 0;
|
|
|
|
if (line > mDocument->count()) {
|
|
|
|
result++;
|
|
|
|
tempString = QString(insertCol - 1,' ') + str;
|
|
|
|
mDocument->add("");
|
|
|
|
if (!mUndoing) {
|
2022-03-15 15:34:34 +08:00
|
|
|
result++;
|
2022-07-04 10:01:40 +08:00
|
|
|
lineBreakPos.line = line - 1;
|
|
|
|
lineBreakPos.ch = mDocument->getString(line - 2).length() + 1;
|
|
|
|
mUndoList->addChange(SynChangeReason::LineBreak,
|
2022-07-02 10:39:31 +08:00
|
|
|
lineBreakPos,
|
|
|
|
lineBreakPos,
|
2022-07-04 10:01:40 +08:00
|
|
|
QStringList(), SynSelectionMode::Normal);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
} else {
|
|
|
|
tempString = mDocument->getString(line - 1);
|
|
|
|
len = stringColumns(tempString,0);
|
|
|
|
if (len < insertCol) {
|
|
|
|
insertPos = tempString.length()+1;
|
|
|
|
tempString = tempString + QString(insertCol - len - 1,' ') + str;
|
|
|
|
} else {
|
|
|
|
insertPos = columnToChar(line,insertCol);
|
|
|
|
tempString.insert(insertPos-1,str);
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-02 10:39:31 +08:00
|
|
|
properSetLine(line - 1, tempString);
|
|
|
|
// Add undo change here from PasteFromClipboard
|
|
|
|
if (!mUndoing) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Insert,
|
2022-07-02 10:39:31 +08:00
|
|
|
BufferCoord{insertPos, line},
|
|
|
|
BufferCoord{insertPos+str.length(), line},
|
|
|
|
QStringList(),
|
2022-07-04 10:01:40 +08:00
|
|
|
SynSelectionMode::Normal);
|
2022-07-02 17:59:07 +08:00
|
|
|
}
|
|
|
|
if (i<text.length()-1) {
|
|
|
|
i++;
|
2022-03-25 22:13:00 +08:00
|
|
|
}
|
2022-07-02 17:59:07 +08:00
|
|
|
line++;
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
|
|
|
newPos=pos;
|
|
|
|
if (!text[0].isEmpty()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
newPos.ch+=text[0].length();
|
2022-07-02 10:39:31 +08:00
|
|
|
// mCaretX+=firstLineLen;
|
|
|
|
// mStatusChanges.setFlag(SynStatusChange::scCaretX);
|
|
|
|
}
|
|
|
|
if (!mUndoing) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2022-05-18 23:17:06 +08:00
|
|
|
}
|
2022-03-15 15:34:34 +08:00
|
|
|
return result;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2022-07-02 10:39:31 +08:00
|
|
|
int SynEdit::doInsertTextByLineMode(const BufferCoord& pos, const QStringList& text, BufferCoord &newPos)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
QString Str;
|
|
|
|
int Result = 0;
|
2022-07-02 10:39:31 +08:00
|
|
|
newPos=pos;
|
2022-07-04 10:01:40 +08:00
|
|
|
newPos.ch=1;
|
2022-07-02 10:39:31 +08:00
|
|
|
// mCaretX = 1;
|
|
|
|
// emit statusChanged(SynStatusChange::scCaretX);
|
2021-06-03 20:26:36 +08:00
|
|
|
// Insert string before current line
|
2022-07-02 10:39:31 +08:00
|
|
|
for (int i=0;i<text.length();i++) {
|
2022-04-19 21:18:41 +08:00
|
|
|
if ((mCaretY == mDocument->count()) || mInserting) {
|
|
|
|
mDocument->insert(mCaretY - 1, "");
|
2021-06-03 20:26:36 +08:00
|
|
|
Result++;
|
|
|
|
}
|
2021-09-20 15:57:48 +08:00
|
|
|
properSetLine(mCaretY - 1, Str);
|
2022-07-04 10:01:40 +08:00
|
|
|
newPos.line++;
|
2022-07-02 10:39:31 +08:00
|
|
|
// mCaretY++;
|
|
|
|
// mStatusChanges.setFlag(SynStatusChange::scCaretY);
|
|
|
|
}
|
|
|
|
if (!mUndoing) {
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->addChange(
|
|
|
|
SynChangeReason::Insert,
|
|
|
|
BufferCoord{1,pos.line},newPos,
|
|
|
|
QStringList(),SynSelectionMode::Line);
|
2022-07-02 10:39:31 +08:00
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
return Result;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:57:48 +08:00
|
|
|
void SynEdit::deleteFromTo(const BufferCoord &start, const BufferCoord &end)
|
2021-06-03 20:26:36 +08:00
|
|
|
{
|
|
|
|
if (mReadOnly)
|
|
|
|
return;
|
|
|
|
doOnPaintTransient(SynTransientType::ttBefore);
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((start.ch != end.ch) || (start.line != end.line)) {
|
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
2021-06-03 20:26:36 +08:00
|
|
|
setBlockBegin(start);
|
|
|
|
setBlockEnd(end);
|
2022-07-04 10:01:40 +08:00
|
|
|
doDeleteText(start,end,SynSelectionMode::Normal);
|
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
internalSetCaretXY(start);
|
|
|
|
}
|
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
bool SynEdit::onGetSpecialLineColors(int, QColor &, QColor &)
|
|
|
|
{
|
2021-05-26 18:32:17 +08:00
|
|
|
return false;
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::onGetEditingAreas(int, SynEditingAreaList &)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
void SynEdit::onGutterGetText(int , QString &)
|
2021-05-21 23:33:53 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
void SynEdit::onGutterPaint(QPainter &, int , int , int )
|
2021-05-24 00:41:00 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::onPaint(QPainter &)
|
2021-05-21 23:33:53 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
void SynEdit::onPreparePaintHighlightToken(int , int , const QString &,
|
|
|
|
PSynHighlighterAttribute , SynFontStyles &, QColor &, QColor &)
|
2021-08-28 09:01:40 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-05 23:43:45 +08:00
|
|
|
void SynEdit::onProcessCommand(SynEditorCommand , QChar , void *)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-05 23:43:45 +08:00
|
|
|
void SynEdit::onCommandProcessed(SynEditorCommand , QChar , void *)
|
2021-05-27 20:33:25 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::ExecuteCommand(SynEditorCommand Command, QChar AChar, void *pData)
|
|
|
|
{
|
2022-02-01 14:09:27 +08:00
|
|
|
hideCaret();
|
2021-05-29 21:35:46 +08:00
|
|
|
incPaintLock();
|
2022-02-01 14:09:27 +08:00
|
|
|
|
2021-05-29 21:35:46 +08:00
|
|
|
auto action=finally([this] {
|
|
|
|
decPaintLock();
|
2022-02-01 14:09:27 +08:00
|
|
|
showCaret();
|
2021-05-29 21:35:46 +08:00
|
|
|
});
|
2021-05-27 20:33:25 +08:00
|
|
|
switch(Command) {
|
2021-05-27 21:39:18 +08:00
|
|
|
//horizontal caret movement or selection
|
2021-05-27 20:33:25 +08:00
|
|
|
case SynEditorCommand::ecLeft:
|
|
|
|
case SynEditorCommand::ecSelLeft:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretHorz(-1, Command == SynEditorCommand::ecSelLeft);
|
2021-05-27 20:33:25 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecRight:
|
|
|
|
case SynEditorCommand::ecSelRight:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretHorz(1, Command == SynEditorCommand::ecSelRight);
|
2021-05-27 20:33:25 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecPageLeft:
|
|
|
|
case SynEditorCommand::ecSelPageLeft:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretHorz(-mCharsInWindow, Command == SynEditorCommand::ecSelPageLeft);
|
2021-05-27 20:33:25 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecPageRight:
|
|
|
|
case SynEditorCommand::ecSelPageRight:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretHorz(mCharsInWindow, Command == SynEditorCommand::ecSelPageRight);
|
2021-05-27 20:33:25 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecLineStart:
|
|
|
|
case SynEditorCommand::ecSelLineStart:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretToLineStart(Command == SynEditorCommand::ecSelLineStart);
|
2021-05-27 20:33:25 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecLineEnd:
|
|
|
|
case SynEditorCommand::ecSelLineEnd:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretToLineEnd(Command == SynEditorCommand::ecSelLineEnd);
|
2021-05-27 20:33:25 +08:00
|
|
|
break;
|
2021-05-27 21:39:18 +08:00
|
|
|
// vertical caret movement or selection
|
|
|
|
case SynEditorCommand::ecUp:
|
|
|
|
case SynEditorCommand::ecSelUp:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretVert(-1, Command == SynEditorCommand::ecSelUp);
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDown:
|
|
|
|
case SynEditorCommand::ecSelDown:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretVert(1, Command == SynEditorCommand::ecSelDown);
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecPageUp:
|
|
|
|
case SynEditorCommand::ecSelPageUp:
|
|
|
|
case SynEditorCommand::ecPageDown:
|
|
|
|
case SynEditorCommand::ecSelPageDown:
|
|
|
|
{
|
|
|
|
int counter = mLinesInWindow;
|
|
|
|
if (mOptions.testFlag(eoHalfPageScroll))
|
|
|
|
counter /= 2;
|
|
|
|
if (mOptions.testFlag(eoScrollByOneLess)) {
|
|
|
|
counter -=1;
|
|
|
|
}
|
|
|
|
if (counter<0)
|
|
|
|
break;
|
|
|
|
if (Command == SynEditorCommand::ecPageUp || Command == SynEditorCommand::ecSelPageUp) {
|
|
|
|
counter = -counter;
|
|
|
|
}
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretVert(counter, Command == SynEditorCommand::ecSelPageUp || Command == SynEditorCommand::ecSelPageDown);
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SynEditorCommand::ecPageTop:
|
|
|
|
case SynEditorCommand::ecSelPageTop:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretVert(mTopLine-mCaretY, Command == SynEditorCommand::ecSelPageTop);
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecPageBottom:
|
|
|
|
case SynEditorCommand::ecSelPageBottom:
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretVert(mTopLine+mLinesInWindow-1-mCaretY, Command == SynEditorCommand::ecSelPageBottom);
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
2021-10-13 17:20:31 +08:00
|
|
|
case SynEditorCommand::ecEditorStart:
|
|
|
|
case SynEditorCommand::ecSelEditorStart:
|
|
|
|
moveCaretVert(1-mCaretY, Command == SynEditorCommand::ecSelEditorStart);
|
|
|
|
moveCaretToLineStart(Command == SynEditorCommand::ecSelEditorStart);
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
2021-10-13 17:20:31 +08:00
|
|
|
case SynEditorCommand::ecEditorEnd:
|
|
|
|
case SynEditorCommand::ecSelEditorEnd:
|
2022-04-19 21:18:41 +08:00
|
|
|
if (!mDocument->empty()) {
|
|
|
|
moveCaretVert(mDocument->count()-mCaretY, Command == SynEditorCommand::ecSelEditorEnd);
|
2022-04-07 15:16:11 +08:00
|
|
|
moveCaretToLineEnd(Command == SynEditorCommand::ecSelEditorEnd);
|
2021-10-13 17:20:31 +08:00
|
|
|
}
|
2021-05-27 21:39:18 +08:00
|
|
|
break;
|
2021-05-27 23:45:22 +08:00
|
|
|
// goto special line / column position
|
|
|
|
case SynEditorCommand::ecGotoXY:
|
|
|
|
case SynEditorCommand::ecSelGotoXY:
|
|
|
|
if (pData)
|
2021-09-20 15:57:48 +08:00
|
|
|
moveCaretAndSelection(caretXY(), *((BufferCoord *)(pData)), Command == SynEditorCommand::ecSelGotoXY);
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
|
|
|
// word selection
|
|
|
|
case SynEditorCommand::ecWordLeft:
|
|
|
|
case SynEditorCommand::ecSelWordLeft:
|
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord CaretNew = prevWordPos();
|
|
|
|
moveCaretAndSelection(caretXY(), CaretNew, Command == SynEditorCommand::ecSelWordLeft);
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SynEditorCommand::ecWordRight:
|
|
|
|
case SynEditorCommand::ecSelWordRight:
|
|
|
|
{
|
2021-09-20 15:57:48 +08:00
|
|
|
BufferCoord CaretNew = nextWordPos();
|
|
|
|
moveCaretAndSelection(caretXY(), CaretNew, Command == SynEditorCommand::ecSelWordRight);
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
case SynEditorCommand::ecSelWord:
|
2021-09-20 15:57:48 +08:00
|
|
|
setSelWord();
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecSelectAll:
|
2021-06-07 11:02:03 +08:00
|
|
|
doSelectAll();
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDeleteLastChar:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDeleteLastChar();
|
2021-05-29 21:35:46 +08:00
|
|
|
break;
|
2021-06-03 20:26:36 +08:00
|
|
|
case SynEditorCommand::ecDeleteChar:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDeleteCurrentChar();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDeleteWord:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDeleteWord();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDeleteEOL:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDeleteToEOL();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
2022-01-24 11:23:30 +08:00
|
|
|
case SynEditorCommand::ecDeleteWordStart:
|
|
|
|
doDeleteToWordStart();
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDeleteWordEnd:
|
|
|
|
doDeleteToWordEnd();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDeleteBOL:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDeleteFromBOL();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDeleteLine:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDeleteLine();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecDuplicateLine:
|
2021-06-07 11:02:03 +08:00
|
|
|
doDuplicateLine();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecMoveSelUp:
|
2021-06-07 11:02:03 +08:00
|
|
|
doMoveSelUp();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecMoveSelDown:
|
2021-06-07 11:02:03 +08:00
|
|
|
doMoveSelDown();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecClearAll:
|
2021-06-07 11:02:03 +08:00
|
|
|
clearAll();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecInsertLine:
|
2022-03-16 21:13:59 +08:00
|
|
|
insertLine(false);
|
2021-11-22 16:16:58 +08:00
|
|
|
break;
|
2021-06-03 20:26:36 +08:00
|
|
|
case SynEditorCommand::ecLineBreak:
|
2022-03-16 21:13:59 +08:00
|
|
|
insertLine(true);
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecLineBreakAtEnd:
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
2022-03-16 21:13:59 +08:00
|
|
|
moveCaretToLineEnd(false);
|
|
|
|
insertLine(true);
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecTab:
|
2021-06-07 11:02:03 +08:00
|
|
|
doTabKey();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecShiftTab:
|
2021-06-07 11:02:03 +08:00
|
|
|
doShiftTabKey();
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecChar:
|
2021-06-07 11:02:03 +08:00
|
|
|
doAddChar(AChar);
|
2021-06-03 20:26:36 +08:00
|
|
|
break;
|
2021-06-03 23:18:51 +08:00
|
|
|
case SynEditorCommand::ecInsertMode:
|
|
|
|
if (!mReadOnly)
|
2021-06-12 22:36:23 +08:00
|
|
|
setInsertMode(true);
|
2021-06-03 23:18:51 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecOverwriteMode:
|
|
|
|
if (!mReadOnly)
|
2021-06-12 22:36:23 +08:00
|
|
|
setInsertMode(false);
|
2021-06-03 23:18:51 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecToggleMode:
|
|
|
|
if (!mReadOnly) {
|
2021-06-12 22:36:23 +08:00
|
|
|
setInsertMode(!mInserting);
|
2021-06-03 23:18:51 +08:00
|
|
|
}
|
|
|
|
break;
|
2021-06-05 23:43:45 +08:00
|
|
|
case SynEditorCommand::ecCut:
|
2022-01-10 18:37:00 +08:00
|
|
|
if (!mReadOnly)
|
2021-06-07 11:02:03 +08:00
|
|
|
doCutToClipboard();
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecCopy:
|
2022-01-10 18:37:00 +08:00
|
|
|
doCopyToClipboard();
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecPaste:
|
|
|
|
if (!mReadOnly)
|
2021-06-07 11:02:03 +08:00
|
|
|
doPasteFromClipboard();
|
2021-06-05 23:43:45 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecImeStr:
|
2021-06-07 11:02:03 +08:00
|
|
|
case SynEditorCommand::ecString:
|
2021-06-05 23:43:45 +08:00
|
|
|
if (!mReadOnly)
|
|
|
|
doAddStr(*((QString*)pData));
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecUndo:
|
|
|
|
if (!mReadOnly)
|
|
|
|
doUndo();
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecRedo:
|
|
|
|
if (!mReadOnly)
|
|
|
|
doRedo();
|
|
|
|
break;
|
2021-06-07 11:02:03 +08:00
|
|
|
case SynEditorCommand::ecZoomIn:
|
|
|
|
doZoomIn();
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecZoomOut:
|
|
|
|
doZoomOut();
|
|
|
|
break;
|
2021-06-21 11:21:26 +08:00
|
|
|
case SynEditorCommand::ecComment:
|
|
|
|
doComment();
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecUncomment:
|
|
|
|
doUncomment();
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecToggleComment:
|
|
|
|
doToggleComment();
|
|
|
|
break;
|
2022-03-12 18:03:52 +08:00
|
|
|
case SynEditorCommand::ecToggleBlockComment:
|
|
|
|
doToggleBlockComment();
|
|
|
|
break;
|
2022-01-13 07:39:56 +08:00
|
|
|
case SynEditorCommand::ecNormalSelect:
|
2022-07-04 10:01:40 +08:00
|
|
|
setSelectionMode(SynSelectionMode::Normal);
|
2022-01-13 07:39:56 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecLineSelect:
|
2022-07-04 10:01:40 +08:00
|
|
|
setSelectionMode(SynSelectionMode::Line);
|
2022-01-13 07:39:56 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecColumnSelect:
|
2022-07-04 10:01:40 +08:00
|
|
|
setSelectionMode(SynSelectionMode::Column);
|
2022-01-13 07:39:56 +08:00
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecScrollLeft:
|
|
|
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value()-mMouseWheelScrollSpeed);
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecScrollRight:
|
|
|
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value()+mMouseWheelScrollSpeed);
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecScrollUp:
|
|
|
|
verticalScrollBar()->setValue(verticalScrollBar()->value()-mMouseWheelScrollSpeed);
|
|
|
|
break;
|
|
|
|
case SynEditorCommand::ecScrollDown:
|
|
|
|
verticalScrollBar()->setValue(verticalScrollBar()->value()+mMouseWheelScrollSpeed);
|
|
|
|
break;
|
2022-03-13 18:31:05 +08:00
|
|
|
case SynEditorCommand::ecMatchBracket:
|
2022-03-13 18:45:48 +08:00
|
|
|
{
|
|
|
|
BufferCoord coord = getMatchingBracket();
|
2022-07-04 10:01:40 +08:00
|
|
|
if (coord.ch!=0 && coord.line!=0)
|
2022-03-13 18:45:48 +08:00
|
|
|
internalSetCaretXY(coord);
|
|
|
|
}
|
2022-03-13 18:31:05 +08:00
|
|
|
break;
|
2021-10-21 19:33:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
2021-05-27 20:33:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-04 11:07:35 +08:00
|
|
|
void SynEdit::onEndFirstPaintLock()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::onBeginFirstPaintLock()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-01 01:06:43 +08:00
|
|
|
bool SynEdit::isIdentChar(const QChar &ch)
|
|
|
|
{
|
|
|
|
if (mHighlighter) {
|
|
|
|
return mHighlighter->isIdentChar(ch);
|
|
|
|
} else {
|
|
|
|
if (ch == '_') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((ch>='0') && (ch <= '9')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((ch>='a') && (ch <= 'z')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ((ch>='A') && (ch <= 'Z')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:59:03 +08:00
|
|
|
void SynEdit::setRainbowAttrs(const PSynHighlighterAttribute &attr0, const PSynHighlighterAttribute &attr1, const PSynHighlighterAttribute &attr2, const PSynHighlighterAttribute &attr3)
|
|
|
|
{
|
|
|
|
mRainbowAttr0 = attr0;
|
|
|
|
mRainbowAttr1 = attr1;
|
|
|
|
mRainbowAttr2 = attr2;
|
|
|
|
mRainbowAttr3 = attr3;
|
|
|
|
}
|
|
|
|
|
2021-09-27 00:52:25 +08:00
|
|
|
void SynEdit::updateMouseCursor(){
|
|
|
|
QPoint p = mapFromGlobal(cursor().pos());
|
|
|
|
if (p.y() >= clientHeight() || p.x()>= clientWidth()) {
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
} else if (p.x() > mGutterWidth) {
|
|
|
|
setCursor(Qt::IBeamCursor);
|
|
|
|
} else {
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 23:13:43 +08:00
|
|
|
bool SynEdit::isCaretVisible()
|
|
|
|
{
|
|
|
|
if (mCaretY < mTopLine)
|
|
|
|
return false;
|
|
|
|
if (mCaretY >= mTopLine + mLinesInWindow )
|
|
|
|
return false;
|
|
|
|
if (mCaretX < mLeftChar)
|
|
|
|
return false;
|
|
|
|
if (mCaretX >= mLeftChar + mCharsInWindow)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
void SynEdit::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
if (mPainterLock>0)
|
|
|
|
return;
|
|
|
|
if (mPainting)
|
|
|
|
return;
|
|
|
|
mPainting = true;
|
2021-05-24 21:48:03 +08:00
|
|
|
auto action = finally([&,this] {
|
|
|
|
mPainting = false;
|
|
|
|
});
|
2021-05-14 23:56:43 +08:00
|
|
|
|
|
|
|
// Now paint everything while the caret is hidden.
|
2021-05-24 00:41:00 +08:00
|
|
|
QPainter painter(viewport());
|
|
|
|
//Get the invalidated rect.
|
|
|
|
QRect rcClip = event->rect();
|
2021-10-05 00:42:35 +08:00
|
|
|
QRect rcCaret = calculateCaretRect();
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-05-24 18:11:07 +08:00
|
|
|
if (rcCaret == rcClip) {
|
2021-05-24 00:41:00 +08:00
|
|
|
// only update caret
|
|
|
|
// calculate the needed invalid area for caret
|
2021-05-27 21:39:18 +08:00
|
|
|
//qDebug()<<"update caret"<<rcCaret;
|
2022-04-02 18:22:29 +08:00
|
|
|
QRectF cacheRC;
|
|
|
|
qreal dpr = mContentImage->devicePixelRatioF();
|
|
|
|
cacheRC.setLeft(rcClip.left()*dpr);
|
|
|
|
cacheRC.setTop(rcClip.top()*dpr);
|
|
|
|
cacheRC.setWidth(rcClip.width()*dpr);
|
|
|
|
cacheRC.setHeight(rcClip.height()*dpr);
|
|
|
|
painter.drawImage(rcCaret,*mContentImage,cacheRC);
|
2021-05-24 00:41:00 +08:00
|
|
|
} else {
|
|
|
|
QRect rcDraw;
|
|
|
|
int nL1, nL2, nC1, nC2;
|
|
|
|
// Compute the invalid area in lines / columns.
|
|
|
|
// columns
|
|
|
|
nC1 = mLeftChar;
|
|
|
|
if (rcClip.left() > mGutterWidth + 2 )
|
|
|
|
nC1 += (rcClip.left() - mGutterWidth - 2 ) / mCharWidth;
|
|
|
|
nC2 = mLeftChar +
|
|
|
|
(rcClip.right() - mGutterWidth - 2 + mCharWidth - 1) / mCharWidth;
|
|
|
|
// lines
|
2021-09-11 18:42:49 +08:00
|
|
|
nL1 = minMax(mTopLine + rcClip.top() / mTextHeight, mTopLine, displayLineCount());
|
|
|
|
nL2 = minMax(mTopLine + (rcClip.bottom() + mTextHeight - 1) / mTextHeight, 1, displayLineCount());
|
2021-05-24 00:41:00 +08:00
|
|
|
|
2021-06-09 17:12:23 +08:00
|
|
|
//qDebug()<<"Paint:"<<nL1<<nL2<<nC1<<nC2;
|
2021-05-24 21:48:03 +08:00
|
|
|
|
2021-05-24 18:11:07 +08:00
|
|
|
QPainter cachePainter(mContentImage.get());
|
|
|
|
cachePainter.setFont(font());
|
|
|
|
SynEditTextPainter textPainter(this, &cachePainter,
|
2021-05-24 00:41:00 +08:00
|
|
|
nL1,nL2,nC1,nC2);
|
|
|
|
// First paint paint the text area if it was (partly) invalidated.
|
|
|
|
if (rcClip.right() > mGutterWidth ) {
|
|
|
|
rcDraw = rcClip;
|
|
|
|
rcDraw.setLeft( std::max(rcDraw.left(), mGutterWidth));
|
|
|
|
textPainter.paintTextLines(rcDraw);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then the gutter area if it was (partly) invalidated.
|
|
|
|
if (rcClip.left() < mGutterWidth) {
|
|
|
|
rcDraw = rcClip;
|
2021-05-27 20:33:25 +08:00
|
|
|
rcDraw.setRight(mGutterWidth-1);
|
2021-05-24 18:11:07 +08:00
|
|
|
textPainter.paintGutter(rcDraw);
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//PluginsAfterPaint(Canvas, rcClip, nL1, nL2);
|
|
|
|
// If there is a custom paint handler call it.
|
2021-05-24 18:11:07 +08:00
|
|
|
onPaint(painter);
|
2021-05-24 00:41:00 +08:00
|
|
|
doOnPaintTransient(SynTransientType::ttAfter);
|
2022-04-02 18:22:29 +08:00
|
|
|
QRectF cacheRC;
|
|
|
|
qreal dpr = mContentImage->devicePixelRatioF();
|
|
|
|
cacheRC.setLeft(rcClip.left()*dpr);
|
|
|
|
cacheRC.setTop(rcClip.top()*dpr);
|
|
|
|
cacheRC.setWidth(rcClip.width()*dpr);
|
|
|
|
cacheRC.setHeight(rcClip.height()*dpr);
|
|
|
|
painter.drawImage(rcClip,*mContentImage,cacheRC);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2021-05-24 18:11:07 +08:00
|
|
|
paintCaret(painter, rcCaret);
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
void SynEdit::resizeEvent(QResizeEvent *)
|
2021-05-24 00:41:00 +08:00
|
|
|
{
|
|
|
|
//resize the cache image
|
2022-04-02 18:22:29 +08:00
|
|
|
qreal dpr = devicePixelRatioF();
|
|
|
|
std::shared_ptr<QImage> image = std::make_shared<QImage>(clientWidth()*dpr,clientHeight()*dpr,
|
2021-05-24 00:41:00 +08:00
|
|
|
QImage::Format_ARGB32);
|
2022-04-02 18:22:29 +08:00
|
|
|
image->setDevicePixelRatio(dpr);
|
2021-05-24 00:41:00 +08:00
|
|
|
QRect newRect = image->rect().intersected(mContentImage->rect());
|
|
|
|
|
|
|
|
QPainter painter(image.get());
|
|
|
|
|
|
|
|
painter.drawImage(newRect,*mContentImage);
|
|
|
|
|
|
|
|
mContentImage = image;
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
onSizeOrFontChanged(false);
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::timerEvent(QTimerEvent *event)
|
|
|
|
{
|
|
|
|
if (event->timerId() == m_blinkTimerId) {
|
2021-05-24 18:11:07 +08:00
|
|
|
m_blinkStatus = 1- m_blinkStatus;
|
2021-05-27 23:45:22 +08:00
|
|
|
updateCaret();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-05-24 00:41:00 +08:00
|
|
|
bool SynEdit::event(QEvent *event)
|
|
|
|
{
|
|
|
|
switch(event->type()) {
|
2021-10-02 13:29:45 +08:00
|
|
|
case QEvent::KeyPress:{
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent *>(event);
|
|
|
|
if(keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab)
|
|
|
|
{
|
|
|
|
// process tab key presse event
|
|
|
|
keyPressEvent(keyEvent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-06-07 11:02:03 +08:00
|
|
|
case QEvent::FontChange:
|
2021-05-24 00:41:00 +08:00
|
|
|
synFontChanged();
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mDocument)
|
|
|
|
mDocument->setFontMetrics(font());
|
2021-05-24 00:41:00 +08:00
|
|
|
break;
|
2021-06-07 11:02:03 +08:00
|
|
|
case QEvent::MouseMove: {
|
2021-09-27 00:52:25 +08:00
|
|
|
updateMouseCursor();
|
2021-06-07 11:02:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-10-21 19:33:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
2021-05-24 00:41:00 +08:00
|
|
|
}
|
2021-06-21 22:01:35 +08:00
|
|
|
return QAbstractScrollArea::event(event);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:48:03 +08:00
|
|
|
void SynEdit::focusInEvent(QFocusEvent *)
|
|
|
|
{
|
|
|
|
showCaret();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::focusOutEvent(QFocusEvent *)
|
|
|
|
{
|
|
|
|
hideCaret();
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:33:25 +08:00
|
|
|
void SynEdit::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
2022-03-25 22:13:00 +08:00
|
|
|
if (event->key() == Qt::Key_Escape && mActiveSelectionMode != mSelectionMode) {
|
|
|
|
setActiveSelectionMode(selectionMode());
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
2021-05-27 20:33:25 +08:00
|
|
|
event->accept();
|
2022-03-25 22:13:00 +08:00
|
|
|
} else {
|
|
|
|
SynEditorCommand cmd=TranslateKeyCode(event->key(),event->modifiers());
|
|
|
|
if (cmd!=SynEditorCommand::ecNone) {
|
|
|
|
commandProcessor(cmd,QChar(),nullptr);
|
2021-05-27 20:33:25 +08:00
|
|
|
event->accept();
|
2022-03-25 22:13:00 +08:00
|
|
|
} else if (!event->text().isEmpty()) {
|
|
|
|
QChar c = event->text().at(0);
|
|
|
|
if (c=='\t' || c.isPrint()) {
|
|
|
|
commandProcessor(SynEditorCommand::ecChar,c,nullptr);
|
|
|
|
event->accept();
|
|
|
|
}
|
2021-05-27 20:33:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!event->isAccepted()) {
|
|
|
|
QAbstractScrollArea::keyPressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-27 23:45:22 +08:00
|
|
|
void SynEdit::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
bool bWasSel = false;
|
|
|
|
bool bStartDrag = false;
|
2021-06-09 17:12:23 +08:00
|
|
|
mMouseMoved = false;
|
2021-05-27 23:45:22 +08:00
|
|
|
Qt::MouseButton button = event->button();
|
|
|
|
int X=event->pos().x();
|
|
|
|
int Y=event->pos().y();
|
|
|
|
|
|
|
|
QAbstractScrollArea::mousePressEvent(event);
|
|
|
|
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord oldCaret=caretXY();
|
2021-05-27 23:45:22 +08:00
|
|
|
if (button == Qt::RightButton) {
|
|
|
|
if (mOptions.testFlag(eoRightMouseMovesCursor) &&
|
2021-09-20 15:57:48 +08:00
|
|
|
( (selAvail() && ! isPointInSelection(displayToBufferPos(pixelsToRowColumn(X, Y))))
|
2021-05-27 23:45:22 +08:00
|
|
|
|| ! selAvail())) {
|
|
|
|
invalidateSelection();
|
2022-07-04 10:01:40 +08:00
|
|
|
//mBlockEnd=mBlockBegin;
|
2022-03-07 20:30:45 +08:00
|
|
|
computeCaret();
|
2021-05-27 23:45:22 +08:00
|
|
|
}else {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-02 20:19:37 +08:00
|
|
|
} else if (button == Qt::LeftButton) {
|
2021-05-27 23:45:22 +08:00
|
|
|
if (selAvail()) {
|
|
|
|
//remember selection state, as it will be cleared later
|
|
|
|
bWasSel = true;
|
2021-05-29 21:35:46 +08:00
|
|
|
mMouseDownPos = event->pos();
|
2021-05-27 23:45:22 +08:00
|
|
|
}
|
2022-03-07 20:30:45 +08:00
|
|
|
computeCaret();
|
2021-05-27 23:45:22 +08:00
|
|
|
mStateFlags.setFlag(SynStateFlag::sfWaitForDragging,false);
|
|
|
|
if (bWasSel && mOptions.testFlag(eoDragDropEditing) && (X >= mGutterWidth + 2)
|
2022-07-04 10:01:40 +08:00
|
|
|
&& (mSelectionMode == SynSelectionMode::Normal) && isPointInSelection(displayToBufferPos(pixelsToRowColumn(X, Y))) ) {
|
2021-11-13 12:33:33 +08:00
|
|
|
bStartDrag = true;
|
2021-05-27 23:45:22 +08:00
|
|
|
}
|
|
|
|
if (bStartDrag) {
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfWaitForDragging);
|
|
|
|
} else {
|
2021-10-13 17:20:31 +08:00
|
|
|
if (event->modifiers() == Qt::ShiftModifier) {
|
2021-05-29 21:35:46 +08:00
|
|
|
//BlockBegin and BlockEnd are restored to their original position in the
|
|
|
|
//code from above and SetBlockEnd will take care of proper invalidation
|
|
|
|
setBlockEnd(caretXY());
|
2021-10-13 17:20:31 +08:00
|
|
|
} else if (mOptions.testFlag(eoAltSetsColumnMode) &&
|
2022-07-04 10:01:40 +08:00
|
|
|
(mActiveSelectionMode != SynSelectionMode::Line)) {
|
2021-05-29 21:35:46 +08:00
|
|
|
if (event->modifiers() == Qt::AltModifier)
|
2022-07-04 10:01:40 +08:00
|
|
|
setActiveSelectionMode(SynSelectionMode::Column);
|
2021-05-29 21:35:46 +08:00
|
|
|
else
|
2022-03-25 10:43:07 +08:00
|
|
|
setActiveSelectionMode(selectionMode());
|
2021-10-13 17:20:31 +08:00
|
|
|
//Selection mode must be set before calling SetBlockBegin
|
|
|
|
setBlockBegin(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
2022-03-07 20:30:45 +08:00
|
|
|
computeScroll(false);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if (oldCaret!=caretXY()) {
|
|
|
|
if (mOptions.testFlag(SynEditorOption::eoGroupUndo))
|
|
|
|
mUndoList->addGroupBreak();
|
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractScrollArea::mouseReleaseEvent(event);
|
|
|
|
int X=event->pos().x();
|
2022-04-01 23:10:38 +08:00
|
|
|
/* int Y=event->pos().y(); */
|
2021-05-29 21:35:46 +08:00
|
|
|
|
|
|
|
if (!mMouseMoved && (X < mGutterWidth + 2)) {
|
2021-06-09 17:12:23 +08:00
|
|
|
processGutterClick(event);
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
2022-07-04 10:01:40 +08:00
|
|
|
BufferCoord oldCaret=caretXY();
|
2021-05-29 21:35:46 +08:00
|
|
|
if (mStateFlags.testFlag(SynStateFlag::sfWaitForDragging) &&
|
|
|
|
!mStateFlags.testFlag(SynStateFlag::sfDblClicked)) {
|
2022-03-07 20:30:45 +08:00
|
|
|
computeCaret();
|
2021-06-05 23:43:45 +08:00
|
|
|
if (! (event->modifiers() & Qt::ShiftModifier))
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
setBlockEnd(caretXY());
|
2021-05-29 21:35:46 +08:00
|
|
|
mStateFlags.setFlag(SynStateFlag::sfWaitForDragging, false);
|
|
|
|
}
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfDblClicked,false);
|
2022-07-04 10:01:40 +08:00
|
|
|
if (oldCaret!=caretXY()) {
|
|
|
|
if (mOptions.testFlag(SynEditorOption::eoGroupUndo))
|
|
|
|
mUndoList->addGroupBreak();
|
|
|
|
}
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractScrollArea::mouseMoveEvent(event);
|
|
|
|
mMouseMoved = true;
|
|
|
|
Qt::MouseButtons buttons = event->buttons();
|
|
|
|
if ((mStateFlags.testFlag(SynStateFlag::sfWaitForDragging))) {
|
|
|
|
if ( ( event->pos() - mMouseDownPos).manhattanLength()>=QApplication::startDragDistance()) {
|
2021-11-13 12:33:33 +08:00
|
|
|
mStateFlags.setFlag(SynStateFlag::sfWaitForDragging,false);
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
QMimeData *mimeData = new QMimeData;
|
|
|
|
|
|
|
|
mimeData->setText(selText());
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
|
2021-11-13 17:15:07 +08:00
|
|
|
drag->exec(Qt::CopyAction | Qt::MoveAction);
|
2021-05-27 23:45:22 +08:00
|
|
|
}
|
2021-06-05 23:43:45 +08:00
|
|
|
} else if ((buttons == Qt::LeftButton)) {
|
2022-03-25 10:43:07 +08:00
|
|
|
if (mOptions.testFlag(eoAltSetsColumnMode) &&
|
2022-07-04 10:01:40 +08:00
|
|
|
(mActiveSelectionMode != SynSelectionMode::Line)) {
|
2022-03-25 10:43:07 +08:00
|
|
|
if (event->modifiers() == Qt::AltModifier)
|
2022-07-04 10:01:40 +08:00
|
|
|
setActiveSelectionMode(SynSelectionMode::Column);
|
2022-03-25 10:43:07 +08:00
|
|
|
else
|
|
|
|
setActiveSelectionMode(selectionMode());
|
|
|
|
}
|
2021-06-07 11:02:03 +08:00
|
|
|
} else if (buttons == Qt::NoButton) {
|
2021-09-27 00:52:25 +08:00
|
|
|
updateMouseCursor();
|
2021-05-29 21:35:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractScrollArea::mouseDoubleClickEvent(event);
|
|
|
|
QPoint ptMouse = event->pos();
|
|
|
|
if (ptMouse.x() >= mGutterWidth + 2) {
|
2021-09-20 15:57:48 +08:00
|
|
|
setWordBlock(caretXY());
|
2022-04-07 17:30:06 +08:00
|
|
|
mStateFlags.setFlag(SynStateFlag::sfDblClicked);
|
2021-05-27 23:45:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 23:43:45 +08:00
|
|
|
void SynEdit::inputMethodEvent(QInputMethodEvent *event)
|
|
|
|
{
|
2021-10-05 21:25:23 +08:00
|
|
|
// qDebug()<<event->replacementStart()<<":"<<event->replacementLength()<<" - "
|
|
|
|
// << event->preeditString()<<" - "<<event->commitString();
|
2021-10-05 00:42:35 +08:00
|
|
|
|
|
|
|
QString oldString = mInputPreeditString;
|
|
|
|
mInputPreeditString = event->preeditString();
|
|
|
|
if (oldString!=mInputPreeditString) {
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column) {
|
2022-03-26 19:10:14 +08:00
|
|
|
BufferCoord selBegin = blockBegin();
|
|
|
|
BufferCoord selEnd = blockEnd();
|
2022-07-04 10:01:40 +08:00
|
|
|
invalidateLines(selBegin.line,selEnd.line);
|
2022-03-26 19:10:14 +08:00
|
|
|
} else
|
|
|
|
invalidateLine(mCaretY);
|
2021-10-05 00:42:35 +08:00
|
|
|
}
|
2021-06-05 23:43:45 +08:00
|
|
|
QString s = event->commitString();
|
|
|
|
if (!s.isEmpty()) {
|
2021-09-20 15:57:48 +08:00
|
|
|
commandProcessor(SynEditorCommand::ecImeStr,QChar(),&s);
|
2021-06-05 23:43:45 +08:00
|
|
|
// for (QChar ch:s) {
|
|
|
|
// CommandProcessor(SynEditorCommand::ecChar,ch);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 10:14:07 +08:00
|
|
|
void SynEdit::leaveEvent(QEvent *)
|
2021-06-07 11:02:03 +08:00
|
|
|
{
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:12:23 +08:00
|
|
|
void SynEdit::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
2021-11-13 12:33:33 +08:00
|
|
|
if (event->modifiers() == Qt::ShiftModifier) {
|
|
|
|
if (event->angleDelta().y()>0) {
|
|
|
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value()-mMouseWheelScrollSpeed);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
} else if (event->angleDelta().y()<0) {
|
|
|
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value()+mMouseWheelScrollSpeed);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (event->angleDelta().y()>0) {
|
|
|
|
verticalScrollBar()->setValue(verticalScrollBar()->value()-mMouseWheelScrollSpeed);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
} else if (event->angleDelta().y()<0) {
|
|
|
|
verticalScrollBar()->setValue(verticalScrollBar()->value()+mMouseWheelScrollSpeed);
|
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
2021-06-09 17:12:23 +08:00
|
|
|
}
|
|
|
|
QAbstractScrollArea::wheelEvent(event);
|
|
|
|
}
|
|
|
|
|
2021-06-07 21:34:48 +08:00
|
|
|
bool SynEdit::viewportEvent(QEvent * event)
|
|
|
|
{
|
2021-08-27 23:51:42 +08:00
|
|
|
// switch (event->type()) {
|
|
|
|
// case QEvent::Resize:
|
|
|
|
// sizeOrFontChanged(false);
|
|
|
|
// break;
|
|
|
|
// }
|
2021-06-07 21:34:48 +08:00
|
|
|
return QAbstractScrollArea::viewportEvent(event);
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:19:48 +08:00
|
|
|
QVariant SynEdit::inputMethodQuery(Qt::InputMethodQuery property) const
|
|
|
|
{
|
|
|
|
QRect rect = calculateInputCaretRect();
|
|
|
|
|
|
|
|
switch(property) {
|
|
|
|
case Qt::ImCursorRectangle:
|
|
|
|
return rect;
|
|
|
|
default:
|
|
|
|
return QWidget::inputMethodQuery(property);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:33:33 +08:00
|
|
|
void SynEdit::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("text/plain")) {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
mDragCaretSave = caretXY();
|
|
|
|
mDragSelBeginSave = blockBegin();
|
|
|
|
mDragSelEndSave = blockEnd();
|
2022-03-31 19:36:46 +08:00
|
|
|
BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(event->pos().x(),
|
2021-11-13 12:33:33 +08:00
|
|
|
event->pos().y()));
|
2021-12-23 22:05:57 +08:00
|
|
|
internalSetCaretXY(coord);
|
2021-11-13 12:33:33 +08:00
|
|
|
setBlockBegin(mDragSelBeginSave);
|
|
|
|
setBlockEnd(mDragSelEndSave);
|
|
|
|
showCaret();
|
2022-03-07 20:30:45 +08:00
|
|
|
computeScroll(true);
|
2021-11-13 12:33:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::dropEvent(QDropEvent *event)
|
|
|
|
{
|
2021-11-26 19:04:04 +08:00
|
|
|
//mScrollTimer->stop();
|
2021-11-26 19:54:05 +08:00
|
|
|
|
2022-03-31 19:36:46 +08:00
|
|
|
BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(event->pos().x(),
|
2021-11-26 19:04:04 +08:00
|
|
|
event->pos().y()));
|
2021-11-26 19:54:05 +08:00
|
|
|
if (coord>=mDragSelBeginSave && coord<=mDragSelEndSave) {
|
|
|
|
//do nothing if drag onto itself
|
2022-07-02 14:06:10 +08:00
|
|
|
event->acceptProposedAction();
|
|
|
|
mDropped = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int topLine = mTopLine;
|
|
|
|
int leftChar = mLeftChar;
|
|
|
|
QStringList text=splitStrings(event->mimeData()->text());
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->beginBlock();
|
2022-07-02 14:06:10 +08:00
|
|
|
addLeftTopToUndo();
|
|
|
|
addCaretToUndo();
|
|
|
|
addSelectionToUndo();
|
|
|
|
internalSetCaretXY(coord);
|
|
|
|
if (event->proposedAction() == Qt::DropAction::CopyAction) {
|
2021-11-26 19:54:05 +08:00
|
|
|
//just copy it
|
2022-07-04 10:01:40 +08:00
|
|
|
doInsertText(coord,text,mActiveSelectionMode,coord.line,coord.line+text.length()-1);
|
2021-11-26 19:54:05 +08:00
|
|
|
} else if (event->proposedAction() == Qt::DropAction::MoveAction) {
|
|
|
|
if (coord < mDragSelBeginSave ) {
|
|
|
|
//delete old
|
2022-07-02 14:06:10 +08:00
|
|
|
doDeleteText(mDragSelBeginSave,mDragSelEndSave,mActiveSelectionMode);
|
2021-11-26 19:54:05 +08:00
|
|
|
//paste to new position
|
2022-07-04 10:01:40 +08:00
|
|
|
doInsertText(coord,text,mActiveSelectionMode,coord.line,coord.line+text.length()-1);
|
2021-11-26 19:54:05 +08:00
|
|
|
} else {
|
|
|
|
//paste to new position
|
2022-07-04 10:01:40 +08:00
|
|
|
doInsertText(coord,text,mActiveSelectionMode,coord.line,coord.line+text.length()-1);
|
2021-11-26 19:54:05 +08:00
|
|
|
//delete old
|
2022-07-02 14:06:10 +08:00
|
|
|
doDeleteText(mDragSelBeginSave,mDragSelEndSave,mActiveSelectionMode);
|
2021-11-26 19:54:05 +08:00
|
|
|
//set caret to right pos
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mDragSelBeginSave.line == mDragSelEndSave.line) {
|
|
|
|
if (coord.line == mDragSelEndSave.line) {
|
|
|
|
coord.ch -= mDragSelEndSave.ch-mDragSelBeginSave.ch;
|
2021-11-26 19:54:05 +08:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
if (coord.line == mDragSelEndSave.line) {
|
|
|
|
coord.ch -= mDragSelEndSave.ch-1;
|
2021-11-26 19:54:05 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
coord.line -= mDragSelEndSave.line-mDragSelBeginSave.line;
|
|
|
|
topLine -= mDragSelEndSave.line-mDragSelBeginSave.line;
|
2021-11-26 19:54:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->endBlock();
|
2022-07-02 14:06:10 +08:00
|
|
|
|
2021-11-26 19:54:05 +08:00
|
|
|
}
|
2021-11-13 12:33:33 +08:00
|
|
|
event->acceptProposedAction();
|
2022-03-12 18:03:52 +08:00
|
|
|
mDropped = true;
|
2022-07-02 14:06:10 +08:00
|
|
|
setTopLine(topLine);
|
|
|
|
setLeftChar(leftChar);
|
|
|
|
internalSetCaretXY(coord);
|
2021-11-13 12:33:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
{
|
|
|
|
if (event->keyboardModifiers() == Qt::ControlModifier) {
|
|
|
|
event->setDropAction(Qt::CopyAction);
|
|
|
|
} else {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
}
|
2021-11-26 08:14:23 +08:00
|
|
|
// should we begin scrolling?
|
2022-03-04 19:04:45 +08:00
|
|
|
// computeScroll(event->pos().x(),
|
|
|
|
// event->pos().y(),true);
|
2021-11-26 08:14:23 +08:00
|
|
|
|
2022-03-07 20:30:45 +08:00
|
|
|
QPoint iMousePos = QCursor::pos();
|
|
|
|
iMousePos = mapFromGlobal(iMousePos);
|
|
|
|
int X=iMousePos.x();
|
|
|
|
int Y=iMousePos.y();
|
2022-03-31 19:36:46 +08:00
|
|
|
BufferCoord coord = displayToBufferPos(pixelsToNearestRowColumn(X,Y));
|
2021-11-26 08:14:23 +08:00
|
|
|
internalSetCaretXY(coord);
|
2021-11-13 12:33:33 +08:00
|
|
|
setBlockBegin(mDragSelBeginSave);
|
|
|
|
setBlockEnd(mDragSelEndSave);
|
|
|
|
showCaret();
|
|
|
|
}
|
|
|
|
|
2021-11-13 17:15:07 +08:00
|
|
|
void SynEdit::dragLeaveEvent(QDragLeaveEvent *)
|
2021-11-13 12:33:33 +08:00
|
|
|
{
|
2021-11-26 08:29:00 +08:00
|
|
|
// setCaretXY(mDragCaretSave);
|
|
|
|
// setBlockBegin(mDragSelBeginSave);
|
|
|
|
// setBlockEnd(mDragSelEndSave);
|
2021-12-13 12:09:37 +08:00
|
|
|
// showCaret();
|
|
|
|
}
|
|
|
|
|
2021-06-08 21:41:42 +08:00
|
|
|
int SynEdit::maxScrollHeight() const
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-06-08 21:41:42 +08:00
|
|
|
if (mOptions.testFlag(eoScrollPastEof))
|
|
|
|
return std::max(displayLineCount(),1);
|
|
|
|
else
|
|
|
|
return std::max(displayLineCount()-mLinesInWindow+1, 1);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SynEdit::modified() const
|
|
|
|
{
|
|
|
|
return mModified;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setModified(bool Value)
|
|
|
|
{
|
2021-11-04 00:38:40 +08:00
|
|
|
if (Value) {
|
2021-05-14 23:56:43 +08:00
|
|
|
mLastModifyTime = QDateTime::currentDateTime();
|
2021-11-04 00:38:40 +08:00
|
|
|
emit statusChanged(SynStatusChange::scModified);
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
if (Value != mModified) {
|
|
|
|
mModified = Value;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mOptions.testFlag(SynEditorOption::eoGroupUndo) && (!Value) ) {
|
|
|
|
mUndoList->addGroupBreak();
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
mUndoList->setInitialState(!Value);
|
2021-11-04 00:38:40 +08:00
|
|
|
emit statusChanged(SynStatusChange::scModifyChanged);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::gutterWidth() const
|
|
|
|
{
|
|
|
|
return mGutterWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setGutterWidth(int Value)
|
|
|
|
{
|
|
|
|
Value = std::max(Value, 0);
|
|
|
|
if (mGutterWidth != Value) {
|
|
|
|
mGutterWidth = Value;
|
2021-09-02 12:14:02 +08:00
|
|
|
onSizeOrFontChanged(false);
|
2021-06-07 21:34:48 +08:00
|
|
|
invalidate();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::charWidth() const
|
|
|
|
{
|
|
|
|
return mCharWidth;
|
|
|
|
}
|
|
|
|
|
2022-03-20 18:10:53 +08:00
|
|
|
void SynEdit::setUndoLimit(int size)
|
|
|
|
{
|
|
|
|
mUndoList->setMaxUndoActions(size);
|
|
|
|
|
|
|
|
mRedoList->setMaxUndoActions(size);
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
int SynEdit::charsInWindow() const
|
|
|
|
{
|
|
|
|
return mCharsInWindow;
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onBookMarkOptionsChanged()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
invalidateGutter();
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onLinesChanged()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
SynSelectionMode vOldMode;
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfLinesChanging, false);
|
|
|
|
|
|
|
|
updateScrollbars();
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column) {
|
2022-03-26 11:49:41 +08:00
|
|
|
BufferCoord oldBlockStart = blockBegin();
|
|
|
|
BufferCoord oldBlockEnd = blockEnd();
|
2022-07-04 10:01:40 +08:00
|
|
|
oldBlockStart.ch = mCaretX;
|
|
|
|
int colEnd = charToColumn(oldBlockStart.line,oldBlockStart.ch);
|
|
|
|
int charEnd = columnToChar(oldBlockEnd.line,colEnd);
|
|
|
|
oldBlockEnd.ch = charEnd;
|
2022-03-26 11:49:41 +08:00
|
|
|
setBlockBegin(oldBlockStart);
|
|
|
|
setBlockEnd(oldBlockEnd);
|
|
|
|
} else {
|
|
|
|
vOldMode = mActiveSelectionMode;
|
|
|
|
setBlockBegin(caretXY());
|
|
|
|
mActiveSelectionMode = vOldMode;
|
|
|
|
}
|
2021-06-12 22:36:23 +08:00
|
|
|
if (mInvalidateRect.width()==0)
|
|
|
|
invalidate();
|
|
|
|
else
|
|
|
|
invalidateRect(mInvalidateRect);
|
2021-05-14 23:56:43 +08:00
|
|
|
mInvalidateRect = {0,0,0,0};
|
|
|
|
if (mGutter.showLineNumbers() && (mGutter.autoSize()))
|
2022-04-19 21:18:41 +08:00
|
|
|
mGutter.autoSizeDigitCount(mDocument->count());
|
2021-06-08 21:41:42 +08:00
|
|
|
//if (!mOptions.testFlag(SynEditorOption::eoScrollPastEof))
|
|
|
|
setTopLine(mTopLine);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onLinesChanging()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
mStateFlags.setFlag(SynStateFlag::sfLinesChanging);
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onLinesCleared()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
if (mUseCodeFolding)
|
|
|
|
foldOnListCleared();
|
|
|
|
clearUndo();
|
|
|
|
// invalidate the *whole* client area
|
|
|
|
mInvalidateRect={0,0,0,0};
|
|
|
|
invalidate();
|
|
|
|
// set caret and selected block to start of text
|
|
|
|
setCaretXY({1,1});
|
|
|
|
// scroll to start of text
|
|
|
|
setTopLine(1);
|
|
|
|
setLeftChar(1);
|
|
|
|
mStatusChanges.setFlag(SynStatusChange::scAll);
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onLinesDeleted(int index, int count)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2022-06-28 21:46:20 +08:00
|
|
|
if (mUseCodeFolding)
|
|
|
|
foldOnListDeleted(index + 1, count);
|
2022-07-03 11:22:10 +08:00
|
|
|
if (mHighlighter && mDocument->count() > 0)
|
|
|
|
scanFrom(index, index+1);
|
2021-05-14 23:56:43 +08:00
|
|
|
invalidateLines(index + 1, INT_MAX);
|
|
|
|
invalidateGutterLines(index + 1, INT_MAX);
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onLinesInserted(int index, int count)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
if (mUseCodeFolding)
|
|
|
|
foldOnListInserted(index + 1, count);
|
2022-04-19 21:18:41 +08:00
|
|
|
if (mHighlighter && mDocument->count() > 0) {
|
2021-09-23 12:06:26 +08:00
|
|
|
// int vLastScan = index;
|
|
|
|
// do {
|
2021-10-09 23:09:08 +08:00
|
|
|
scanFrom(index, index+count);
|
2021-09-23 12:06:26 +08:00
|
|
|
// vLastScan++;
|
|
|
|
// } while (vLastScan < index + count) ;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
invalidateLines(index + 1, INT_MAX);
|
|
|
|
invalidateGutterLines(index + 1, INT_MAX);
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:06:26 +08:00
|
|
|
void SynEdit::onLinesPutted(int index, int count)
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
int vEndLine = index + 1;
|
|
|
|
if (mHighlighter) {
|
2021-09-23 12:06:26 +08:00
|
|
|
vEndLine = std::max(vEndLine, scanFrom(index, index+count) + 1);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
invalidateLines(index + 1, vEndLine);
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onUndoAdded()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
updateModifiedStatus();
|
|
|
|
|
|
|
|
// we have to clear the redo information, since adding undo info removes
|
|
|
|
// the necessary context to undo earlier edit actions
|
|
|
|
if (! mUndoList->insideRedo() &&
|
2022-07-04 10:01:40 +08:00
|
|
|
mUndoList->peekItem() && (mUndoList->peekItem()->changeReason()!=SynChangeReason::GroupBreak))
|
|
|
|
mRedoList->clear();
|
2021-05-14 23:56:43 +08:00
|
|
|
if (mUndoList->blockCount() == 0 )
|
2021-09-02 12:14:02 +08:00
|
|
|
onChanged();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SynSelectionMode SynEdit::activeSelectionMode() const
|
|
|
|
{
|
|
|
|
return mActiveSelectionMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setActiveSelectionMode(const SynSelectionMode &Value)
|
|
|
|
{
|
|
|
|
if (mActiveSelectionMode != Value) {
|
|
|
|
if (selAvail())
|
|
|
|
invalidateSelection();
|
|
|
|
mActiveSelectionMode = Value;
|
|
|
|
if (selAvail())
|
|
|
|
invalidateSelection();
|
|
|
|
setStatusChanged(SynStatusChange::scSelection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferCoord SynEdit::blockEnd() const
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column)
|
2022-05-18 23:17:06 +08:00
|
|
|
return mBlockEnd;
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((mBlockEnd.line < mBlockBegin.line)
|
|
|
|
|| ((mBlockEnd.line == mBlockBegin.line) && (mBlockEnd.ch < mBlockBegin.ch)))
|
2021-06-03 20:26:36 +08:00
|
|
|
return mBlockBegin;
|
|
|
|
else
|
|
|
|
return mBlockEnd;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setBlockEnd(BufferCoord Value)
|
|
|
|
{
|
2022-03-25 10:43:07 +08:00
|
|
|
//setActiveSelectionMode(mSelectionMode);
|
2022-07-04 10:01:40 +08:00
|
|
|
Value.line = minMax(Value.line, 1, mDocument->count());
|
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Normal) {
|
|
|
|
if (Value.line >= 1 && Value.line <= mDocument->count())
|
|
|
|
Value.ch = std::min(Value.ch, getDisplayStringAtLine(Value.line).length() + 1);
|
2022-04-07 17:30:06 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
Value.ch = 1;
|
2022-06-28 21:46:20 +08:00
|
|
|
} else {
|
|
|
|
int maxLen = mDocument->lengthOfLongestLine();
|
|
|
|
if (highlighter())
|
|
|
|
maxLen = maxLen+stringColumns(highlighter()->foldString(),maxLen);
|
2022-07-04 10:01:40 +08:00
|
|
|
Value.ch = minMax(Value.ch, 1, maxLen+1);
|
2022-04-07 17:30:06 +08:00
|
|
|
}
|
2022-07-04 10:01:40 +08:00
|
|
|
if (Value.ch != mBlockEnd.ch || Value.line != mBlockEnd.line) {
|
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Column && Value.ch != mBlockEnd.ch) {
|
2022-04-07 17:30:06 +08:00
|
|
|
invalidateLines(
|
2022-07-04 10:01:40 +08:00
|
|
|
std::min(mBlockBegin.line, std::min(mBlockEnd.line, Value.line)),
|
|
|
|
std::max(mBlockBegin.line, std::max(mBlockEnd.line, Value.line)));
|
2022-04-07 17:30:06 +08:00
|
|
|
mBlockEnd = Value;
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
int nLine = mBlockEnd.line;
|
2022-04-07 17:30:06 +08:00
|
|
|
mBlockEnd = Value;
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode != SynSelectionMode::Column || mBlockBegin.ch != mBlockEnd.ch)
|
|
|
|
invalidateLines(nLine, mBlockEnd.line);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
2022-04-07 17:30:06 +08:00
|
|
|
setStatusChanged(SynStatusChange::scSelection);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 20:26:36 +08:00
|
|
|
void SynEdit::setSelLength(int Value)
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mBlockBegin.line>mDocument->count() || mBlockBegin.line<=0)
|
2021-06-03 20:26:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (Value >= 0) {
|
2022-07-04 10:01:40 +08:00
|
|
|
int y = mBlockBegin.line;
|
|
|
|
int ch = mBlockBegin.ch;
|
2021-06-03 20:26:36 +08:00
|
|
|
int x = ch + Value;
|
|
|
|
QString line;
|
2022-04-19 21:18:41 +08:00
|
|
|
while (y<=mDocument->count()) {
|
|
|
|
line = mDocument->getString(y-1);
|
2021-06-03 20:26:36 +08:00
|
|
|
if (x <= line.length()+2) {
|
|
|
|
if (x==line.length()+2)
|
|
|
|
x = line.length()+1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
x -= line.length()+2;
|
|
|
|
y ++;
|
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
if (y>mDocument->count()) {
|
|
|
|
y = mDocument->count();
|
|
|
|
x = mDocument->getString(y-1).length()+1;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
BufferCoord iNewEnd{x,y};
|
|
|
|
setCaretAndSelection(iNewEnd, mBlockBegin, iNewEnd);
|
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
int y = mBlockBegin.line;
|
|
|
|
int ch = mBlockBegin.ch;
|
2021-06-03 20:26:36 +08:00
|
|
|
int x = ch + Value;
|
|
|
|
QString line;
|
|
|
|
while (y>=1) {
|
|
|
|
if (x>=0) {
|
|
|
|
if (x==0)
|
|
|
|
x = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
y--;
|
2022-04-19 21:18:41 +08:00
|
|
|
line = mDocument->getString(y-1);
|
2021-06-03 20:26:36 +08:00
|
|
|
x += line.length()+2;
|
|
|
|
}
|
2022-04-19 21:18:41 +08:00
|
|
|
if (y>mDocument->count()) {
|
|
|
|
y = mDocument->count();
|
|
|
|
x = mDocument->getString(y-1).length()+1;
|
2021-06-03 20:26:36 +08:00
|
|
|
}
|
|
|
|
BufferCoord iNewStart{x,y};
|
|
|
|
setCaretAndSelection(iNewStart, iNewStart, mBlockBegin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 23:35:45 +08:00
|
|
|
void SynEdit::setSelText(const QString &text)
|
|
|
|
{
|
|
|
|
doSetSelText(text);
|
|
|
|
}
|
|
|
|
|
2021-05-14 23:56:43 +08:00
|
|
|
BufferCoord SynEdit::blockBegin() const
|
|
|
|
{
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mActiveSelectionMode==SynSelectionMode::Column)
|
2022-05-18 23:17:06 +08:00
|
|
|
return mBlockBegin;
|
2022-07-04 10:01:40 +08:00
|
|
|
if ((mBlockEnd.line < mBlockBegin.line)
|
|
|
|
|| ((mBlockEnd.line == mBlockBegin.line) && (mBlockEnd.ch < mBlockBegin.ch)))
|
2021-06-03 20:26:36 +08:00
|
|
|
return mBlockEnd;
|
|
|
|
else
|
|
|
|
return mBlockBegin;
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setBlockBegin(BufferCoord value)
|
|
|
|
{
|
|
|
|
int nInval1, nInval2;
|
|
|
|
bool SelChanged;
|
2022-03-25 10:43:07 +08:00
|
|
|
//setActiveSelectionMode(mSelectionMode);
|
2022-07-04 10:01:40 +08:00
|
|
|
value.line = minMax(value.line, 1, mDocument->count());
|
|
|
|
if (mActiveSelectionMode == SynSelectionMode::Normal) {
|
|
|
|
if (value.line >= 1 && value.line <= mDocument->count())
|
|
|
|
value.ch = std::min(value.ch, getDisplayStringAtLine(value.line).length() + 1);
|
2021-05-14 23:56:43 +08:00
|
|
|
else
|
2022-07-04 10:01:40 +08:00
|
|
|
value.ch = 1;
|
2022-06-28 21:46:20 +08:00
|
|
|
} else {
|
|
|
|
int maxLen = mDocument->lengthOfLongestLine();
|
|
|
|
if (highlighter())
|
|
|
|
maxLen = maxLen+stringColumns(highlighter()->foldString(),maxLen);
|
2022-07-04 10:01:40 +08:00
|
|
|
value.ch = minMax(value.ch, 1, maxLen+1);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
if (selAvail()) {
|
2022-07-04 10:01:40 +08:00
|
|
|
if (mBlockBegin.line < mBlockEnd.line) {
|
|
|
|
nInval1 = std::min(value.line, mBlockBegin.line);
|
|
|
|
nInval2 = std::max(value.line, mBlockEnd.line);
|
2021-05-14 23:56:43 +08:00
|
|
|
} else {
|
2022-07-04 10:01:40 +08:00
|
|
|
nInval1 = std::min(value.line, mBlockEnd.line);
|
|
|
|
nInval2 = std::max(value.line, mBlockBegin.line);
|
2021-05-14 23:56:43 +08:00
|
|
|
};
|
|
|
|
mBlockBegin = value;
|
|
|
|
mBlockEnd = value;
|
|
|
|
invalidateLines(nInval1, nInval2);
|
|
|
|
SelChanged = true;
|
|
|
|
} else {
|
|
|
|
SelChanged =
|
2022-07-04 10:01:40 +08:00
|
|
|
(mBlockBegin.ch != value.ch) || (mBlockBegin.line != value.line) ||
|
|
|
|
(mBlockEnd.ch != value.ch) || (mBlockEnd.line != value.line);
|
2021-05-14 23:56:43 +08:00
|
|
|
mBlockBegin = value;
|
|
|
|
mBlockEnd = value;
|
|
|
|
}
|
|
|
|
if (SelChanged)
|
|
|
|
setStatusChanged(SynStatusChange::scSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::leftChar() const
|
|
|
|
{
|
|
|
|
return mLeftChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setLeftChar(int Value)
|
|
|
|
{
|
2021-06-08 21:41:42 +08:00
|
|
|
//int MaxVal;
|
2021-05-14 23:56:43 +08:00
|
|
|
//QRect iTextArea;
|
2021-06-08 21:41:42 +08:00
|
|
|
Value = std::min(Value,maxScrollWidth());
|
2021-05-14 23:56:43 +08:00
|
|
|
if (Value != mLeftChar) {
|
2021-05-24 21:48:03 +08:00
|
|
|
horizontalScrollBar()->setValue(Value);
|
2021-05-14 23:56:43 +08:00
|
|
|
setStatusChanged(SynStatusChange::scLeftChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::linesInWindow() const
|
|
|
|
{
|
|
|
|
return mLinesInWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SynEdit::topLine() const
|
|
|
|
{
|
|
|
|
return mTopLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynEdit::setTopLine(int Value)
|
|
|
|
{
|
2021-06-08 21:41:42 +08:00
|
|
|
Value = std::min(Value,maxScrollHeight());
|
|
|
|
// if (mOptions.testFlag(SynEditorOption::eoScrollPastEof))
|
|
|
|
// Value = std::min(Value, displayLineCount());
|
|
|
|
// else
|
|
|
|
// Value = std::min(Value, displayLineCount() - mLinesInWindow + 1);
|
2021-05-14 23:56:43 +08:00
|
|
|
Value = std::max(Value, 1);
|
|
|
|
if (Value != mTopLine) {
|
2021-05-24 21:48:03 +08:00
|
|
|
verticalScrollBar()->setValue(Value);
|
2021-05-14 23:56:43 +08:00
|
|
|
setStatusChanged(SynStatusChange::scTopLine);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onRedoAdded()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
updateModifiedStatus();
|
|
|
|
|
|
|
|
if (mRedoList->blockCount() == 0 )
|
2021-09-02 12:14:02 +08:00
|
|
|
onChanged();
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onGutterChanged()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
|
|
|
if (mGutter.showLineNumbers() && mGutter.autoSize())
|
2022-04-19 21:18:41 +08:00
|
|
|
mGutter.autoSizeDigitCount(mDocument->count());
|
2021-05-14 23:56:43 +08:00
|
|
|
int nW;
|
|
|
|
if (mGutter.useFontStyle()) {
|
|
|
|
QFontMetrics fm=QFontMetrics(mGutter.font());
|
|
|
|
nW = mGutter.realGutterWidth(fm.averageCharWidth());
|
|
|
|
} else {
|
|
|
|
nW = mGutter.realGutterWidth(mCharWidth);
|
|
|
|
}
|
|
|
|
if (nW == mGutterWidth)
|
|
|
|
invalidateGutter();
|
|
|
|
else
|
|
|
|
setGutterWidth(nW);
|
|
|
|
}
|
|
|
|
|
2021-09-02 12:14:02 +08:00
|
|
|
void SynEdit::onScrollTimeout()
|
2021-05-14 23:56:43 +08:00
|
|
|
{
|
2021-11-26 19:04:04 +08:00
|
|
|
doMouseScroll(false);
|
|
|
|
}
|
2021-05-14 23:56:43 +08:00
|
|
|
|
2021-11-26 19:04:04 +08:00
|
|
|
void SynEdit::onDraggingScrollTimeout()
|
|
|
|
{
|
|
|
|
doMouseScroll(true);
|
2021-05-14 23:56:43 +08:00
|
|
|
}
|