Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Added indentation support in OMEdit.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25340 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Mar 31, 2015
1 parent a8e54ba commit ee8884e
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 25 deletions.
64 changes: 60 additions & 4 deletions OMEdit/OMEditGUI/Editors/ModelicaTextEditor.cpp
Expand Up @@ -38,8 +38,6 @@
#include "BreakpointMarker.h"
#include "ModelicaTextEditor.h"
#include "Helper.h"
#include <QtGui>
#include <QSettings>

/*!
\class CommentDefinition
Expand Down Expand Up @@ -251,8 +249,10 @@ bool ModelicaTextEditor::validateModelicaText()

void ModelicaTextEditor::keyPressEvent(QKeyEvent *pEvent)
{
if (pEvent->modifiers().testFlag(Qt::ControlModifier) && pEvent->key() == Qt::Key_K)
{
if (pEvent->key() == Qt::Key_Tab || pEvent->key() == Qt::Key_Backtab) {
indentOrUnindent(pEvent->key() == Qt::Key_Tab);
return;
} else if (pEvent->modifiers().testFlag(Qt::ControlModifier) && pEvent->key() == Qt::Key_K) {
toggleCommentSelection();
return;
}
Expand Down Expand Up @@ -472,6 +472,62 @@ void ModelicaTextEditor::toggleCommentSelection()
cursor.endEditBlock();
}

/*!
* \brief ModelicaTextEditor::indentOrUnindent
* Indents or unindents the code.
* \param doIndent
*/
void ModelicaTextEditor::indentOrUnindent(bool doIndent)
{
const ModelicaTabSettings *pModelicaTabSettings;
pModelicaTabSettings = mpModelWidget->getModelWidgetContainer()->getMainWindow()->getOptionsDialog()->getModelicaTabSettings();
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
// Indent or unindent the selected lines
if (cursor.hasSelection()) {
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
QTextDocument *doc = document();
QTextBlock startBlock = doc->findBlock(start);
QTextBlock endBlock = doc->findBlock(end-1).next();
// Only one line partially selected.
if (startBlock.next() == endBlock && (start > startBlock.position() || end < endBlock.position() - 1)) {
cursor.removeSelectedText();
} else {
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int indentPosition = pModelicaTabSettings->lineIndentPosition(text);
if (!doIndent && !indentPosition) {
indentPosition = pModelicaTabSettings->firstNonSpace(text);
}
int targetColumn = pModelicaTabSettings->indentedColumn(pModelicaTabSettings->columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.insertText(pModelicaTabSettings->indentationString(0, targetColumn));
cursor.setPosition(block.position());
cursor.setPosition(block.position() + indentPosition, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
cursor.endEditBlock();
return;
}
}
// Indent or unindent at cursor position
QTextBlock block = cursor.block();
QString text = block.text();
int indentPosition = cursor.positionInBlock();
int spaces = pModelicaTabSettings->spacesLeftFromPosition(text, indentPosition);
int startColumn = pModelicaTabSettings->columnAt(text, indentPosition - spaces);
int targetColumn = pModelicaTabSettings->indentedColumn(pModelicaTabSettings->columnAt(text, indentPosition), doIndent);
cursor.setPosition(block.position() + indentPosition);
cursor.setPosition(block.position() + indentPosition - spaces, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.insertText(pModelicaTabSettings->indentationString(startColumn, targetColumn));
cursor.endEditBlock();
setTextCursor(cursor);
}

//! Reimplementation of QPlainTextEdit::setPlainText method.
//! Makes sure we dont update if the passed text is same.
//! @param text the string to set.
Expand Down
3 changes: 1 addition & 2 deletions OMEdit/OMEditGUI/Editors/ModelicaTextEditor.h
Expand Up @@ -39,9 +39,7 @@
#ifndef MODELICATEXTEDITOR_H
#define MODELICATEXTEDITOR_H

#include <QToolButton>
#include <QSyntaxHighlighter>
#include <QSettings>

#include "MainWindow.h"
#include "Helper.h"
Expand Down Expand Up @@ -99,6 +97,7 @@ public slots:
void contentsHasChanged(int position, int charsRemoved, int charsAdded);
void setLineWrapping();
void toggleCommentSelection();
void indentOrUnindent(bool doIndent);
};

class ModelicaTextSettings;
Expand Down

0 comments on commit ee8884e

Please sign in to comment.