Skip to content

Commit 7bef4fd

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

19 files changed

+2913
-0
lines changed

Diff for: 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 <[email protected]>
38+
* g: @asifamin13
39+
* M: Asif Amin <[email protected]>
40+
* W:
41+
* S: Maintained
42+
3643
codenav
3744
P: Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
3845
g:

Diff for: 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

Diff for: README

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Available plugins are:
4949
* ``addons`` -- the Addons plugin
5050
* ``autoclose`` -- the Autoclose plugin
5151
* ``automark`` -- the Automark plugin
52+
* ``bracketcolors`` -- the BracketColors plugin
5253
* ``codenav`` -- the CodeNav plugin
5354
* ``commander`` -- the Commander plugin
5455
* ``debugger`` -- the Debugger plugin

Diff for: bracketcolors/AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Asif Amin <[email protected]>

Diff for: bracketcolors/COPYING

+339
Large diffs are not rendered by default.

Diff for: bracketcolors/ChangeLog

Whitespace-only changes.

Diff for: 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

Diff for: bracketcolors/NEWS

Whitespace-only changes.

Diff for: 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, often referred as "rainbow brackets".
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 <[email protected]>

Diff for: bracketcolors/src/BracketMap.cc

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* BracketMap.cc
3+
*
4+
* Copyright 2023 Asif Amin <[email protected]>
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+
std::set<BracketMap::Index> BracketMap::ComputeOrder()
72+
/*
73+
74+
----------------------------------------------------------------------------- */
75+
{
76+
std::stack<Index> orderStack;
77+
std::set<Index> updatedBrackets;
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+
Order newOrder = orderStack.size() - 1;
110+
Order currOrder = GetOrder(bracket);
111+
if (newOrder != currOrder) {
112+
updatedBrackets.insert(startIndex);
113+
}
114+
115+
GetOrder(bracket) = newOrder;
116+
}
117+
118+
return updatedBrackets;
119+
}

Diff for: bracketcolors/src/BracketMap.h

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* BracketMap.h
3+
*
4+
* Copyright 2023 Asif Amin <[email protected]>
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+
#include <set>
27+
28+
#include <glib.h>
29+
30+
31+
// -----------------------------------------------------------------------------
32+
struct BracketMap
33+
/*
34+
Purpose: data structure which stores and computes nesting order
35+
----------------------------------------------------------------------------- */
36+
{
37+
typedef gint Length, Order, Index;
38+
typedef std::tuple<Length, Order> Bracket;
39+
std::map<Index, Bracket> mBracketMap;
40+
41+
BracketMap();
42+
~BracketMap();
43+
44+
void Update(Index index, Length length);
45+
std::set<Index> ComputeOrder();
46+
47+
static const gint UNDEFINED = -1;
48+
49+
static Length& GetLength(Bracket &bracket) {
50+
return std::get<0>(bracket);
51+
}
52+
static Order& GetOrder(Bracket &bracket) {
53+
return std::get<1>(bracket);
54+
}
55+
56+
static const Length& GetLength(const Bracket &bracket) {
57+
return std::get<0>(bracket);
58+
}
59+
static const Order& GetOrder(const Bracket &bracket) {
60+
return std::get<1>(bracket);
61+
}
62+
};
63+
64+
#endif

Diff for: 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)