cocalc/src / webapp-lib / jupyter / components / codemirror / addon / search / matchesonscrollbar.js
50675 views// CodeMirror, copyright (c) by Marijn Haverbeke and others1// Distributed under an MIT license: http://codemirror.net/LICENSE23(function(mod) {4if (typeof exports == "object" && typeof module == "object") // CommonJS5mod(require("../../lib/codemirror"), require("./searchcursor"), require("../scroll/annotatescrollbar"));6else if (typeof define == "function" && define.amd) // AMD7define(["../../lib/codemirror", "./searchcursor", "../scroll/annotatescrollbar"], mod);8else // Plain browser env9mod(CodeMirror);10})(function(CodeMirror) {11"use strict";1213CodeMirror.defineExtension("showMatchesOnScrollbar", function(query, caseFold, className) {14return new SearchAnnotation(this, query, caseFold, className);15});1617function SearchAnnotation(cm, query, caseFold, className) {18this.cm = cm;19this.annotation = cm.annotateScrollbar(className || "CodeMirror-search-match");20this.query = query;21this.caseFold = caseFold;22this.gap = {from: cm.firstLine(), to: cm.lastLine() + 1};23this.matches = [];24this.update = null;2526this.findMatches();27this.annotation.update(this.matches);2829var self = this;30cm.on("change", this.changeHandler = function(_cm, change) { self.onChange(change); });31}3233var MAX_MATCHES = 1000;3435SearchAnnotation.prototype.findMatches = function() {36if (!this.gap) return;37for (var i = 0; i < this.matches.length; i++) {38var match = this.matches[i];39if (match.from.line >= this.gap.to) break;40if (match.to.line >= this.gap.from) this.matches.splice(i--, 1);41}42var cursor = this.cm.getSearchCursor(this.query, CodeMirror.Pos(this.gap.from, 0), this.caseFold);43while (cursor.findNext()) {44var match = {from: cursor.from(), to: cursor.to()};45if (match.from.line >= this.gap.to) break;46this.matches.splice(i++, 0, match);47if (this.matches.length > MAX_MATCHES) break;48}49this.gap = null;50};5152function offsetLine(line, changeStart, sizeChange) {53if (line <= changeStart) return line;54return Math.max(changeStart, line + sizeChange);55}5657SearchAnnotation.prototype.onChange = function(change) {58var startLine = change.from.line;59var endLine = CodeMirror.changeEnd(change).line;60var sizeChange = endLine - change.to.line;61if (this.gap) {62this.gap.from = Math.min(offsetLine(this.gap.from, startLine, sizeChange), change.from.line);63this.gap.to = Math.max(offsetLine(this.gap.to, startLine, sizeChange), change.from.line);64} else {65this.gap = {from: change.from.line, to: endLine + 1};66}6768if (sizeChange) for (var i = 0; i < this.matches.length; i++) {69var match = this.matches[i];70var newFrom = offsetLine(match.from.line, startLine, sizeChange);71if (newFrom != match.from.line) match.from = CodeMirror.Pos(newFrom, match.from.ch);72var newTo = offsetLine(match.to.line, startLine, sizeChange);73if (newTo != match.to.line) match.to = CodeMirror.Pos(newTo, match.to.ch);74}75clearTimeout(this.update);76var self = this;77this.update = setTimeout(function() { self.updateAfterChange(); }, 250);78};7980SearchAnnotation.prototype.updateAfterChange = function() {81this.findMatches();82this.annotation.update(this.matches);83};8485SearchAnnotation.prototype.clear = function() {86this.cm.off("change", this.changeHandler);87this.annotation.clear();88};89});909192