-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: editor state recover & lsp & search & session
- Loading branch information
Showing
34 changed files
with
741 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/libro-code-editor/src/code-editor-state-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Contribution } from '@difizen/mana-app'; | ||
import { Priority } from '@difizen/mana-app'; | ||
import { contrib } from '@difizen/mana-app'; | ||
import { singleton } from '@difizen/mana-app'; | ||
|
||
import type { IModel } from './code-editor-model.js'; | ||
import type { EditorState, IEditorStateOptions } from './code-editor-protocol.js'; | ||
import { CodeEditorContribution } from './code-editor-protocol.js'; | ||
|
||
@singleton() | ||
export class CodeEditorStateManager { | ||
protected readonly codeEditorProvider: Contribution.Provider<CodeEditorContribution>; | ||
protected stateCache: Map<string, EditorState> = new Map(); | ||
|
||
constructor( | ||
@contrib(CodeEditorContribution) | ||
codeEditorProvider: Contribution.Provider<CodeEditorContribution>, | ||
) { | ||
this.codeEditorProvider = codeEditorProvider; | ||
} | ||
|
||
protected findCodeEditorProvider(model: IModel) { | ||
const prioritized = Priority.sortSync( | ||
this.codeEditorProvider.getContributions(), | ||
(contribution) => contribution.canHandle(model.mimeType), | ||
); | ||
const sorted = prioritized.map((c) => c.value); | ||
return sorted[0]; | ||
} | ||
|
||
async getOrCreateEditorState(option: IEditorStateOptions): Promise<EditorState> { | ||
if (this.stateCache.has(option.uuid)) { | ||
const state = this.stateCache.get(option.uuid)!; | ||
return state; | ||
} | ||
const factory = this.findCodeEditorProvider(option.model)?.stateFactory; | ||
if (!factory) { | ||
throw new Error( | ||
`no code editor state factory registered for mimetype: ${option.model.mimeType}`, | ||
); | ||
} | ||
const state = factory(option); | ||
this.stateCache.set(option.uuid, state); | ||
return state; | ||
} | ||
|
||
updateEditorState(id: string, state: EditorState) { | ||
this.stateCache.set(id, state); | ||
} | ||
|
||
removeEditorState(id: string) { | ||
this.stateCache.delete(id); | ||
} | ||
} |
Oops, something went wrong.