-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
8649995
ad03676
5f90d2e
044c695
dc33a09
f3242fe
dc91f64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"} | ||
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. Negative numeric literal case? |
||
`('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) | ||
}, | ||
) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,8 @@ import { | |||||
type SuggestionEntry, | ||||||
type SuggestionId, | ||||||
} from '@/stores/suggestionDatabase/entry' | ||||||
import { isIdentifier, type AstId, type Identifier } from '@/util/ast/abstract' | ||||||
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' | ||||||
|
@@ -19,7 +20,7 @@ import { Range } from 'ydoc-shared/util/data/range' | |||||
|
||||||
/** Information how the component browser is used, needed for proper input initializing. */ | ||||||
export type Usage = | ||||||
| { type: 'newNode'; sourcePort?: AstId | undefined } | ||||||
| { type: 'newNode'; sourcePort?: Ast.AstId | undefined } | ||||||
| { type: 'editNode'; node: NodeId; cursorPos: number } | ||||||
|
||||||
/** | ||||||
|
@@ -32,6 +33,7 @@ export type ComponentBrowserMode = | |||||
| { | ||||||
mode: 'componentBrowsing' | ||||||
filter: Filter | ||||||
literal?: Ast.TextLiteral | Ast.NumericLiteral | Ast.NegationApp | undefined | ||||||
} | ||||||
| { | ||||||
mode: 'codeEditing' | ||||||
|
@@ -55,7 +57,7 @@ export function useComponentBrowserInput( | |||||
const imports = shallowRef<RequiredImport[]>([]) | ||||||
const processingAIPrompt = ref(false) | ||||||
const toastError = useToast.error() | ||||||
const sourceNodeIdentifier = ref<Identifier>() | ||||||
const sourceNodeIdentifier = ref<Ast.Identifier>() | ||||||
const switchedToCodeMode = ref<{ appliedSuggestion?: SuggestionEntry }>() | ||||||
|
||||||
// Text Model to being edited externally (by user). | ||||||
|
@@ -110,12 +112,20 @@ export function useComponentBrowserInput( | |||||
: {}), | ||||||
} | ||||||
} else { | ||||||
let literal: Ast.MutableTextLiteral | Ast.NumericLiteral | Ast.NegationApp | undefined = | ||||||
Ast.TextLiteral.tryParse(text.value) | ||||||
if (literal == null) { | ||||||
literal = Ast.NumericLiteral.tryParseWithSign(text.value) | ||||||
} else { | ||||||
literal.fixBoundaries() | ||||||
} | ||||||
return { | ||||||
mode: 'componentBrowsing', | ||||||
filter: { | ||||||
pattern: text.value, | ||||||
...(sourceNodeType.value != null ? { selfArg: sourceNodeType.value } : {}), | ||||||
}, | ||||||
literal, | ||||||
} | ||||||
} | ||||||
}) | ||||||
|
@@ -172,7 +182,7 @@ export function useComponentBrowserInput( | |||||
qnJoin( | ||||||
owner.path ? qnLastSegment(owner.path) | ||||||
: owner.project ? qnLastSegment(owner.project) | ||||||
: ('Main' as Identifier), | ||||||
: ('Main' as Ast.Identifier), | ||||||
entry.name, | ||||||
) | ||||||
: entry.name) + ' ', | ||||||
|
@@ -192,7 +202,9 @@ export function useComponentBrowserInput( | |||||
const alreadyAdded = finalImports.some((existing) => requiredImportEquals(existing, anImport)) | ||||||
const importedIdent = | ||||||
anImport.kind == 'Qualified' ? | ||||||
qnLastSegment(anImport.module.path ?? anImport.module.project ?? ('Main' as Identifier)) | ||||||
qnLastSegment( | ||||||
anImport.module.path ?? anImport.module.project ?? ('Main' as Ast.Identifier), | ||||||
) | ||||||
: anImport.import | ||||||
const noLongerNeeded = !text.value.includes(importedIdent) | ||||||
if (!noLongerNeeded && !alreadyAdded) { | ||||||
|
@@ -207,7 +219,7 @@ export function useComponentBrowserInput( | |||||
case 'newNode': | ||||||
if (usage.sourcePort) { | ||||||
const ident = graphDb.getOutputPortIdentifier(usage.sourcePort) | ||||||
sourceNodeIdentifier.value = ident != null && isIdentifier(ident) ? ident : undefined | ||||||
sourceNodeIdentifier.value = ident != null && Ast.isIdentifier(ident) ? ident : undefined | ||||||
} else { | ||||||
sourceNodeIdentifier.value = undefined | ||||||
} | ||||||
|
@@ -234,7 +246,7 @@ export function useComponentBrowserInput( | |||||
const matchedCode = sourceNodeMatch?.[2] | ||||||
if ( | ||||||
matchedSource != null && | ||||||
isIdentifier(matchedSource) && | ||||||
Ast.isIdentifier(matchedSource) && | ||||||
matchedCode != null && | ||||||
graphDb.getIdentDefiningNode(matchedSource) | ||||||
) | ||||||
|
@@ -271,7 +283,12 @@ export function useComponentBrowserInput( | |||||
} | ||||||
|
||||||
function applySourceNode(text: string) { | ||||||
return sourceNodeIdentifier.value ? `${sourceNodeIdentifier.value}.${text}` : text | ||||||
return ( | ||||||
sourceNodeIdentifier.value ? | ||||||
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 | ||||||
) | ||||||
} | ||||||
|
||||||
return proxyRefs({ | ||||||
|
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.
This might be a little simpler with
unwrapOr
.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.
currentModule may be
undefined
, so I wouldn't avoid?: