Skip to content

Commit

Permalink
Merge pull request #179 from opensumi/v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored Sep 13, 2024
2 parents bad35d8 + 5b3e12b commit 457d5db
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 132 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "2.0.2"
"version": "2.0.4"
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-cli",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-cli",
"main": "lib/commander.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/code-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-code-api",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-code-api",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/code-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-code-service",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-code-service",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-common",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-common",
"main": "lib/index.js",
"typing": "lib/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './mutex';
export * from './request';
export * from './types';
22 changes: 22 additions & 0 deletions packages/common/src/mutex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export class Mutex {
private mutex = Promise.resolve();

lock(): PromiseLike<() => void> {
let begin: (unlock: () => void) => void = () => {};

this.mutex = this.mutex.then(() => new Promise(begin));

return new Promise((res) => {
begin = res;
});
}

async dispatch<T>(fn: () => PromiseLike<T>): Promise<T> {
const unlock = await this.lock();
try {
return await Promise.resolve(fn());
} finally {
unlock();
}
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-core",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-core",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
41 changes: 27 additions & 14 deletions packages/core/src/core/diff-viewer/internal/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
private readonly _onDidTabChange = this._disposables.add(new Emitter<ITabChangedEvent>());
public readonly onDidTabChange: Event<ITabChangedEvent> = this._onDidTabChange.event;

private sequencer = new Sequencer();
private fileSequencer = new Sequencer();

getFullPath(filePath: string) {
return path.join(this.appConfig.workspaceDir, filePath);
}
Expand All @@ -90,18 +93,22 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
return result;
}

openFileInTab = async (filePath: string, content: string, options?: IResourceOpenOptions) => {
private async _openFileInTab(filePath: string, content: string, options?: IResourceOpenOptions) {
const fullPath = this.getFullPath(filePath);
if (!fsExtra.pathExistsSync(fullPath)) {
fsExtra.ensureFileSync(fullPath);
fsExtra.writeFileSync(fullPath, content);
if (!await fsExtra.pathExists(fullPath)) {
await fsExtra.ensureFile(fullPath);
await fsExtra.writeFile(fullPath, content);
}

const uri = URI.file(fullPath);
return {
uri,
result: await this.workbenchEditorService.open(uri, options),
};
}

openFileInTab = async (filePath: string, content: string, options?: IResourceOpenOptions) => {
return this.fileSequencer.queue(() => this._openFileInTab(filePath, content, options));
};

private _openDiffInTab = async (
Expand Down Expand Up @@ -165,8 +172,12 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
previewer.revealFirstDiff();
};

private sequencer = new Sequencer();
openDiffInTab = async (filePath, oldContent, newContent, options?: IResourceOpenOptions) => {
openDiffInTab = async (
filePath: string,
oldContent: string,
newContent: string,
options?: IResourceOpenOptions,
) => {
await this.sequencer.queue(() => this._openDiffInTab(filePath, oldContent, newContent, options));
};

Expand Down Expand Up @@ -304,7 +315,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
toChangedLines: 0,
};

const snapshot = resourceDiff.createSnapshot();
const snapshot = resourceDiff.currentSnapshotStore || resourceDiff.createSnapshot();
const list = snapshot.decorationSnapshotData.partialEditWidgetList;
const unresolved = list.filter(v => v.status === 'pending');
result.total = list.length;
Expand All @@ -317,15 +328,17 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
}

getDiffInfoForUri = (uri: URI) => {
let resourceDiff = (this.inlineDiffHandler as any)._previewerNodeStore.get(uri.toString()) as
| InlineStreamDiffHandler
| undefined;
let resourceDiff: InlineStreamDiffHandler | undefined;

const previewer = this.inlineDiffHandler.getPreviewer() as LiveInlineDiffPreviewer;
if (previewer && previewer.isModel(uri.toString())) {
resourceDiff = previewer.getNode();
}

if (!resourceDiff) {
const previewer = this.inlineDiffHandler.getPreviewer() as LiveInlineDiffPreviewer;
if (previewer && previewer.isModel(uri.toString())) {
resourceDiff = previewer.getNode();
}
resourceDiff = (this.inlineDiffHandler as any)._previewerNodeStore.get(uri.toString()) as
| InlineStreamDiffHandler
| undefined;
}

return this.computeDiffInfo(resourceDiff) || {
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-i18n",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-i18n",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-plugin",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-plugin",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/registry/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-registry",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-registry",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/startup/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-startup",
"version": "2.0.2",
"version": "2.0.4",
"description": "@codeblitzjs/ide-startup",
"main": "lib/index.js",
"typing": "types/index.d.ts",
Expand Down
120 changes: 64 additions & 56 deletions packages/startup/src/provider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,67 +1,75 @@
import { AppProvider, CodeEditor, DiffEditor } from '@codeblitzjs/ide-core';
import React from 'react';
import React, { useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import '@codeblitzjs/ide-core/languages';
import { SampleModule } from './module'
import { SampleModule, diffsDeferred } from './module'
import '../index.css';
import './index.css'

const App = () => (
<AppProvider
appConfig={{
workspaceDir: 'my-workspace',
layoutConfig: {},
modules: [SampleModule],
}}
runtimeConfig={{
biz: 'startup',
workspace: {
filesystem: {
fs: 'FileIndexSystem',
options: {
// 初始全量文件索引
requestFileIndex() {
return Promise.resolve({
'main.html': '<div id="root"></div>',
'main.css': 'body {}',
'main.js': 'console.log("main")',
'package.json': '{\n "name": "startup"\n}',
});
},
},
}
},
}}
>
<CodeEditor
uri="main.js"
style={{ width: 1000, height: 300, marginBottom: 16 }}
editorOptions={{
scrollbar: {
alwaysConsumeMouseWheel: false
}
const App = () => {
const [diffs, setDiffs] = useState<{filePath: string, oldFileContent: string | null, newFileContent: string | null }[]>([])

useEffect(() => {
setTimeout(() => {
const diffs = [{
filePath: 'a.js',
oldFileContent: null,
newFileContent: 'console.log(123)'
}, {
filePath: 'a1.js',
oldFileContent: 'const add = (x, y) => {\n return x + y\n}',
newFileContent: 'const add = (x, y) => {\n return x + y + 1\n}'
}]
setDiffs(diffs)
diffsDeferred.resolve(diffs)
}, 1000)
}, [])

if (!diffs) return null

return (
<AppProvider
appConfig={{
workspaceDir: 'my-workspace',
layoutConfig: {},
modules: [SampleModule],
}}
/>
<CodeEditor
uri="main.css"
style={{ width: 1000, height: 300 }}
editorOptions={{
scrollbar: {
alwaysConsumeMouseWheel: false
}
runtimeConfig={{
biz: 'startup',
}}
/>
<DiffEditor
originalUri="sample:/a1.js"
modifiedUri="sample:/a2.js"
editorOptions={{
scrollbar: {
alwaysConsumeMouseWheel: false
>
{diffs.map(({ filePath, oldFileContent, newFileContent }) => {
if (!oldFileContent) {
return (
<CodeEditor
key={filePath}
uri={`sample://new/${filePath}`}
style={{ width: 1000, height: 300, marginBottom: 16 }}
editorOptions={{
scrollbar: {
alwaysConsumeMouseWheel: false
}
}}
/>
)
} else {
return (
<DiffEditor
key={filePath}
originalUri={`sample://old/${filePath}`}
modifiedUri={`sample://new/${filePath}`}
editorOptions={{
scrollbar: {
alwaysConsumeMouseWheel: false
}
}}
style={{ width: 1000, height: 300 }}
/>
)
}
}}
style={{ width: 1000, height: 300 }}
/>
</AppProvider>
);
})}
</AppProvider>
)
};

createRoot(document.getElementById('main') as HTMLElement).render(<App />);
21 changes: 8 additions & 13 deletions packages/startup/src/provider/module.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { Autowired, Injectable, Provider } from '@opensumi/di';
import { BrowserModule } from '@opensumi/ide-core-browser';
import { IEditorDocumentModelContentProvider, BrowserEditorContribution, IEditorDocumentModelContentRegistry } from '@opensumi/ide-editor/lib/browser'
import { URI, Emitter, Event, Domain } from '@opensumi/ide-core-common'
import { URI, Emitter, Event, Domain, Deferred } from '@opensumi/ide-core-common'

const contentMap = {
'a1.js': `const add = (x, y) => {
return x + y
}
`,
'a2.js': `const add = (x, y) => {
return x + y + 1
}
`,
}
export const diffsDeferred = new Deferred<{filePath: string, oldFileContent: string | null, newFileContent: string }[]>()

@Injectable()
export class SampleSchemeDocumentProvider implements IEditorDocumentModelContentProvider {
Expand All @@ -21,11 +12,15 @@ export class SampleSchemeDocumentProvider implements IEditorDocumentModelContent
}

async provideEditorDocumentModelContent(uri: URI): Promise<string> {
return contentMap[uri.codeUri.path.slice(1)]
const diffs = await diffsDeferred.promise
const diff = diffs.find(item => item.filePath === uri.codeUri.path.slice(1))
if (!diff) return ''
if (uri.authority === 'new') return diff.newFileContent
return diff.oldFileContent || ''
}

isReadonly() {
return true;
return false;
}

private _onDidChangeContent: Emitter<URI> = new Emitter();
Expand Down
2 changes: 1 addition & 1 deletion packages/sumi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codeblitzjs/ide-sumi-core",
"version": "2.0.2",
"version": "2.0.4",
"description": "core",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
Loading

0 comments on commit 457d5db

Please sign in to comment.