Skip to content

Commit

Permalink
Bracket Colors
Browse files Browse the repository at this point in the history
Add bracketcolors plugin. Color {}, [], () based on nesting order

Closes #1148
  • Loading branch information
asifamin13 committed Mar 10, 2023
1 parent 2665511 commit 1e9a8b0
Show file tree
Hide file tree
Showing 25 changed files with 3,156 additions and 593 deletions.
7 changes: 7 additions & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ M: Pavel Roschin <rpg89(at)post(dot)ru>
W:
S: Maintained

bracketcolors
* P: Asif Amin <[email protected]>
* g: @asifamin13
* M: Asif Amin <[email protected]>
* W:
* S: Maintained

codenav
P: Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
g:
Expand Down
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ if ENABLE_AUTOMARK
SUBDIRS += automark
endif

if ENABLE_BRACKETCOLORS
SUBDIRS += bracketcolors
endif

if ENABLE_CODENAV
SUBDIRS += codenav
endif
Expand Down
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Available plugins are:
* ``addons`` -- the Addons plugin
* ``autoclose`` -- the Autoclose plugin
* ``automark`` -- the Automark plugin
* ``bracketcolors`` -- the BracketColors plugin
* ``codenav`` -- the CodeNav plugin
* ``commander`` -- the Commander plugin
* ``debugger`` -- the Debugger plugin
Expand Down
1 change: 1 addition & 0 deletions bracketcolors/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Asif Amin <[email protected]>
339 changes: 339 additions & 0 deletions bracketcolors/COPYING

Large diffs are not rendered by default.

Empty file added bracketcolors/ChangeLog
Empty file.
4 changes: 4 additions & 0 deletions bracketcolors/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include $(top_srcdir)/build/vars.auxfiles.mk

SUBDIRS = src
plugin = bracketcolors
Empty file added bracketcolors/NEWS
Empty file.
32 changes: 32 additions & 0 deletions bracketcolors/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Color brackets, parenthesis, and braces
=======================================

.. contents::

About
-----

This plugin enables bracket coloring features. Brackets are colored based on
nesting level, often referred as "rainbow brackets".

Features
--------

* Color brackets for: { }, [ ], ( )

Usage
-----

Install the plugin (https://plugins.geany.org/install.html) then
load it in Geany's plugin manager.

Requirements
------------

* Geany >= 1.38
* C++17

Contact developers
------------------

Asif Amin <[email protected]>
110 changes: 110 additions & 0 deletions bracketcolors/src/BracketMap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* BracketMap.cc
*
* Copyright 2023 Asif Amin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stack>
#include <set>

#include "BracketMap.h"


// -----------------------------------------------------------------------------
BracketMap::BracketMap()
/*
Constructor
----------------------------------------------------------------------------- */
{

}


// -----------------------------------------------------------------------------
BracketMap::~BracketMap()
/*
Destructor
----------------------------------------------------------------------------- */
{

}


// -----------------------------------------------------------------------------
void BracketMap::Update(Index index, Length length)
/*
----------------------------------------------------------------------------- */
{
auto it = mBracketMap.find(index);
if (it != mBracketMap.end()) {
auto &bracket = it->second;
GetLength(bracket) = length;
}
else {
mBracketMap.insert(
std::make_pair(index, std::make_tuple(length, 0))
);
}
}


// -----------------------------------------------------------------------------
void BracketMap::ComputeOrder()
/*
----------------------------------------------------------------------------- */
{
std::stack<Index> orderStack;

for (auto &it : mBracketMap) {

const Index &startIndex = it.first;
Bracket &bracket = it.second;
Length length = GetLength(bracket);
Index endPos = startIndex + length;

if (length == UNDEFINED) {
// Invalid brackets
GetOrder(bracket) = UNDEFINED;
continue;
}


if (orderStack.size() == 0) {
// First bracket
orderStack.push(endPos);
}
else if (startIndex > orderStack.top()) {
// not nested
while(orderStack.size() > 0 and orderStack.top() < startIndex) {
orderStack.pop();
}
orderStack.push(endPos);
}
else {
// nested bracket
orderStack.push(endPos);
}

GetOrder(bracket) = orderStack.size() - 1;
}
}
63 changes: 63 additions & 0 deletions bracketcolors/src/BracketMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* BracketMap.h
*
* Copyright 2023 Asif Amin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#ifndef __BRACKET_MAP_H__
#define __BRACKET_MAP_H__

#include <map>
#include <tuple>

#include <glib.h>


// -----------------------------------------------------------------------------
struct BracketMap
/*
Purpose: data structure which stores and computes nesting order
----------------------------------------------------------------------------- */
{
typedef gint Length, Order, Index;
typedef std::tuple<Length, Order> Bracket;
std::map<Index, Bracket> mBracketMap;

BracketMap();
~BracketMap();

void Update(Index index, Length length);
void ComputeOrder();

static const gint UNDEFINED = -1;

static Length& GetLength(Bracket &bracket) {
return std::get<0>(bracket);
}
static Order& GetOrder(Bracket &bracket) {
return std::get<1>(bracket);
}

static const Length& GetLength(const Bracket &bracket) {
return std::get<0>(bracket);
}
static const Order& GetOrder(const Bracket &bracket) {
return std::get<1>(bracket);
}
};

#endif
15 changes: 15 additions & 0 deletions bracketcolors/src/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include $(top_srcdir)/build/vars.build.mk
plugin = bracketcolors

geanyplugins_LTLIBRARIES = bracketcolors.la

bracketcolors_srcs = \
bracketcolors.cc \
BracketMap.cc \
BracketMap.h

bracketcolors_la_SOURCES = $(bracketcolors_srcs)
bracketcolors_la_CXXFLAGS = $(AM_CXXFLAGS) $(AM_CFLAGS) -DG_LOG_DOMAIN=\"BracketColors\"
bracketcolors_la_LIBADD = $(COMMONLIBS)

include $(top_srcdir)/build/cppcheck.mk
Loading

0 comments on commit 1e9a8b0

Please sign in to comment.