Skip to content

Commit

Permalink
Persist a subset of IdMap (#10347)
Browse files Browse the repository at this point in the history
close #9257

Changelog:
- update: Store in the file only the subset of IdMap containing the IDs used in the metadata section on the 2nd line. The full IdMap is transmitted as a parameter of the `text/applyEdit` request.
  • Loading branch information
4e6 authored Jul 8, 2024
1 parent 30d9775 commit b2c4559
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
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]

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

0 comments on commit b2c4559

Please sign in to comment.