OSDN Git Service

9fe7bee819fb372f4286510e14172f3c184c6044
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / generichighlighter / highlightdefinition.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "highlightdefinition.h"
35 #include "highlighterexception.h"
36 #include "context.h"
37 #include "keywordlist.h"
38 #include "itemdata.h"
39 #include "reuse.h"
40
41 #include <QtCore/QString>
42
43 using namespace TextEditor;
44 using namespace Internal;
45
46 HighlightDefinition::HighlightDefinition() :
47     m_singleLineCommentAfterWhiteSpaces(false),
48     m_keywordCaseSensitivity(Qt::CaseSensitive),
49     m_indentationBasedFolding(false)
50 {
51     QString s(QLatin1String(".():!+,-<=>%&/;?[]^{|}~\\*, \t"));
52     foreach (const QChar &c, s)
53         m_delimiters.insert(c);
54 }
55
56 HighlightDefinition::~HighlightDefinition()
57 {}
58
59 template <class Element, class Container>
60 QSharedPointer<Element> HighlightDefinition::
61 GenericHelper::create(const QString &name, Container &container)
62 {
63     if (name.isEmpty())
64         throw HighlighterException();
65
66     if (container.contains(name))
67         throw HighlighterException();
68
69     return container.insert(name, QSharedPointer<Element>(new Element)).value();
70 }
71
72 template <class Element, class Container>
73 QSharedPointer<Element> HighlightDefinition::
74 GenericHelper::find(const QString &name, const Container &container) const
75 {
76     typename Container::const_iterator it = container.find(name);
77     if (it == container.end())
78         throw HighlighterException();
79
80     return it.value();
81 }
82
83 QSharedPointer<KeywordList> HighlightDefinition::createKeywordList(const QString &list)
84 { return m_helper.create<KeywordList>(list, m_lists); }
85
86 QSharedPointer<KeywordList> HighlightDefinition::keywordList(const QString &list)
87 { return m_helper.find<KeywordList>(list, m_lists); }
88
89 QSharedPointer<Context> HighlightDefinition::createContext(const QString &context, bool initial)
90 {
91     if (initial)
92         m_initialContext = context;
93
94     QSharedPointer<Context> newContext = m_helper.create<Context>(context, m_contexts);
95     newContext->setName(context);
96     return newContext;
97 }
98
99 QSharedPointer<Context> HighlightDefinition::initialContext() const
100 { return m_helper.find<Context>(m_initialContext, m_contexts); }
101
102 QSharedPointer<Context> HighlightDefinition::context(const QString &context) const
103 { return m_helper.find<Context>(context, m_contexts); }
104
105 const QHash<QString, QSharedPointer<Context> > &HighlightDefinition::contexts() const
106 { return m_contexts; }
107
108 QSharedPointer<ItemData> HighlightDefinition::createItemData(const QString &itemData)
109 { return m_helper.create<ItemData>(itemData, m_itemsData); }
110
111 QSharedPointer<ItemData> HighlightDefinition::itemData(const QString &itemData) const
112 { return m_helper.find<ItemData>(itemData, m_itemsData); }
113
114 void HighlightDefinition::setSingleLineComment(const QString &start)
115 { m_singleLineComment = start; }
116
117 const QString &HighlightDefinition::singleLineComment() const
118 { return m_singleLineComment; }
119
120 void HighlightDefinition::setCommentAfterWhitespaces(const QString &after)
121 {
122     if (after == QLatin1String("afterwhitespace"))
123         m_singleLineCommentAfterWhiteSpaces = true;
124 }
125
126 bool HighlightDefinition::isCommentAfterWhiteSpaces() const
127 { return m_singleLineCommentAfterWhiteSpaces; }
128
129 void HighlightDefinition::setMultiLineCommentStart(const QString &start)
130 { m_multiLineCommentStart = start; }
131
132 const QString &HighlightDefinition::multiLineCommentStart() const
133 { return m_multiLineCommentStart; }
134
135 void HighlightDefinition::setMultiLineCommentEnd(const QString &end)
136 { m_multiLineCommentEnd = end; }
137
138 const QString &HighlightDefinition::multiLineCommentEnd() const
139 { return m_multiLineCommentEnd; }
140
141 void HighlightDefinition::setMultiLineCommentRegion(const QString &region)
142 { m_multiLineCommentRegion = region; }
143
144 const QString &HighlightDefinition::multiLineCommentRegion() const
145 { return m_multiLineCommentRegion; }
146
147 void HighlightDefinition::removeDelimiters(const QString &characters)
148 {
149     for (int i = 0; i < characters.length(); ++i)
150         m_delimiters.remove(characters.at(i));
151 }
152
153 void HighlightDefinition::addDelimiters(const QString &characters)
154 {
155     for (int i = 0; i < characters.length(); ++i) {
156         if (!m_delimiters.contains(characters.at(i)))
157             m_delimiters.insert(characters.at(i));
158     }
159 }
160
161 bool HighlightDefinition::isDelimiter(const QChar &character) const
162 {
163     if (m_delimiters.contains(character))
164         return true;
165     return false;
166 }
167
168 void HighlightDefinition::setKeywordsSensitive(const QString &sensitivity)
169 {
170     if (!sensitivity.isEmpty())
171         m_keywordCaseSensitivity = toCaseSensitivity(toBool(sensitivity));
172 }
173
174 Qt::CaseSensitivity HighlightDefinition::keywordsSensitive() const
175 { return m_keywordCaseSensitivity; }
176
177 void HighlightDefinition::setIndentationBasedFolding(const QString &indentationBasedFolding)
178 { m_indentationBasedFolding = toBool(indentationBasedFolding); }
179
180 bool HighlightDefinition::isIndentationBasedFolding() const
181 { return m_indentationBasedFolding; }