Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / source_frame / CodeMirrorDictionary.js
blob96ee2b55a822f5d53dafa682dfc32f59cd556ada
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6 * @constructor
7 * @param {!CodeMirror} codeMirror
8 * @param {string=} additionalWordChars
9 */
10 WebInspector.CodeMirrorDictionary = function(codeMirror, additionalWordChars)
12 this._codeMirror = codeMirror;
13 this._additionalWordChars = new Set(/** @type {!Iterable} */ (additionalWordChars));
14 this._dictionary = new WebInspector.TextDictionary();
15 this._addText(this._codeMirror.getValue());
17 this._changes = this._changes.bind(this);
18 this._beforeChange = this._beforeChange.bind(this);
19 this._codeMirror.on("beforeChange", this._beforeChange);
20 this._codeMirror.on("changes", this._changes);
23 WebInspector.CodeMirrorDictionary.prototype = {
24 /**
25 * @param {!CodeMirror} codeMirror
26 * @param {!CodeMirror.BeforeChangeObject} changeObject
28 _beforeChange: function(codeMirror, changeObject)
30 this._updatedLines = this._updatedLines || {};
31 for (var i = changeObject.from.line; i <= changeObject.to.line; ++i)
32 this._updatedLines[i] = this._codeMirror.getLine(i);
35 /**
36 * @param {!CodeMirror} codeMirror
37 * @param {!Array.<!CodeMirror.ChangeObject>} changes
39 _changes: function(codeMirror, changes)
41 if (!changes.length || !this._updatedLines)
42 return;
44 for (var lineNumber in this._updatedLines)
45 this._removeText(this._updatedLines[lineNumber]);
46 delete this._updatedLines;
48 var linesToUpdate = {};
49 for (var changeIndex = 0; changeIndex < changes.length; ++changeIndex) {
50 var changeObject = changes[changeIndex];
51 var editInfo = WebInspector.CodeMirrorUtils.changeObjectToEditOperation(changeObject);
52 for (var i = editInfo.newRange.startLine; i <= editInfo.newRange.endLine; ++i)
53 linesToUpdate[i] = this._codeMirror.getLine(i);
55 for (var lineNumber in linesToUpdate)
56 this._addText(linesToUpdate[lineNumber]);
59 /**
60 * @param {string} word
61 * @return {boolean}
63 _validWord: function(word)
65 return !!word.length && (word[0] < '0' || word[0] > '9');
68 /**
69 * @param {string} text
71 _addText: function(text)
73 WebInspector.TextUtils.textToWords(text, this.isWordChar.bind(this), addWord.bind(this));
75 /**
76 * @param {string} word
77 * @this {WebInspector.CodeMirrorDictionary}
79 function addWord(word)
81 if (this._validWord(word))
82 this._dictionary.addWord(word);
86 /**
87 * @param {string} text
89 _removeText: function(text)
91 WebInspector.TextUtils.textToWords(text, this.isWordChar.bind(this), removeWord.bind(this));
93 /**
94 * @param {string} word
95 * @this {WebInspector.CodeMirrorDictionary}
97 function removeWord(word)
99 if (this._validWord(word))
100 this._dictionary.removeWord(word);
105 * @param {string} char
106 * @return {boolean}
108 isWordChar: function(char)
110 return WebInspector.TextUtils.isWordChar(char) || this._additionalWordChars.has(char);
114 * @param {string} prefix
115 * @return {!Array.<string>}
117 wordsWithPrefix: function(prefix)
119 return this._dictionary.wordsWithPrefix(prefix);
123 * @param {string} word
124 * @return {boolean}
126 hasWord: function(word)
128 return this._dictionary.hasWord(word);
132 * @param {string} word
133 * @return {number}
135 wordCount: function(word)
137 return this._dictionary.wordCount(word);
140 dispose: function()
142 this._codeMirror.off("beforeChange", this._beforeChange);
143 this._codeMirror.off("changes", this._changes);
144 this._dictionary.reset();