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
|
/*
* Copyright (C) 2003 Roberto Raggi (roberto@kdevelop.org)
*
* 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include "js_colorizer.h"
#include "qeditor_part.h"
#include "paragdata.h"
#include <qfont.h>
#include <private/qrichtext_p.h>
#include <qdom.h>
#include <qfile.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kstandarddirs.h>
static const char *js_keywords[] = {
"null",
"true",
"false",
"abstract",
"break",
"case",
"catch",
"class",
"const",
"constructor",
"default",
"extends",
"final",
"finally",
"for",
"import",
"instanceof",
"is",
"new",
"var",
"continue",
"function",
"return",
"void",
"delete",
"if",
"this",
"do",
"while",
"else",
"in",
"package",
"private",
"public",
"static",
"switch",
"throw",
"try",
"typeof",
"with",
"abstract",
"boolean",
"byte",
"char",
"debugger",
"double",
"enum",
"export",
"final",
"float",
"goto",
"implements",
"int",
"interface",
"long",
"native",
"private",
"protected",
"public",
"short",
"static",
"super",
"synchronized",
"throws",
"transient",
"volatile",
0
};
using namespace std;
JSColorizer::JSColorizer( QEditor* editor )
: QSourceColorizer( editor )
{
loadDynamicKeywords();
// default context
HLItemCollection* context0 = new HLItemCollection( 0 );
context0->appendChild( new WhiteSpacesHLItem( Normal, 0 ) );
context0->appendChild( new StringHLItem( "'", String, 1 ) );
context0->appendChild( new StringHLItem( "\"", String, 2 ) );
context0->appendChild( new StringHLItem( "/*", Comment, 3 ) );
context0->appendChild( new StartsWithHLItem( "//", Comment, 0 ) );
context0->appendChild( new HexHLItem( Constant, 0 ) );
context0->appendChild( new NumberHLItem( Constant, 0 ) );
context0->appendChild( new KeywordsHLItem( m_dynamicKeywords, BuiltInClass, Normal, 0, false ) );
context0->appendChild( new KeywordsHLItem( js_keywords, Keyword, Normal, 0 ) );
HLItemCollection* context1 = new HLItemCollection( String );
context1->appendChild( new StringHLItem( "\\\\", String, 1 ) );
context1->appendChild( new StringHLItem( "\\'", String, 1 ) );
context1->appendChild( new StringHLItem( "'", String, 0 ) );
HLItemCollection* context2 = new HLItemCollection( String );
context2->appendChild( new StringHLItem( "\\\\", String, 2 ) );
context2->appendChild( new StringHLItem( "\\\"", String, 2 ) );
context2->appendChild( new StringHLItem( "\"", String, 0 ) );
HLItemCollection* context3 = new HLItemCollection( Comment );
context3->appendChild( new StringHLItem( "*/", Comment, 0 ) );
m_items.append( context0 );
m_items.append( context1 );
m_items.append( context2 );
m_items.append( context3 );
}
JSColorizer::~JSColorizer()
{
}
void JSColorizer::loadDynamicKeywords()
{
QString strFileNameTag( "name" );
QString strClassNameTag( "name" );
m_dynamicKeywords.clear();
QString hlFileDir = KGlobal::dirs()->findResourceDir( "data", "qeditorpart/highlight/highlighting.xml" );
hlFileDir += "qeditorpart/highlight/";
//kdDebug(9032) << "Highlighting Dir: " << hlFileDir << endl;
if( hlFileDir.isNull() )
return;
QDomDocument hlFile( "hlfile" ), curDoc ( "classlist" );
QFile hlRawFile( hlFileDir + "highlighting.xml" );
int keywordIndex = 0;
if( !hlRawFile.open( IO_ReadOnly ) )
return;
if( !hlFile.setContent( &hlRawFile ) ) {
hlRawFile.close();
return;
}
hlRawFile.close();
QDomElement e = hlFile.documentElement();
QDomNode n = e.firstChild();
while( !n.isNull() ) {
e = n.toElement();
if( !e.isNull() ) {
// kdDebug(9032) << "Loading classes-file: " << (hlFileDir + e.attribute( strFileNameTag )) << endl;
QFile clsRawFile( hlFileDir + e.attribute( strFileNameTag ) );
if( clsRawFile.open( IO_ReadOnly ) && curDoc.setContent( &clsRawFile ) ) {
QDomElement e = curDoc.documentElement();
QDomNode n = e.firstChild();
while( !n.isNull() ) {
e = n.toElement();
if( !e.isNull()) {
// kdDebug(9032) << "Adding dynamic keyword: '" << e.attribute( strClassNameTag ) << "'" << endl;
m_dynamicKeywords.insert( e.attribute( strClassNameTag ), keywordIndex++ );
}
n = n.nextSibling();
}
}
clsRawFile.close();
}
n = n.nextSibling();
}
}
int JSColorizer::computeLevel( QTextParagraph* parag, int startLevel )
{
int level = startLevel;
ParagData* data = (ParagData*) parag->extraData();
if( !data ){
return startLevel;
}
data->setBlockStart( false );
QValueList<Symbol> symbols = data->symbolList();
QValueList<Symbol>::Iterator it = symbols.begin();
while( it != symbols.end() ){
Symbol sym = *it++;
if( sym.ch() == '{' ){
++level;
} else if( sym.ch() == '}' ){
--level;
}
}
if( level > startLevel ){
data->setBlockStart( true );
}
return level;
}
|