From 07fb7738db3d581e086fa01d8f6ed7697e181418 Mon Sep 17 00:00:00 2001 From: Adam Jimenez Date: Thu, 5 Jun 2014 19:56:46 +0100 Subject: [PATCH] Highlight matching HTML tag fixes #1308 --- lib/ace/editor.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index ef2d9f1a..57c6adb2 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -48,6 +48,7 @@ var EventEmitter = require("./lib/event_emitter").EventEmitter; var CommandManager = require("./commands/command_manager").CommandManager; var defaultCommands = require("./commands/default_commands").commands; var config = require("./config"); +var TokenIterator = require("./token_iterator").TokenIterator; /** * The main entry point into the Ace functionality. @@ -534,6 +535,83 @@ var Editor = function(renderer, session) { self.session.$bracketHighlight = self.session.addMarker(range, "ace_bracket", "text"); }, 50); }; + + this.$highlightTags = function() { + var session = this.session; + + if (session.$tagHighlight) { + session.removeMarker(session.$tagHighlight); + session.$tagHighlight = null; + } + + if (this.$highlightTagPending) { + return; + } + + // perform highlight async to not block the browser during navigation + var self = this; + this.$highlightTagPending = true; + setTimeout(function() { + self.$highlightTagPending = false; + + var pos = editor.getCursorPosition(); + var iterator = new TokenIterator(self.session, pos.row, pos.column); + var token = iterator.getCurrentToken(); + + if( !token || token.type.indexOf('tag-name') === -1 ){ + return; + } + + var tag = token.value; + var depth = 0; + var prevToken = iterator.stepBackward(); + + if (prevToken.value == '<'){ + //find closing tag + do { + prevToken = token; + token = iterator.stepForward(); + + if (token && token.value === tag && token.type.indexOf('tag-name') !== -1) { + if (prevToken.value==='<'){ + depth++; + } else if (prevToken.value==='=0); + }else{ + //find opening tag + do { + token = prevToken; + prevToken = iterator.stepBackward(); + + if(token && token.value === tag && token.type.indexOf('tag-name') !== -1) { + if (prevToken.value==='<') { + depth++; + } else if( prevToken.value==='