New plugin: Pair Tag Highlighter #99

Merged
merged 1 commit into from Apr 6, 2013

Conversation

Projects
None yet
3 participants
Contributor

vmkononenko commented Apr 4, 2013

The plugin finds and highlights matching opening/closing
HTML tag by clicking or moving cursor inside a tag.

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
@@ -0,0 +1,347 @@
+/*
+ * Pair Tag Highlighter
+ *
+ * highlights matching opening/closing HTML tags
+ *
+ * Author: Volodymyr Kononenko aka kvm
+ * Email: vm@kononenko.ws
+ *
+ */
+
+#include <geanyplugin.h>
+#include "Scintilla.h" // for the SCNotification struct
@b4n

b4n Apr 4, 2013

Owner

We prefer C89 comments (/* ... */) rather than C++ comments (// ...)

Owner

b4n commented Apr 4, 2013

And you need to update root Makefile.am too (sorry if I forgot to mention it before)

diff --git a/Makefile.am b/Makefile.am
index 3c1d40b..f81fe5d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -94,6 +94,10 @@ if ENABLE_MULTITERM
 SUBDIRS += multiterm
 endif

+if ENABLE_PAIRTAGHIGHLIGHTER
+SUBDIRS += pairtaghighlighter
+endif
+
 if ENABLE_POHELPER
 SUBDIRS += pohelper
 endif

@b4n b4n commented on the diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+#define MAX_TAG_NAME 64
+
+/* These items are set by Geany before plugin_init() is called. */
+GeanyPlugin *geany_plugin;
+GeanyData *geany_data;
+GeanyFunctions *geany_functions;
+
+static ScintillaObject *sci;
+
+/* Is needed for clearing highlighting after moving cursor out
+ * from the tag */
+static gint highlightedBrackets[] = {0, 0, 0, 0};
+
+PLUGIN_VERSION_CHECK(211)
+
+PLUGIN_SET_TRANSLATABLE_INFO(LOCALEDIR, GETTEXT_PACKAGE, _("Pair Tag Highlighter"),
@b4n

b4n Apr 4, 2013

Owner

pair_tag_highlighter.c:33:1: error:GETTEXT_PACKAGE' undeclared (first use in this function)You need to#include "config.h"`

And try build your plugin with the build system, please ;)

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+/* These items are set by Geany before plugin_init() is called. */
+GeanyPlugin *geany_plugin;
+GeanyData *geany_data;
+GeanyFunctions *geany_functions;
+
+static ScintillaObject *sci;
+
+/* Is needed for clearing highlighting after moving cursor out
+ * from the tag */
+static gint highlightedBrackets[] = {0, 0, 0, 0};
+
+PLUGIN_VERSION_CHECK(211)
+
+PLUGIN_SET_TRANSLATABLE_INFO(LOCALEDIR, GETTEXT_PACKAGE, _("Pair Tag Highlighter"),
+ _("Finds and highlights matching opening/closing HTML tag"),
+ "1.0", "Volodymyr Kononenko <vm@kononenko.ws>");
@b4n

b4n Apr 4, 2013

Owner

pair_tag_highlighter.c:35:76: warning: ISO C does not allow extra;' outside of a function [-pedantic]`
No need for a semicolon here.

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+ scintilla_send_message(sci, SCI_INDICATORCLEARRANGE, rangeStart, rangeEnd+1);
+}
+
+
+static gboolean is_tag_self_closing(gint closingBracket)
+{
+ gboolean isTagSelfClosing = FALSE;
+ gchar charBeforeBracket = sci_get_char_at(sci, closingBracket-1);
+
+ if('/' == charBeforeBracket)
+ isTagSelfClosing = TRUE;
+ return isTagSelfClosing;
+}
+
+
+static gboolean is_tag_opening(openingBracket)
@b4n

b4n Apr 4, 2013

Owner

