Skip to content

Commit

Permalink
Bracket Colors (#1)
Browse files Browse the repository at this point in the history
Add bracketcolors plugin

Color {}, [], () based on nesting order
  • Loading branch information
asifamin13 authored Jan 30, 2023
1 parent 2665511 commit d005a26
Show file tree
Hide file tree
Showing 17 changed files with 3,040 additions and 0 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 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

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]>
143 changes: 143 additions & 0 deletions bracketcolors/src/BracketMap.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* BracketMap.cc
*
* Copyright 2013 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;
}
}



// -----------------------------------------------------------------------------
void BracketMap::Show()
/*
----------------------------------------------------------------------------- */
{ g_debug("%s: Showing bracket map ...", __FUNCTION__);

for (const auto it : mBracketMap) {

const Index &startIndex = it.first;
const Bracket &bracket = it.second;

Length length = std::get<0>(bracket);
Order order = std::get<1>(bracket);

Index end = -1;
if (length > 0) {
end = startIndex + length;
}

g_debug(
"%s: Bracket at %d, Length: %d, End: %d, Order: %d",
__FUNCTION__, startIndex, length, end, order
);
}

g_debug("%s: ... Finished showing bracket map", __FUNCTION__);
}

76 changes: 76 additions & 0 deletions bracketcolors/src/BracketMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* BracketMap.h
*
* Copyright 2013 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 Show();

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 d005a26

Please sign in to comment.