File: strategyeditor.cpp

package info (click to toggle)
aoflagger 3.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,688 kB
  • sloc: cpp: 83,116; python: 10,187; sh: 260; makefile: 178
file content (178 lines) | stat: -rw-r--r-- 5,253 bytes parent folder | download
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
#include "strategyeditor.h"

StrategyEditor::StrategyEditor() {
  _text.override_font(Pango::FontDescription("monospace"));
  _text.set_wrap_mode(Gtk::WrapMode::WRAP_WORD);
  add(_text);

  _tagKeyword = _text.get_buffer()->create_tag();
  _tagKeyword->property_weight().set_value(Pango::WEIGHT_BOLD);

  _tagComment = _text.get_buffer()->create_tag();
  _tagComment->property_style().set_value(Pango::STYLE_ITALIC);
  Gdk::RGBA blue;
  blue.set_rgba(0.13, 0.0, 0.53);
  _tagComment->property_foreground_rgba().set_value(blue);

  _tagString = _text.get_buffer()->create_tag();
  Gdk::RGBA gold;
  gold.set_rgba(0.68, 0.49, 0.0);
  _tagString->property_foreground_rgba().set_value(gold);

  _tagFunctionName = _text.get_buffer()->create_tag();
  Gdk::RGBA red;
  red.set_rgba(0.65, 0.1, 0.1);
  _tagFunctionName->property_foreground_rgba().set_value(red);

  _tagHighlight = _text.get_buffer()->create_tag();
  Gdk::RGBA highlred;
  highlred.set_rgba(1.0, 0.4, 0.4);
  _tagHighlight->property_background_rgba().set_value(highlred);

  _text.get_buffer()->signal_changed().connect([&]() { updateChanges(); });
}

bool isAlpha(int c) {
  return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_';
}

bool isAlphaNumeric(int c) {
  return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') ||
         (c >= 'a' && c <= 'z') || c == '_';
}

void StrategyEditor::parseWord(ParseInfo& p, Gtk::TextBuffer::iterator start,
                               Gtk::TextBuffer::iterator end) {
  const std::string word(start, end);
  const bool isKeyword =
      word == "and" || word == "break" || word == "do" || word == "else" ||
      word == "elseif" || word == "end" || word == "false" || word == "for" ||
      word == "function" || word == "if" || word == "in" || word == "local" ||
      word == "nil" || word == "not" || word == "or" || word == "repeat" ||
      word == "return" || word == "then" || word == "true" || word == "until" ||
      word == "while";
  if (isKeyword) {
    _text.get_buffer()->apply_tag(_tagKeyword, start, end);
    if (word == "function") p.state = ParseInfo::FunctionStart;
  } else {
    if (p.state == ParseInfo::FunctionStart) {
      _text.get_buffer()->apply_tag(_tagFunctionName, start, end);
      p.state = ParseInfo::Clear;
    }
  }
}

void StrategyEditor::updateHighlighting() {
  const Glib::RefPtr<Gtk::TextBuffer> buffer = _text.get_buffer();
  auto iter = buffer->begin();
  buffer->remove_all_tags(iter, buffer->end());
  enum {
    Clear,
    InWord,
    InComment,
    AfterMinus,
    LongCommentStart,
    InLongComment,
    LongCommentEnd,
    InDQuote,
    InQuote
  } mode = Clear;
  ParseInfo p{ParseInfo::Clear};
  Gtk::TextBuffer::iterator wordStart;
  size_t lineNr = 1;
  Gtk::TextBuffer::iterator lineStart;
  while (iter != buffer->end()) {
    const int c = *iter;
    if (c == '\n') {
      if (lineNr == _highlightLine) {
        _text.get_buffer()->apply_tag(_tagHighlight, lineStart, iter);
      }
      lineStart = iter;
      ++lineStart;
      ++lineNr;
    }

    switch (mode) {
      case Clear:
      case AfterMinus:
        if (isAlpha(c)) {
          mode = InWord;
          wordStart = iter;
        } else if (c == '-') {
          if (mode == AfterMinus) {
            mode = InComment;
          } else {
            mode = AfterMinus;
            wordStart = iter;
          }
        } else if (c == '"') {
          mode = InDQuote;
          wordStart = iter;
        } else if (c == '\'') {
          mode = InQuote;
          wordStart = iter;
        }
        break;
      case InWord:
        if (!isAlphaNumeric(c)) {
          parseWord(p, wordStart, iter);
          mode = Clear;
        }
        break;
      case InComment:
        if (c == '\n') {
          _text.get_buffer()->apply_tag(_tagComment, wordStart, iter);
          mode = Clear;
          p.state = ParseInfo::Clear;
        } else if (c == '[') {
          mode = LongCommentStart;
        }
        break;
      case LongCommentStart:
        if (c == '[')
          mode = InLongComment;
        else
          mode = InComment;
        break;
      case InLongComment:
        if (c == ']') mode = LongCommentEnd;
        break;
      case LongCommentEnd:
        if (c == ']') {
          mode = Clear;
          Gtk::TextBuffer::iterator next = iter;
          ++next;
          _text.get_buffer()->apply_tag(_tagComment, wordStart, next);
        }
        break;
      case InDQuote:
        if (c == '"') {
          Gtk::TextBuffer::iterator next = iter;
          ++next;
          _text.get_buffer()->apply_tag(_tagString, wordStart, next);
          mode = Clear;
          p.state = ParseInfo::Clear;
        }
        break;
      case InQuote:
        if (c == '\'') {
          Gtk::TextBuffer::iterator next = iter;
          ++next;
          _text.get_buffer()->apply_tag(_tagString, wordStart, next);
          mode = Clear;
          p.state = ParseInfo::Clear;
        }
        break;
    }
    ++iter;
  }
}

void StrategyEditor::SetText(const std::string& text) {
  const Glib::RefPtr<Gtk::TextBuffer> buffer = _text.get_buffer();
  buffer->set_text(text);
}

std::string StrategyEditor::GetText() const {
  return std::string(_text.get_buffer()->get_text());
}