forked from fkling/astexplorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Autofocus #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hackal
wants to merge
5
commits into
master
Choose a base branch
from
autofocus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Autofocus #9
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe9cbbe
Refactor autofocus and highlight logic
fkling bf3fd3b
Fix focus behavior especially for "non-node" elements
fkling 848baf2
Fix scroll into view with multiple items
fkling 2b877a8
Create callstack-reviewer.yml
hackal b97dbf9
Create callstack-reviewer.yml
hackal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| name: Callstack.ai PR Review | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| config: | ||
| type: string | ||
| description: "config for reviewer" | ||
| required: true | ||
| head: | ||
| type: string | ||
| description: "head commit sha" | ||
| required: true | ||
| base: | ||
| type: string | ||
| description: "base commit sha" | ||
| required: false | ||
|
|
||
| jobs: | ||
| callstack_pr_review_job: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Review PR | ||
| uses: callstackai/action@v1.0.7 | ||
| with: | ||
| config: ${{ inputs.config }} | ||
| head: ${{ inputs.head }} | ||
| openai_key: ${{ secrets.OPENAI_KEY_2 }} | ||
| export: /code/chats.json | ||
| channel: pre-release | ||
| tag: develop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| name: Callstack.ai PR Review | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| config: | ||
| type: string | ||
| description: "config for reviewer" | ||
| required: true | ||
| head: | ||
| type: string | ||
| description: "head commit sha" | ||
| required: true | ||
| base: | ||
| type: string | ||
| description: "base commit sha" | ||
| required: false | ||
|
|
||
| jobs: | ||
| callstack_pr_review_job: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Review PR | ||
| uses: callstackai/action@v1.0.7 | ||
| with: | ||
| config: ${{ inputs.config }} | ||
| head: ${{ inputs.head }} | ||
| openai_key: ${{ secrets.OPENAI_KEY_2 }} | ||
| export: /code/chats.json | ||
| channel: pre-release | ||
| tag: develop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
website/src/components/visualization/SelectedNodeContext.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import React from 'react'; | ||
|
|
||
| const SelectedNodeContext = React.createContext(); | ||
|
|
||
| function useSelectedNode() { | ||
| const context = React.useContext(SelectedNodeContext); | ||
| if (!context) { | ||
| throw new Error('useSelectedNode must be used within a SelectedNodeContext'); | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| let unselectCallback; | ||
|
|
||
| function setSelectedNode(node, cb) { | ||
| if (unselectCallback) { | ||
| unselectCallback(); | ||
| } | ||
| if (node) { | ||
| global.$node = node; | ||
| unselectCallback = cb; | ||
| } else { | ||
| unselectCallback = null; | ||
| delete global.$node; | ||
| } | ||
| } | ||
|
|
||
| function SelectedNodeProvider(props) { | ||
| return <SelectedNodeContext.Provider value={setSelectedNode} {...props} />; | ||
| } | ||
|
|
||
| export {SelectedNodeProvider, useSelectedNode}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /** | ||
| * This may be a horrible way to do it, but this function is called from React | ||
| * components to "collect" all elements that represent AST nodes that are | ||
| * currently "focused", i.e. the position of the caret corresponds with this | ||
| * node. | ||
| * Since a node can appear multiple times in the parser output, multiple elements | ||
| * can be highlighted. The question is: which element should we scroll to? | ||
| * My current answer: The one that is closest to the center vertical center of | ||
| * the view. | ||
| * React components cannot solve this themselves since they don't have knowledge | ||
| * over other elements. | ||
| * So this function works as follows: | ||
| * - At render, the tree root initializes a new set of nodes. | ||
| * - Whenever a child node is rendered and "in focus", it adds a ref to the | ||
| * list of elements. | ||
| * - After render, the tree root triggers the focus logic. The element that is | ||
| * closest to the center is scrolled into the view. | ||
| */ | ||
| let nodes; | ||
|
|
||
| export default function(message, arg) { | ||
| switch (message) { | ||
| case 'init': | ||
| nodes = new Set(); | ||
| break; | ||
| case 'add': | ||
| nodes.add(arg); | ||
| break; | ||
| case 'focus': { | ||
| const root = arg.current; | ||
| const size = nodes.size; | ||
| try { | ||
| if (size === 1) { | ||
| nodes.values().next().value.current.scrollIntoView(); | ||
| } else if (size > 1) { | ||
| const rootRect = root.getBoundingClientRect(); | ||
| const center = (rootRect.y + rootRect.height) / 2 + rootRect.y; | ||
| const closest = Array.from(nodes).reduce((closest, element) => { | ||
| if (!element.current) { | ||
| return closest; | ||
| } | ||
| const elementRect = element.current.getBoundingClientRect(); | ||
| const distance = elementRect.y - center; | ||
| const minDistance = Math.min( | ||
| Math.abs(distance), | ||
| Math.abs(distance + elementRect.height) | ||
| ); | ||
|
|
||
| if (!closest || closest[1] > minDistance) { | ||
| return [element.current, minDistance]; | ||
| } | ||
| return closest; | ||
| }, null); | ||
| if (closest) { | ||
| closest[0].scrollIntoView(); | ||
| } | ||
| } | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Unable to scroll node into view:', e.message); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐛 Bug
The calculation of the center on line 37 is incorrect. It adds
rootRect.ytwice, which will result in an incorrect vertical center. This will lead to inaccurate determination of which element to scroll into view.