| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2014-01-07 13:27:11 +01:00
										 |  |  | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Contact: http://www.qt-project.org/legal
 | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** This file is part of Qt Creator. | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Commercial License Usage | 
					
						
							|  |  |  | ** Licensees holding valid commercial Qt licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the commercial license agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and Digia.  For licensing terms and | 
					
						
							| 
									
										
										
										
											2014-10-01 13:21:18 +02:00
										 |  |  | ** conditions see http://www.qt.io/licensing.  For further information
 | 
					
						
							|  |  |  | ** use the contact form at http://www.qt.io/contact-us.
 | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | ** | 
					
						
							|  |  |  | ** GNU Lesser General Public License Usage | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
					
						
							| 
									
										
										
										
											2014-10-01 13:21:18 +02:00
										 |  |  | ** General Public License version 2.1 or version 3 as published by the Free | 
					
						
							|  |  |  | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | 
					
						
							|  |  |  | ** LICENSE.LGPLv3 included in the packaging of this file.  Please review the | 
					
						
							|  |  |  | ** following information to ensure the GNU Lesser General Public License | 
					
						
							|  |  |  | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
 | 
					
						
							|  |  |  | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ** | 
					
						
							|  |  |  | ** In addition, as a special exception, Digia gives you certain additional | 
					
						
							|  |  |  | ** rights.  These rights are described in the Digia Qt LGPL Exception | 
					
						
							| 
									
										
										
										
											2010-12-17 17:14:20 +01:00
										 |  |  | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2012-10-02 09:12:39 +02:00
										 |  |  | ****************************************************************************/ | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "cppqtstyleindenter.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  | #include "cppcodeformatter.h"
 | 
					
						
							|  |  |  | #include "cpptoolssettings.h"
 | 
					
						
							|  |  |  | #include "cppcodestylepreferences.h"
 | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-15 10:42:41 +01:00
										 |  |  | #include <QChar>
 | 
					
						
							|  |  |  | #include <QTextDocument>
 | 
					
						
							|  |  |  | #include <QTextBlock>
 | 
					
						
							|  |  |  | #include <QTextCursor>
 | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  | using namespace CppTools; | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | CppQtStyleIndenter::CppQtStyleIndenter() | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  |     : m_cppCodeStylePreferences(0) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-07-12 08:52:32 +02:00
										 |  |  |     // Just for safety. setCodeStylePreferences should be called when the editor the
 | 
					
						
							|  |  |  |     // indenter belongs to gets initialized.
 | 
					
						
							| 
									
										
										
										
											2011-08-16 10:45:23 +02:00
										 |  |  |     m_cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle(); | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | CppQtStyleIndenter::~CppQtStyleIndenter() | 
					
						
							|  |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-30 14:14:33 +01:00
										 |  |  | bool CppQtStyleIndenter::isElectricCharacter(const QChar &ch) const | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-30 22:54:29 +03:00
										 |  |  |     switch (ch.toLatin1()) { | 
					
						
							|  |  |  |     case '{': | 
					
						
							|  |  |  |     case '}': | 
					
						
							|  |  |  |     case ':': | 
					
						
							|  |  |  |     case '#': | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |     case '<': | 
					
						
							|  |  |  |     case '>': | 
					
						
							| 
									
										
										
										
											2014-10-14 17:56:29 +04:00
										 |  |  |     case ';': | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  | static bool isElectricInLine(const QChar ch, const QString &text) | 
					
						
							| 
									
										
										
										
											2010-12-08 13:50:24 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |     switch (ch.toLatin1()) { | 
					
						
							| 
									
										
										
										
											2014-10-14 17:56:29 +04:00
										 |  |  |     case ';': | 
					
						
							|  |  |  |         return text.contains(QLatin1String("break")); | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |     case ':': | 
					
						
							|  |  |  |         // switch cases and access declarations should be reindented
 | 
					
						
							|  |  |  |         if (text.contains(QLatin1String("case")) | 
					
						
							|  |  |  |                 || text.contains(QLatin1String("default")) | 
					
						
							|  |  |  |                 || text.contains(QLatin1String("public")) | 
					
						
							|  |  |  |                 || text.contains(QLatin1String("private")) | 
					
						
							|  |  |  |                 || text.contains(QLatin1String("protected")) | 
					
						
							|  |  |  |                 || text.contains(QLatin1String("signals")) | 
					
						
							|  |  |  |                 || text.contains(QLatin1String("Q_SIGNALS"))) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-12-08 13:50:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |         // fall-through
 | 
					
						
							|  |  |  |         // lines that start with : might have a constructor initializer list
 | 
					
						
							|  |  |  |     case '<': | 
					
						
							|  |  |  |     case '>': { | 
					
						
							| 
									
										
										
										
											2014-05-22 23:34:05 +03:00
										 |  |  |         // Electric if at line beginning (after space indentation)
 | 
					
						
							|  |  |  |         for (int i = 0, len = text.count(); i < len; ++i) { | 
					
						
							|  |  |  |             if (!text.at(i).isSpace()) | 
					
						
							|  |  |  |                 return text.at(i) == ch; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-12-08 13:50:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2010-12-08 13:50:24 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-30 14:14:33 +01:00
										 |  |  | void CppQtStyleIndenter::indentBlock(QTextDocument *doc, | 
					
						
							|  |  |  |                                      const QTextBlock &block, | 
					
						
							|  |  |  |                                      const QChar &typedChar, | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |                                      const TextEditor::TabSettings &tabSettings) | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     Q_UNUSED(doc) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |     CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     codeFormatter.updateStateUntil(block); | 
					
						
							|  |  |  |     int indent; | 
					
						
							|  |  |  |     int padding; | 
					
						
							|  |  |  |     codeFormatter.indentFor(block, &indent, &padding); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (isElectricCharacter(typedChar)) { | 
					
						
							| 
									
										
										
										
											2010-12-08 13:50:24 +01:00
										 |  |  |         // : should not be electric for labels
 | 
					
						
							| 
									
										
										
										
											2014-04-30 22:55:59 +03:00
										 |  |  |         if (!isElectricInLine(typedChar, block.text())) | 
					
						
							| 
									
										
										
										
											2010-12-07 14:30:17 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // only reindent the current line when typing electric characters if the
 | 
					
						
							|  |  |  |         // indent is the same it would be if the line were empty
 | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |         int newlineIndent; | 
					
						
							|  |  |  |         int newlinePadding; | 
					
						
							|  |  |  |         codeFormatter.indentForNewLineAfter(block.previous(), &newlineIndent, &newlinePadding); | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |         if (tabSettings.indentationColumn(block.text()) != newlineIndent + newlinePadding) | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |     tabSettings.indentLine(block, indent + padding, padding); | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-30 14:14:33 +01:00
										 |  |  | void CppQtStyleIndenter::indent(QTextDocument *doc, | 
					
						
							|  |  |  |                                 const QTextCursor &cursor, | 
					
						
							|  |  |  |                                 const QChar &typedChar, | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |                                 const TextEditor::TabSettings &tabSettings) | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (cursor.hasSelection()) { | 
					
						
							|  |  |  |         QTextBlock block = doc->findBlock(cursor.selectionStart()); | 
					
						
							|  |  |  |         const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |         CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings()); | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |         codeFormatter.updateStateUntil(block); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |         QTextCursor tc = cursor; | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |         tc.beginEditBlock(); | 
					
						
							|  |  |  |         do { | 
					
						
							|  |  |  |             int indent; | 
					
						
							|  |  |  |             int padding; | 
					
						
							|  |  |  |             codeFormatter.indentFor(block, &indent, &padding); | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |             tabSettings.indentLine(block, indent + padding, padding); | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |             codeFormatter.updateLineStateChange(block); | 
					
						
							|  |  |  |             block = block.next(); | 
					
						
							|  |  |  |         } while (block.isValid() && block != end); | 
					
						
							|  |  |  |         tc.endEditBlock(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2011-08-04 11:19:25 +02:00
										 |  |  |         indentBlock(doc, cursor.block(), typedChar, tabSettings); | 
					
						
							| 
									
										
										
										
											2010-11-05 14:27:16 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-16 10:45:23 +02:00
										 |  |  | void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences) | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     CppTools::CppCodeStylePreferences *cppCodeStylePreferences | 
					
						
							|  |  |  |             = qobject_cast<CppTools::CppCodeStylePreferences *>(preferences); | 
					
						
							|  |  |  |     if (cppCodeStylePreferences) | 
					
						
							|  |  |  |         m_cppCodeStylePreferences = cppCodeStylePreferences; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-16 10:45:23 +02:00
										 |  |  | void CppQtStyleIndenter::invalidateCache(QTextDocument *doc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     CppTools::QtStyleCodeFormatter formatter; | 
					
						
							|  |  |  |     formatter.invalidateCache(doc); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  | CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_cppCodeStylePreferences) | 
					
						
							| 
									
										
										
										
											2011-08-16 10:45:23 +02:00
										 |  |  |         return m_cppCodeStylePreferences->currentCodeStyleSettings(); | 
					
						
							| 
									
										
										
										
											2011-02-03 15:48:14 +01:00
										 |  |  |     return CppCodeStyleSettings(); | 
					
						
							|  |  |  | } |