1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
SuperCollider Qt IDE
Copyright (c) 2012 Jakob Leben & Tim Blechmann
http://www.audiosynth.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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "editor.hpp"
#include "tokens.hpp"
#include "../core/settings/theme.hpp"
#include <QTextBlock>
#include <utility>
namespace ScIDE {
typedef std::pair<TokenIterator, TokenIterator> BracketPair;
class ScCodeEditor : public GenericCodeEditor {
Q_OBJECT
public:
/** \brief Used to indicate whether an edit action should start a new edit
* block, which affects undo/redo operation grouping.
*/
enum EditBlockMode {
NewEditBlock, ///< Start a new edit block.
JoinEditBlock ///< Join this edit to the previous one.
};
public:
static TokenIterator previousOpeningBracket(TokenIterator it);
static TokenIterator nextClosingBracket(TokenIterator it);
static void matchBracket(const TokenIterator& bracket, BracketPair& match);
static void nextBracketPair(const TokenIterator& startIt, BracketPair& bracketPair);
public:
ScCodeEditor(Document*, QWidget* parent = 0);
QTextCursor currentRegion();
void blinkCode(const QTextCursor& c);
bool spaceIndent() { return mSpaceIndent; }
public slots:
void applySettings(Settings::Manager*);
void setSpaceIndent(bool on) { mSpaceIndent = on; }
/// Indents the currently-selected text.
void indent(EditBlockMode = NewEditBlock);
void triggerAutoCompletion();
void triggerMethodCallAid();
void toggleComment();
void gotoPreviousBlock();
void gotoNextBlock();
void selectBlockAroundCursor();
void selectCurrentRegion();
void gotoNextRegion();
void gotoPreviousRegion();
bool openDocumentation();
void openDefinition();
void openCommandLine();
void findReferences();
void evaluateLine();
void evaluateRegion();
void evaluateDocument();
protected:
virtual bool event(QEvent*);
virtual void keyPressEvent(QKeyEvent*);
virtual void mouseReleaseEvent(QMouseEvent*);
virtual void mouseDoubleClickEvent(QMouseEvent*);
virtual void mouseMoveEvent(QMouseEvent*);
virtual void dragEnterEvent(QDragEnterEvent*);
virtual bool canInsertFromMimeData(const QMimeData* data) const;
virtual void insertFromMimeData(const QMimeData* data);
private slots:
void matchBrackets();
private:
QTextCursor cursorAt(const TokenIterator, int offset = 0);
QTextCursor selectionForPosition(int position);
QTextCursor regionAroundCursor(const QTextCursor&);
QTextCursor blockAroundCursor(const QTextCursor&);
void moveToNextToken(QTextCursor&, QTextCursor::MoveMode);
void moveToPreviousToken(QTextCursor&, QTextCursor::MoveMode);
void updateExtraSelections();
void indentCurrentRegion();
void toggleCommentSingleLine(QTextCursor);
void toggleCommentSingleLine();
void toggleCommentSelection();
void addSingleLineComment(QTextCursor, int indentationLevel);
void removeSingleLineComment(QTextCursor);
/** \brief Returns the index of the first non-whitespace character in
* \c block, or <tt>block.text().size()</tt> if the block is all whitespace.
*/
int indentedStartOfLine(const QTextBlock& block);
/// Indents the text in \c selection according to editor preferences.
void indent(const QTextCursor& selection, EditBlockMode = NewEditBlock);
/** \brief Indents a line to a chosen level of indentation.
*
* \param block The line to indent.
* \param level The level of indentation.
* \return A new \c QTextBlock indented to the requested level.
*/
QTextBlock indent(const QTextBlock& block, int level);
/** \brief Creates a string of indentation characters acccording to
* editor preferences.
*
* \param level The indentation level.
*/
QString makeIndentationString(int level);
/** \brief Get the indentation level for the cursor's selection start.
*
* \param cursor A cursor in the document.
* \return The number of levels of indentation there should be for
* the line where the \c cursor's selection starts, or -1 on failure.
*/
int indentationLevel(const QTextCursor& cursor);
/** \brief Inserts enough whitespace at \c cursor to reach the next
* tab stop according to editor preferences.
*/
void insertSpaceToNextTabStop(QTextCursor& cursor);
bool insertMatchingTokens(const QString& token);
bool removeMatchingTokens();
// Data members
/// If \c true, use spaces to indent.
bool mSpaceIndent;
bool mStepForwardEvaluation;
int mBlinkDuration;
bool mInsertMatchingTokens;
QTextCharFormat mBracketHighlight;
QTextCharFormat mBracketMismatchFormat;
bool mHighlightBracketContents;
QList<QTextEdit::ExtraSelection> mBracketSelections;
bool mMouseBracketMatch;
class AutoCompleter* mAutoCompleter;
};
} // namespace ScIDE
|