CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Exclamation Codemirror highlightSelectionMatches count

    I am wanting to get the count for the matches it finds per work that the cursor is over. Currently this works fine for the standard code that's being used but it highlights words that do not repeat (aka have a match)

    for simplicity sake, I set up a fiddle so that It's easier to show you what I am talking about above.

    Any help would be great as I have been poking at the code for a few hours now and haven't been able to find any function that houses the match found(s).

    match code:
    Code:
    (function(mod) {
      if (typeof exports == "object" && typeof module == "object") // CommonJS
        mod(require("../../lib/codemirror"));
      else if (typeof define == "function" && define.amd) // AMD
        define(["../../lib/codemirror"], mod);
      else // Plain browser env
        mod(CodeMirror);
    })(function(CodeMirror) {
      "use strict";
    
      var DEFAULT_MIN_CHARS = 2;
      var DEFAULT_TOKEN_STYLE = "matchhighlight";
      var DEFAULT_DELAY = 100;
      var DEFAULT_WORDS_ONLY = false;
    
      function State(options) {
        if (typeof options == "object") {
          this.minChars = options.minChars;
          this.style = options.style;
          this.showToken = options.showToken;
          this.delay = options.delay;
          this.wordsOnly = options.wordsOnly;
        }
        if (this.style == null) this.style = DEFAULT_TOKEN_STYLE;
        if (this.minChars == null) this.minChars = DEFAULT_MIN_CHARS;
        if (this.delay == null) this.delay = DEFAULT_DELAY;
        if (this.wordsOnly == null) this.wordsOnly = DEFAULT_WORDS_ONLY;
        this.overlay = this.timeout = null;
      }
    
      CodeMirror.defineOption("highlightSelectionMatches", false, function(cm, val, old) {
        if (old && old != CodeMirror.Init) {
          var over = cm.state.matchHighlighter.overlay;
          if (over) cm.removeOverlay(over);
          clearTimeout(cm.state.matchHighlighter.timeout);
          cm.state.matchHighlighter = null;
          cm.off("cursorActivity", cursorActivity);
        }
        if (val) {
          cm.state.matchHighlighter = new State(val);
          highlightMatches(cm);
          cm.on("cursorActivity", cursorActivity);
        }
      });
    
      function cursorActivity(cm) {
        var state = cm.state.matchHighlighter;
        clearTimeout(state.timeout);
        state.timeout = setTimeout(function() {highlightMatches(cm);}, state.delay);
      }
    
      function highlightMatches(cm) {
        cm.operation(function() {
          var state = cm.state.matchHighlighter;
          if (state.overlay) {
            cm.removeOverlay(state.overlay);
            state.overlay = null;
          }
          if (!cm.somethingSelected() && state.showToken) {
            var re = state.showToken === true ? /[\w$]/ : state.showToken;
            var cur = cm.getCursor(), line = cm.getLine(cur.line), start = cur.ch, end = start;
            while (start && re.test(line.charAt(start - 1))) --start;
            while (end < line.length && re.test(line.charAt(end))) ++end;
            if (start < end)
              cm.addOverlay(state.overlay = makeOverlay(line.slice(start, end), re, state.style));
            return;
          }
          var from = cm.getCursor("from"), to = cm.getCursor("to");
          if (from.line != to.line) return;
          if (state.wordsOnly && !isWord(cm, from, to)) return;
          var selection = cm.getRange(from, to).replace(/^\s+|\s+$/g, "");
          if (selection.length >= state.minChars)
            cm.addOverlay(state.overlay = makeOverlay(selection, false, state.style));
        });
      }
    
      function isWord(cm, from, to) {
        var str = cm.getRange(from, to);
        if (str.match(/^\w+$/) !== null) {
            if (from.ch > 0) {
                var pos = {line: from.line, ch: from.ch - 1};
                var chr = cm.getRange(pos, from);
                if (chr.match(/\W/) === null) return false;
            }
            if (to.ch < cm.getLine(from.line).length) {
                var pos = {line: to.line, ch: to.ch + 1};
                var chr = cm.getRange(to, pos);
                if (chr.match(/\W/) === null) return false;
            }
            return true;
        } else return false;
      }
    
      function boundariesAround(stream, re) {
        return (!stream.start || !re.test(stream.string.charAt(stream.start - 1))) &&
          (stream.pos == stream.string.length || !re.test(stream.string.charAt(stream.pos)));
      }
    
      function makeOverlay(query, hasBoundary, style) {
        return {token: function(stream) {
          if (stream.match(query) &&
              (!hasBoundary || boundariesAround(stream, hasBoundary)))
            return style;
          stream.next();
          stream.skipTo(query.charAt(0)) || stream.skipToEnd();
        }};
      }
    });

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Codemirror highlightSelectionMatches count

    CodeMirror is massively extensive. If you want to deal with it, I highly suggest you talk directly to the CodeMirror Community.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured