KDevelop API Documentation

js_colorizer.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003 Roberto Raggi (roberto@kdevelop.org)
00003  *
00004  *  This program is free software; you can redistribute it and/or
00005  *  modify it under the terms of the GNU General Public
00006  *  License as published by the Free Software Foundation; either
00007  *  version 2 of the License, or (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; see the file COPYING.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  *
00019  */
00020 
00021 
00022 #include "js_colorizer.h"
00023 #include "qeditor_part.h"
00024 #include "paragdata.h"
00025 
00026 #include <qfont.h>
00027 #include <private/qrichtext_p.h>
00028 
00029 #include <qdom.h>
00030 #include <qfile.h>
00031 
00032 #include <kapplication.h>
00033 #include <kdebug.h>
00034 #include <kconfig.h>
00035 #include <kstandarddirs.h>
00036 
00037 
00038 static const char *js_keywords[] = {
00039 "null",
00040 "true",
00041 "false",
00042 "abstract",
00043 "break",
00044 "case",
00045 "catch",
00046 "class",
00047 "const",
00048 "constructor",
00049 "default",
00050 "extends",
00051 "final",
00052 "finally",
00053 "for",
00054 "import",
00055 "instanceof",
00056 "is",
00057 "new",
00058 "var",
00059 "continue",
00060 "function",
00061 "return",
00062 "void",
00063 "delete",
00064 "if",
00065 "this",
00066 "do",
00067 "while",
00068 "else",
00069 "in",
00070 "package",
00071 "private",
00072 "public",
00073 "static",
00074 "switch",
00075 "throw",
00076 "try",
00077 "typeof",
00078 "with",
00079 "abstract",
00080 "boolean",
00081 "byte",
00082 "char",
00083 "debugger",
00084 "double",
00085 "enum",
00086 "export",
00087 "final",
00088 "float",
00089 "goto",
00090 "implements",
00091 "int",
00092 "interface",
00093 "long",
00094 "native",
00095 "private",
00096 "protected",
00097 "public",
00098 "short",
00099 "static",
00100 "super",
00101 "synchronized",
00102 "throws",
00103 "transient",
00104 "volatile",
00105     0
00106 };
00107 
00108 
00109 using namespace std;
00110 
00111 JSColorizer::JSColorizer( QEditor* editor )
00112     : QSourceColorizer( editor )
00113 {
00114     loadDynamicKeywords();
00115 
00116     // default context
00117     HLItemCollection* context0 = new HLItemCollection( 0 );
00118     context0->appendChild( new WhiteSpacesHLItem( Normal, 0 ) );
00119     context0->appendChild( new StringHLItem( "'", String, 1 ) );
00120     context0->appendChild( new StringHLItem( "\"", String, 2 ) );
00121     context0->appendChild( new StringHLItem( "/*", Comment, 3 ) );
00122     context0->appendChild( new StartsWithHLItem( "//", Comment, 0 ) );
00123     context0->appendChild( new HexHLItem( Constant, 0 ) );
00124     context0->appendChild( new NumberHLItem( Constant, 0 ) );
00125     context0->appendChild( new KeywordsHLItem( m_dynamicKeywords, BuiltInClass, Normal, 0, false ) );
00126     context0->appendChild( new KeywordsHLItem( js_keywords, Keyword, Normal, 0 ) );
00127 
00128     HLItemCollection* context1 = new HLItemCollection( String );
00129     context1->appendChild( new StringHLItem( "\\\\", String, 1 ) );
00130     context1->appendChild( new StringHLItem( "\\'", String, 1 ) );
00131     context1->appendChild( new StringHLItem( "'", String, 0 ) );
00132 
00133     HLItemCollection* context2 = new HLItemCollection( String );
00134     context2->appendChild( new StringHLItem( "\\\\", String, 2 ) );
00135     context2->appendChild( new StringHLItem( "\\\"", String, 2 ) );
00136     context2->appendChild( new StringHLItem( "\"", String, 0 ) );
00137 
00138     HLItemCollection* context3 = new HLItemCollection( Comment );
00139     context3->appendChild( new StringHLItem( "*/", Comment, 0 ) );
00140 
00141 
00142     m_items.append( context0 );
00143     m_items.append( context1 );
00144     m_items.append( context2 );
00145     m_items.append( context3 );
00146 }
00147 
00148 JSColorizer::~JSColorizer()
00149 {
00150 }
00151 
00152 void JSColorizer::loadDynamicKeywords()
00153 {
00154     QString strFileNameTag( "name" );
00155     QString strClassNameTag( "name" );
00156 
00157     m_dynamicKeywords.clear();
00158 
00159     QString hlFileDir = KGlobal::dirs()->findResourceDir( "data", "qeditorpart/highlight/highlighting.xml" );
00160 
00161     hlFileDir += "qeditorpart/highlight/";
00162 
00163     //kdDebug(9032) << "Highlighting Dir: " << hlFileDir << endl;
00164 
00165     if( hlFileDir.isNull() )
00166     return;
00167 
00168     QDomDocument hlFile( "hlfile" ), curDoc ( "classlist" );
00169     QFile hlRawFile( hlFileDir + "highlighting.xml" );
00170     int keywordIndex = 0;
00171     if( !hlRawFile.open( IO_ReadOnly ) )
00172     return;
00173     if( !hlFile.setContent( &hlRawFile ) ) {
00174     hlRawFile.close();
00175     return;
00176     }
00177     hlRawFile.close();
00178 
00179     QDomElement e = hlFile.documentElement();
00180     QDomNode n = e.firstChild();
00181     while( !n.isNull() ) {
00182     e = n.toElement();
00183     if( !e.isNull() ) {
00184 
00185         // kdDebug(9032) << "Loading classes-file: " << (hlFileDir + e.attribute( strFileNameTag )) << endl;
00186 
00187         QFile clsRawFile( hlFileDir + e.attribute( strFileNameTag ) );
00188         if( clsRawFile.open( IO_ReadOnly ) && curDoc.setContent( &clsRawFile ) ) {
00189 
00190         QDomElement e = curDoc.documentElement();
00191         QDomNode n = e.firstChild();
00192         while( !n.isNull() ) {
00193             e = n.toElement();
00194             if( !e.isNull()) {
00195             // kdDebug(9032) << "Adding dynamic keyword: '" << e.attribute( strClassNameTag ) << "'" << endl;
00196             m_dynamicKeywords.insert( e.attribute( strClassNameTag ), keywordIndex++ );
00197             }
00198             n = n.nextSibling();
00199         }
00200 
00201         }
00202         clsRawFile.close();
00203     }
00204     n = n.nextSibling();
00205     }
00206 }
00207 
00208 int JSColorizer::computeLevel( QTextParagraph* parag, int startLevel )
00209 {
00210     int level = startLevel;
00211 
00212     ParagData* data = (ParagData*) parag->extraData();
00213     if( !data ){
00214         return startLevel;
00215     }
00216 
00217     data->setBlockStart( false );
00218 
00219     QValueList<Symbol> symbols = data->symbolList();
00220     QValueList<Symbol>::Iterator it = symbols.begin();
00221     while( it != symbols.end() ){
00222         Symbol sym = *it++;
00223         if( sym.ch() == '{' ){
00224             ++level;
00225         } else if( sym.ch() == '}' ){
00226             --level;
00227         }
00228     }
00229 
00230     if( level > startLevel ){
00231         data->setBlockStart( true );
00232     }
00233 
00234     return level;
00235 }
00236 
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:41 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003