-
Notifications
You must be signed in to change notification settings - Fork 326
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
Literals and operators in Component Browser #12420
Open
farmaazon
wants to merge
7
commits into
develop
Choose a base branch
from
wip/farmaazon/cb-literal-component
base: develop
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8649995
Literal component
farmaazon ad03676
Do not put dots on operators
farmaazon 5f90d2e
Fix
farmaazon 044c695
Review
farmaazon dc33a09
Test for reading literals
farmaazon f3242fe
Add missing tests
farmaazon dc91f64
Fixes and missing parts
farmaazon 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 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 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
67 changes: 67 additions & 0 deletions
67
app/gui/src/project-view/components/ComponentBrowser/__tests__/input.test.ts
This file contains 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,67 @@ | ||
import { useComponentBrowserInput } from '@/components/ComponentBrowser/input' | ||
import { GraphDb, NodeId } from '@/stores/graph/graphDatabase' | ||
import { ComputedValueRegistry } from '@/stores/project/computedValueRegistry' | ||
import { SuggestionDb } from '@/stores/suggestionDatabase' | ||
import { unwrap } from '@/util/data/result' | ||
import { parseAbsoluteProjectPathRaw } from '@/util/projectPath' | ||
import { expect, test } from 'vitest' | ||
import { assert, assertUnreachable } from 'ydoc-shared/util/assert' | ||
import { Range } from 'ydoc-shared/util/data/range' | ||
|
||
const aiMock = { query: assertUnreachable } | ||
const operator1Id = '3d0e9b96-3ca0-4c35-a820-7d3a1649de55' as NodeId | ||
const operator2Id = '5eb16101-dd2b-4034-a6e2-476e8bfa1f2b' as NodeId | ||
|
||
function mockGraphDb() { | ||
const computedValueRegistryMock = ComputedValueRegistry.Mock() | ||
computedValueRegistryMock.db.set(operator1Id, { | ||
typename: unwrap(parseAbsoluteProjectPathRaw('Standard.Base.Number')), | ||
rawTypename: 'Standard.Base.Number', | ||
methodCall: undefined, | ||
payload: { type: 'Value' }, | ||
profilingInfo: [], | ||
}) | ||
const db = GraphDb.Mock(computedValueRegistryMock) | ||
db.mockNode('operator1', operator1Id, 'Data.read') | ||
db.mockNode('operator2', operator2Id) | ||
return db | ||
} | ||
|
||
test.each` | ||
inputContent | expectedLiteral | ||
${'read'} | ${undefined} | ||
${'operator1'} | ${undefined} | ||
${'12 + 14'} | ${undefined} | ||
${'12'} | ${'12'} | ||
${'12.6'} | ${'12.6'} | ||
${'"text"'} | ${'"text"'} | ||
${"'text'"} | ${"'text'"} | ||
${"'text"} | ${"'text'"} | ||
${"'''text"} | ${"'''text"} | ||
`('Reading literal from $inputContent', ({ inputContent, expectedLiteral }) => { | ||
const input = useComponentBrowserInput(mockGraphDb(), new SuggestionDb(), aiMock) | ||
input.reset({ type: 'newNode' }) | ||
input.content = { text: inputContent, selection: Range.empty } | ||
assert(input.mode.mode === 'componentBrowsing') | ||
expect(input.mode.literal?.code()).toBe(expectedLiteral) | ||
}) | ||
|
||
test.each` | ||
inputContent | source | expectedCode | ||
${'read'} | ${'operator1'} | ${'operator1.read'} | ||
${'read'} | ${'operator2'} | ${'operator2.read'} | ||
${'read "file"'} | ${'operator2'} | ${'operator2.read "file"'} | ||
${'+ 2'} | ${'operator1'} | ${'operator1 + 2'} | ||
${'+ 3'} | ${'operator2'} | ${'operator2 + 3'} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some more good cases:
|
||
`( | ||
'Code generated by CB from $inputContent with source node $sourceNode', | ||
({ inputContent, source, expectedCode }) => { | ||
const db = mockGraphDb() | ||
const input = useComponentBrowserInput(db, new SuggestionDb(), aiMock) | ||
const sourcePort = db.getNodeFirstOutputPort(db.getIdentDefiningNode(source)) | ||
assert(sourcePort != null) | ||
input.reset({ type: 'newNode', sourcePort }) | ||
input.content = { text: inputContent, selection: Range.empty } | ||
expect(input.code).toBe(expectedCode) | ||
}, | ||
) |
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -10,6 +10,7 @@ import { | |||||
type SuggestionId, | ||||||
} from '@/stores/suggestionDatabase/entry' | ||||||
import { Ast } from '@/util/ast' | ||||||
import { startsWithIdentifier } from '@/util/ast/abstract' | ||||||
import { Err, Ok, type Result } from '@/util/data/result' | ||||||
import { type ProjectPath } from '@/util/projectPath' | ||||||
import { qnJoin, qnLastSegment } from '@/util/qualifiedName' | ||||||
|
@@ -32,7 +33,7 @@ export type ComponentBrowserMode = | |||||
| { | ||||||
mode: 'componentBrowsing' | ||||||
filter: Filter | ||||||
literal?: Ast.TextLiteral | Ast.NumericLiteral | ||||||
literal?: Ast.TextLiteral | Ast.NumericLiteral | Ast.NegationApp | undefined | ||||||
} | ||||||
| { | ||||||
mode: 'codeEditing' | ||||||
|
@@ -111,10 +112,10 @@ export function useComponentBrowserInput( | |||||
: {}), | ||||||
} | ||||||
} else { | ||||||
let literal: Ast.MutableTextLiteral | Ast.NumericLiteral | undefined = | ||||||
let literal: Ast.MutableTextLiteral | Ast.NumericLiteral | Ast.NegationApp | undefined = | ||||||
Ast.TextLiteral.tryParse(text.value) | ||||||
if (literal == null) { | ||||||
literal = Ast.NumericLiteral.tryParse(text.value) | ||||||
literal = Ast.NumericLiteral.tryParseWithSign(text.value) | ||||||
} else { | ||||||
literal.fixBoundaries() | ||||||
} | ||||||
|
@@ -124,7 +125,7 @@ export function useComponentBrowserInput( | |||||
pattern: text.value, | ||||||
...(sourceNodeType.value != null ? { selfArg: sourceNodeType.value } : {}), | ||||||
}, | ||||||
...(literal ? { literal } : {}), | ||||||
literal, | ||||||
} | ||||||
} | ||||||
}) | ||||||
|
@@ -284,8 +285,7 @@ export function useComponentBrowserInput( | |||||
function applySourceNode(text: string) { | ||||||
return ( | ||||||
sourceNodeIdentifier.value ? | ||||||
/^[a-zA-Z]/.test(text) ? | ||||||
`${sourceNodeIdentifier.value}.${text}` | ||||||
startsWithIdentifier(sourceNodeIdentifier.value) ? `${sourceNodeIdentifier.value}.${text}` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
: `${sourceNodeIdentifier.value} ${text}` | ||||||
: text | ||||||
) | ||||||
|
This file contains 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 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 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 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 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 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 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
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.
Negative numeric literal case?