2023-10-04 13:53:54 +03:00
|
|
|
/**
|
|
|
|
* @file A module responsible for translating file edits between the Yjs document updates and the
|
|
|
|
* Language server protocol structures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import diff from 'fast-diff'
|
2024-02-02 12:22:18 +03:00
|
|
|
import type { ModuleUpdate } from '../shared/ast'
|
|
|
|
import { MutableModule, print, spanMapToIdMap } from '../shared/ast'
|
|
|
|
import { EnsoFileParts } from '../shared/ensoFile'
|
2023-10-04 13:53:54 +03:00
|
|
|
import { TextEdit } from '../shared/languageServerTypes'
|
2024-02-02 12:22:18 +03:00
|
|
|
import { assert } from '../shared/util/assert'
|
|
|
|
import { IdMap, ModuleDoc, type VisualizationMetadata } from '../shared/yjsModel'
|
2023-10-04 13:53:54 +03:00
|
|
|
import * as fileFormat from './fileFormat'
|
|
|
|
|
|
|
|
interface AppliedUpdates {
|
2024-02-02 12:22:18 +03:00
|
|
|
newCode: string | undefined
|
|
|
|
newIdMap: IdMap | undefined
|
|
|
|
newMetadata: fileFormat.IdeMetadata['node'] | undefined
|
2023-10-04 13:53:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function applyDocumentUpdates(
|
|
|
|
doc: ModuleDoc,
|
2024-02-02 12:22:18 +03:00
|
|
|
synced: EnsoFileParts,
|
|
|
|
update: ModuleUpdate,
|
2023-10-04 13:53:54 +03:00
|
|
|
): AppliedUpdates {
|
2024-02-02 12:22:18 +03:00
|
|
|
const codeChanged = update.fieldsUpdated.length !== 0
|
|
|
|
let idsChanged = false
|
|
|
|
let metadataChanged = false
|
|
|
|
for (const { changes } of update.metadataUpdated) {
|
|
|
|
for (const [key] of changes) {
|
|
|
|
if (key === 'externalId') {
|
|
|
|
idsChanged = true
|
|
|
|
} else {
|
|
|
|
metadataChanged = true
|
2023-12-19 20:58:11 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-02 12:22:18 +03:00
|
|
|
if (idsChanged && metadataChanged) break
|
2023-12-19 20:58:11 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
let newIdMap = undefined
|
|
|
|
let newCode = undefined
|
|
|
|
let newMetadata = undefined
|
2023-10-04 13:53:54 +03:00
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
const syncModule = new MutableModule(doc.ydoc)
|
|
|
|
const root = syncModule.root()
|
|
|
|
assert(root != null)
|
|
|
|
if (codeChanged || idsChanged || synced.idMapJson == null) {
|
|
|
|
const { code, info } = print(root)
|
|
|
|
if (codeChanged) newCode = code
|
|
|
|
newIdMap = spanMapToIdMap(info)
|
2023-10-04 13:53:54 +03:00
|
|
|
}
|
2024-02-02 12:22:18 +03:00
|
|
|
if (codeChanged || idsChanged || metadataChanged) {
|
|
|
|
// Update the metadata object.
|
|
|
|
// Depth-first key order keeps diffs small.
|
|
|
|
newMetadata = {} satisfies fileFormat.IdeMetadata['node']
|
|
|
|
root.visitRecursiveAst((ast) => {
|
|
|
|
let pos = ast.nodeMetadata.get('position')
|
|
|
|
const vis = ast.nodeMetadata.get('visualization')
|
|
|
|
if (vis && !pos) pos = { x: 0, y: 0 }
|
|
|
|
if (pos) {
|
|
|
|
newMetadata![ast.externalId] = {
|
|
|
|
position: { vector: [Math.round(pos.x), Math.round(-pos.y)] },
|
|
|
|
visualization: vis && translateVisualizationToFile(vis),
|
2023-10-04 13:53:54 +03:00
|
|
|
}
|
|
|
|
}
|
2024-02-02 12:22:18 +03:00
|
|
|
})
|
2023-10-04 13:53:54 +03:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
return { newCode, newIdMap, newMetadata }
|
2023-10-04 13:53:54 +03:00
|
|
|
}
|
|
|
|
|
2023-10-07 23:57:47 +03:00
|
|
|
function translateVisualizationToFile(
|
|
|
|
vis: VisualizationMetadata,
|
|
|
|
): fileFormat.VisualizationMetadata | undefined {
|
|
|
|
let project = undefined
|
2023-12-01 15:41:24 +03:00
|
|
|
switch (vis.identifier?.module.kind) {
|
2023-10-07 23:57:47 +03:00
|
|
|
case 'Builtin':
|
|
|
|
project = { project: 'Builtin' } as const
|
|
|
|
break
|
|
|
|
case 'CurrentProject':
|
|
|
|
project = { project: 'CurrentProject' } as const
|
|
|
|
break
|
|
|
|
case 'Library':
|
2023-12-01 15:41:24 +03:00
|
|
|
project = { project: 'Library', contents: vis.identifier.module.name } as const
|
2023-10-07 23:57:47 +03:00
|
|
|
break
|
|
|
|
default:
|
2023-12-01 15:41:24 +03:00
|
|
|
return { show: vis.visible }
|
2023-10-07 23:57:47 +03:00
|
|
|
}
|
|
|
|
return {
|
2023-12-01 15:41:24 +03:00
|
|
|
name: vis.identifier.name,
|
2023-10-07 23:57:47 +03:00
|
|
|
show: vis.visible,
|
|
|
|
project,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function translateVisualizationFromFile(
|
|
|
|
vis: fileFormat.VisualizationMetadata,
|
|
|
|
): VisualizationMetadata | undefined {
|
|
|
|
let module
|
2023-12-01 15:41:24 +03:00
|
|
|
switch (vis.project?.project) {
|
2023-10-07 23:57:47 +03:00
|
|
|
case 'Builtin':
|
|
|
|
module = { kind: 'Builtin' } as const
|
|
|
|
break
|
|
|
|
case 'CurrentProject':
|
|
|
|
module = { kind: 'CurrentProject' } as const
|
|
|
|
break
|
|
|
|
case 'Library':
|
|
|
|
module = { kind: 'Library', name: vis.project.contents } as const
|
|
|
|
break
|
|
|
|
default:
|
2023-12-01 15:41:24 +03:00
|
|
|
module = null
|
2023-10-07 23:57:47 +03:00
|
|
|
}
|
|
|
|
return {
|
2023-12-01 15:41:24 +03:00
|
|
|
identifier: module && vis.name ? { name: vis.name, module } : null,
|
2023-10-07 23:57:47 +03:00
|
|
|
visible: vis.show,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 13:53:54 +03:00
|
|
|
export function applyDiffAsTextEdits(
|
|
|
|
lineOffset: number,
|
|
|
|
oldString: string,
|
|
|
|
newString: string,
|
|
|
|
): TextEdit[] {
|
|
|
|
const changes = diff(oldString, newString)
|
|
|
|
let newIndex = 0
|
|
|
|
let lineNum = lineOffset
|
|
|
|
let lineStartIdx = 0
|
|
|
|
const edits = []
|
|
|
|
for (const [op, text] of changes) {
|
|
|
|
if (op === 1) {
|
|
|
|
const pos = {
|
|
|
|
character: newIndex - lineStartIdx,
|
|
|
|
line: lineNum,
|
|
|
|
}
|
|
|
|
edits.push({ range: { start: pos, end: pos }, text })
|
|
|
|
const numLineBreaks = (text.match(/\n/g) ?? []).length
|
|
|
|
if (numLineBreaks > 0) {
|
|
|
|
lineNum += numLineBreaks
|
|
|
|
lineStartIdx = newIndex + text.lastIndexOf('\n') + 1
|
|
|
|
}
|
|
|
|
newIndex += text.length
|
|
|
|
} else if (op === -1) {
|
|
|
|
const start = {
|
|
|
|
character: newIndex - lineStartIdx,
|
|
|
|
line: lineNum,
|
|
|
|
}
|
|
|
|
const numLineBreaks = (text.match(/\n/g) ?? []).length
|
|
|
|
const character =
|
|
|
|
numLineBreaks > 0
|
|
|
|
? text.length - (text.lastIndexOf('\n') + 1)
|
|
|
|
: newIndex - lineStartIdx + text.length
|
|
|
|
const end = {
|
|
|
|
character,
|
|
|
|
line: lineNum + numLineBreaks,
|
|
|
|
}
|
|
|
|
edits.push({ range: { start, end }, text: '' })
|
|
|
|
} else if (op === 0) {
|
|
|
|
const numLineBreaks = (text.match(/\n/g) ?? []).length
|
|
|
|
lineNum += numLineBreaks
|
|
|
|
if (numLineBreaks > 0) {
|
|
|
|
lineStartIdx = newIndex + text.lastIndexOf('\n') + 1
|
|
|
|
}
|
|
|
|
newIndex += text.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return edits
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prettyPrintDiff(from: string, to: string): string {
|
|
|
|
const colReset = '\x1b[0m'
|
|
|
|
const colRed = '\x1b[31m'
|
|
|
|
const colGreen = '\x1b[32m'
|
|
|
|
|
|
|
|
const diffs = diff(from, to)
|
|
|
|
if (diffs.length === 1 && diffs[0]![0] === 0) return 'No changes'
|
|
|
|
let content = ''
|
|
|
|
for (let i = 0; i < diffs.length; i++) {
|
|
|
|
const [op, text] = diffs[i]!
|
|
|
|
if (op === 1) {
|
|
|
|
content += colGreen + text
|
|
|
|
} else if (op === -1) {
|
|
|
|
content += colRed + text
|
|
|
|
} else if (op === 0) {
|
|
|
|
content += colReset
|
|
|
|
const numNewlines = (text.match(/\n/g) ?? []).length
|
|
|
|
if (numNewlines < 2) {
|
|
|
|
content += text
|
|
|
|
} else {
|
|
|
|
const firstNewline = text.indexOf('\n')
|
|
|
|
const lastNewline = text.lastIndexOf('\n')
|
|
|
|
const firstLine = text.slice(0, firstNewline + 1)
|
|
|
|
const lastLine = text.slice(lastNewline + 1)
|
|
|
|
const isFirst = i === 0
|
|
|
|
const isLast = i === diffs.length - 1
|
|
|
|
if (!isFirst) content += firstLine
|
|
|
|
if (!isFirst && !isLast) content += '...\n'
|
|
|
|
if (!isLast) content += lastLine
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content += colReset
|
|
|
|
return content
|
|
|
|
}
|