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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qlanguagedefinition.h"
/*!
\file qlanguagedefinition.cpp
\brief Implementation of QLanguageDefinition
\see QLanguageDefinition
*/
/*!
\ingroup language
@{
\class QLanguageDefinition
\brief Interface for language definition.
This class is meant to be subclassed, see \see QGenericDefinition for more
informations, and added to a QLanguageFactory.
A language definition is a wrapper that creates interfaces for a given file
extension from internally handled data (XML files in the case of
QGenericDefinition)
\see QLanguageFactory
*/
#include "qdocument.h"
#include "qdocumentcursor.h"
#include "qdocumentline.h"
#include "qlanguagefactory.h"
/*!
\brief Empty constructor
*/
QLanguageDefinition::QLanguageDefinition()
{
}
/*!
\brief Empty destructor
*/
QLanguageDefinition::~QLanguageDefinition()
{
}
/*!
\fn QLanguageDefinition::language()
\return The language supported by this definition
*/
/*!
\fn QLanguageDefinition::extensions()
\return the file extensions corrseponding to the supported language
\see language()
\see QFileInfo::completeSuffix()
*/
/*!
\brief Entry point for syntax highlighting
*/
int QLanguageDefinition::tokenize(QDocument *d, int line, int count)
{
Q_UNUSED(d)
Q_UNUSED(line)
return count;
}
/*!
\brief Return the string starting a single line comment, if any offered by the language
*/
QString QLanguageDefinition::singleLineComment() const
{
return QString();
}
/*!
\brief Let language specify which line mark should be toggled by left clicking a line mark panel
*/
QString QLanguageDefinition::defaultLineMark() const
{
return QString();
}
int QLanguageDefinition::parenthesisWeight(int id) const{
Q_UNUSED(id);
return 0;
}
bool QLanguageDefinition::possibleEndingOfOpeningParenthesis(const QString& text) const{
Q_UNUSED(text);
return false;
}
/*!
\brief Brace matching entry point
*/
void QLanguageDefinition::clearMatches(QDocument *d)
{
Q_UNUSED(d)
}
/*!
\brief Brace matching entry point
*/
void QLanguageDefinition::match(QDocumentCursor& c)
{
Q_UNUSED(c)
}
/*!
\brief Return the indent to use when inserting a line at a given cursor position
*/
QString QLanguageDefinition::indent(const QDocumentCursor& c, int* indentCount)
{
Q_UNUSED(c)
Q_UNUSED(indentCount)
return QString();
}
/*!
\brief Determines whether the given key event at the given position should cause unindent to happen
*/
bool QLanguageDefinition::unindent (const QDocumentCursor& c, const QString& ktxt)
{
Q_UNUSED(c)
Q_UNUSED(ktxt)
return false;
}
/*!
\brief Expand a collapsed block at a given line
*/
void QLanguageDefinition::expand(QDocument *d, int line)
{
Q_UNUSED(d)
Q_UNUSED(line)
}
/*!
\brief Collapse a text block at a given line
*/
void QLanguageDefinition::collapse(QDocument *d, int line)
{
Q_UNUSED(d)
Q_UNUSED(line)
}
bool QLanguageDefinition::correctFolding(QDocument *d){
Q_UNUSED(d);
return false;
}
QFoldedLineIterator QLanguageDefinition::foldedLineIterator(QDocument *d, int line) const{
QFoldedLineIterator fli;
fli.doc=d;
fli.def=this;
fli.hidden=false;
fli.hiddenDepth=0;
for (fli.lineNr=-1;fli.lineNr<line;++fli);
return fli;
}
QFoldedLineIterator& QFoldedLineIterator::operator++(){
open = 0;
close = 0;
lineNr++;
line=doc->line(lineNr);
if (!line.isValid())
return *this;
int oldHiddenDepth=hiddenDepth;
int maxClosedParenthesisWeight=0, maxClosedHiddenParenthesisWeight=0;
collapsedBlockStart=line.hasFlag(QDocumentLine::CollapsedBlockStart);
foreach (const QParenthesis& par, line.parentheses()){
if (!(par.role & QParenthesis::Fold))
continue;
if (par.role & QParenthesis::Close && !openParentheses.empty()) {
if (openParentheses.last().id==par.id) {
//Close last bracket
if (openParentheses.last().line != lineNr) {
close++;
if (openParentheses.last().hiding) {
hiddenDepth--;
maxClosedHiddenParenthesisWeight=qMax(maxClosedHiddenParenthesisWeight, openParentheses.last().weight);
} else
maxClosedParenthesisWeight=qMax(maxClosedHiddenParenthesisWeight, openParentheses.last().weight);
} else open--;
openParentheses.removeLast();
} else {
//Close a matching bracket if there are only "lighter" brackets in-between
int weight=def->parenthesisWeight(par.id);
int removedHidingBrackets=0;
int removedOwnBrackets=0;
int removedMaxClosedPW = 0;
int removedMaxClosedHiddenPW = 0;
for (int i=openParentheses.size()-1;i>=0;i--){
if (openParentheses[i].weight>weight) break;
if (openParentheses[i].line == lineNr) removedOwnBrackets++;
else if (openParentheses[i].hiding) {
removedHidingBrackets++;
removedMaxClosedHiddenPW=qMax(removedMaxClosedHiddenPW, openParentheses[i].weight);
} else
removedMaxClosedPW=qMax(removedMaxClosedPW, openParentheses[i].weight);
if ( openParentheses[i].weight==weight ) {
if ( openParentheses[i].id!=par.id ) break;
open-=removedOwnBrackets;
close+=(openParentheses.size()-i) - removedOwnBrackets;
hiddenDepth-=removedHidingBrackets;
openParentheses.erase(openParentheses.begin()+i,openParentheses.end());
maxClosedParenthesisWeight=qMax(maxClosedParenthesisWeight, removedMaxClosedPW);
maxClosedHiddenParenthesisWeight=qMax(maxClosedHiddenParenthesisWeight, removedMaxClosedHiddenPW);
break;
}
}
}
}
if (par.role & QParenthesis::Open){
//Open new bracket
openParentheses << FoldedParenthesis(lineNr, par.id, def->parenthesisWeight(par.id), collapsedBlockStart );
open++;
}
}
collapsedBlockStart = collapsedBlockStart && open;
collapsedBlockEnd = hiddenDepth!=oldHiddenDepth;
hiddenCollapsedBlockEnd = oldHiddenDepth>0 && !open && maxClosedParenthesisWeight <= maxClosedHiddenParenthesisWeight;
hidden = (hiddenDepth>0) || //line contained in a hidden block
(hiddenDepth==0 && hiddenCollapsedBlockEnd); //a closing bracket is hidden iff there is no new block to open
if (collapsedBlockStart)
hiddenDepth+=open;
return *this;
}
bool QFoldedLineIterator::lineFlagsInvalid() const{
return line.hasFlag(QDocumentLine::Hidden) != hidden ||
line.hasFlag(QDocumentLine::CollapsedBlockStart) != collapsedBlockStart ||
line.hasFlag(QDocumentLine::CollapsedBlockEnd) != collapsedBlockEnd;
}
void QFoldedLineIterator::incrementUntilBlockEnd(){
int firstParenthesisPos = openParentheses.size() - open;
const FoldedParenthesis firstParenthesis = openParentheses[firstParenthesisPos];
while ( lineNr < doc->lines() &&
firstParenthesisPos < openParentheses.size() &&
openParentheses[firstParenthesisPos] == firstParenthesis ){
++(*this);
}
}
/*! @} */
|