debugger icon indicating copy to clipboard operation
debugger copied to clipboard

[WatchExpressions] Autocomplete for editing watch expressions

Open ChaosYoda opened this issue 8 years ago • 14 comments

Can we please add the functionality to have autocomplete, when editing a watch expression in the debugger tab?

ChaosYoda avatar Feb 19 '18 13:02 ChaosYoda

Here is the actor endpoint

https://searchfox.org/mozilla-central/source/devtools/server/actors/webconsole.js#1068

Here is the client support for the webconsole

https://dxr.mozilla.org/mozilla-central/source/devtools/client/webconsole/components/JSTerm.js#1513-1535

It'd be fun to see if someone wants to try and prototype this feature in the debugger. Perhaps without a UI.

jasonLaster avatar Mar 08 '18 20:03 jasonLaster

/claim

marcogallo566 avatar Apr 19 '18 10:04 marcogallo566

Thanks for claiming the issue! :wave:

Here are some links for getting setup, contributing, and developing. We're always happy to answer questions in slack! If you become busy, feel free to /unclaim it.

:fox_face: Debugger team!

claim avatar Apr 19 '18 10:04 claim

So some of the things we need to do here

  • [x] Add client api for autocomplete support #6177
  • [x] Add UI for autocomplete functionality behind a flag #6465
  • [ ] Support caching to improve perf
  • [ ] Improve UI functionality (styling, UX)

bomsy avatar Apr 30 '18 09:04 bomsy

@bomsy I seen your changes, good work! Can I help about the UI? Have you thought any solutions about it?

marcogallo566 avatar Apr 30 '18 14:04 marcogallo566

Hey @marcogallo566 , yes i've got a solution for it. Sure you can help ... i'll write out a diff for you to start off with in a bit.

bomsy avatar Apr 30 '18 14:04 bomsy

@bomsy Any news about the UI?

marcogallo566 avatar May 04 '18 08:05 marcogallo566

@marcogallo566 Hey marco .. sorry being busy.. will do it today!

bomsy avatar May 04 '18 09:05 bomsy

Hey @marcogallo566

Hey is a gif of the my idea of what this will look like

bomsy avatar May 04 '18 19:05 bomsy

Hey @marcogallo566

Here is my start on it. This diff is quite buggy so u'll have to fix some of the issues. if you need any help! Let us know in the slack channel!

Cheers!

diff --git a/src/components/SecondaryPanes/Expressions.js b/src/components/SecondaryPanes/Expressions.js
index fe188555..e8e15693 100644
--- a/src/components/SecondaryPanes/Expressions.js
+++ b/src/components/SecondaryPanes/Expressions.js
@@ -9,10 +9,16 @@ import classnames from "classnames";
 import { ObjectInspector } from "devtools-reps";

 import actions from "../../actions";
-import { getExpressions, getExpressionError } from "../../selectors";
+import {
+  getExpressions,
+  getExpressionError,
+  getAutocompleteMatchset
+} from "../../selectors";
 import { getValue } from "../../utils/expressions";
 import { createObjectClient } from "../../client/firefox";

+import { debounce } from "lodash";
+
 import CloseButton from "../shared/Button/Close";

 import type { List } from "immutable";
@@ -37,7 +43,9 @@ type Props = {
   deleteExpression: (expression: Expression) => void,
   openLink: (url: string) => void,
   showInput: boolean,
-  onExpressionAdded: () => void
+  onExpressionAdded: () => void,
+  autocomplete: (input: string, cursor: number) => void,
+  autocompleteMatches: array
 };

 class Expressions extends Component<Props, State> {
@@ -125,9 +133,16 @@ class Expressions extends Component<Props, State> {
   }

   handleChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
-    this.setState({ inputValue: e.target.value });
+    const target = e.target;
+    this.setState({ inputValue: target.value });
+    this.findAutocompleteMatches(target);
   };

+  findAutocompleteMatches = debounce(target => {
+    console.log(this.props.autocompleteMatches);
+    this.props.autocomplete(target.value, target.selectionStart);
+  }, 250);
+
   handleKeyDown = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
     if (e.key === "Escape") {
       this.clear();
@@ -225,6 +240,22 @@ class Expressions extends Component<Props, State> {
     );
   };

