Skip to content
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

Small boolean widget tweaks #8994

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions app/gui2/src/components/GraphEditor/widgets/WidgetCheckbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import CheckboxWidget from '@/components/widgets/CheckboxWidget.vue'
import { Score, WidgetInput, defineWidget, widgetProps } from '@/providers/widgetRegistry'
import { useGraphStore } from '@/stores/graph'
import { requiredImportsByFQN } from '@/stores/graph/imports'
import { SuggestionDb, useSuggestionDbStore } from '@/stores/suggestionDatabase'
import { assert } from '@/util/assert'
import { Ast } from '@/util/ast'
import type { TokenId } from '@/util/ast/abstract.ts'
Expand All @@ -11,31 +13,42 @@ import { computed } from 'vue'

const props = defineProps(widgetProps(widgetDefinition))
const graph = useGraphStore()
const suggestionDb = useSuggestionDbStore()

const trueImport = computed(() =>
requiredImportsByFQN(
suggestionDb.entries,
'Standard.Base.Data.Boolean.Boolean.True' as QualifiedName,
true,
),
)
const falseImport = computed(() =>
requiredImportsByFQN(
suggestionDb.entries,
'Standard.Base.Data.Boolean.Boolean.False' as QualifiedName,
true,
),
)
const value = computed({
get() {
return WidgetInput.valueRepr(props.input)?.endsWith('True') ?? false
},
set(value) {
const edit = graph.startEdit()
const theImport = value ? trueImport.value : falseImport.value
if (props.input.value instanceof Ast.Ast) {
setBoolNode(
const { requiresImport } = setBoolNode(
edit.getVersion(props.input.value),
value ? ('True' as Identifier) : ('False' as Identifier),
)
if (requiresImport) graph.addMissingImports(edit, theImport)
props.onUpdate({ edit })
} else {
graph.addMissingImports(edit, [
{
kind: 'Unqualified',
from: 'Standard.Base.Data.Boolean' as QualifiedName,
import: 'Boolean' as Identifier,
},
])
graph.addMissingImports(edit, theImport)
props.onUpdate({
edit,
portUpdate: {
value: value ? 'Boolean.True' : 'Boolean.False',
value: value ? 'True' : 'False',
origin: asNot<TokenId>(props.input.portId),
},
})
Expand All @@ -54,12 +67,14 @@ function isBoolNode(ast: Ast.Ast) {
: undefined
return candidate && ['True', 'False'].includes(candidate.code())
}
function setBoolNode(ast: Ast.Mutable, value: Identifier) {
function setBoolNode(ast: Ast.Mutable, value: Identifier): { requiresImport: boolean } {
if (ast instanceof Ast.MutablePropertyAccess) {
ast.setRhs(value)
return { requiresImport: false }
} else {
assert(ast instanceof Ast.MutableIdent)
ast.setToken(value)
return { requiresImport: true }
}
}

Expand Down
17 changes: 15 additions & 2 deletions app/gui2/src/components/GraphEditor/widgets/WidgetSelection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
import { useGraphStore } from '@/stores/graph'
import { requiredImports, type RequiredImport } from '@/stores/graph/imports.ts'
import { useSuggestionDbStore } from '@/stores/suggestionDatabase'
import { type SuggestionEntry } from '@/stores/suggestionDatabase/entry.ts'
import {
type SuggestionEntry,
type SuggestionEntryArgument,
} from '@/stores/suggestionDatabase/entry.ts'
import { Ast } from '@/util/ast'
import type { TokenId } from '@/util/ast/abstract.ts'
import { ArgumentInfoKey } from '@/util/callTree'
Expand Down Expand Up @@ -136,11 +139,21 @@ watch(selectedIndex, (_index) => {
</script>

<script lang="ts">
function hasBooleanTagValues(parameter: SuggestionEntryArgument): boolean {
if (parameter.tagValues == null) return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function could be made simpler by creating some sorts of arrayEquals utility and using it here.

if (parameter.tagValues.length != 2) return false
const [first, second] = Array.from(parameter.tagValues).sort()
if (first === 'Boolean.False' && second === 'Boolean.True') return true
return false
}

export const widgetDefinition = defineWidget(WidgetInput.isAstOrPlaceholder, {
priority: 50,
score: (props) => {
if (props.input.dynamicConfig?.kind === 'Single_Choice') return Score.Perfect
if (props.input[ArgumentInfoKey]?.info?.tagValues != null) return Score.Perfect
// Boolean arguments also have tag values, but the checkbox widget should handle them.
if (props.input[ArgumentInfoKey]?.info?.tagValues != null && !hasBooleanTagValues)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the hasBooleanTagValues function is not actually called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we enable some lint for checking it?

return Score.Perfect
return Score.Mismatch
},
})
Expand Down
58 changes: 36 additions & 22 deletions app/gui2/src/stores/graph/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,37 +221,41 @@ function requiredImportToAst(value: RequiredImport, module?: MutableModule) {
}

/** A list of required imports for specific suggestion entry */
export function requiredImports(db: SuggestionDb, entry: SuggestionEntry): RequiredImport[] {
export function requiredImports(
db: SuggestionDb,
entry: SuggestionEntry,
directConImport: boolean = false,
): RequiredImport[] {
const unqualifiedImport = (from: QualifiedName): UnqualifiedImport[] => [
{
kind: 'Unqualified',
from,
import: entry.name,
},
]
switch (entry.kind) {
case SuggestionKind.Module:
return entry.reexportedIn
? [
{
kind: 'Unqualified',
from: entry.reexportedIn,
import: entry.name,
},
]
? unqualifiedImport(entry.reexportedIn)
: [
{
kind: 'Qualified',
module: entry.definedIn,
},
]
case SuggestionKind.Type: {
const from = entry.reexportedIn ? entry.reexportedIn : entry.definedIn
return [
{
kind: 'Unqualified',
from,
import: entry.name,
},
]
}
case SuggestionKind.Constructor: {
const selfType = selfTypeEntry(db, entry)
return selfType ? requiredImports(db, selfType) : []
}
case SuggestionKind.Type:
return unqualifiedImport(entry.reexportedIn ? entry.reexportedIn : entry.definedIn)
case SuggestionKind.Constructor:
if (directConImport) {
return entry.reexportedIn
? unqualifiedImport(entry.reexportedIn)
: entry.memberOf
? unqualifiedImport(entry.memberOf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure this particular line is correct. IIRC, memberOf would be the module in which the type is defined, so from Module import Constructor will probably be invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, memberOf is type on which it's defined. The module where the entity is defined is actually definedIn field.

: []
} else {
const selfType = selfTypeEntry(db, entry)
return selfType ? requiredImports(db, selfType) : []
}
case SuggestionKind.Method: {
const isStatic = entry.selfType == null
const selfType = selfTypeEntry(db, entry)
Expand All @@ -272,6 +276,16 @@ export function requiredImports(db: SuggestionDb, entry: SuggestionEntry): Requi
}
}

export function requiredImportsByFQN(
db: SuggestionDb,
fqn: QualifiedName,
directConImport: boolean = false,
) {
const entry = db.getEntryByQualifiedName(fqn)
if (!entry) return []
return requiredImports(db, entry, directConImport)
}

function selfTypeEntry(db: SuggestionDb, entry: SuggestionEntry): SuggestionEntry | undefined {
if (entry.memberOf) {
return db.getEntryByQualifiedName(entry.memberOf)
Expand Down
Loading