ScriptsSearchScope.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * @implements {WebInspector.SearchScope}
  31. * @param {WebInspector.Workspace} workspace
  32. */
  33. WebInspector.ScriptsSearchScope = function(workspace)
  34. {
  35. // FIXME: Add title once it is used by search controller.
  36. WebInspector.SearchScope.call(this)
  37. this._searchId = 0;
  38. this._workspace = workspace;
  39. }
  40. WebInspector.ScriptsSearchScope.prototype = {
  41. /**
  42. * @param {WebInspector.Progress} progress
  43. * @param {function(boolean)} indexingFinishedCallback
  44. */
  45. performIndexing: function(progress, indexingFinishedCallback)
  46. {
  47. this.stopSearch();
  48. function filterOutServiceProjects(project)
  49. {
  50. return !project.isServiceProject();
  51. }
  52. var projects = this._workspace.projects().filter(filterOutServiceProjects);
  53. var barrier = new CallbackBarrier();
  54. var compositeProgress = new WebInspector.CompositeProgress(progress);
  55. progress.addEventListener(WebInspector.Progress.Events.Canceled, indexingCanceled.bind(this));
  56. for (var i = 0; i < projects.length; ++i) {
  57. var project = projects[i];
  58. var projectProgress = compositeProgress.createSubProgress(project.uiSourceCodes().length);
  59. project.indexContent(projectProgress, barrier.createCallback());
  60. }
  61. barrier.callWhenDone(indexingFinishedCallback.bind(this, true));
  62. function indexingCanceled()
  63. {
  64. indexingFinishedCallback(false);
  65. progress.done();
  66. }
  67. },
  68. /**
  69. * @param {WebInspector.SearchConfig} searchConfig
  70. * @param {WebInspector.Progress} progress
  71. * @param {function(WebInspector.FileBasedSearchResultsPane.SearchResult)} searchResultCallback
  72. * @param {function(boolean)} searchFinishedCallback
  73. */
  74. performSearch: function(searchConfig, progress, searchResultCallback, searchFinishedCallback)
  75. {
  76. this.stopSearch();
  77. /**
  78. * @param {WebInspector.Project} project
  79. */
  80. function filterOutServiceProjects(project)
  81. {
  82. return !project.isServiceProject();
  83. }
  84. var projects = this._workspace.projects().filter(filterOutServiceProjects);
  85. var barrier = new CallbackBarrier();
  86. var compositeProgress = new WebInspector.CompositeProgress(progress);
  87. for (var i = 0; i < projects.length; ++i) {
  88. var project = projects[i];
  89. var projectProgress = compositeProgress.createSubProgress(project.uiSourceCodes().length);
  90. var callback = barrier.createCallback(searchCallbackWrapper.bind(this, this._searchId, project));
  91. project.searchInContent(searchConfig.query, !searchConfig.ignoreCase, searchConfig.isRegex, projectProgress, callback);
  92. }
  93. barrier.callWhenDone(searchFinishedCallback.bind(this, true));
  94. /**
  95. * @param {number} searchId
  96. * @param {WebInspector.Project} project
  97. * @param {StringMap} searchMatches
  98. */
  99. function searchCallbackWrapper(searchId, project, searchMatches)
  100. {
  101. if (searchId !== this._searchId) {
  102. searchFinishedCallback(false);
  103. return;
  104. }
  105. var paths = searchMatches.keys();
  106. for (var i = 0; i < paths.length; ++i) {
  107. var uiSourceCode = project.uiSourceCode(paths[i]);
  108. var searchResult = new WebInspector.FileBasedSearchResultsPane.SearchResult(uiSourceCode, searchMatches.get(paths[i]));
  109. searchResultCallback(searchResult);
  110. }
  111. }
  112. },
  113. stopSearch: function()
  114. {
  115. ++this._searchId;
  116. },
  117. /**
  118. * @param {WebInspector.SearchConfig} searchConfig
  119. */
  120. createSearchResultsPane: function(searchConfig)
  121. {
  122. return new WebInspector.FileBasedSearchResultsPane(searchConfig);
  123. },
  124. __proto__: WebInspector.SearchScope.prototype
  125. }