summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/actions/project-text-search.js
blob: 70a74d560c89577ef1bc10b2b1b8e3773215cac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

/**
 * Redux actions for the search state
 * @module actions/search
 */

import { isFulfilled } from "../utils/async-value";
import {
  getFirstSourceActorForGeneratedSource,
  getSourceList,
  getSettledSourceTextContent,
  isSourceBlackBoxed,
  getSearchOptions,
} from "../selectors/index";
import { createLocation } from "../utils/location";
import { matchesGlobPatterns } from "../utils/source";
import { loadSourceText } from "./sources/loadSourceText";
import { searchKeys } from "../constants";

export function searchSources(query, onUpdatedResults, signal) {
  return async ({ dispatch, getState, searchWorker }) => {
    dispatch({
      type: "SET_PROJECT_SEARCH_QUERY",
      query,
    });

    const searchOptions = getSearchOptions(
      getState(),
      searchKeys.PROJECT_SEARCH
    );
    const validSources = getSourceList(getState()).filter(
      source =>
        !isSourceBlackBoxed(getState(), source) &&
        !matchesGlobPatterns(source, searchOptions.excludePatterns)
    );
    // Sort original entries first so that search results are more useful.
    // Deprioritize third-party scripts, so their results show last.
    validSources.sort((a, b) => {
      function isThirdParty(source) {
        return (
          source?.url &&
          (source.url.includes("node_modules") ||
            source.url.includes("bower_components"))
        );
      }

      if (a.isOriginal && !isThirdParty(a)) {
        return -1;
      }

      if (b.isOriginal && !isThirdParty(b)) {
        return 1;
      }

      if (!isThirdParty(a) && isThirdParty(b)) {
        return -1;
      }
      if (isThirdParty(a) && !isThirdParty(b)) {
        return 1;
      }
      return 0;
    });
    const results = [];
    for (const source of validSources) {
      const sourceActor = getFirstSourceActorForGeneratedSource(
        getState(),
        source.id
      );
      await dispatch(loadSourceText(source, sourceActor));

      // This is the only asynchronous call in this method.
      // We may have stopped the search by closing the search panel or changing the query.
      // Avoid any further unecessary computation when the React Component tells us the query was cancelled.
      if (signal.aborted) {
        return;
      }

      const result = await searchSource(source, sourceActor, query, {
        getState,
        searchWorker,
      });
      if (signal.aborted) {
        return;
      }

      if (result) {
        results.push(result);
        onUpdatedResults(results, false, signal);
      }
    }
    onUpdatedResults(results, true, signal);
  };
}

export async function searchSource(
  source,
  sourceActor,
  query,
  { getState, searchWorker }
) {
  const state = getState();
  const location = createLocation({
    source,
    sourceActor,
  });

  const content = getSettledSourceTextContent(state, location);
  let matches = [];

  if (content && isFulfilled(content) && content.value.type === "text") {
    const options = getSearchOptions(state, searchKeys.PROJECT_SEARCH);
    matches = await searchWorker.findSourceMatches(
      content.value,
      query,
      options
    );
  }
  if (!matches.length) {
    return null;
  }
  return {
    type: "RESULT",
    location,
    // `matches` are generated by project-search worker's `findSourceMatches` method
    matches: matches.map(m => ({
      type: "MATCH",
      location: createLocation({
        ...location,
        // `matches` only contain line and column
        // `location` will already refer to the right source/sourceActor
        line: m.line,
        column: m.column,
      }),
      matchIndex: m.matchIndex,
      match: m.match,
      value: m.value,
    })),
  };
}