Mercurial > hg > octave-lyh
changeset 13420:485dfb3846cb
Reformatted and refactored SyntaxHighlighter class.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Wed, 13 Apr 2011 09:02:04 +0200 |
parents | cb41924c25e9 |
children | 24ab14786f81 |
files | gui//src/SyntaxHighlighter.cpp gui//src/SyntaxHighlighter.h |
diffstat | 2 files changed, 399 insertions(+), 541 deletions(-) [+] |
line wrap: on
line diff
--- a/gui//src/SyntaxHighlighter.cpp +++ b/gui//src/SyntaxHighlighter.cpp @@ -28,183 +28,162 @@ QStringList SyntaxHighlighter::octave_comands; /*** Xml Handler ***/ -class SyntaxXmlHandler:public QXmlDefaultHandler -{ +class SyntaxXmlHandler:public QXmlDefaultHandler { private: - SyntaxHighlighter *syntax; - QString type_name, text; - struct Tag - { - QString tag, type; - QStringList items; - QList<Tag> childs; - }; - QList<Tag> stack; + SyntaxHighlighter *syntax; + QString type_name, text; + struct Tag + { + QString tag, type; + QStringList items; + QList<Tag> childs; + }; + QList<Tag> stack; + QStringList *octave_comands; - QStringList *octave_comands; public: - // Constructor - SyntaxXmlHandler(SyntaxHighlighter *s, QStringList *octave_comands): QXmlDefaultHandler(), syntax(s) - { - this->octave_comands=octave_comands; - } + // Constructor + SyntaxXmlHandler(SyntaxHighlighter *s, QStringList *octave_comands) + : QXmlDefaultHandler(), syntax(s) { + this->octave_comands=octave_comands; + } - bool startElement(const QString &/*namespaceURI*/, const QString &/*localName*/, - const QString &qname, const QXmlAttributes &atts) - { - Tag tag; - tag.tag=qname; + bool startElement(const QString &/*namespaceURI*/, const QString &/*localName*/, + const QString &qname, const QXmlAttributes &atts) { + Tag tag; + tag.tag=qname; - if(qname == "list") - {// List block. Get the type name. - tag.type = atts.value("name").trimmed(); - if(tag.type=="functions") - { - tag.items << (*octave_comands); - } - } - //else if(qname == "item") - //{// Item. Next string is an item. - //} - else if(qname == "comment") - {// Comments. - syntax->setComment(atts.value("start"), "$", atts.value("name")); - } + if(qname == "list") + {// List block. Get the type name. + tag.type = atts.value("name").trimmed(); + if(tag.type=="functions") + { + tag.items << (*octave_comands); + } + } + //else if(qname == "item") + //{// Item. Next string is an item. + //} + else if(qname == "comment") + {// Comments. + syntax->setComment(atts.value("start"), "$", atts.value("name")); + } - - stack.append(tag); - return true; - } + stack.append(tag); + return true; + } - bool characters(const QString &ch) - { - text+=ch; - return true; - } - - bool endElement( const QString & /*namespaceURI*/, const QString & /*localName*/, const QString & qname ) - { - Tag tag; + bool characters(const QString &ch) { + text+=ch; + return true; + } - if(stack.isEmpty()) return true; - - tag=stack.last(); - stack.removeLast(); + bool endElement(const QString & /*namespaceURI*/, + const QString & /*localName*/, + const QString & qname ) { + Tag tag; - if(tag.tag!=qname) - { - printf("Error reading XML syntax\n"); - return false; - } + if(stack.isEmpty()) return true; + + tag=stack.last(); + stack.removeLast(); - if(qname == "list") - {// List block. Get the type name. - if(stack.last().tag=="list") - { - stack.last().childs.append(tag); - } - else - { - syntax->setItem(tag.items.join("|"), tag.type); - for(int i=0;i<tag.childs.size();i++) - { - syntax->setItem(tag.childs[i].items.join("|"), tag.childs[i].type,tag.type); - } - } - } - else if(qname == "item") - {// Item. Next string is an item. - if(! text.trimmed().isEmpty() ) - stack.last().items << text.trimmed(); - } + if(tag.tag!=qname) { + printf("Error reading XML syntax\n"); + return false; + } - text=""; + if(qname == "list") {// List block. Get the type name. + if(stack.last().tag=="list") { + stack.last().childs.append(tag); + } else { + syntax->setItem(tag.items.join("|"), tag.type); + for(int i=0;i<tag.childs.size();i++) { + syntax->setItem(tag.childs[i].items.join("|"), tag.childs[i].type,tag.type); + } + } + } else if(qname == "item") { + // Item. Next string is an item. + if(! text.trimmed().isEmpty() ) + stack.last().items << text.trimmed(); + } - return true; - } + text=""; + return true; + } }; - /*** Block data ***/ - -class BlockData:public QTextBlockUserData -{ - public: - BlockData() {braket_start_pos=braket_end_pos=-1;} - ~BlockData (){} - int braket_start_pos, braket_end_pos; - QHash<int,QString> bracket; +class BlockData:public QTextBlockUserData { +public: + BlockData() {braket_start_pos=braket_end_pos=-1;} + ~BlockData (){} + int braket_start_pos, braket_end_pos; + QHash<int,QString> bracket; }; - - /*** Syntax ***/ - -SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent): QSyntaxHighlighter(parent) -{ - QTextCharFormat f; +SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent) + : QSyntaxHighlighter(parent) { + QTextCharFormat f; - QFont text_edit_font; - QString font_name="Monospace";//get_config("textEditFont"); - QString font_size="10";//get_config("textEditFontSize"); - if(font_name.isEmpty()) - { - font_name=text_edit_font.family(); - } - if(font_size.isEmpty()) - { - font_size=QString::number(text_edit_font.pointSize()); - } - text_edit_font.setFamily(font_name); - text_edit_font.setPointSize(font_size.toInt()); + QFont text_edit_font; + QString font_name="Monospace";//get_config("textEditFont"); + QString font_size="10";//get_config("textEditFontSize"); + if(font_name.isEmpty()) { + font_name=text_edit_font.family(); + } + + if(font_size.isEmpty()) { + font_size=QString::number(text_edit_font.pointSize()); + } + + text_edit_font.setFamily(font_name); + text_edit_font.setPointSize(font_size.toInt()); - f.setFont(text_edit_font); - f.setFontWeight(QFont::Bold); - _format["keywords"] = f; - _format["commands"] = f; - f.setFontWeight(QFont::Normal); - f.setForeground(Qt::darkGreen); - _format["builtin"] = f; - f.setForeground(Qt::blue); - _format["functions"] = f; - // operators - f.setForeground(Qt::black); - _format["variables"] = f; - f.setForeground(Qt::darkMagenta); - _format["numbers"] = f; - f.setForeground(Qt::red); - _format["strings"] = f; - // delimiters - f.setForeground(Qt::darkGray); - _format["singleLine"] = f; - //Brackets matched - f.setForeground(Qt::black); - //f.setFontWeight(QFont::Bold); - f.setBackground(Qt::yellow); - _format["bracket match"]=f; + f.setFont(text_edit_font); + f.setFontWeight(QFont::Bold); + _format["keywords"] = f; + _format["commands"] = f; + f.setFontWeight(QFont::Normal); + f.setForeground(Qt::darkGreen); + _format["builtin"] = f; + f.setForeground(Qt::blue); + _format["functions"] = f; + // operators + f.setForeground(Qt::black); + _format["variables"] = f; + f.setForeground(Qt::darkMagenta); + _format["numbers"] = f; + f.setForeground(Qt::red); + _format["strings"] = f; + // delimiters + f.setForeground(Qt::darkGray); + _format["singleLine"] = f; + //Brackets matched + f.setForeground(Qt::black); + //f.setFontWeight(QFont::Bold); + f.setBackground(Qt::yellow); + _format["bracket match"]=f; - active_ok=true; + active_ok=true; - braketsMacth_ok=false; + braketsMacth_ok=false; - //printf("Syntax Builded\n"); + //printf("Syntax Builded\n"); - //Add rules to vectors to help to highlightBlock method - __re.clear(); - __i_aux.clear(); - for(int n=0;n<rules.size();n++) - { - __re.append(rules[n]->pattern); - __i_aux.append(-1); - //printf("%s %d %d\n", rules.at(n)->type.toLocal8Bit().data(), __re.size(), __i_aux[n]); - } - + //Add rules to vectors to help to highlightBlock method + __re.clear(); + __i_aux.clear(); + for(int n=0;n<rules.size();n++) { + __re.append(rules[n]->pattern); + __i_aux.append(-1); + //printf("%s %d %d\n", rules.at(n)->type.toLocal8Bit().data(), __re.size(), __i_aux[n]); + } } - -SyntaxHighlighter::~SyntaxHighlighter() -{ +SyntaxHighlighter::~SyntaxHighlighter() { //foreach(Rule *value, rules_map) //{ // delete value; @@ -214,397 +193,277 @@ setDocument(NULL); } -void SyntaxHighlighter::load(const QString &path) -{ - if(octave_comands.isEmpty()) - { - QString home=QDir::home().path()+"/.qtoctave/commands.txt"; +void SyntaxHighlighter::load(const QString &path) { + if(octave_comands.isEmpty()) { + QString home=QDir::home().path()+"/.qtoctave/commands.txt"; - QFile file(home); + QFile file(home); - if (file.open(QFile::ReadOnly)) - { - char buf[1024]; + if(file.open(QFile::ReadOnly)) { + char buf[1024]; - while(file.readLine(buf, sizeof(buf))>=0) - { - octave_comands.append(QString(buf).trimmed()); - } - - file.close(); - } + while(file.readLine(buf, sizeof(buf))>=0) { + octave_comands.append(QString(buf).trimmed()); + } + file.close(); + } + } - - } - - //rules = &(instances[path]); - if(rules.isEmpty()) - { - // Load from file - FILE *fl; + //rules = &(instances[path]); + if(rules.isEmpty()) { + // Load from file + FILE *fl; - fl = fopen(path.toLocal8Bit().constData(), "rt"); - if(!fl) - { - std::cerr << "[Syntax::load] Can not load the syntax file" << std::endl; - return; - } + fl = fopen(path.toLocal8Bit().constData(), "rt"); + if(!fl) { + std::cerr << "[Syntax::load] Can not load the syntax file" << std::endl; + return; + } - QFile file(path); - QXmlSimpleReader parser; - QXmlInputSource source(&file); - SyntaxXmlHandler handler(this, &octave_comands); + QFile file(path); + QXmlSimpleReader parser; + QXmlInputSource source(&file); + SyntaxXmlHandler handler(this, &octave_comands); - file.open(fl, QIODevice::ReadOnly); + file.open(fl, QIODevice::ReadOnly); - parser.setContentHandler(&handler); - parser.setErrorHandler(&handler); + parser.setContentHandler(&handler); + parser.setErrorHandler(&handler); - parser.parse(&source); + parser.parse(&source); - file.close(); + file.close(); - fclose(fl); + fclose(fl); - std::cout << "[Sytax::load] " - << path.toLocal8Bit().constData() - << " loaded" - << std::endl; - } - - + std::cout << "[Sytax::load] " + << path.toLocal8Bit().constData() + << " loaded" + << std::endl; + } } -// void Syntax::setItem(const QString &item, const QString &type) -// { -// Rule r; -// if(!item.isEmpty()) -// { -// r.pattern = QRegExp(item); -// r.type = type; -// r.format = _format[type]; -// rules.push_back(r); -// } -// } - -void SyntaxHighlighter::setItem(const QString &item, const QString &type, const QString parent) -{ - Rule *r; - if(!item.isEmpty()) - { - r=new Rule; - r->pattern = QRegExp(item); - r->type = type; - r->format = _format[type]; - rules_map[type]=r; - if(parent.isEmpty() || !rules_map.contains(parent)) - rules.push_back(r); - else - rules_map[parent]->rules.push_back(r); - } +void SyntaxHighlighter::setItem(const QString &item, const QString &type, const QString parent) { + Rule *r; + if(!item.isEmpty()) { + r=new Rule; + r->pattern = QRegExp(item); + r->type = type; + r->format = _format[type]; + rules_map[type]=r; + if(parent.isEmpty() || !rules_map.contains(parent)) + rules.push_back(r); + else + rules_map[parent]->rules.push_back(r); + } } -void SyntaxHighlighter::setComment(const QString &start, const QString &end, const QString &type) -{ - Rule *r; - if(!type.isEmpty()) - { - r=new Rule; - r->pattern = QRegExp(/*QString("^") +*/ start + ".*" + end); - r->type = type; - r->format = _format[type]; - rules_map[type]=r; - rules.push_back(r); - } +void SyntaxHighlighter::setComment(const QString &start, const QString &end, const QString &type) { + Rule *r; + if(!type.isEmpty()) { + r=new Rule; + r->pattern = QRegExp(/*QString("^") +*/ start + ".*" + end); + r->type = type; + r->format = _format[type]; + rules_map[type]=r; + rules.push_back(r); + } } -void SyntaxHighlighter::setType(const QString &type, const QTextCharFormat &f) -{ - _format[type] = f; +void SyntaxHighlighter::setType(const QString &type, const QTextCharFormat &f) { + _format[type] = f; } -void SyntaxHighlighter::highlightBlock(const QString &str) -{ - //Para aumentar el rendimiento se hace una tabla i_aux con la posición de lo - //que ha encontrado cada expresión regular rules.at(n)->pattern. - //Se aplicará el formato debido a la Rule que tenga la i_aux más pequeña - if( !str.isEmpty() && !rules.isEmpty() && active_ok ) - { - - - //printf("Current block %d\n", currentBlock().blockNumber()); - - //setFormat(0, str.length(), _format["variables"]); +void SyntaxHighlighter::highlightBlock(const QString &str) { + //Para aumentar el rendimiento se hace una tabla i_aux con la posición de lo + //que ha encontrado cada expresión regular rules.at(n)->pattern. + //Se aplicará el formato debido a la Rule que tenga la i_aux más pequeña + if(!str.isEmpty() && !rules.isEmpty() && active_ok) { - int i=0, len=0; //Actual position - int n_min; //Minimal position + int i=0, len=0; //Actual position + int n_min; //Minimal position - BlockData *dat=(BlockData *)currentBlockUserData(); - if(dat!=NULL) - { - dat->bracket.clear(); - } - - //int *__i_aux=new int[rules.size()]; //Auxiliar position - //QRegExp *__re=new QRegExp[rules.size()]; - - //printf("rules %d re %d i_aux %d\n", rules.size(), __re.size(), __i_aux.size()); + BlockData *dat=(BlockData *)currentBlockUserData(); + if(dat!=NULL) { + dat->bracket.clear(); + } - for(int n=0;n<__re.size();n++) - { - //re[n]=rules.at(n)->pattern; - __i_aux[n] = __re[n].indexIn( str, i); - //printf("%s %d %d\n", rules.at(n)->type.toLocal8Bit().data(), n, __i_aux[n]); - } + for(int n=0;n<__re.size();n++) { + //re[n]=rules.at(n)->pattern; + __i_aux[n] = __re[n].indexIn( str, i); + //printf("%s %d %d\n", rules.at(n)->type.toLocal8Bit().data(), n, __i_aux[n]); + } - while(i >= 0) - { - n_min=-1; - for(int n=0;n<__re.size();n++) - { - if(__i_aux[n]<0) continue; - if(__i_aux[n]<i ) __i_aux[n] = __re[n].indexIn( str, i); - //printf("%s n=%d i_aux=%d n_min=%d i=%d\n", rules.at(n)->type.toLocal8Bit().data(), n, i_aux[n], n_min, i); - if( n_min<0 || __i_aux[n_min]<0 || (__i_aux[n]>=0 && __i_aux[n]<__i_aux[n_min]) ) - { - n_min=n; - if(__i_aux[n]==i) break; - } - } - //printf("n_min=%d elegido\n", n_min); - if(n_min>=0) i=__i_aux[n_min]; - else break; - if( i<0 ) break; - len = __re[n_min].matchedLength(); + while(i >= 0) { + n_min=-1; + for(int n=0;n<__re.size();n++) + { + if(__i_aux[n]<0) continue; + if(__i_aux[n]<i ) __i_aux[n] = __re[n].indexIn( str, i); + //printf("%s n=%d i_aux=%d n_min=%d i=%d\n", rules.at(n)->type.toLocal8Bit().data(), n, i_aux[n], n_min, i); + if( n_min<0 || __i_aux[n_min]<0 || (__i_aux[n]>=0 && __i_aux[n]<__i_aux[n_min]) ) + { + n_min=n; + if(__i_aux[n]==i) break; + } + } + //printf("n_min=%d elegido\n", n_min); + if(n_min>=0) i=__i_aux[n_min]; + else break; + if( i<0 ) break; + len = __re[n_min].matchedLength(); - //QStringList list=re[n_min].capturedTexts (); - //printf("\n"); - //for(int n=0;n<list.size();n++) - //{ - // printf("%d >%s<\n", n, list.at(n).toLocal8Bit().data() ); - //} - //printf("Aplicando %s i=%d len=%d\n", rules.at(n_min)->type.toLocal8Bit().data(), i, len); - if(len<1) break; - //QTextCharFormat i_format=format(i); - //if( !(i_format==strings) ) + //QStringList list=re[n_min].capturedTexts (); + //printf("\n"); + //for(int n=0;n<list.size();n++) + //{ + // printf("%d >%s<\n", n, list.at(n).toLocal8Bit().data() ); + //} + //printf("Aplicando %s i=%d len=%d\n", rules.at(n_min)->type.toLocal8Bit().data(), i, len); + if(len<1) break; + //QTextCharFormat i_format=format(i); + //if( !(i_format==strings) ) - if(rules.at(n_min)->rules.isEmpty()) - { - setFormat(i, len, rules.at(n_min)->format); + if(rules.at(n_min)->rules.isEmpty()) + { + setFormat(i, len, rules.at(n_min)->format); - if( rules.at(n_min)->type=="delimiters" ) - { - QString bracket_found=__re[n_min].cap(); + if( rules.at(n_min)->type=="delimiters" ) + { + QString bracket_found=__re[n_min].cap(); - if(dat==NULL) - { - dat=new BlockData(); - setCurrentBlockUserData(dat); - } - dat->bracket[i]=bracket_found; + if(dat==NULL) + { + dat=new BlockData(); + setCurrentBlockUserData(dat); + } + dat->bracket[i]=bracket_found; - //Do brackets macth - if( braketsMacth_ok && dat != NULL ) - { - if(dat->braket_start_pos>=0) - setFormat(dat->braket_start_pos, 1, _format["bracket match"]); - if(dat->braket_end_pos>=0) - setFormat(dat->braket_end_pos, 1, _format["bracket match"]); - } - } + //Do brackets macth + if( braketsMacth_ok && dat != NULL ) + { + if(dat->braket_start_pos>=0) + setFormat(dat->braket_start_pos, 1, _format["bracket match"]); + if(dat->braket_end_pos>=0) + setFormat(dat->braket_end_pos, 1, _format["bracket match"]); + } + } - } - else - { - //Rules can contains another rules - QString text=str.mid(i,len); - //printf("text=%s\n", text.toLocal8Bit().data() ); - bool format_ok=true; - for(int n=0;n<rules.at(n_min)->rules.size(); n++) - { - if(rules.at(n_min)->rules.at(n)->pattern.exactMatch(text)) - { - setFormat(i, len, rules.at(n_min)->rules.at(n)->format); - format_ok=false; - break; - } - } - if(format_ok) setFormat(i, len, rules.at(n_min)->format); - } - i+=len; - //printf("i=%d\n",i); - } + } + else + { + //Rules can contains another rules + QString text=str.mid(i,len); + //printf("text=%s\n", text.toLocal8Bit().data() ); + bool format_ok=true; + for(int n=0;n<rules.at(n_min)->rules.size(); n++) + { + if(rules.at(n_min)->rules.at(n)->pattern.exactMatch(text)) + { + setFormat(i, len, rules.at(n_min)->rules.at(n)->format); + format_ok=false; + break; + } + } + if(format_ok) setFormat(i, len, rules.at(n_min)->format); + } + i+=len; - //delete [] i_aux; - //delete [] re; - - } + } +} } -int SyntaxHighlighter::forward_search(QTextBlock & block, int pos, char bracket_start, char bracket_end) -{ - int i=pos, open=0; - - while(block.isValid()) - { - /* - if(!block.text().isEmpty()) - { - QString str=block.text(); - int len=str.length(); +int SyntaxHighlighter::forwardSearch(QTextBlock &textBlock, int position, char bracketStart, char bracketEnd) { + int i=position, open=0; - //This line is added to check lower position - if(i<0) i=0; - - for(;i<len;i++) //i<len checks upper position - { - QChar ch=str.at(i); - - if(ch==bracket_end) - { - open--; - if(open==0) return i; - } - else if(ch==bracket_start) open++; - } - }*/ + while(textBlock.isValid()) { + BlockData *dat=(BlockData *)textBlock.userData(); + if(dat!=NULL) { + QList<int> positions=dat->bracket.keys(); + qSort(positions); + for(int k=0;k<positions.size();k++) { + int b_pos=positions[k]; + if(b_pos<i) continue; - BlockData *dat=(BlockData *)block.userData(); - if(dat!=NULL) - { - QList<int> positions=dat->bracket.keys(); - qSort(positions); - for(int k=0;k<positions.size();k++) - { - int b_pos=positions[k]; - if(b_pos<i) continue; - - QChar ch=dat->bracket[b_pos].at(0); + QChar ch=dat->bracket[b_pos].at(0); - if(ch==bracket_end) - { - open--; - if(open==0) return b_pos; - } - else if(ch==bracket_start) open++; - } - } - - block=block.next(); - - i=0; - } - - return -1; + if(ch==bracketEnd) { + open--; + if(open==0) return b_pos; + } + else if(ch==bracketStart) open++; + } + } + textBlock=textBlock.next(); + i=0; + } + return -1; } -int SyntaxHighlighter::backward_search(QTextBlock & block, int pos, char bracket_start, char bracket_end) -{ - int i=pos, open=0; +int SyntaxHighlighter::backwardSearch(QTextBlock & textBlock, int position, char bracketStart, char bracketEnd) { + int i=position, open=0; + while(textBlock.isValid()) { + BlockData *dat=(BlockData *)textBlock.userData(); + if(dat!=NULL) { + QList<int> positions=dat->bracket.keys(); + qSort(positions); + for(int k=positions.size()-1;k>=0;k--) { + int b_pos=positions[k]; + if(b_pos>i) continue; - while(block.isValid()) - { - /* - if(!block.text().isEmpty()) - { - QString str=block.text(); - int len=str.length(); - - //This line is added to check upper position - if(i>=len) i=len-1; - - for(;i>=0;i--) //i>=0 checks lower position - { - QChar ch=str.at(i); + QChar ch=dat->bracket[b_pos].at(0); - if(ch==bracket_start) - { - open--; - if(open==0) return i; - } - else if(ch==bracket_end) open++; - } - } - */ + if(ch==bracketStart) { + open--; + if(open==0) return b_pos; + } + else if(ch==bracketEnd) open++; + } + } - BlockData *dat=(BlockData *)block.userData(); - if(dat!=NULL) - { - QList<int> positions=dat->bracket.keys(); - qSort(positions); - for(int k=positions.size()-1;k>=0;k--) - { - int b_pos=positions[k]; - if(b_pos>i) continue; - - QChar ch=dat->bracket[b_pos].at(0); - - if(ch==bracket_start) - { - open--; - if(open==0) return b_pos; - } - else if(ch==bracket_end) open++; - } - } - - block=block.previous(); - - if(block.isValid() && !block.text().isEmpty()) i=block.length()-1; - } - - return -1; + textBlock=textBlock.previous(); + if(textBlock.isValid() && !textBlock.text().isEmpty()) i=textBlock.length()-1; + } + return -1; } -static void set_block_data(QTextBlock & block0, QTextBlock & block1, int start, int end) -{ - BlockData *udat=(BlockData *)block0.userData(); - if(udat==NULL) - { - udat=new BlockData(); - block0.setUserData(udat); - } - udat->braket_start_pos=start; +static void set_block_data(QTextBlock & block0, QTextBlock & block1, int start, int end) { + BlockData *udat=(BlockData *)block0.userData(); + if(udat==NULL) { + udat=new BlockData(); + block0.setUserData(udat); + } + udat->braket_start_pos=start; - if(block0==block1) - { - udat->braket_end_pos=end; - } - else - { - BlockData *udat=(BlockData *)block1.userData(); - if(udat==NULL) - { - udat=new BlockData(); - block1.setUserData(udat); - } - udat->braket_end_pos=end; - } + if(block0==block1) { + udat->braket_end_pos=end; + } else { + BlockData *udat=(BlockData *)block1.userData(); + if(udat==NULL) { + udat=new BlockData(); + block1.setUserData(udat); + } + udat->braket_end_pos=end; + } } -static void clear_block_data(QTextDocument *doc, bool rehigh ) -{ - QTextBlock block=doc->findBlock(0); - - while( block.isValid() ) - { - BlockData *udat=(BlockData *)block.userData(); - if(udat!=NULL && (udat->braket_end_pos!=-1 || udat->braket_start_pos!=-1) ) - { - udat->braket_end_pos=-1; udat->braket_start_pos=-1; - if(rehigh) - { - //QTextCursor cursor(doc); - //cursor.setPosition(block.position()); - //cursor.setBlockFormat(block.blockFormat()); - } - } - block=block.next(); - } +static void clear_block_data(QTextDocument *doc, bool rehigh) { + QTextBlock block=doc->findBlock(0); + while(block.isValid()) { + BlockData *udat=(BlockData *)block.userData(); + if(udat!=NULL && (udat->braket_end_pos!=-1 || udat->braket_start_pos!=-1)) { + udat->braket_end_pos=-1; udat->braket_start_pos=-1; + if(rehigh) { + //QTextCursor cursor(doc); + //cursor.setPosition(block.position()); + //cursor.setBlockFormat(block.blockFormat()); + } + } + block=block.next(); + } } -void SyntaxHighlighter::setActive(bool active) -{ - active_ok=active; +void SyntaxHighlighter::setActive(bool active) { + active_ok=active; }
--- a/gui//src/SyntaxHighlighter.h +++ b/gui//src/SyntaxHighlighter.h @@ -16,8 +16,8 @@ * Boston, MA 02111-1307, USA. */ -#ifndef __SYNTAX_H__ -#define __SYNTAX_H__ +#ifndef SYNTAXHIGHLIGHTER_H +#define SYNTAXHIGHLIGHTER_H #include <QPlainTextEdit> #include <QSyntaxHighlighter> #include <QVector> @@ -26,55 +26,54 @@ #include "config.h" /**SyntaxHighlighter for Octave code.*/ -class SyntaxHighlighter: public QSyntaxHighlighter -{ - Q_OBJECT - public: - SyntaxHighlighter(QTextDocument *parent); - ~SyntaxHighlighter(); - void highlightBlock(const QString &str); - void load(const QString &file); +class SyntaxHighlighter: public QSyntaxHighlighter { + Q_OBJECT +public: + SyntaxHighlighter(QTextDocument *parent); + ~SyntaxHighlighter(); + void highlightBlock(const QString &str); + void load(const QString &file); - //void setItem(const QString &item, const QString &type); - void setItem(const QString &item, const QString &type, const QString parent=QString() ); - void setComment(const QString &start, const QString &end, const QString &type); - void setType(const QString &type, const QTextCharFormat &format); - - /**Stops syntax highlight*/ - void setActive(bool active); - - static QStringList octave_comands; - - public slots: - /**Return true or false if brackets are been macthed*/ - inline bool getIsActiveBraketsMacth() {return braketsMacth_ok;} + //void setItem(const QString &item, const QString &type); + void setItem(const QString &item, const QString &type, const QString parent=QString() ); + void setComment(const QString &start, const QString &end, const QString &type); + void setType(const QString &type, const QTextCharFormat &format); + + /**Stops syntax highlight*/ + void setActive(bool active); + + static QStringList octave_comands; + +public slots: + /**Return true or false if brackets are been macthed*/ + inline bool getIsActiveBraketsMacth() {return braketsMacth_ok;} - private: - struct Rule - { - QRegExp pattern; - QString type; - QTextCharFormat format; - QList<Rule*> rules; - }; - - int backward_search(QTextBlock & block, int pos, char bracket_start, char bracket_end); - int forward_search(QTextBlock & block, int pos, char bracket_start, char bracket_end); +private: + struct Rule + { + QRegExp pattern; + QString type; + QTextCharFormat format; + QList<Rule*> rules; + }; + + int backwardSearch(QTextBlock &textBlock, int position, char bracketStart, char bracketEnd); + int forwardSearch(QTextBlock &textBlock, int position, char bracketStart, char bracketEnd); - //static QMap<QString, QList<Rule> > instances; - - QMap<QString, Rule *> rules_map; + //static QMap<QString, QList<Rule> > instances; + + QMap<QString, Rule *> rules_map; + + static QList<Rule*> rules; + QMap<QString, QTextCharFormat> _format; - static QList<Rule*> rules; - QMap<QString, QTextCharFormat> _format; - - //Next two properties are used inside highlightBlock method - QVector<int> __i_aux; //Auxiliar positions - QVector<QRegExp> __re; //Regular expresions - - bool active_ok; - bool braketsMacth_ok; + //Next two properties are used inside highlightBlock method + QVector<int> __i_aux; //Auxiliar positions + QVector<QRegExp> __re; //Regular expresions + + bool active_ok; + bool braketsMacth_ok; }; -#endif +#endif // SYNTAXHIGHLIGHTER_H