• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

antonvw / wex / 13091451436

01 Feb 2025 06:48PM UTC coverage: 58.704% (-0.003%) from 58.707%
13091451436

push

github

web-flow
use std::ranges::all_of instead of std::all_of (#833)

16540 of 31034 branches covered (53.3%)

Branch coverage included in aggregate %.

13325 of 19840 relevant lines covered (67.16%)

1217.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.17
/src/syntax/style.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
// Name:      style.cpp
3
// Purpose:   Implementation of wex::style class
4
// Author:    Anton van Wezenbeek
5
// Copyright: (c) 2010-2025 Anton van Wezenbeek
6
////////////////////////////////////////////////////////////////////////////////
7

8
#include <boost/algorithm/string.hpp>
9
#include <boost/tokenizer.hpp>
10

11
#include <wex/core/config.h>
12
#include <wex/core/log.h>
13
#include <wex/core/regex.h>
14
#include <wex/syntax/lexers.h>
15
#include <wex/syntax/style.h>
16
#include <wx/stc/stc.h>
17

18
#include <charconv>
19

20
namespace wex
21
{
22
bool check_style_spec(const std::string& spec, const std::string& colour)
40,235✔
23
{
24
  if (regex r(colour + ":(.*)"); r.match(spec) > 0)
40,235✔
25
  {
26
    if (!wxColour(r[0]).IsOk())
13,417✔
27
    {
28
      log("style " + colour + " colour") << r[0];
1✔
29
      return false;
1✔
30
    }
31
  }
40,235✔
32

33
  return true;
40,234✔
34
}
35

36
wxFont default_font()
3,642✔
37
{
38
  return config(_("stc.Default font"))
7,284✔
39
    .get(wxFont(
3,642✔
40
      12,
41
      wxFONTFAMILY_DEFAULT,
42
      wxFONTSTYLE_NORMAL,
43
      wxFONTWEIGHT_NORMAL));
10,926✔
44
}
45
} // namespace wex
46

47
bool wex::style::apply(wxStyledTextCtrl* stc) const
9,614✔
48
{
49
  if (stc->GetParent() == nullptr)
9,614✔
50
  {
51
    return false;
2✔
52
  }
53

54
  // Currently, the default style is constructed using
55
  // default constructor.
56
  // If this is the only style, reset stc.
57
  if (m_no.empty())
9,612!
58
  {
59
    if (!lexers::get()->get_lexers().empty())
×
60
    {
61
      stc->StyleResetDefault();
×
62
    }
63
  }
64
  else
65
  {
66
    if (const auto& tok(boost::tokenizer<boost::char_separator<char>>(
9,612✔
67
          m_value,
9,612✔
68
          boost::char_separator<char>(",")));
9,612✔
69
        !std::ranges::all_of(
9,612✔
70
          tok,
71
          [](const auto& it)
20,118✔
72
          {
73
            return check_style_spec(it, "back") && check_style_spec(it, "fore");
100,588!
74
          }))
75
    {
76
      return false;
1✔
77
    }
9,612✔
78

79
    for (const auto& it : m_no)
19,226✔
80
    {
81
      stc->StyleSetSpec(it, m_value);
9,615✔
82
    }
83
  }
84

85
  return true;
9,611✔
86
}
87

88
void wex::style::clear()
2✔
89
{
90
  m_define.clear();
2✔
91
  m_no.clear();
2✔
92
  m_value.clear();
2✔
93
}
2✔
94

95
bool wex::style::contains_default_style() const
110✔
96
{
97
  return (m_no.find(wxSTC_STYLE_DEFAULT) != m_no.end());
110✔
98
}
99

100
int wex::style::default_font_size() const
1,822✔
101
{
102
  return default_font().GetPointSize();
1,822✔
103
}
104

105
int wex::style::number() const
65✔
106
{
107
  return m_no.empty() ? -1 : *m_no.begin();
65!
108
}
109

110
void wex::style::set(const pugi::xml_node& node, const std::string& macro)
1,820✔
111
{
112
  m_define = node.attribute("no").value();
1,820✔
113

114
  set_no(lexers::get()->apply_macro(m_define, macro), macro, node);
1,820✔
115

116
  const auto text(std::string(node.text().get()));
1,820✔
117
  const auto font(default_font());
1,820✔
118
  const auto font_size(std::to_string(default_font_size()));
1,820✔
119

120
  // The style is parsed using the themed macros, and
121
  // you can specify several styles separated by a + sign.
122
  for (const auto& it : boost::tokenizer<boost::char_separator<char>>(
1,820✔
123
         text,
124
         boost::char_separator<char>("+")))
6,087✔
125
  {
126
    // Collect each single field style.
127
    const auto& single = it;
2,447✔
128

129
    if (const auto& it = lexers::get()->theme_macros().find(single);
2,447✔
130
        it != lexers::get()->theme_macros().end())
2,447✔
131
    {
132
      std::string value(it->second);
1,799✔
133

134
      if (value.contains("default-font"))
1,799✔
135
      {
136
        boost::algorithm::replace_all(
1,011✔
137
          value,
138
          "default-font",
139
          "face:" + font.GetFaceName() + ",size:" + font_size);
2,022✔
140

141
        if (const auto style = font.GetStyle();
1,011!
142
            style == wxFONTSTYLE_ITALIC || style == wxFONTSTYLE_SLANT)
1,011!
143
        {
144
          value += ",italic";
×
145
        }
146

147
        if (font.GetWeight() == wxFONTWEIGHT_BOLD)
1,011!
148
        {
149
          value += ",bold";
×
150
        }
151

152
        if (font.GetUnderlined())
1,011!
153
        {
154
          value += ",underline";
×
155
        }
156
      }
157

158
      m_value = (m_value.empty() ? value : m_value + "," + value);
1,799!
159
    }
1,799✔
160
    else
161
    {
162
      m_value = (m_value.empty() ? single : m_value + "," + single);
648!
163
    }
164
  }
1,820✔
165

166
  if (m_value.empty())
1,820!
167
  {
168
    log("empty style") << number() << node;
×
169
  }
170
}
1,820✔
171

172
void wex::style::set_no(
16,264✔
173
  const std::string&    no,
174
  const std::string&    macro,
175
  const pugi::xml_node& node)
176
{
177
  m_no.clear();
16,264✔
178

179
  // Collect each single no in the vector.
180
  for (const auto& it : boost::tokenizer<boost::char_separator<char>>(
16,264✔
181
         no,
182
         boost::char_separator<char>(",")))
48,868✔
183
  {
184
    const auto& single = lexers::get()->apply_macro(it, macro);
16,340✔
185

186
    try
187
    {
188
      if (int style_no = 0; std::from_chars(
16,340✔
189
                              single.data(),
190
                              single.data() + single.size(),
16,340✔
191
                              style_no)
192
                                .ec == std::errc() &&
32,677✔
193
                            style_no >= 0 && style_no <= wxSTC_STYLE_MAX)
16,340!
194
      {
195
        m_no.insert(style_no);
16,336✔
196
      }
197
      else
198
      {
199
        log("invalid style") << single;
8✔
200
      }
201
    }
202
    catch (std::exception& e)
×
203
    {
204
      log(e) << "style:" << single;
×
205
    }
×
206
  }
32,604✔
207
}
16,264✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc