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

Persist a subset of IdMap #10347

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions app/gui2/shared/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
ExpressionId,
FileEdit,
FileSystemObject,
IdMapTriple,
IdMapTuple,
Notifications,
Path,
RegisterOptions,
Expand Down Expand Up @@ -288,8 +290,12 @@ export class LanguageServer extends ObservableV2<Notifications & TransportEvents
}

/** [Documentation](https://github.com/enso-org/enso/blob/develop/docs/language-server/protocol-language-server.md#textapplyedit) */
applyEdit(edit: FileEdit, execute: boolean): Promise<LsRpcResult<void>> {
return this.request('text/applyEdit', { edit, execute })
applyEdit(
edit: FileEdit,
execute: boolean,
idMap?: IdMapTriple[] | IdMapTuple[],
): Promise<LsRpcResult<void>> {
return this.request('text/applyEdit', { edit, execute, idMap })
}

/** [Documentation](https://github.com/enso-org/enso/blob/develop/docs/language-server/protocol-language-server.md#filewrite) */
Expand Down
9 changes: 9 additions & 0 deletions app/gui2/shared/languageServerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ export interface Position {
character: number
}

interface IdMapSpan {
index: { value: number }
size: { value: number }
}

export type IdMapTuple = [IdMapSpan, string]

export type IdMapTriple = [number, number, string]
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved

export type RegisterOptions = { path: Path } | { contextId: ContextId } | {}

export interface CapabilityRegistration {
Expand Down
30 changes: 24 additions & 6 deletions app/gui2/ydoc-server/languageServerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
translateVisualizationFromFile,
} from './edits'
import * as fileFormat from './fileFormat'
import { deserializeIdMap, serializeIdMap } from './serialization'
import { deserializeIdMap, idMapToArray, serializeIdMap } from './serialization'
import { WSSharedDoc } from './ydoc'

const SOURCE_DIR = 'src'
Expand Down Expand Up @@ -457,6 +457,18 @@ class ModulePersistence extends ObservableV2<{ removed: () => void }> {
}
}

private static getIdMapToPersist(
idMap: IdMap | undefined,
metadata: fileFormat.IdeMetadata['node'],
): IdMap | undefined {
if (idMap === undefined) {
return
} else {
const entriesIntersection = idMap.entries().filter(([, id]) => id in metadata)
return new IdMap(entriesIntersection)
}
}

private sendLsUpdate(
synced: EnsoFileParts,
newCode: string | undefined,
Expand All @@ -468,11 +480,17 @@ class ModulePersistence extends ObservableV2<{ removed: () => void }> {
const code = newCode ?? synced.code
const newMetadataJson =
newMetadata &&
json.stringify({ ...this.syncedMeta, ide: { ...this.syncedMeta.ide, node: newMetadata } })
const newIdMapJson = newIdMap && serializeIdMap(newIdMap)
json.stringify({
...this.syncedMeta,
ide: { ...this.syncedMeta.ide, node: newMetadata },
})
const idMapToPersist =
(newIdMap || newMetadata) &&
ModulePersistence.getIdMapToPersist(newIdMap, newMetadata ?? this.syncedMeta.ide.node)
const newIdMapToPersistJson = idMapToPersist && serializeIdMap(idMapToPersist)
const newContent = combineFileParts({
code,
idMapJson: newIdMapJson ?? synced.idMapJson ?? '[]',
idMapJson: newIdMapToPersistJson ?? synced.idMapJson ?? '[]',
metadataJson: newMetadataJson ?? synced.metadataJson ?? '{}',
})

Expand Down Expand Up @@ -502,7 +520,7 @@ class ModulePersistence extends ObservableV2<{ removed: () => void }> {

const execute = newCode != null || newIdMap != null
const edit: FileEdit = { path: this.path, edits, oldVersion: this.syncedVersion, newVersion }
const apply = this.ls.applyEdit(edit, execute)
const apply = this.ls.applyEdit(edit, execute, newIdMap && idMapToArray(newIdMap))
const handleError = (error: unknown) => {
console.error('Could not apply edit:', error)
// Try to recover by reloading the file.
Expand All @@ -521,7 +539,7 @@ class ModulePersistence extends ObservableV2<{ removed: () => void }> {
this.syncedVersion = newVersion
if (newMetadata) this.syncedMeta.ide.node = newMetadata
if (newCode) this.syncedCode = newCode
if (newIdMapJson) this.syncedIdMap = newIdMapJson
if (newIdMapToPersistJson) this.syncedIdMap = newIdMapToPersistJson
if (newMetadataJson) this.syncedMetaJson = newMetadataJson
this.setState(LsSyncState.Synchronized)
}, handleError)
Expand Down
2 changes: 1 addition & 1 deletion app/gui2/ydoc-server/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function serializeIdMap(map: IdMap): string {
return json.stringify(idMapToArray(map))
}

function idMapToArray(map: IdMap): fileFormat.IdMapEntry[] {
export function idMapToArray(map: IdMap): fileFormat.IdMapEntry[] {
const entries: fileFormat.IdMapEntry[] = []
map.entries().forEach(([rangeBuffer, id]) => {
const decoded = sourceRangeFromKey(rangeBuffer)
Expand Down
Loading