Fix addRequiredImport so it is not fooled by existing module changes (#8813)

A follow-up of [this comment](https://github.com/enso-org/enso/pull/8740#discussion_r1452282468)

`addRequiredImport` now reads the existing import from the edit instead of taking them from the current module. This way, it will consider any imports added so far as part of this edit.
This commit is contained in:
Adam Obuchowicz 2024-01-24 13:03:25 +01:00 committed by GitHub
parent edfcfde11c
commit f5c02d36e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 21 deletions

View File

@ -144,6 +144,22 @@ export interface UnqualifiedImport {
import: Identifier
}
/** Read imports from given module block */
export function readImports(ast: Ast.Ast): Import[] {
const imports: Import[] = []
ast.visitRecursive((node) => {
if (node instanceof Ast.Import) {
const recognized = recognizeImport(node)
if (recognized) {
imports.push(recognized)
}
return false
}
return true
})
return imports
}
/** Insert the given imports into the given block at an appropriate location. */
export function addImports(
edit: MutableModule,
@ -288,10 +304,10 @@ export function covers(existing: Import, required: RequiredImport): boolean {
}
export function filterOutRedundantImports(
existing: { import: Import }[],
existing: Import[],
required: RequiredImport[],
): RequiredImport[] {
return required.filter((info) => !existing.some((existing) => covers(existing.import, info)))
return required.filter((info) => !existing.some((existing) => covers(existing, info)))
}
if (import.meta.vitest) {

View File

@ -5,6 +5,7 @@ import { GraphDb } from '@/stores/graph/graphDatabase'
import {
addImports,
filterOutRedundantImports,
readImports,
recognizeImport,
type Import,
type RequiredImport,
@ -95,7 +96,6 @@ export const useGraphStore = defineStore('graph', () => {
)
const portInstances = reactive(new Map<PortId, Set<PortViewInstance>>())
const editedNodeInfo = ref<NodeEditInfo>()
const imports = ref<{ import: Import; span: SourceRange }[]>([])
const methodAst = ref<Ast.Function>()
const currentNodeIds = ref(new Set<ExprId>())
@ -126,18 +126,6 @@ export const useGraphStore = defineStore('graph', () => {
moduleRoot.value = newRoot.exprId
module.doc.setIdMap(idMap_)
imports.value = []
newRoot.visitRecursive((node) => {
if (node instanceof Ast.Import) {
const recognized = recognizeImport(node)
if (recognized) {
imports.value.push({ import: recognized, span: node.span! })
}
return false
}
return true
})
methodAst.value = methodAstInModule(astModule)
if (methodAst.value) {
currentNodeIds.value = db.readFunctionAst(methodAst.value, (id) => meta.get(id))
@ -257,14 +245,15 @@ export const useGraphStore = defineStore('graph', () => {
function addMissingImports(edit: MutableModule, newImports: RequiredImport[]) {
if (!newImports.length) return
const topLevel_ = topLevel.value
if (!topLevel_) {
console.error(`BUG: Cannot add required imports: No module root.`)
const topLevel = moduleRoot.value ? edit.get(moduleRoot.value) : null
if (!topLevel || !(topLevel instanceof Ast.BodyBlock)) {
console.error(`BUG: Cannot add required imports: No BodyBlock module root.`)
return
}
const importsToAdd = filterOutRedundantImports(imports.value, newImports)
const existingImports = readImports(topLevel)
const importsToAdd = filterOutRedundantImports(existingImports, newImports)
if (!importsToAdd.length) return
addImports(edit, topLevel_, importsToAdd)
addImports(edit, topLevel, importsToAdd)
}
function deleteNodes(ids: ExprId[]) {
@ -525,7 +514,6 @@ export const useGraphStore = defineStore('graph', () => {
transact,
db: markRaw(db),
mockExpressionUpdate,
imports,
editedNodeInfo,
unconnectedEdge,
edges,