NCL Composer  0.1.5
 All Classes Functions Variables Pages
MyLexer.cpp
1 /*
2  * Copyright 2011 TeleMidia/PUC-Rio.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 #include "MyLexer.h"
19 
20 //TODO: MOVE TO OTHER PLACE
21 int text_partition_cmp (const text_partition &a, const text_partition &b) {
22  if(a.begin != b.begin)
23  return a.begin < b.begin;
24 
25  if(a.end != a.end)
26  return a.end < b.end;
27 
28  return a.id < b.id;
29 }
30 
31 MyLexer::MyLexer(QObject *parent)
32  : QsciLexerCustom(parent)
33 {
34  qDebug() << __FUNCTION__;
35 }
36 
37 MyLexer::~MyLexer()
38 {
39 }
40 
41 const char* MyLexer::language() const
42 {
43  return "AsciiDoc";
44 }
45 
46 QString MyLexer::description(int style) const
47 {
48  switch(style){
49  case Default:
50  return "Default";
51 
52  case Comment:
53  return "Comment";
54  }
55 
56  return QString();
57 }
58 
59 void MyLexer::styleText(int start, int end)
60 {
61  QString source;
62  qDebug() << __FUNCTION__
63  << "start =" << start
64  << " end =" << end;
65 
66  if (!editor())
67  return;
68 
69  char *chars = (char *) malloc ((end - start) * sizeof(char) + 1);
70  editor()->SendScintilla(QsciScintilla::SCI_GETTEXTRANGE, start, end, chars);
71  source = QString(chars);
72 
73  qDebug() << "source =" << source;
74  startStyling(start, 0x1f);
75 
76  vector <text_partition> parts = makePartitions (chars, 0, source.length());
77  sort(parts.begin(), parts.end(), text_partition_cmp);
78  int lastIndex = 0;
79  for (unsigned int i = 0; i < parts.size(); i++) {
80  qDebug() << "partition id=" << parts[i].id << " begin=" << parts[i].begin << " end=" << parts[i].end;
81  setStyling(parts[i].begin-lastIndex, getStyle(Default));
82  setStyling(parts[i].end-parts[i].begin, partition_style[parts[i].id]);
83  lastIndex = parts[i].end;
84 
85  }
86  if (source.length()-lastIndex > 0) {
87  setStyling(source.length()-lastIndex, getStyle(Default));
88  }
89  free(chars);
90 }
91 
92 QColor MyLexer::defaultColor(int style)
93 {
94  switch(style){
95  case Default:
96  return QColor(0x00, 0x0, 0x0);
97  case Comment:
98  return QColor(0x0, 0xe0, 0x0);
99  }
100  return QsciLexer::defaultColor(style);
101 }
102 
103 QFont MyLexer::defaultFont(int style)
104 {
105  return QFont("Courier New", 10);
106 }
107 
108 QColor MyLexer::defaultPaper(int style)
109 {
110  return QsciLexer::defaultPaper(style);
111 }
112 
113 QsciStyle MyLexer::getStyle(int style)
114 {
115  if (style < MaxStyle) {
116  return QsciStyle(style, description(style), defaultColor(style),
117  defaultPaper(style), defaultFont(style));
118  } else {
119  return QsciStyle(style);
120  }
121 }
122 
123 bool MyLexer::addTextPartition (int partition_id, const QRegExp &regex,
124  const QsciStyle &style) {
125 
126  partition_regex.insert(partition_id, regex);
127  partition_style.insert(partition_id, style);
128 
129  return true;
130 }
131 
132 vector <text_partition > MyLexer::makePartitions (char *chars, int begin, int end){
133  vector <text_partition> partitions;
134  QRegExp regex;
135  int PARTITION_ID = -1;
136  //GET FISRT PARTITION
137  QMap <int, QRegExp>::const_iterator i = partition_regex.constBegin();
138 
139  int lastIndex = begin;
140  while(i != partition_regex.constEnd()) {
141  PARTITION_ID = i.key();
142  regex = i.value();
143  ++i;
144  if(PARTITION_ID == -1)
145  return partitions;
146 
147  lastIndex = begin;
148 
149  //TODO: IMPROVE PERFORMANCE
150  int index = regex.indexIn(chars, lastIndex);
151  while(index != -1){
152  if (index + regex.matchedLength() > end)
153  break;
154  text_partition part;
155  part.id = PARTITION_ID;
156  part.begin = index;
157  part.end = index + regex.matchedLength();
158  partitions.push_back (part);
159 
160  lastIndex = (index + regex.matchedLength());
161  index = regex.indexIn(chars, lastIndex);
162  }
163  }
164  return partitions;
165 }