pair_tag_highlighter.c:115:17: warning: function declaration isn't a prototype [-Wstrict-prototypes]
The type of the openingBracket argument is missing.

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+ sci_get_text_range(sci, nameStart, nameEnd-1, tagName);
+}
+
+
+static void findMatchingOpeningTag(gchar *tagName, gint openingBracket)
+{
+ gint pos;
+ gint openingTagsCount = 0;
+ gint closingTagsCount = 1;
+
+ for(pos=openingBracket; pos>0; pos--)
+ {
+ /* are we inside tag? */
+ gint lineNumber = sci_get_line_from_position(sci, pos);
+ gint lineStart = sci_get_position_from_line(sci, lineNumber);
+ gint matchingOpeningBracket = findBracket(pos, lineStart, '<', NULL, FALSE);
@b4n

b4n Apr 4, 2013

Owner

pair_tag_highlighter.c:156:9: warning: passing argument 4 offindBracket' makes integer from pointer without a cast [enabled by default]pair_tag_highlighter.c:44:13: note: expected gchar' but argument is of typevoid 'NULLisn't a valid value for agchar:gcharis a byte, isn't notgchar(pointer to byte(s)). You probably want0(or'\0'`)

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+}
+
+
+static void findMatchingOpeningTag(gchar *tagName, gint openingBracket)
+{
+ gint pos;
+ gint openingTagsCount = 0;
+ gint closingTagsCount = 1;
+
+ for(pos=openingBracket; pos>0; pos--)
+ {
+ /* are we inside tag? */
+ gint lineNumber = sci_get_line_from_position(sci, pos);
+ gint lineStart = sci_get_position_from_line(sci, lineNumber);
+ gint matchingOpeningBracket = findBracket(pos, lineStart, '<', NULL, FALSE);
+ gint matchingClosingBracket = findBracket(pos, lineStart, '>', NULL, FALSE);
@b4n

b4n Apr 4, 2013

Owner

Same here

@b4n b4n commented on the diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+ {
+ /* are we inside tag? */
+ gint lineNumber = sci_get_line_from_position(sci, pos);
+ gint lineStart = sci_get_position_from_line(sci, lineNumber);
+ gint matchingOpeningBracket = findBracket(pos, lineStart, '<', NULL, FALSE);
+ gint matchingClosingBracket = findBracket(pos, lineStart, '>', NULL, FALSE);
+
+ if(-1 != matchingOpeningBracket && -1 != matchingClosingBracket
+ && (matchingClosingBracket > matchingOpeningBracket))
+ {
+ /* we are inside of some tag. Let us check what tag*/
+ gchar matchingTagName[MAX_TAG_NAME];
+ gboolean isMatchingTagOpening = is_tag_opening(matchingOpeningBracket);
+ get_tag_name(matchingOpeningBracket, matchingClosingBracket,
+ matchingTagName, isMatchingTagOpening);
+ if(strcmp(tagName, matchingTagName) == 0)
@b4n

b4n Apr 4, 2013

Owner

pair_tag_highlighter.c:167:13: error: implicit declaration of functionstrcmp' [-Werror=implicit-function-declaration]Please don't forget to#include <string.h>`

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+
+
+static void findMatchingClosingTag(gchar *tagName, gint closingBracket)
+{
+ gint pos;
+ gint linesInDocument = sci_get_line_count(sci);
+ gint endOfDocument = sci_get_position_from_line(sci, linesInDocument);
+ gint openingTagsCount = 1;
+ gint closingTagsCount = 0;
+
+ for(pos=closingBracket; pos<endOfDocument; pos++)
+ {
+ /* are we inside tag? */
+ gint lineNumber = sci_get_line_from_position(sci, pos);
+ gint lineEnd = sci_get_line_end_position(sci, lineNumber);
+ gint matchingOpeningBracket = findBracket(pos, endOfDocument, '<', NULL, TRUE);
@b4n

b4n Apr 4, 2013

Owner

Same as above

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+
+static void findMatchingClosingTag(gchar *tagName, gint closingBracket)
+{
+ gint pos;
+ gint linesInDocument = sci_get_line_count(sci);
+ gint endOfDocument = sci_get_position_from_line(sci, linesInDocument);
+ gint openingTagsCount = 1;
+ gint closingTagsCount = 0;
+
+ for(pos=closingBracket; pos<endOfDocument; pos++)
+ {
+ /* are we inside tag? */
+ gint lineNumber = sci_get_line_from_position(sci, pos);
+ gint lineEnd = sci_get_line_end_position(sci, lineNumber);
+ gint matchingOpeningBracket = findBracket(pos, endOfDocument, '<', NULL, TRUE);
+ gint matchingClosingBracket = findBracket(pos, endOfDocument, '>', NULL, TRUE);
@b4n

b4n Apr 4, 2013

Owner

And here too

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+ pos = lineEnd;
+ continue;
+ }
+ if(openingTagsCount == closingTagsCount)
+ {
+ /* matching tag is found */
+ highlight_tag(matchingOpeningBracket, matchingClosingBracket);
+ highlightedBrackets[2] = matchingOpeningBracket;
+ highlightedBrackets[3] = matchingClosingBracket;
+ break;
+ }
+ }
+}
+
+
+static void findMatchingTag(openingBracket, closingBracket)
@b4n

b4n Apr 4, 2013

Owner
pair_tag_highlighter.c:249:13: warning: function declaration isn't a prototype [-Wstrict-prototypes]
pair_tag_highlighter.c: In function `findMatchingTag':
pair_tag_highlighter.c:249:13: warning: type of `openingBracket' defaults to `int' [-Wmissing-parameter-type]
pair_tag_highlighter.c:249:13: warning: type of `closingBracket' defaults to `int' [-Wmissing-parameter-type]

Argument types missing

@b4n b4n commented on an outdated diff Apr 4, 2013

pairtaghighlighter/src/pair_tag_highlighter.c
+
+
+static void run_tag_highlighter(void)
+{
+ gint position = sci_get_current_position(sci);
+ gint lineNumber = sci_get_current_line(sci);
+ gint lineStart = sci_get_position_from_line(sci, lineNumber);
+ gint lineEnd = sci_get_line_end_position(sci, lineNumber);
+ gint openingBracket = findBracket(position, lineStart, '<', '>', FALSE);
+ gint closingBracket = findBracket(position, lineEnd, '>', '<', TRUE);
+
+ if(-1 == openingBracket || -1 == closingBracket)
+ {
+ clear_previous_highlighting(highlightedBrackets[0], highlightedBrackets[1]);
+ clear_previous_highlighting(highlightedBrackets[2], highlightedBrackets[3]);
+ int i;
@b4n

b4n Apr 4, 2013

Owner

pair_tag_highlighter.c:275:9: warning: ISO C90 forbids mixed declarations and code [-pedantic]
We try to follow C89/C90 whenever possible, so you might want to move this to the start of the block (e.g. between lines 272 and 273)

@vmkononenko vmkononenko New plugin: Pair Tag Highlighter
The plugin finds and highlights matching opening/closing
HTML tag by clicking or moving cursor inside a tag.
f81769e

frlan merged commit d1d8425 into geany:master Apr 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment