From a6594b3a214fe5fa1b84dc7ebfefb9e326986488 Mon Sep 17 00:00:00 2001 From: Jason Laster Date: Thu, 8 Jun 2017 18:03:53 -0400 Subject: [PATCH 1/3] Create search worker --- configs/development.json | 3 +- src/components/Editor/SearchBar.js | 7 +-- src/utils/bootstrap.js | 4 ++ src/utils/editor/index.js | 2 - src/utils/editor/source-search.js | 15 +----- src/utils/pretty-print/worker.js | 42 +++++----------- src/utils/search/index.js | 8 +++ src/utils/{ => search}/tests/build-query.js | 2 +- src/utils/search/tests/countMatches.js | 49 +++++++++++++++++++ .../{editor => search/utils}/build-query.js | 0 src/utils/search/worker.js | 19 +++++++ webpack.config.js | 1 + 12 files changed, 101 insertions(+), 51 deletions(-) create mode 100644 src/utils/search/index.js rename src/utils/{ => search}/tests/build-query.js (99%) create mode 100644 src/utils/search/tests/countMatches.js rename src/utils/{editor => search/utils}/build-query.js (100%) create mode 100644 src/utils/search/worker.js diff --git a/configs/development.json b/configs/development.json index d6c0fed830..0667802a96 100644 --- a/configs/development.json +++ b/configs/development.json @@ -15,7 +15,8 @@ "workers": { "parserURL": "http://localhost:8000/assets/build/parser-worker.js", "sourceMapURL": "http://localhost:8000/assets/build/source-map-worker.js", - "prettyPrintURL": "http://localhost:8000/assets/build/pretty-print-worker.js" + "prettyPrintURL": "http://localhost:8000/assets/build/pretty-print-worker.js", + "searchURL": "http://localhost:8000/assets/build/search-worker.js" }, "features": { "blackbox": { diff --git a/src/components/Editor/SearchBar.js b/src/components/Editor/SearchBar.js index 5dde329046..ff24ebb8a4 100644 --- a/src/components/Editor/SearchBar.js +++ b/src/components/Editor/SearchBar.js @@ -24,10 +24,11 @@ import { findNext, findPrev, removeOverlay, - countMatches, clearIndex } from "../../utils/editor"; +import { countMatches } from "../../utils/search"; + import { scrollList } from "../../utils/result-list"; import classnames from "classnames"; import debounce from "lodash/debounce"; @@ -394,7 +395,7 @@ class SearchBar extends Component { } } - searchContents(query: string) { + async searchContents(query: string) { const { selectedSource, modifiers, @@ -408,7 +409,7 @@ class SearchBar extends Component { const ctx = { ed, cm: ed.codeMirror }; - const newCount = countMatches( + const newCount = await countMatches( query, selectedSource.get("text"), modifiers.toJS() diff --git a/src/utils/bootstrap.js b/src/utils/bootstrap.js index 74b776209e..9231c5cf38 100644 --- a/src/utils/bootstrap.js +++ b/src/utils/bootstrap.js @@ -4,6 +4,8 @@ import ReactDOM from "react-dom"; import { getValue, isFirefoxPanel } from "devtools-config"; import { renderRoot } from "devtools-launchpad"; import { startSourceMapWorker, stopSourceMapWorker } from "devtools-source-map"; +import { startSearchWorker, stopSearchWorker } from "../utils/search"; + import { startPrettyPrintWorker, stopPrettyPrintWorker @@ -52,6 +54,7 @@ export function bootstrapWorkers() { } startPrettyPrintWorker(getValue("workers.prettyPrintURL")); startParserWorker(getValue("workers.parserURL")); + startSearchWorker(getValue("workers.searchURL")); } export function teardownWorkers() { @@ -61,4 +64,5 @@ export function teardownWorkers() { } stopPrettyPrintWorker(); stopParserWorker(); + stopSearchWorker(); } diff --git a/src/utils/editor/index.js b/src/utils/editor/index.js index 764b0e7a5a..4a6a7b84a7 100644 --- a/src/utils/editor/index.js +++ b/src/utils/editor/index.js @@ -1,7 +1,6 @@ import { isEnabled } from "devtools-config"; import { isPretty, isJavaScript } from "../source"; import { isOriginalId } from "devtools-source-map"; -import buildQuery from "./build-query"; import * as sourceDocumentUtils from "./source-documents"; const { getDocument } = sourceDocumentUtils; @@ -119,7 +118,6 @@ module.exports = Object.assign( createEditor, shouldShowPrettyPrint, shouldShowFooter, - buildQuery, isTextForSource, breakpointAtLocation, traverseResults, diff --git a/src/utils/editor/source-search.js b/src/utils/editor/source-search.js index 82fe7fd53f..3f58d5576e 100644 --- a/src/utils/editor/source-search.js +++ b/src/utils/editor/source-search.js @@ -1,6 +1,6 @@ // @flow -import buildQuery from "./build-query"; +import buildQuery from "../search/utils/build-query"; import type { SearchModifiers } from "../../types"; @@ -296,22 +296,9 @@ function clearIndex(ctx: any, query: string, modifiers: SearchModifiers) { state.matchIndex = -1; } -function countMatches( - query: string, - text: string, - modifiers: SearchModifiers -): number { - const regexQuery = buildQuery(query, modifiers, { - isGlobal: true - }); - const match = text.match(regexQuery); - return match ? match.length : 0; -} - export { buildQuery, clearIndex, - countMatches, find, findNext, findPrev, diff --git a/src/utils/pretty-print/worker.js b/src/utils/pretty-print/worker.js index dd0d98657f..41d8d29598 100644 --- a/src/utils/pretty-print/worker.js +++ b/src/utils/pretty-print/worker.js @@ -1,7 +1,9 @@ // @flow import prettyFast from "pretty-fast"; -import assert from "../assert"; + +import { workerUtils } from "devtools-utils"; +const { workerHandler } = workerUtils; type Mappings = { _array: Mapping[] @@ -24,19 +26,15 @@ type InvertedMapping = { }; function prettyPrint({ url, indent, source }) { - try { - const prettified = prettyFast(source, { - url: url, - indent: " ".repeat(indent) - }); + const prettified = prettyFast(source, { + url: url, + indent: " ".repeat(indent) + }); - return { - code: prettified.code, - mappings: prettified.map._mappings - }; - } catch (e) { - throw new Error(`${e.message}\n${e.stack}`); - } + return { + code: prettified.code, + mappings: invertMappings(prettified.map._mappings) + }; } function invertMappings(mappings: Mappings) { @@ -59,20 +57,4 @@ function invertMappings(mappings: Mappings) { }); } -self.onmessage = function(msg) { - const { id, args } = msg.data; - assert(msg.data.method === "prettyPrint", "Method must be `prettyPrint`"); - - try { - let { code, mappings } = prettyPrint(args[0]); - self.postMessage({ - id, - response: { - code, - mappings: invertMappings(mappings) - } - }); - } catch (e) { - self.postMessage({ id, error: e }); - } -}; +self.onmessage = workerHandler({ prettyPrint }); diff --git a/src/utils/search/index.js b/src/utils/search/index.js new file mode 100644 index 0000000000..2ec930c23c --- /dev/null +++ b/src/utils/search/index.js @@ -0,0 +1,8 @@ +import { workerUtils } from "devtools-utils"; +const { WorkerDispatcher } = workerUtils; + +const dispatcher = new WorkerDispatcher(); +export const startSearchWorker = dispatcher.start.bind(dispatcher); +export const stopSearchWorker = dispatcher.stop.bind(dispatcher); + +export const countMatches = dispatcher.task("countMatches"); diff --git a/src/utils/tests/build-query.js b/src/utils/search/tests/build-query.js similarity index 99% rename from src/utils/tests/build-query.js rename to src/utils/search/tests/build-query.js index d5ea98d752..448d7a98de 100644 --- a/src/utils/tests/build-query.js +++ b/src/utils/search/tests/build-query.js @@ -1,5 +1,5 @@ import escapeRegExp from "lodash/escapeRegExp"; -import { buildQuery } from "../editor"; +import { buildQuery } from "../utils"; describe("build-query", () => { it("case-sensitive, whole-word, regex search", () => { diff --git a/src/utils/search/tests/countMatches.js b/src/utils/search/tests/countMatches.js new file mode 100644 index 0000000000..e904aae944 --- /dev/null +++ b/src/utils/search/tests/countMatches.js @@ -0,0 +1,49 @@ +import { countMatches } from "../worker"; + +describe("search", () => { + describe("countMatches", () => { + it("counts basic string match", () => { + const text = "the test string with test in it multiple times test."; + const query = "test"; + const count = countMatches(query, text, { + caseSensitive: true, + wholeWord: false, + regexMatch: false + }); + expect(count).toBe(3); + }); + + it("counts basic string match case-sensitive", () => { + const text = "the Test string with test in it multiple times test."; + const query = "Test"; + const count = countMatches(query, text, { + caseSensitive: true, + wholeWord: false, + regexMatch: false + }); + expect(count).toBe(1); + }); + + it("counts whole word string match", () => { + const text = "the test string test in it multiple times whoatestthe."; + const query = "test"; + const count = countMatches(query, text, { + caseSensitive: true, + wholeWord: true, + regexMatch: false + }); + expect(count).toBe(2); + }); + + it("counts regex match", () => { + const text = "the test string test in it multiple times whoatestthe."; + const query = "(\\w+)\\s+(\\w+)"; + const count = countMatches(query, text, { + caseSensitive: true, + wholeWord: false, + regexMatch: true + }); + expect(count).toBe(4); + }); + }); +}); diff --git a/src/utils/editor/build-query.js b/src/utils/search/utils/build-query.js similarity index 100% rename from src/utils/editor/build-query.js rename to src/utils/search/utils/build-query.js diff --git a/src/utils/search/worker.js b/src/utils/search/worker.js new file mode 100644 index 0000000000..f552dcc390 --- /dev/null +++ b/src/utils/search/worker.js @@ -0,0 +1,19 @@ +import buildQuery from "./utils/build-query"; +import { workerUtils } from "devtools-utils"; +const { workerHandler } = workerUtils; + +export function countMatches( + query: string, + text: string, + modifiers: SearchModifiers +): number { + debugger; + const regexQuery = buildQuery(query, modifiers, { + isGlobal: true + }); + const match = text.match(regexQuery); + return match ? match.length : 0; +} + +debugger; +self.onmessage = workerHandler({ countMatches }); diff --git a/webpack.config.js b/webpack.config.js index c121e04b47..b8bba05392 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -19,6 +19,7 @@ let webpackConfig = { debugger: getEntry("main.js"), "parser-worker": getEntry("utils/parser/worker.js"), "pretty-print-worker": getEntry("utils/pretty-print/worker.js"), + "search-worker": getEntry("utils/search/worker.js"), "integration-tests": getEntry("test/integration/tests.js") }, From 97e835669377145f3f299136e8c1a1b5e0048075 Mon Sep 17 00:00:00 2001 From: Jason Laster Date: Thu, 8 Jun 2017 18:09:19 -0400 Subject: [PATCH 2/3] clean up some of the code --- assets/panel/moz.build | 1 + configs/firefox-panel.json | 3 +- src/actions/ast.js | 2 +- src/utils/parser/index.js | 19 ++++------- src/utils/pretty-print/index.js | 14 ++++---- src/utils/search/tests/build-query.js | 2 +- src/utils/search/utils/build-query.js | 2 +- src/utils/search/worker.js | 2 -- src/utils/tests/source-search.js | 47 --------------------------- 9 files changed, 19 insertions(+), 73 deletions(-) diff --git a/assets/panel/moz.build b/assets/panel/moz.build index 2711b17f3c..ecf13ff91f 100644 --- a/assets/panel/moz.build +++ b/assets/panel/moz.build @@ -9,4 +9,5 @@ DevToolsModules( 'panel.js', 'parser-worker.js', 'pretty-print-worker.js', + 'search-worker.js', ) diff --git a/configs/firefox-panel.json b/configs/firefox-panel.json index 176a5b4c16..54ea5ee4e2 100644 --- a/configs/firefox-panel.json +++ b/configs/firefox-panel.json @@ -7,7 +7,8 @@ }, "workers": { "parserURL": "resource://devtools/client/debugger/new/parser-worker.js", - "prettyPrintURL": "resource://devtools/client/debugger/new/pretty-print-worker.js" + "prettyPrintURL": "resource://devtools/client/debugger/new/pretty-print-worker.js", + "searchURL": "resource://devtools/client/debugger/new/search-worker.js" }, "features": { "blackbox": { "enabled": true }, diff --git a/src/actions/ast.js b/src/actions/ast.js index c77ed86e77..e7cf4e2596 100644 --- a/src/actions/ast.js +++ b/src/actions/ast.js @@ -10,7 +10,7 @@ import { } from "../selectors"; import { PROMISE } from "../utils/redux/middleware/promise"; -import parser from "../utils/parser"; +import * as parser from "../utils/parser"; import type { Source } from "debugger-html"; import type { ThunkArgs } from "./types"; diff --git a/src/utils/parser/index.js b/src/utils/parser/index.js index d835cfbe6a..0da0fc30e5 100644 --- a/src/utils/parser/index.js +++ b/src/utils/parser/index.js @@ -4,20 +4,13 @@ import { workerUtils } from "devtools-utils"; const { WorkerDispatcher } = workerUtils; const dispatcher = new WorkerDispatcher(); +export const startParserWorker = dispatcher.start.bind(dispatcher); +export const stopParserWorker = dispatcher.stop.bind(dispatcher); -const getClosestExpression = dispatcher.task("getClosestExpression"); -const getSymbols = dispatcher.task("getSymbols"); -const getVariablesInScope = dispatcher.task("getVariablesInScope"); -const getOutOfScopeLocations = dispatcher.task("getOutOfScopeLocations"); +export const getClosestExpression = dispatcher.task("getClosestExpression"); +export const getSymbols = dispatcher.task("getSymbols"); +export const getVariablesInScope = dispatcher.task("getVariablesInScope"); +export const getOutOfScopeLocations = dispatcher.task("getOutOfScopeLocations"); export type { SymbolDeclaration, SymbolDeclarations } from "./getSymbols"; export type { AstLocation } from "./types"; - -module.exports = { - getSymbols, - getVariablesInScope, - getOutOfScopeLocations, - getClosestExpression, - startParserWorker: dispatcher.start.bind(dispatcher), - stopParserWorker: dispatcher.stop.bind(dispatcher) -}; diff --git a/src/utils/pretty-print/index.js b/src/utils/pretty-print/index.js index f7a40620ed..90f9242d72 100644 --- a/src/utils/pretty-print/index.js +++ b/src/utils/pretty-print/index.js @@ -8,6 +8,8 @@ import assert from "../assert"; import type { Source, SourceText } from "../../types"; const dispatcher = new WorkerDispatcher(); +export const startPrettyPrintWorker = dispatcher.start.bind(dispatcher); +export const stopPrettyPrintWorker = dispatcher.stop.bind(dispatcher); const _prettyPrint = dispatcher.task("prettyPrint"); type PrettyPrintOpts = { @@ -16,7 +18,11 @@ type PrettyPrintOpts = { url: string }; -async function prettyPrint({ source, sourceText, url }: PrettyPrintOpts) { +export async function prettyPrint({ + source, + sourceText, + url +}: PrettyPrintOpts) { const contentType = sourceText ? sourceText.contentType : ""; const indent = 2; @@ -31,9 +37,3 @@ async function prettyPrint({ source, sourceText, url }: PrettyPrintOpts) { source: sourceText ? sourceText.text : undefined }); } - -module.exports = { - prettyPrint, - startPrettyPrintWorker: dispatcher.start.bind(dispatcher), - stopPrettyPrintWorker: dispatcher.stop.bind(dispatcher) -}; diff --git a/src/utils/search/tests/build-query.js b/src/utils/search/tests/build-query.js index 448d7a98de..876d338ccc 100644 --- a/src/utils/search/tests/build-query.js +++ b/src/utils/search/tests/build-query.js @@ -1,5 +1,5 @@ import escapeRegExp from "lodash/escapeRegExp"; -import { buildQuery } from "../utils"; +import buildQuery from "../utils/build-query"; describe("build-query", () => { it("case-sensitive, whole-word, regex search", () => { diff --git a/src/utils/search/utils/build-query.js b/src/utils/search/utils/build-query.js index 31a1c65b51..a534d52b33 100644 --- a/src/utils/search/utils/build-query.js +++ b/src/utils/search/utils/build-query.js @@ -1,7 +1,7 @@ // @flow import escapeRegExp from "lodash/escapeRegExp"; -import type { SearchModifiers } from "../../types"; +import type { SearchModifiers } from "../../../types"; type QueryOptions = { isGlobal?: boolean, diff --git a/src/utils/search/worker.js b/src/utils/search/worker.js index f552dcc390..dbba6c14a3 100644 --- a/src/utils/search/worker.js +++ b/src/utils/search/worker.js @@ -7,7 +7,6 @@ export function countMatches( text: string, modifiers: SearchModifiers ): number { - debugger; const regexQuery = buildQuery(query, modifiers, { isGlobal: true }); @@ -15,5 +14,4 @@ export function countMatches( return match ? match.length : 0; } -debugger; self.onmessage = workerHandler({ countMatches }); diff --git a/src/utils/tests/source-search.js b/src/utils/tests/source-search.js index e49e17b449..f015e2b7c6 100644 --- a/src/utils/tests/source-search.js +++ b/src/utils/tests/source-search.js @@ -1,53 +1,6 @@ -import { countMatches } from "../editor"; import { getMatchIndex } from "../editor/source-search"; describe("source-search", () => { - describe("countMatches", () => { - it("counts basic string match", () => { - const text = "the test string with test in it multiple times test."; - const query = "test"; - const count = countMatches(query, text, { - caseSensitive: true, - wholeWord: false, - regexMatch: false - }); - expect(count).toBe(3); - }); - - it("counts basic string match case-sensitive", () => { - const text = "the Test string with test in it multiple times test."; - const query = "Test"; - const count = countMatches(query, text, { - caseSensitive: true, - wholeWord: false, - regexMatch: false - }); - expect(count).toBe(1); - }); - - it("counts whole word string match", () => { - const text = "the test string test in it multiple times whoatestthe."; - const query = "test"; - const count = countMatches(query, text, { - caseSensitive: true, - wholeWord: true, - regexMatch: false - }); - expect(count).toBe(2); - }); - - it("counts regex match", () => { - const text = "the test string test in it multiple times whoatestthe."; - const query = "(\\w+)\\s+(\\w+)"; - const count = countMatches(query, text, { - caseSensitive: true, - wholeWord: false, - regexMatch: true - }); - expect(count).toBe(4); - }); - }); - describe("getMatchIndex", () => { it("iterates in the matches", () => { const count = 3; From 4157c6549d78b7f6f2541842fdb04259f9afa5c3 Mon Sep 17 00:00:00 2001 From: Jason Laster Date: Fri, 9 Jun 2017 10:17:50 -0400 Subject: [PATCH 3/3] foo --- bin/run-mochitests-docker | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/run-mochitests-docker b/bin/run-mochitests-docker index 9ee1593444..1d59a3a9af 100755 --- a/bin/run-mochitests-docker +++ b/bin/run-mochitests-docker @@ -6,6 +6,7 @@ docker run -it \ -v `pwd`/assets/build/debugger.js:/gecko/devtools/client/debugger/new/debugger.js \ -v `pwd`/assets/build/pretty-print-worker.js:/gecko/devtools/client/debugger/new/pretty-print-worker.js \ -v `pwd`/assets/build/parser-worker.js:/gecko/devtools/client/debugger/new/parser-worker.js \ + -v `pwd`/assets/build/search-worker.js:/gecko/devtools/client/debugger/new/search-worker.js \ -v `pwd`/assets/build/integration-tests.js:/gecko/devtools/client/debugger/new/integration-tests.js \ -v `pwd`/assets/build/debugger.css:/gecko/devtools/client/debugger/new/debugger.css \ -v `pwd`/assets/build/panel/debugger.properties:/gecko/devtools/client/locales/en-US/debugger.properties \