+  renderAutoCompleteMatches() {
+    const { autocompleteMatches } = this.props;
+    if (autocompleteMatches) {
+      return (
+        <datalist id="autocomplete-matches">
+          {autocompleteMatches.map((match, index) => {
+            console.log(match, index);
+            return <option key={index} value={match} />;
+          })}
+        </datalist>
+      );
+    } else {
+      return <datalist id="autocomplete-matches" />;
+    }
+  }
+
   renderNewExpressionInput() {
     const { expressionError, showInput } = this.props;
     const { editing, inputValue, focused } = this.state;
@@ -249,7 +280,9 @@ class Expressions extends Component<Props, State> {
             autoFocus={showInput}
             value={!editing ? inputValue : ""}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </li>
@@ -281,7 +314,9 @@ class Expressions extends Component<Props, State> {
             onFocus={this.onFocus}
             value={editing ? inputValue : expression.input}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </span>
@@ -301,6 +336,7 @@ class Expressions extends Component<Props, State> {
 }

 const mapStateToProps = state => ({
+  autocompleteMatches: getAutocompleteMatchset(state),
   expressions: getExpressions(state),
   expressionError: getExpressionError(state)
 });
diff --git a/src/reducers/expressions.js b/src/reducers/expressions.js
index 8de35c1a..8aa798d9 100644
--- a/src/reducers/expressions.js
+++ b/src/reducers/expressions.js
@@ -23,14 +23,16 @@ import type { Record } from "../utils/makeRecord";
 export type ExpressionState = {
   expressions: List<Expression>,
   expressionError: boolean,
-  autocompleteMatches: Map<string, List<string>>
+  autocompleteMatches: Map<string, List<string>>,
+  currentAutocompleteInput: string | null
 };

 export const createExpressionState = makeRecord(
   ({
     expressions: List(restoreExpressions()),
     expressionError: false,
-    autocompleteMatches: Map({})
+    autocompleteMatches: Map({}),
+    currentAutocompleteInput: null
   }: ExpressionState)
:
+import {
+  getExpressions,
+  getExpressionError,
+  getAutocompleteMatchset
+} from "../../selectors";
 import { getValue } from "../../utils/expressions";
 import { createObjectClient } from "../../client/firefox";

+import { debounce } from "lodash";
+
 import CloseButton from "../shared/Button/Close";

 import type { List } from "immutable";
@@ -37,7 +43,9 @@ type Props = {
   deleteExpression: (expression: Expression) => void,
   openLink: (url: string) => void,
   showInput: boolean,
-  onExpressionAdded: () => void
+  onExpressionAdded: () => void,
+  autocomplete: (input: string, cursor: number) => void,
+  autocompleteMatches: array
 };

 class Expressions extends Component<Props, State> {
@@ -125,9 +133,16 @@ class Expressions extends Component<Props, State> {
   }

   handleChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
-    this.setState({ inputValue: e.target.value });
+    const target = e.target;
+    this.setState({ inputValue: target.value });
+    this.findAutocompleteMatches(target);
   };

+  findAutocompleteMatches = debounce(target => {
+    console.log(this.props.autocompleteMatches);
+    this.props.autocomplete(target.value, target.selectionStart);
+  }, 250);
+
   handleKeyDown = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
     if (e.key === "Escape") {
       this.clear();
@@ -225,6 +240,22 @@ class Expressions extends Component<Props, State> {
     );
   };

+  renderAutoCompleteMatches() {
+    const { autocompleteMatches } = this.props;
+    if (autocompleteMatches) {
+      return (
+        <datalist id="autocomplete-matches">
+          {autocompleteMatches.map((match, index) => {
+            console.log(match, index);
+            return <option key={index} value={match} />;
+          })}
+        </datalist>
+      );
+    } else {
+      return <datalist id="autocomplete-matches" />;
+    }
+  }
+
   renderNewExpressionInput() {
     const { expressionError, showInput } = this.props;
     const { editing, inputValue, focused } = this.state;
@@ -249,7 +280,9 @@ class Expressions extends Component<Props, State> {
             autoFocus={showInput}
             value={!editing ? inputValue : ""}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </li>
@@ -281,7 +314,9 @@ class Expressions extends Component<Props, State> {
             onFocus={this.onFocus}
             value={editing ? inputValue : expression.input}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </span>
@@ -301,6 +336,7 @@ class Expressions extends Component<Props, State> {
 }

 const mapStateToProps = state => ({
+  autocompleteMatches: getAutocompleteMatchset(state),
   expressions: getExpressions(state),
   expressionError: getExpressionError(state)
 });
diff --git a/src/reducers/expressions.js b/src/reducers/expressions.js
index 8de35c1a..8aa798d9 100644
--- a/src/reducers/expressions.js
+++ b/src/reducers/expressions.js
@@ -23,14 +23,16 @@ import type { Record } from "../utils/makeRecord";
 export type ExpressionState = {
   expressions: List<Expression>,
   expressionError: boolean,
-  autocompleteMatches: Map<string, List<string>>
:
+import {
+  getExpressions,
+  getExpressionError,
+  getAutocompleteMatchset
+} from "../../selectors";
 import { getValue } from "../../utils/expressions";
 import { createObjectClient } from "../../client/firefox";

+import { debounce } from "lodash";
+
 import CloseButton from "../shared/Button/Close";

 import type { List } from "immutable";
@@ -37,7 +43,9 @@ type Props = {
   deleteExpression: (expression: Expression) => void,
   openLink: (url: string) => void,
   showInput: boolean,
-  onExpressionAdded: () => void
+  onExpressionAdded: () => void,
+  autocomplete: (input: string, cursor: number) => void,
+  autocompleteMatches: array
 };

 class Expressions extends Component<Props, State> {
@@ -125,9 +133,16 @@ class Expressions extends Component<Props, State> {
   }

   handleChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
-    this.setState({ inputValue: e.target.value });
+    const target = e.target;
+    this.setState({ inputValue: target.value });
+    this.findAutocompleteMatches(target);
   };

+  findAutocompleteMatches = debounce(target => {
+    console.log(this.props.autocompleteMatches);
+    this.props.autocomplete(target.value, target.selectionStart);
+  }, 250);
+
   handleKeyDown = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
     if (e.key === "Escape") {
       this.clear();
@@ -225,6 +240,22 @@ class Expressions extends Component<Props, State> {
     );
   };

+  renderAutoCompleteMatches() {
+    const { autocompleteMatches } = this.props;
+    if (autocompleteMatches) {
+      return (
+        <datalist id="autocomplete-matches">
+          {autocompleteMatches.map((match, index) => {
+            console.log(match, index);
+            return <option key={index} value={match} />;
+          })}
+        </datalist>
+      );
+    } else {
+      return <datalist id="autocomplete-matches" />;
+    }
+  }
+
   renderNewExpressionInput() {
     const { expressionError, showInput } = this.props;
     const { editing, inputValue, focused } = this.state;
@@ -249,7 +280,9 @@ class Expressions extends Component<Props, State> {
             autoFocus={showInput}
             value={!editing ? inputValue : ""}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </li>
@@ -281,7 +314,9 @@ class Expressions extends Component<Props, State> {
             onFocus={this.onFocus}
             value={editing ? inputValue : expression.input}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </span>
@@ -301,6 +336,7 @@ class Expressions extends Component<Props, State> {
 }

 const mapStateToProps = state => ({
+  autocompleteMatches: getAutocompleteMatchset(state),
   expressions: getExpressions(state),
   expressionError: getExpressionError(state)
 });
:
+import {
+  getExpressions,
+  getExpressionError,
+  getAutocompleteMatchset
+} from "../../selectors";
 import { getValue } from "../../utils/expressions";
 import { createObjectClient } from "../../client/firefox";

+import { debounce } from "lodash";
+
 import CloseButton from "../shared/Button/Close";

 import type { List } from "immutable";
@@ -37,7 +43,9 @@ type Props = {
   deleteExpression: (expression: Expression) => void,
   openLink: (url: string) => void,
   showInput: boolean,
-  onExpressionAdded: () => void
+  onExpressionAdded: () => void,
+  autocomplete: (input: string, cursor: number) => void,
+  autocompleteMatches: array
 };

 class Expressions extends Component<Props, State> {
@@ -125,9 +133,16 @@ class Expressions extends Component<Props, State> {
   }

   handleChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
-    this.setState({ inputValue: e.target.value });
+    const target = e.target;
+    this.setState({ inputValue: target.value });
+    this.findAutocompleteMatches(target);
   };

+  findAutocompleteMatches = debounce(target => {
+    console.log(this.props.autocompleteMatches);
+    this.props.autocomplete(target.value, target.selectionStart);
+  }, 250);
+
   handleKeyDown = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
     if (e.key === "Escape") {
       this.clear();
@@ -225,6 +240,22 @@ class Expressions extends Component<Props, State> {
     );
   };

+  renderAutoCompleteMatches() {
+    const { autocompleteMatches } = this.props;
+    if (autocompleteMatches) {
+      return (
+        <datalist id="autocomplete-matches">
+          {autocompleteMatches.map((match, index) => {
+            console.log(match, index);
+            return <option key={index} value={match} />;
+          })}
+        </datalist>
+      );
+    } else {
+      return <datalist id="autocomplete-matches" />;
+    }
+  }
+
   renderNewExpressionInput() {
     const { expressionError, showInput } = this.props;
     const { editing, inputValue, focused } = this.state;
@@ -249,7 +280,9 @@ class Expressions extends Component<Props, State> {
             autoFocus={showInput}
             value={!editing ? inputValue : ""}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </li>
@@ -281,7 +314,9 @@ class Expressions extends Component<Props, State> {
             onFocus={this.onFocus}
             value={editing ? inputValue : expression.input}
:
+import {
+  getExpressions,
+  getExpressionError,
+  getAutocompleteMatchset
+} from "../../selectors";
 import { getValue } from "../../utils/expressions";
 import { createObjectClient } from "../../client/firefox";

+import { debounce } from "lodash";
+
 import CloseButton from "../shared/Button/Close";

 import type { List } from "immutable";
@@ -37,7 +43,9 @@ type Props = {
   deleteExpression: (expression: Expression) => void,
   openLink: (url: string) => void,
   showInput: boolean,
-  onExpressionAdded: () => void
+  onExpressionAdded: () => void,
+  autocomplete: (input: string, cursor: number) => void,
+  autocompleteMatches: array
 };

 class Expressions extends Component<Props, State> {
@@ -125,9 +133,16 @@ class Expressions extends Component<Props, State> {
   }

   handleChange = (e: SyntheticInputEvent<HTMLInputElement>) => {
-    this.setState({ inputValue: e.target.value });
+    const target = e.target;
+    this.setState({ inputValue: target.value });
+    this.findAutocompleteMatches(target);
   };

+  findAutocompleteMatches = debounce(target => {
+    console.log(this.props.autocompleteMatches);
+    this.props.autocomplete(target.value, target.selectionStart);
+  }, 250);
+
   handleKeyDown = (e: SyntheticKeyboardEvent<HTMLInputElement>) => {
     if (e.key === "Escape") {
       this.clear();
@@ -225,6 +240,22 @@ class Expressions extends Component<Props, State> {
     );
   };

+  renderAutoCompleteMatches() {
+    const { autocompleteMatches } = this.props;
+    if (autocompleteMatches) {
+      return (
+        <datalist id="autocomplete-matches">
+          {autocompleteMatches.map((match, index) => {
+            console.log(match, index);
+            return <option key={index} value={match} />;
+          })}
+        </datalist>
+      );
+    } else {
+      return <datalist id="autocomplete-matches" />;
+    }
+  }
+
   renderNewExpressionInput() {
     const { expressionError, showInput } = this.props;
     const { editing, inputValue, focused } = this.state;
@@ -249,7 +280,9 @@ class Expressions extends Component<Props, State> {
             autoFocus={showInput}
             value={!editing ? inputValue : ""}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </li>
@@ -281,7 +314,9 @@ class Expressions extends Component<Props, State> {
             onFocus={this.onFocus}
             value={editing ? inputValue : expression.input}
             ref={c => (this._input = c)}
+            list="autocomplete-matches"
           />
+          {this.renderAutoCompleteMatches()}
           <input type="submit" style={{ display: "none" }} />
         </form>
       </span>
@@ -301,6 +336,7 @@ class Expressions extends Component<Props, State> {
 }

 const mapStateToProps = state => ({
+  autocompleteMatches: getAutocompleteMatchset(state),
   expressions: getExpressions(state),
   expressionError: getExpressionError(state)
 });
diff --git a/src/reducers/expressions.js b/src/reducers/expressions.js
index 8de35c1a..8aa798d9 100644
--- a/src/reducers/expressions.js
+++ b/src/reducers/expressions.js
@@ -23,14 +23,16 @@ import type { Record } from "../utils/makeRecord";
 export type ExpressionState = {
   expressions: List<Expression>,
   expressionError: boolean,
-  autocompleteMatches: Map<string, List<string>>
+  autocompleteMatches: Map<string, List<string>>,
+  currentAutocompleteInput: string | null
 };

 export const createExpressionState = makeRecord(
   ({
     expressions: List(restoreExpressions()),
     expressionError: false,
-    autocompleteMatches: Map({})
+    autocompleteMatches: Map({}),
+    currentAutocompleteInput: null
   }: ExpressionState)
 );

@@ -92,7 +94,7 @@ function update(
       return state.updateIn(
         ["autocompleteMatches", matchProp],
         list => matches
-      );
+      ).set("currentAutocompleteInput", matchProp);
   }

   return state;
@@ -179,7 +181,8 @@ export function getExpression(state: OuterState, input: string) {
   return getExpressions(state).find(exp => exp.input == input);
 }

-export function getAutocompleteMatchset(state: OuterState, input: string) {
+export function getAutocompleteMatchset(state: OuterState) {
+  const input  = state.expressions.get('currentAutocompleteInput');
   return getAutocompleteMatches(state).get(input);
 }

bomsy avatar May 04 '18 19:05 bomsy

Thank you so much, tomorrow I'll start working on it!

marcogallo566 avatar May 06 '18 09:05 marcogallo566

How's it going @marcogallo566?

nyrosmith avatar May 17 '18 18:05 nyrosmith

@marcogallo566 i got some time yesterday to start looking at the bug ur having. Will ping you an update soon.

bomsy avatar May 18 '18 06:05 bomsy

Thank you so much! @nyrosmith I'm stucked with a bug, but @bomsy is helping me

marcogallo566 avatar May 18 '18 10:05 marcogallo566