Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit e533954

Browse files
Łukasz SobekjasonLaster
authored andcommitted
Adds option to execute selected text in editor right-click menu (#4934)
1 parent 468bda4 commit e533954

File tree

8 files changed

+66
-10
lines changed

8 files changed

+66
-10
lines changed

assets/panel/debugger.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ copyStackTrace.accesskey=c
4848
# that expands the left and right panes in the debugger UI.
4949
expandPanes=Expand panes
5050

51+
# LOCALIZATION NOTE (executeInConsole): Editor right-click menu item
52+
# to execute selected text in browser console.
53+
evaluateInConsole.label=Evaluate in console
54+
5155
# LOCALIZATION NOTE (pauseButtonTooltip): The tooltip that is displayed for the pause
5256
# button when the debugger is in a running state.
5357
pauseButtonTooltip=Pause %S

src/actions/pause/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function hasAwait(source, pauseLocation) {
164164

165165
return source.text
166166
.split("\n")
167-
[line - 1].slice(column, column + 200)
167+
[line - 1].slice(column - 200, column + 200)
168168
.match(/(yield|await)/);
169169
}
170170

src/actions/pause/paused.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ export function paused(pauseInfo: Pause) {
3434
return async function({ dispatch, getState, client, sourceMaps }: ThunkArgs) {
3535
const { frames, why, loadedObjects } = pauseInfo;
3636

37+
const location = isGeneratedId(visibleFrame.location.sourceId);
38+
if (
39+
isEqual(previousPauseLocation, pauseLocation) ||
40+
(isOriginalFile() && parser.atBadLocation(pauseLcation))
41+
) {
42+
dispatch(stepOver());
43+
return;
44+
}
45+
3746
dispatch({
3847
type: "PAUSED",
3948
why,
@@ -56,10 +65,10 @@ export function paused(pauseInfo: Pause) {
5665

5766
if (selectedFrame) {
5867
const visibleFrame = getVisibleSelectedFrame(getState());
59-
const location = isGeneratedId(visibleFrame.location.sourceId)
60-
? selectedFrame.generatedLocation
61-
: selectedFrame.location;
62-
await dispatch(selectLocation(location));
68+
// const location = isGeneratedId(visibleFrame.location.sourceId)
69+
// ? selectedFrame.generatedLocation
70+
// : selectedFrame.location;
71+
await dispatch(selectLocation(selectedFrame.location));
6372
}
6473

6574
dispatch(togglePaneCollapse("end", false));

src/actions/tests/toolbox.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { actions, createStore } from "../../utils/test-head";
2+
const threadClient = {
3+
evaluate: jest.fn()
4+
};
5+
6+
describe("toolbox", () => {
7+
describe("evaluate in console", () => {
8+
it("variable", () => {
9+
const { dispatch } = createStore(threadClient);
10+
dispatch(actions.evaluateInConsole("foo"));
11+
12+
expect(threadClient.evaluate).toBeCalledWith(
13+
'console.log("foo"); console.log(foo)',
14+
{ frameId: undefined }
15+
);
16+
});
17+
});
18+
});

src/actions/toolbox.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// @flow
66

77
const { isDevelopment } = require("devtools-config");
8+
const { getSelectedFrameId } = require("../selectors");
89

910
import type { ThunkArgs } from "./types";
1011
import type { Worker } from "../types";
@@ -36,3 +37,13 @@ export function openWorkerToolbox(worker: Worker) {
3637
}
3738
};
3839
}
40+
41+
export function evaluateInConsole(inputString: string) {
42+
return async ({ client, getState }: ThunkArgs) => {
43+
const frameId = getSelectedFrameId(getState());
44+
client.evaluate(
45+
`console.log("${inputString}"); console.log(${inputString})`,
46+
{ frameId }
47+
);
48+
};
49+
}

src/components/Editor/EditorMenu.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function getMenuItems(
3434
{
3535
addExpression,
3636
editor,
37+
evaluateInConsole,
3738
flashLineRange,
3839
getFunctionLocation,
3940
getFunctionText,
@@ -62,7 +63,7 @@ function getMenuItems(
6263
selectedLocation,
6364
event
6465
);
65-
const textSelected = editor.codeMirror.somethingSelected();
66+
const isTextSelected = editor.codeMirror.somethingSelected();
6667

6768
// localizations
6869
const blackboxKey = L10N.getStr("sourceFooter.blackbox.accesskey");
@@ -77,6 +78,7 @@ function getMenuItems(
7778
const copySourceLabel = L10N.getStr("copySource");
7879
const copySourceUri2Key = L10N.getStr("copySourceUri2.accesskey");
7980
const copySourceUri2Label = L10N.getStr("copySourceUri2");
81+
const evaluateInConsoleLabel = L10N.getStr("evaluateInConsole.label");
8082
const jumpToMappedLocKey = L10N.getStr(
8183
"editor.jumpToMappedLocation1.accesskey"
8284
);
@@ -157,6 +159,12 @@ function getMenuItems(
157159
click: () => addExpression(editor.codeMirror.getSelection())
158160
};
159161

162+
const evaluateInConsoleItem = {
163+
id: "node-menu-evaluate-in-console",
164+
label: evaluateInConsoleLabel,
165+
click: () => evaluateInConsole(selectionText)
166+
};
167+
160168
// construct menu
161169
const menuItems = [
162170
copySourceItem,
@@ -170,8 +178,8 @@ function getMenuItems(
170178

171179
// conditionally added items
172180
// TODO: Find a new way to only add this for mapped sources?
173-
if (textSelected) {
174-
menuItems.push(watchExpressionItem);
181+
if (isTextSelected) {
182+
menuItems.push(watchExpressionItem, evaluateInConsoleItem);
175183
}
176184

177185
return menuItems;
@@ -207,6 +215,7 @@ class EditorMenu extends PureComponent {
207215

208216
const {
209217
addExpression,
218+
evaluateInConsole,
210219
flashLineRange,
211220
jumpToMappedLocation,
212221
setContextMenu,
@@ -237,6 +246,7 @@ export default connect(
237246
},
238247
{
239248
addExpression,
249+
evaluateInConsole,
240250
flashLineRange,
241251
jumpToMappedLocation,
242252
setContextMenu,

src/reducers/pause.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { OriginalScope } from "../actions/pause/mapScopes";
1919
import type { Action } from "../actions/types";
2020
import type { Why, Scope, SourceId, FrameId } from "../types";
2121

22+
type Command = "StepIn" | "StepOver";
2223
export type PauseState = {
2324
why: ?Why,
2425
isWaitingOnBreak: boolean,
@@ -60,7 +61,10 @@ export const createPauseState = (): PauseState => ({
6061
shouldIgnoreCaughtExceptions: prefs.ignoreCaughtExceptions,
6162
canRewind: false,
6263
debuggeeUrl: "",
63-
command: ""
64+
command: "",
65+
pauseLocation: {
66+
pauseLocation: {}
67+
}
6468
});
6569

6670
const emptyPauseState = {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3850,7 +3850,7 @@ eslint-plugin-jest@^21.5.0:
38503850
version "21.7.0"
38513851
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-21.7.0.tgz#651f1c6ce999af3ac59ab8bf8a376d742fd0fc23"
38523852

3853-
3853+
eslint-plugin-mozilla@^0.8.1:
38543854
version "0.8.1"
38553855
resolved "https://registry.yarnpkg.com/eslint-plugin-mozilla/-/eslint-plugin-mozilla-0.8.1.tgz#623b031b14e901999110c60ce17d0aad67cbba83"
38563856
dependencies:

0 commit comments

Comments
 (0)