AdvancedSearchController.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Copyright (C) 2011 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
  20. * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. */
  31. WebInspector.AdvancedSearchController = function()
  32. {
  33. this._shortcut = WebInspector.AdvancedSearchController.createShortcut();
  34. this._searchId = 0;
  35. WebInspector.settings.advancedSearchConfig = WebInspector.settings.createSetting("advancedSearchConfig", new WebInspector.SearchConfig("", true, false));
  36. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, this._frameNavigated, this);
  37. }
  38. /**
  39. * @return {!WebInspector.KeyboardShortcut.Descriptor}
  40. */
  41. WebInspector.AdvancedSearchController.createShortcut = function()
  42. {
  43. if (WebInspector.isMac())
  44. return WebInspector.KeyboardShortcut.makeDescriptor("f", WebInspector.KeyboardShortcut.Modifiers.Meta | WebInspector.KeyboardShortcut.Modifiers.Alt);
  45. else
  46. return WebInspector.KeyboardShortcut.makeDescriptor("f", WebInspector.KeyboardShortcut.Modifiers.Ctrl | WebInspector.KeyboardShortcut.Modifiers.Shift);
  47. }
  48. WebInspector.AdvancedSearchController.prototype = {
  49. /**
  50. * @param {KeyboardEvent} event
  51. * @return {boolean}
  52. */
  53. handleShortcut: function(event)
  54. {
  55. if (WebInspector.KeyboardShortcut.makeKeyFromEvent(event) === this._shortcut.key) {
  56. if (!this._searchView || !this._searchView.isShowing() || this._searchView._search !== document.activeElement) {
  57. WebInspector.showPanel("scripts");
  58. this.show();
  59. } else
  60. this.close();
  61. event.consume(true);
  62. return true;
  63. }
  64. return false;
  65. },
  66. _frameNavigated: function()
  67. {
  68. this.resetSearch();
  69. },
  70. /**
  71. * @param {WebInspector.SearchScope} searchScope
  72. */
  73. registerSearchScope: function(searchScope)
  74. {
  75. // FIXME: implement multiple search scopes.
  76. this._searchScope = searchScope;
  77. },
  78. show: function()
  79. {
  80. if (!this._searchView)
  81. this._searchView = new WebInspector.SearchView(this);
  82. this._searchView.syncToSelection();
  83. if (this._searchView.isShowing())
  84. this._searchView.focus();
  85. else
  86. WebInspector.showViewInDrawer(this._searchView._searchPanelElement, this._searchView, this.stopSearch.bind(this));
  87. },
  88. close: function()
  89. {
  90. this.stopSearch();
  91. WebInspector.closeViewInDrawer();
  92. },
  93. /**
  94. * @param {number} searchId
  95. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  96. */
  97. _onSearchResult: function(searchId, searchResult)
  98. {
  99. if (searchId !== this._searchId)
  100. return;
  101. this._searchView.addSearchResult(searchResult);
  102. if (!searchResult.searchMatches.length)
  103. return;
  104. if (!this._searchResultsPane)
  105. this._searchResultsPane = this._currentSearchScope.createSearchResultsPane(this._searchConfig);
  106. this._searchView.resultsPane = this._searchResultsPane;
  107. this._searchResultsPane.addSearchResult(searchResult);
  108. },
  109. /**
  110. * @param {number} searchId
  111. * @param {boolean} finished
  112. */
  113. _onSearchFinished: function(searchId, finished)
  114. {
  115. if (searchId !== this._searchId)
  116. return;
  117. if (!this._searchResultsPane)
  118. this._searchView.nothingFound();
  119. this._searchView.searchFinished(finished);
  120. },
  121. /**
  122. * @param {WebInspector.SearchConfig} searchConfig
  123. */
  124. startSearch: function(searchConfig)
  125. {
  126. this.resetSearch();
  127. ++this._searchId;
  128. this._searchConfig = searchConfig;
  129. // FIXME: this._currentSearchScope should be initialized based on searchConfig
  130. this._currentSearchScope = this._searchScope;
  131. var totalSearchResultsCount = this._currentSearchScope.performSearch(searchConfig, this._onSearchResult.bind(this, this._searchId), this._onSearchFinished.bind(this, this._searchId));
  132. this._searchView.searchStarted(totalSearchResultsCount);
  133. },
  134. resetSearch: function()
  135. {
  136. this.stopSearch();
  137. if (this._searchResultsPane) {
  138. this._searchView.resetResults();
  139. delete this._searchResultsPane;
  140. }
  141. },
  142. stopSearch: function()
  143. {
  144. if (this._currentSearchScope)
  145. this._currentSearchScope.stopSearch();
  146. }
  147. }
  148. /**
  149. * @constructor
  150. * @extends {WebInspector.View}
  151. * @param {WebInspector.AdvancedSearchController} controller
  152. */
  153. WebInspector.SearchView = function(controller)
  154. {
  155. WebInspector.View.call(this);
  156. this.registerRequiredCSS("textEditor.css");
  157. this._controller = controller;
  158. this.element.className = "search-view";
  159. this._searchPanelElement = document.createElement("span");
  160. this._searchPanelElement.className = "search-drawer-header";
  161. this._searchPanelElement.addEventListener("keydown", this._onKeyDown.bind(this), false);
  162. this._searchResultsElement = this.element.createChild("div");
  163. this._searchResultsElement.className = "search-results";
  164. this._searchLabel = this._searchPanelElement.createChild("span");
  165. this._searchLabel.textContent = WebInspector.UIString("Search sources");
  166. this._search = this._searchPanelElement.createChild("input");
  167. this._search.setAttribute("type", "search");
  168. this._search.addStyleClass("search-config-search");
  169. this._search.setAttribute("results", "0");
  170. this._search.setAttribute("size", 30);
  171. this._ignoreCaseLabel = this._searchPanelElement.createChild("label");
  172. this._ignoreCaseLabel.addStyleClass("search-config-label");
  173. this._ignoreCaseCheckbox = this._ignoreCaseLabel.createChild("input");
  174. this._ignoreCaseCheckbox.setAttribute("type", "checkbox");
  175. this._ignoreCaseCheckbox.addStyleClass("search-config-checkbox");
  176. this._ignoreCaseLabel.appendChild(document.createTextNode(WebInspector.UIString("Ignore case")));
  177. this._regexLabel = this._searchPanelElement.createChild("label");
  178. this._regexLabel.addStyleClass("search-config-label");
  179. this._regexCheckbox = this._regexLabel.createChild("input");
  180. this._regexCheckbox.setAttribute("type", "checkbox");
  181. this._regexCheckbox.addStyleClass("search-config-checkbox");
  182. this._regexLabel.appendChild(document.createTextNode(WebInspector.UIString("Regular expression")));
  183. this._searchStatusBarElement = document.createElement("div");
  184. this._searchStatusBarElement.className = "search-status-bar-item";
  185. this._searchMessageElement = this._searchStatusBarElement.createChild("div");
  186. this._searchMessageElement.className = "search-status-bar-message";
  187. this._searchResultsMessageElement = document.createElement("span");
  188. this._searchResultsMessageElement.className = "search-results-status-bar-message";
  189. this._load();
  190. }
  191. // Number of recent search queries to store.
  192. WebInspector.SearchView.maxQueriesCount = 20;
  193. WebInspector.SearchView.prototype = {
  194. /**
  195. * @return {Array.<Element>}
  196. */
  197. statusBarItems: function()
  198. {
  199. return [this._searchStatusBarElement, this._searchResultsMessageElement];
  200. },
  201. /**
  202. * @return {WebInspector.SearchConfig}
  203. */
  204. get searchConfig()
  205. {
  206. return new WebInspector.SearchConfig(this._search.value, this._ignoreCaseCheckbox.checked, this._regexCheckbox.checked);
  207. },
  208. syncToSelection: function()
  209. {
  210. var selection = window.getSelection();
  211. if (selection.rangeCount)
  212. this._search.value = selection.toString().replace(/\r?\n.*/, "");
  213. },
  214. /**
  215. * @type {WebInspector.SearchResultsPane}
  216. */
  217. set resultsPane(resultsPane)
  218. {
  219. this.resetResults();
  220. this._searchResultsElement.appendChild(resultsPane.element);
  221. },
  222. /**
  223. * @param {number} totalSearchResultsCount
  224. */
  225. searchStarted: function(totalSearchResultsCount)
  226. {
  227. this.resetResults();
  228. this._resetCounters();
  229. this._searchMessageElement.textContent = WebInspector.UIString("Searching...");
  230. this._progressIndicator = new WebInspector.ProgressIndicator();
  231. this._progressIndicator.setTotalWork(totalSearchResultsCount);
  232. this._progressIndicator.show(this._searchStatusBarElement);
  233. this._updateSearchResultsMessage();
  234. if (!this._searchingView)
  235. this._searchingView = new WebInspector.EmptyView(WebInspector.UIString("Searching..."));
  236. this._searchingView.show(this._searchResultsElement);
  237. },
  238. _updateSearchResultsMessage: function()
  239. {
  240. if (this._searchMatchesCount && this._searchResultsCount)
  241. this._searchResultsMessageElement.textContent = WebInspector.UIString("Found %d matches in %d files.", this._searchMatchesCount, this._nonEmptySearchResultsCount);
  242. else
  243. this._searchResultsMessageElement.textContent = "";
  244. },
  245. resetResults: function()
  246. {
  247. if (this._searchingView)
  248. this._searchingView.detach();
  249. if (this._notFoundView)
  250. this._notFoundView.detach();
  251. this._searchResultsElement.removeChildren();
  252. },
  253. _resetCounters: function()
  254. {
  255. this._searchMatchesCount = 0;
  256. this._searchResultsCount = 0;
  257. this._nonEmptySearchResultsCount = 0;
  258. },
  259. nothingFound: function()
  260. {
  261. this.resetResults();
  262. if (!this._notFoundView)
  263. this._notFoundView = new WebInspector.EmptyView(WebInspector.UIString("No matches found."));
  264. this._notFoundView.show(this._searchResultsElement);
  265. this._searchResultsMessageElement.textContent = WebInspector.UIString("No matches found.");
  266. },
  267. /**
  268. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  269. */
  270. addSearchResult: function(searchResult)
  271. {
  272. this._searchMatchesCount += searchResult.searchMatches.length;
  273. this._searchResultsCount++;
  274. if (searchResult.searchMatches.length)
  275. this._nonEmptySearchResultsCount++;
  276. this._updateSearchResultsMessage();
  277. if (this._progressIndicator.isCanceled())
  278. this._onCancel();
  279. else
  280. this._progressIndicator.setWorked(this._searchResultsCount);
  281. },
  282. /**
  283. * @param {boolean} finished
  284. */
  285. searchFinished: function(finished)
  286. {
  287. this._progressIndicator.done();
  288. this._searchMessageElement.textContent = finished ? WebInspector.UIString("Search finished.") : WebInspector.UIString("Search interrupted.");
  289. },
  290. focus: function()
  291. {
  292. WebInspector.setCurrentFocusElement(this._search);
  293. this._search.select();
  294. },
  295. wasShown: function()
  296. {
  297. this.focus();
  298. },
  299. willHide: function()
  300. {
  301. this._controller.stopSearch();
  302. },
  303. /**
  304. * @param {Event} event
  305. */
  306. _onKeyDown: function(event)
  307. {
  308. switch (event.keyCode) {
  309. case WebInspector.KeyboardShortcut.Keys.Enter.code:
  310. this._onAction();
  311. break;
  312. case WebInspector.KeyboardShortcut.Keys.Esc.code:
  313. this._controller.close();
  314. event.consume(true);
  315. break;
  316. }
  317. },
  318. _save: function()
  319. {
  320. var searchConfig = new WebInspector.SearchConfig(this.searchConfig.query, this.searchConfig.ignoreCase, this.searchConfig.isRegex);
  321. WebInspector.settings.advancedSearchConfig.set(searchConfig);
  322. },
  323. _load: function()
  324. {
  325. var searchConfig = WebInspector.settings.advancedSearchConfig.get();
  326. this._search.value = searchConfig.query;
  327. this._ignoreCaseCheckbox.checked = searchConfig.ignoreCase;
  328. this._regexCheckbox.checked = searchConfig.isRegex;
  329. },
  330. _onCancel: function()
  331. {
  332. this._controller.stopSearch();
  333. this.focus();
  334. },
  335. _onAction: function()
  336. {
  337. if (!this.searchConfig.query || !this.searchConfig.query.length)
  338. return;
  339. this._save();
  340. this._controller.startSearch(this.searchConfig);
  341. },
  342. __proto__: WebInspector.View.prototype
  343. }
  344. /**
  345. * @constructor
  346. * @param {string} query
  347. * @param {boolean} ignoreCase
  348. * @param {boolean} isRegex
  349. */
  350. WebInspector.SearchConfig = function(query, ignoreCase, isRegex)
  351. {
  352. this.query = query;
  353. this.ignoreCase = ignoreCase;
  354. this.isRegex = isRegex;
  355. }
  356. /**
  357. * @interface
  358. */
  359. WebInspector.SearchScope = function()
  360. {
  361. }
  362. WebInspector.SearchScope.prototype = {
  363. /**
  364. * @param {WebInspector.SearchConfig} searchConfig
  365. * @param {function(WebInspector.FileBasedSearchResultsPane.SearchResult)} searchResultCallback
  366. * @param {function(boolean)} searchFinishedCallback
  367. */
  368. performSearch: function(searchConfig, searchResultCallback, searchFinishedCallback) { },
  369. stopSearch: function() { },
  370. /**
  371. * @param {WebInspector.SearchConfig} searchConfig
  372. * @return {WebInspector.SearchResultsPane}
  373. */
  374. createSearchResultsPane: function(searchConfig) { }
  375. }
  376. /**
  377. * @constructor
  378. * @param {number} offset
  379. * @param {number} length
  380. */
  381. WebInspector.SearchResult = function(offset, length)
  382. {
  383. this.offset = offset;
  384. this.length = length;
  385. }
  386. /**
  387. * @constructor
  388. * @param {WebInspector.SearchConfig} searchConfig
  389. */
  390. WebInspector.SearchResultsPane = function(searchConfig)
  391. {
  392. this._searchConfig = searchConfig;
  393. this.element = document.createElement("div");
  394. }
  395. WebInspector.SearchResultsPane.prototype = {
  396. /**
  397. * @return {WebInspector.SearchConfig}
  398. */
  399. get searchConfig()
  400. {
  401. return this._searchConfig;
  402. },
  403. /**
  404. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  405. */
  406. addSearchResult: function(searchResult) { }
  407. }
  408. /**
  409. * @constructor
  410. * @extends {WebInspector.SearchResultsPane}
  411. * @param {WebInspector.SearchConfig} searchConfig
  412. */
  413. WebInspector.FileBasedSearchResultsPane = function(searchConfig)
  414. {
  415. WebInspector.SearchResultsPane.call(this, searchConfig);
  416. this._searchResults = [];
  417. this.element.id ="search-results-pane-file-based";
  418. this._treeOutlineElement = document.createElement("ol");
  419. this._treeOutlineElement.className = "search-results-outline-disclosure";
  420. this.element.appendChild(this._treeOutlineElement);
  421. this._treeOutline = new TreeOutline(this._treeOutlineElement);
  422. this._matchesExpandedCount = 0;
  423. }
  424. WebInspector.FileBasedSearchResultsPane.matchesExpandedByDefaultCount = 20;
  425. WebInspector.FileBasedSearchResultsPane.fileMatchesShownAtOnce = 20;
  426. WebInspector.FileBasedSearchResultsPane.prototype = {
  427. /**
  428. * @param {WebInspector.UISourceCode} uiSourceCode
  429. * @param {number} lineNumber
  430. * @param {number} columnNumber
  431. * @return {Element}
  432. */
  433. _createAnchor: function(uiSourceCode, lineNumber, columnNumber)
  434. {
  435. var anchor = document.createElement("a");
  436. anchor.preferredPanel = "scripts";
  437. anchor.href = sanitizeHref(uiSourceCode.originURL());
  438. anchor.uiSourceCode = uiSourceCode;
  439. anchor.lineNumber = lineNumber;
  440. return anchor;
  441. },
  442. /**
  443. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  444. */
  445. addSearchResult: function(searchResult)
  446. {
  447. this._searchResults.push(searchResult);
  448. var uiSourceCode = searchResult.uiSourceCode;
  449. var searchMatches = searchResult.searchMatches;
  450. var fileTreeElement = this._addFileTreeElement(uiSourceCode.originURL(), searchMatches.length, this._searchResults.length - 1);
  451. },
  452. /**
  453. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  454. * @param {TreeElement} fileTreeElement
  455. */
  456. _fileTreeElementExpanded: function(searchResult, fileTreeElement)
  457. {
  458. if (fileTreeElement._initialized)
  459. return;
  460. var toIndex = Math.min(searchResult.searchMatches.length, WebInspector.FileBasedSearchResultsPane.fileMatchesShownAtOnce);
  461. if (toIndex < searchResult.searchMatches.length) {
  462. this._appendSearchMatches(fileTreeElement, searchResult, 0, toIndex - 1);
  463. this._appendShowMoreMatchesElement(fileTreeElement, searchResult, toIndex - 1);
  464. } else
  465. this._appendSearchMatches(fileTreeElement, searchResult, 0, toIndex);
  466. fileTreeElement._initialized = true;
  467. },
  468. /**
  469. * @param {TreeElement} fileTreeElement
  470. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  471. * @param {number} fromIndex
  472. * @param {number} toIndex
  473. */
  474. _appendSearchMatches: function(fileTreeElement, searchResult, fromIndex, toIndex)
  475. {
  476. var uiSourceCode = searchResult.uiSourceCode;
  477. var searchMatches = searchResult.searchMatches;
  478. var regex = createSearchRegex(this._searchConfig.query, !this._searchConfig.ignoreCase, this._searchConfig.isRegex);
  479. for (var i = fromIndex; i < toIndex; ++i) {
  480. var lineNumber = searchMatches[i].lineNumber;
  481. var lineContent = searchMatches[i].lineContent;
  482. var matchRanges = this._regexMatchRanges(lineContent, regex);
  483. var anchor = this._createAnchor(uiSourceCode, lineNumber, matchRanges[0].offset);
  484. var numberString = numberToStringWithSpacesPadding(lineNumber + 1, 4);
  485. var lineNumberSpan = document.createElement("span");
  486. lineNumberSpan.addStyleClass("webkit-line-number");
  487. lineNumberSpan.addStyleClass("search-match-line-number");
  488. lineNumberSpan.textContent = numberString;
  489. anchor.appendChild(lineNumberSpan);
  490. var contentSpan = this._createContentSpan(lineContent, matchRanges);
  491. anchor.appendChild(contentSpan);
  492. var searchMatchElement = new TreeElement("", null, false);
  493. fileTreeElement.appendChild(searchMatchElement);
  494. searchMatchElement.listItemElement.className = "search-match source-code";
  495. searchMatchElement.listItemElement.appendChild(anchor);
  496. }
  497. },
  498. /**
  499. * @param {TreeElement} fileTreeElement
  500. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  501. * @param {number} startMatchIndex
  502. */
  503. _appendShowMoreMatchesElement: function(fileTreeElement, searchResult, startMatchIndex)
  504. {
  505. var matchesLeftCount = searchResult.searchMatches.length - startMatchIndex;
  506. var showMoreMatchesText = WebInspector.UIString("Show all matches (%d more).", matchesLeftCount);
  507. var showMoreMatchesElement = new TreeElement(showMoreMatchesText, null, false);
  508. fileTreeElement.appendChild(showMoreMatchesElement);
  509. showMoreMatchesElement.listItemElement.addStyleClass("show-more-matches");
  510. showMoreMatchesElement.onselect = this._showMoreMatchesElementSelected.bind(this, searchResult, startMatchIndex, showMoreMatchesElement);
  511. },
  512. /**
  513. * @param {WebInspector.FileBasedSearchResultsPane.SearchResult} searchResult
  514. * @param {number} startMatchIndex
  515. * @param {TreeElement} showMoreMatchesElement
  516. */
  517. _showMoreMatchesElementSelected: function(searchResult, startMatchIndex, showMoreMatchesElement)
  518. {
  519. var fileTreeElement = showMoreMatchesElement.parent;
  520. fileTreeElement.removeChild(showMoreMatchesElement);
  521. this._appendSearchMatches(fileTreeElement, searchResult, startMatchIndex, searchResult.searchMatches.length);
  522. },
  523. /**
  524. * @param {string} fileName
  525. * @param {number} searchMatchesCount
  526. * @param {number} searchResultIndex
  527. */
  528. _addFileTreeElement: function(fileName, searchMatchesCount, searchResultIndex)
  529. {
  530. var fileTreeElement = new TreeElement("", null, true);
  531. fileTreeElement.toggleOnClick = true;
  532. fileTreeElement.selectable = false;
  533. this._treeOutline.appendChild(fileTreeElement);
  534. fileTreeElement.listItemElement.addStyleClass("search-result");
  535. var fileNameSpan = document.createElement("span");
  536. fileNameSpan.className = "search-result-file-name";
  537. fileNameSpan.textContent = fileName;
  538. fileTreeElement.listItemElement.appendChild(fileNameSpan);
  539. var matchesCountSpan = document.createElement("span");
  540. matchesCountSpan.className = "search-result-matches-count";
  541. if (searchMatchesCount === 1)
  542. matchesCountSpan.textContent = WebInspector.UIString("(%d match)", searchMatchesCount);
  543. else
  544. matchesCountSpan.textContent = WebInspector.UIString("(%d matches)", searchMatchesCount);
  545. fileTreeElement.listItemElement.appendChild(matchesCountSpan);
  546. var searchResult = this._searchResults[searchResultIndex];
  547. fileTreeElement.onexpand = this._fileTreeElementExpanded.bind(this, searchResult, fileTreeElement);
  548. // Expand until at least certain amount of matches is expanded.
  549. if (this._matchesExpandedCount < WebInspector.FileBasedSearchResultsPane.matchesExpandedByDefaultCount)
  550. fileTreeElement.expand();
  551. this._matchesExpandedCount += searchResult.searchMatches.length;
  552. return fileTreeElement;
  553. },
  554. /**
  555. * @param {string} lineContent
  556. * @param {RegExp} regex
  557. * @return {Array.<WebInspector.SearchResult>}
  558. */
  559. _regexMatchRanges: function(lineContent, regex)
  560. {
  561. regex.lastIndex = 0;
  562. var match;
  563. var offset = 0;
  564. var matchRanges = [];
  565. while ((regex.lastIndex < lineContent.length) && (match = regex.exec(lineContent)))
  566. matchRanges.push(new WebInspector.SearchResult(match.index, match[0].length));
  567. return matchRanges;
  568. },
  569. /**
  570. * @param {string} lineContent
  571. * @param {Array.<WebInspector.SearchResult>} matchRanges
  572. */
  573. _createContentSpan: function(lineContent, matchRanges)
  574. {
  575. var contentSpan = document.createElement("span");
  576. contentSpan.className = "search-match-content";
  577. contentSpan.textContent = lineContent;
  578. WebInspector.highlightRangesWithStyleClass(contentSpan, matchRanges, "highlighted-match");
  579. return contentSpan;
  580. },
  581. __proto__: WebInspector.SearchResultsPane.prototype
  582. }
  583. /**
  584. * @constructor
  585. * @param {WebInspector.UISourceCode} uiSourceCode
  586. * @param {Array.<Object>} searchMatches
  587. */
  588. WebInspector.FileBasedSearchResultsPane.SearchResult = function(uiSourceCode, searchMatches) {
  589. this.uiSourceCode = uiSourceCode;
  590. this.searchMatches = searchMatches;
  591. }
  592. /**
  593. * @type {WebInspector.AdvancedSearchController}
  594. */
  595. WebInspector.advancedSearchController = null;