| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @constructor | 6 * @constructor |
| 7 * @implements {WebInspector.SuggestBoxDelegate} | 7 * @implements {WebInspector.SuggestBoxDelegate} |
| 8 * @param {!WebInspector.CodeMirrorTextEditor} textEditor | 8 * @param {!WebInspector.CodeMirrorTextEditor} textEditor |
| 9 * @param {!CodeMirror} codeMirror | 9 * @param {!CodeMirror} codeMirror |
| 10 * @param {!WebInspector.AutocompleteConfig} config | 10 * @param {!WebInspector.AutocompleteConfig} config |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 */ | 118 */ |
| 119 _wordsWithPrefix: function(prefixRange, substituteRange) | 119 _wordsWithPrefix: function(prefixRange, substituteRange) |
| 120 { | 120 { |
| 121 var external = this._config.suggestionsCallback ? this._config.suggestio
nsCallback(prefixRange, substituteRange) : null; | 121 var external = this._config.suggestionsCallback ? this._config.suggestio
nsCallback(prefixRange, substituteRange) : null; |
| 122 if (external) | 122 if (external) |
| 123 return external; | 123 return external; |
| 124 | 124 |
| 125 if (!this._dictionary || prefixRange.startColumn === prefixRange.endColu
mn) | 125 if (!this._dictionary || prefixRange.startColumn === prefixRange.endColu
mn) |
| 126 return Promise.resolve([]); | 126 return Promise.resolve([]); |
| 127 | 127 |
| 128 var completions = this._dictionary.wordsWithPrefix(this._textEditor.copy
Range(prefixRange)); | 128 var completions = this._dictionary.wordsWithPrefix(this._textEditor.text
(prefixRange)); |
| 129 var substituteWord = this._textEditor.copyRange(substituteRange); | 129 var substituteWord = this._textEditor.text(substituteRange); |
| 130 if (this._dictionary.wordCount(substituteWord) === 1) | 130 if (this._dictionary.wordCount(substituteWord) === 1) |
| 131 completions = completions.filter((word) => word !== substituteWord); | 131 completions = completions.filter((word) => word !== substituteWord); |
| 132 | 132 |
| 133 completions.sort((a, b) => this._dictionary.wordCount(b) - this._diction
ary.wordCount(a) || a.length - b.length); | 133 completions.sort((a, b) => this._dictionary.wordCount(b) - this._diction
ary.wordCount(a) || a.length - b.length); |
| 134 return Promise.resolve(completions.map(item => ({ title: item }))); | 134 return Promise.resolve(completions.map(item => ({ title: item }))); |
| 135 }, | 135 }, |
| 136 | 136 |
| 137 /** | 137 /** |
| 138 * @param {!CodeMirror} codeMirror | 138 * @param {!CodeMirror} codeMirror |
| 139 * @param {!Array.<!CodeMirror.ChangeObject>} changes | 139 * @param {!Array.<!CodeMirror.ChangeObject>} changes |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 202 |
| 203 /** | 203 /** |
| 204 * @param {!WebInspector.TextRange} mainSelection | 204 * @param {!WebInspector.TextRange} mainSelection |
| 205 * @return {boolean} | 205 * @return {boolean} |
| 206 */ | 206 */ |
| 207 _validateSelectionsContexts: function(mainSelection) | 207 _validateSelectionsContexts: function(mainSelection) |
| 208 { | 208 { |
| 209 var selections = this._codeMirror.listSelections(); | 209 var selections = this._codeMirror.listSelections(); |
| 210 if (selections.length <= 1) | 210 if (selections.length <= 1) |
| 211 return true; | 211 return true; |
| 212 var mainSelectionContext = this._textEditor.copyRange(mainSelection); | 212 var mainSelectionContext = this._textEditor.text(mainSelection); |
| 213 for (var i = 0; i < selections.length; ++i) { | 213 for (var i = 0; i < selections.length; ++i) { |
| 214 var wordRange = this._substituteRange(selections[i].head.line, selec
tions[i].head.ch); | 214 var wordRange = this._substituteRange(selections[i].head.line, selec
tions[i].head.ch); |
| 215 if (!wordRange) | 215 if (!wordRange) |
| 216 return false; | 216 return false; |
| 217 var context = this._textEditor.copyRange(wordRange); | 217 var context = this._textEditor.text(wordRange); |
| 218 if (context !== mainSelectionContext) | 218 if (context !== mainSelectionContext) |
| 219 return false; | 219 return false; |
| 220 } | 220 } |
| 221 return true; | 221 return true; |
| 222 }, | 222 }, |
| 223 | 223 |
| 224 autocomplete: function() | 224 autocomplete: function() |
| 225 { | 225 { |
| 226 this._initializeIfNeeded(); | 226 this._initializeIfNeeded(); |
| 227 if (this._codeMirror.somethingSelected()) { | 227 if (this._codeMirror.somethingSelected()) { |
| 228 this.finishAutocomplete(); | 228 this.finishAutocomplete(); |
| 229 return; | 229 return; |
| 230 } | 230 } |
| 231 | 231 |
| 232 var cursor = this._codeMirror.getCursor("head"); | 232 var cursor = this._codeMirror.getCursor("head"); |
| 233 var substituteRange = this._substituteRange(cursor.line, cursor.ch); | 233 var substituteRange = this._substituteRange(cursor.line, cursor.ch); |
| 234 if (!substituteRange || !this._validateSelectionsContexts(substituteRang
e)) { | 234 if (!substituteRange || !this._validateSelectionsContexts(substituteRang
e)) { |
| 235 this.finishAutocomplete(); | 235 this.finishAutocomplete(); |
| 236 return; | 236 return; |
| 237 } | 237 } |
| 238 | 238 |
| 239 var prefixRange = substituteRange.clone(); | 239 var prefixRange = substituteRange.clone(); |
| 240 prefixRange.endColumn = cursor.ch; | 240 prefixRange.endColumn = cursor.ch; |
| 241 var prefix = this._textEditor.copyRange(prefixRange); | 241 var prefix = this._textEditor.text(prefixRange); |
| 242 var hadSuggestBox = false; | 242 var hadSuggestBox = false; |
| 243 if (this._suggestBox) | 243 if (this._suggestBox) |
| 244 hadSuggestBox = true; | 244 hadSuggestBox = true; |
| 245 | 245 |
| 246 this._wordsWithPrefix(prefixRange, substituteRange).then(wordsAcquired.b
ind(this)); | 246 this._wordsWithPrefix(prefixRange, substituteRange).then(wordsAcquired.b
ind(this)); |
| 247 | 247 |
| 248 /** | 248 /** |
| 249 * @param {!WebInspector.SuggestBox.Suggestions} wordsWithPrefix | 249 * @param {!WebInspector.SuggestBox.Suggestions} wordsWithPrefix |
| 250 * @this {WebInspector.TextEditorAutocompleteController} | 250 * @this {WebInspector.TextEditorAutocompleteController} |
| 251 */ | 251 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 270 }, | 270 }, |
| 271 | 271 |
| 272 /** | 272 /** |
| 273 * @param {string} hint | 273 * @param {string} hint |
| 274 */ | 274 */ |
| 275 _addHintMarker: function(hint) | 275 _addHintMarker: function(hint) |
| 276 { | 276 { |
| 277 this._clearHintMarker(); | 277 this._clearHintMarker(); |
| 278 if (!this._isCursorAtEndOfLine()) | 278 if (!this._isCursorAtEndOfLine()) |
| 279 return; | 279 return; |
| 280 var prefix = this._textEditor.copyRange(this._prefixRange); | 280 var prefix = this._textEditor.text(this._prefixRange); |
| 281 this._lastPrefix = prefix; | 281 this._lastPrefix = prefix; |
| 282 this._hintElement.textContent = hint.substring(prefix.length).split("\n"
)[0]; | 282 this._hintElement.textContent = hint.substring(prefix.length).split("\n"
)[0]; |
| 283 var cursor = this._codeMirror.getCursor("to"); | 283 var cursor = this._codeMirror.getCursor("to"); |
| 284 this._hintMarker = this._textEditor.addBookmark(cursor.line, cursor.ch,
this._hintElement, WebInspector.TextEditorAutocompleteController.HintBookmark, t
rue); | 284 this._hintMarker = this._textEditor.addBookmark(cursor.line, cursor.ch,
this._hintElement, WebInspector.TextEditorAutocompleteController.HintBookmark, t
rue); |
| 285 }, | 285 }, |
| 286 | 286 |
| 287 _clearHintMarker: function() | 287 _clearHintMarker: function() |
| 288 { | 288 { |
| 289 if (!this._hintMarker) | 289 if (!this._hintMarker) |
| 290 return; | 290 return; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 } | 414 } |
| 415 | 415 |
| 416 /** | 416 /** |
| 417 * @typedef {{ | 417 * @typedef {{ |
| 418 * substituteRangeCallback: ((function(number, number):?WebInspector.TextRan
ge)|undefined), | 418 * substituteRangeCallback: ((function(number, number):?WebInspector.TextRan
ge)|undefined), |
| 419 * suggestionsCallback: ((function(!WebInspector.TextRange, !WebInspector.Te
xtRange):?Promise.<!WebInspector.SuggestBox.Suggestions>)|undefined), | 419 * suggestionsCallback: ((function(!WebInspector.TextRange, !WebInspector.Te
xtRange):?Promise.<!WebInspector.SuggestBox.Suggestions>)|undefined), |
| 420 * isWordChar: ((function(string):boolean)|undefined) | 420 * isWordChar: ((function(string):boolean)|undefined) |
| 421 * }} | 421 * }} |
| 422 **/ | 422 **/ |
| 423 WebInspector.AutocompleteConfig; | 423 WebInspector.AutocompleteConfig; |
| OLD | NEW |