Skip to content

Commit 50ae403

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

19 files changed

+2902
-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

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
void BracketMap::ComputeOrder()
72+
/*
73+
74+
----------------------------------------------------------------------------- */
75+
{
76+
std::stack<Index> orderStack;
77+
78+
for (auto &it : mBracketMap) {
79+
80+
const Index &startIndex = it.first;
81+
Bracket &bracket = it.second;
82+
Length length = GetLength(bracket);
83+
Index endPos = startIndex + length;
84+
85+
if (length == UNDEFINED) {
86+
// Invalid brackets
87+
GetOrder(bracket) = UNDEFINED;
88+
continue;
89+
}
90+
91+
92+
if (orderStack.size() == 0) {
93+
// First bracket
94+
orderStack.push(endPos);
95+
}
96+
else if (startIndex > orderStack.top()) {
97+
// not nested
98+
while(orderStack.size() > 0 and orderStack.top() < startIndex) {
99+
orderStack.pop();
100+
}
101+
orderStack.push(endPos);
102+
}
103+
else {
104+
// nested bracket
105+
orderStack.push(endPos);
106+
}
107+
108+
GetOrder(bracket) = orderStack.size() - 1;
109+
}
110+
}

Diff for: bracketcolors/src/BracketMap.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
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 Update(Index index, Length length);
44+
void ComputeOrder();
45+
46+
static const gint UNDEFINED = -1;
47+
48+
static Length& GetLength(Bracket &bracket) {
49+
return std::get<0>(bracket);
50+
}
51+
static Order& GetOrder(Bracket &bracket) {
52+
return std::get<1>(bracket);
53+
}
54+
55+
static const Length& GetLength(const Bracket &bracket) {
56+
return std::get<0>(bracket);
57+
}
58+
static const Order& GetOrder(const Bracket &bracket) {
59+
return std::get<1>(bracket);
60+
}
61+
};
62+
63+
#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)