Commit 44346828 authored by Victor Yacovlev's avatar Victor Yacovlev

Editor capable to work with soft-indents (Python etc.)

parent a141ce01
......@@ -29,11 +29,16 @@ void InsertCommand::redo()
return;
cursorRow = cursor->row();
cursorCol = cursor->column();
bool hardIndents = analizer && !analizer->plugin()->indentsSignificant();
doc->insertText(text, analizer, line, pos, blankLines, blankChars);
QStringList lines = text.split("\n", QString::KeepEmptyParts);
if (lines.size()>1) {
cursor->setRow(cursor->row()+lines.size()-1);
cursor->setColumn(lines.last().length() + doc->indentAt(cursor->row())*2);
int newRow = cursor->row()+lines.size()-1;
int newCol = lines.last().length();
if (hardIndents)
newCol += doc->indentAt(cursor->row())*2;
cursor->setRow(newRow);
cursor->setColumn(newCol);
}
else {
cursor->setColumn(cursor->column()+text.length());
......
......@@ -494,10 +494,12 @@ void EditorInstance::updateFromAnalizer()
}
doc_->at(i).multipleStatementsInLine = analizerInstance_->multipleStatementsInLine(i);
doc_->marginAt(i).errors.clear();
int newIndent = doc_->indentAt(i);
int diffIndent = newIndent - oldIndent;
if (cursor_->row()==i) {
cursor_->setColumn(qMax(cursor_->column()+2*diffIndent, 0u));
if (!analizerPlugin_->indentsSignificant()) {
int newIndent = doc_->indentAt(i);
int diffIndent = newIndent - oldIndent;
if (cursor_->row()==i) {
cursor_->setColumn(qMax(cursor_->column()+2*diffIndent, 0u));
}
}
}
for (int i=0; i<errors.size(); i++) {
......
......@@ -921,6 +921,11 @@ void EditorPlane::paintEvent(QPaintEvent *e)
// Paint a text
paintText(&p, e->rect().translated(-offset()));
// Paint structure marks
if (editor_->analizer() && editor_->analizer()->plugin()->indentsSignificant()) {
paintProgramStructureLines(&p, e->rect().translated(-offset()));
}
// Paint a cursor
paintCursor(&p, e->rect().translated(-offset()));
......@@ -1429,6 +1434,8 @@ void EditorPlane::keyPressEvent(QKeyEvent *e)
if (moveToEnd)
editor_->cursor()->moveTo(editor_->cursor()->row(), curText.length());
QString indent;
int proposedIndent = 4 * editor_->document()->indentAt(editor_->cursor()->row()+1);
indentSpaces = qMax(indentSpaces, proposedIndent);
indent.fill(' ', indentSpaces);
editor_->cursor()->evaluateCommand("\n" + indent);
}
......@@ -2079,9 +2086,10 @@ void EditorPlane::paintSelection(QPainter *p, const QRect &rect)
int lh = lineHeight();
int cw = charWidth();
bool prevLineSelected = false;
bool hardIndent = editor_->analizer() && !editor_->analizerPlugin_->indentsSignificant();
for (int i=startLine; i<endLine+1; i++) {
if (i<editor_->document()->linesCount()) {
int indentSpace = 2 * cw * editor_->document()->indentAt(i);
int indentSpace = hardIndent ? 2 * cw * editor_->document()->indentAt(i) : 0;
if (prevLineSelected) {
p->drawRect(0, i*lh, indentSpace, lh);
}
......@@ -2424,8 +2432,11 @@ void EditorPlane::paintText(QPainter *p, const QRect &rect)
// Draw text lines themselves
for (uint i=startLine; i<=endLine; i++)
{
bool hardIndents = editor_->analizer() &&
!editor_->analizerPlugin_->indentsSignificant();
// Indent count (in logical levels)
uint indent = editor_->document()->indentAt(i);
uint indent = hardIndents ? editor_->document()->indentAt(i) : 0u;
// Bottom text bound (so 'i + 1' instead of 'i')
const uint y = ( i + 1 )* lineHeight();
......@@ -2577,6 +2588,73 @@ void EditorPlane::paintText(QPainter *p, const QRect &rect)
} // end for (uint i=startLine; i<=endLine; i++)
}
static uint countLeadingSpacesInString(const QString & s)
{
uint result = 0u;
for (int i=0; i<s.length(); i++) {
if (s[i].isSpace()) {
result ++;
}
else {
break;
}
}
return result;
}
void EditorPlane::paintProgramStructureLines(QPainter *p, const QRect &rect)
{
p->save();
const QRgb bg = palette().brush(QPalette::Base).color().rgb();
const QRgb fg = palette().brush(QPalette::Text).color().rgb();
const QRgb lc = qRgb(
( qRed(bg) + qRed(fg) ) / 2,
( qGreen(bg) + qGreen(fg) ) / 2,
( qBlue(bg) + qBlue(fg) ) / 2
);
const int CW = charWidth();
const int LH = lineHeight();
const QColor linesColor = QColor::fromRgb(lc);
p->setPen(linesColor);
int linesCount = editor_->document()->linesCount();
int prevIndent = 0;
int curIndent = 0;
int nextIndent = 0;
int x1, x2, y1, y2;
for (int l=0; l<linesCount; l++) {
curIndent = editor_->document()->indentAt(l);
int realIndent = countLeadingSpacesInString(editor_->document()->textAt(l)) / 4;
curIndent = realIndent;
prevIndent = 0;
if (l > 0) {
prevIndent = editor_->document()->indentAt(l-1);
realIndent = countLeadingSpacesInString(editor_->document()->textAt(l-1)) / 4;
prevIndent = realIndent;
}
if (l < linesCount-1) {
nextIndent = editor_->document()->indentAt(l+1);
realIndent = countLeadingSpacesInString(editor_->document()->textAt(l+1)) / 4;
nextIndent = realIndent;
}
for (int i=0; i<curIndent; i++) {
x1 = x2 = i * 4 * CW + CW / 2;
y1 = l * LH;
y2 = y1 + LH;
if (prevIndent < curIndent && i == curIndent-1)
y1 += LH / 2;
p->drawLine(x1, y1, x1, y2);
if (i+1 > nextIndent && nextIndent < curIndent) {
x2 = x1 + CW * 3;
p->drawLine(x1, y2, x2, y2);
}
}
}
p->restore();
}
QPolygon EditorPlane::errorUnderline(int x, int y, int len)
{
......
......@@ -78,6 +78,7 @@ protected:
void paintMarginText(QPainter *p, const QRect & rect);
void paintCursor(QPainter *p, const QRect &rect);
void paintText(QPainter *p, const QRect &rect);
void paintProgramStructureLines(QPainter *p, const QRect &rect);
void paintHiddenTextDelimeterLine(QPainter *p);
void paintSelection(QPainter *p, const QRect &rect);
void paintRectSelection(QPainter *p, const QRect &rect);
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment