Skip to content

Commit b75167c

Browse files
committed
Bracket Colors
Add bracketcolors plugin. Color {}, [], () based on nesting order Closes #1148
1 parent 2665511 commit b75167c

17 files changed

+3040
-0
lines changed

MAINTAINERS

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ M: Pavel Roschin <rpg89(at)post(dot)ru>
3333
W:
3434
S: Maintained
3535

36+
bracketcolors
37+
* P: Asif Amin <asifamin@utexas.edu>
38+
* g: @asifamin13
39+
* M: Asif Amin <asifamin@utexas.edu>
40+
* W:
41+
* S: Maintained
42+
3643
codenav
3744
P: Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
3845
g:

Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ if ENABLE_AUTOMARK
2121
SUBDIRS += automark
2222
endif
2323

24+
if ENABLE_BRACKETCOLORS
25+
SUBDIRS += bracketcolors
26+
endif
27+
2428
if ENABLE_CODENAV
2529
SUBDIRS += codenav
2630
endif

bracketcolors/AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Asif Amin <asifamin@utexas.edu>

bracketcolors/COPYING

+339
Large diffs are not rendered by default.

bracketcolors/ChangeLog

Whitespace-only changes.

bracketcolors/Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include $(top_srcdir)/build/vars.auxfiles.mk
2+
3+
SUBDIRS = src
4+
plugin = bracketcolors

bracketcolors/NEWS

Whitespace-only changes.

bracketcolors/README

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Color brackets, parenthesis, and braces
2+
===================
3+
4+
.. contents::
5+
6+
About
7+
-----
8+
9+
This plugin enables bracket coloring features. Brackets are colored based on
10+
nesting level
11+
12+
Features
13+
--------
14+
15+
* Color brackets for: { }, [ ], ( )
16+
17+
Usage
18+
-----
19+
20+
Install the plugin (https://plugins.geany.org/install.html) then
21+
load it in Geany's plugin manager.
22+
23+
Requirements
24+
------------
25+
26+
* Geany >= 1.38
27+
* C++17
28+
29+
Contact developers
30+
------------------
31+
32+
Asif Amin <asifamin@utexas.edu>

bracketcolors/src/BracketMap.cc

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* BracketMap.cc
3+
*
4+
* Copyright 2013 Asif Amin <asifamin@utexas.edu>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
#ifdef HAVE_CONFIG_H
22+
# include "config.h"
23+
#endif
24+
25+
#include <stack>
26+
#include <set>
27+
28+
#include "BracketMap.h"
29+
30+
31+
// -----------------------------------------------------------------------------
32+
BracketMap::BracketMap()
33+
/*
34+
Constructor
35+
----------------------------------------------------------------------------- */
36+
{
37+
38+
}
39+
40+
41+
// -----------------------------------------------------------------------------
42+
BracketMap::~BracketMap()
43+
/*
44+
Destructor
45+
----------------------------------------------------------------------------- */
46+
{
47+
48+
}
49+
50+
51+
// -----------------------------------------------------------------------------
52+
void BracketMap::Update(Index index, Length length)
53+
/*
54+
55+
----------------------------------------------------------------------------- */
56+
{
57+
auto it = mBracketMap.find(index);
58+
if (it != mBracketMap.end()) {
59+
auto &bracket = it->second;
60+
GetLength(bracket) = length;
61+
}
62+
else {
63+
mBracketMap.insert(
64+
std::make_pair(index, std::make_tuple(length, 0))
65+
);
66+
}
67+
}
68+
69+
70+
71+
// -----------------------------------------------------------------------------
72+
void BracketMap::ComputeOrder()
73+
/*
74+
75+
----------------------------------------------------------------------------- */
76+
{
77+
std::stack<Index> orderStack;
78+
79+
for (auto &it : mBracketMap) {
80+
81+
const Index &startIndex = it.first;
82+
Bracket &bracket = it.second;
83+
Length length = GetLength(bracket);
84+
Index endPos = startIndex + length;
85+
86+
if (length == UNDEFINED) {
87+
// Invalid brackets
88+
GetOrder(bracket) = UNDEFINED;
89+
continue;
90+
}
91+
92+
93+
if (orderStack.size() == 0) {
94+
// First bracket
95+
orderStack.push(endPos);
96+
}
97+
else if (startIndex > orderStack.top()) {
98+
// not nested
99+
while(orderStack.size() > 0 and orderStack.top() < startIndex) {
100+
orderStack.pop();
101+
}
102+
orderStack.push(endPos);
103+
}
104+
else {
105+
// nested bracket
106+
orderStack.push(endPos);
107+
}
108+
109+
GetOrder(bracket) = orderStack.size() - 1;
110+
}
111+
}
112+
113+
114+
115+
// -----------------------------------------------------------------------------
116+
void BracketMap::Show()
117+
/*
118+
119+
----------------------------------------------------------------------------- */
120+
{ g_debug("%s: Showing bracket map ...", __FUNCTION__);
121+
122+
for (const auto it : mBracketMap) {
123+
124+
const Index &startIndex = it.first;
125+
const Bracket &bracket = it.second;
126+
127+
Length length = std::get<0>(bracket);
128+
Order order = std::get<1>(bracket);
129+
130+
Index end = -1;
131+
if (length > 0) {
132+
end = startIndex + length;
133+
}
134+
135+
g_debug(
136+
"%s: Bracket at %d, Length: %d, End: %d, Order: %d",
137+
__FUNCTION__, startIndex, length, end, order
138+
);
139+
}
140+
141+
g_debug("%s: ... Finished showing bracket map", __FUNCTION__);
142+
}
143+

bracketcolors/src/BracketMap.h

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* BracketMap.h
3+
*
4+
* Copyright 2013 Asif Amin <asifamin@utexas.edu>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
#ifndef __BRACKET_MAP_H__
22+
#define __BRACKET_MAP_H__
23+
24+
#include <map>
25+
#include <tuple>
26+
27+
#include <glib.h>
28+
29+
30+
// -----------------------------------------------------------------------------
31+
struct BracketMap
32+
/*
33+
Purpose: data structure which stores and computes nesting order
34+
----------------------------------------------------------------------------- */
35+
{
36+
typedef gint Length, Order, Index;
37+
typedef std::tuple<Length, Order> Bracket;
38+
std::map<Index, Bracket> mBracketMap;
39+
40+
BracketMap();
41+
~BracketMap();
42+
43+
void Show();
44+
45+
void Update(Index index, Length length);
46+
void ComputeOrder();
47+
48+
static const gint UNDEFINED = -1;
49+
50+
static Length& GetLength(Bracket &bracket) {
51+
return std::get<0>(bracket);
52+
}
53+
static Order& GetOrder(Bracket &bracket) {
54+
return std::get<1>(bracket);
55+
}
56+
57+
static const Length& GetLength(const Bracket &bracket) {
58+
return std::get<0>(bracket);
59+
}
60+
static const Order& GetOrder(const Bracket &bracket) {
61+
return std::get<1>(bracket);
62+
}
63+
};
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
#endif

bracketcolors/src/Makefile.am

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include $(top_srcdir)/build/vars.build.mk
2+
plugin = bracketcolors
3+
4+
geanyplugins_LTLIBRARIES = bracketcolors.la
5+
6+
bracketcolors_srcs = \
7+
bracketcolors.cc \
8+
BracketMap.cc \
9+
BracketMap.h
10+
11+
bracketcolors_la_SOURCES = $(bracketcolors_srcs)
12+
bracketcolors_la_CXXFLAGS = $(AM_CXXFLAGS) $(AM_CFLAGS) -DG_LOG_DOMAIN=\"BracketColors\"
13+
bracketcolors_la_LIBADD = $(COMMONLIBS)
14+
15+
include $(top_srcdir)/build/cppcheck.mk

0 commit comments

Comments
 (0)