Skip to content

Commit

Permalink
Merge branch 'main' into fix/file-search-support-absolute-path
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaaaash authored Jan 24, 2025
2 parents 484693a + dcaae39 commit d305ec4
Show file tree
Hide file tree
Showing 31 changed files with 1,981 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export abstract class BaseCodeEditsSource extends Disposable {
private cancellationTokenSource = new CancellationTokenSource();
private readonly relationID = observableValue<string | undefined>(this, undefined);

protected abstract doTrigger(...args: any[]): MaybePromise<void>;

public readonly codeEditsContextBean = disposableObservableValue<CodeEditsContextBean | undefined>(this, undefined);
public abstract priority: number;
public abstract mount(): IDisposable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,102 @@
import { Injectable } from '@opensumi/di';
import { AINativeSettingSectionsId, ECodeEditsSourceTyping, IDisposable } from '@opensumi/ide-core-common';
import { ICursorPositionChangedEvent, Position } from '@opensumi/ide-monaco';
import { ICursorPositionChangedEvent, IModelContentChangedEvent } from '@opensumi/ide-monaco';
import {
autorunDelta,
derivedHandleChanges,
observableFromEvent,
recomputeInitiallyAndOnChange,
} from '@opensumi/ide-monaco/lib/common/observable';

import { BaseCodeEditsSource } from './base';

export interface ILineChangeData {
currentLineNumber: number;
preLineNumber?: number;
change?: IModelContentChangedEvent;
}

@Injectable({ multiple: true })
export class LineChangeCodeEditsSource extends BaseCodeEditsSource {
public priority = 2;

private prePosition = this.monacoEditor.getPosition();

public mount(): IDisposable {
const modelContentChangeObs = observableFromEvent<IModelContentChangedEvent>(
this,
this.monacoEditor.onDidChangeModelContent,
(event: IModelContentChangedEvent) => event,
);
const positionChangeObs = observableFromEvent<ICursorPositionChangedEvent>(
this,
this.monacoEditor.onDidChangeCursorPosition,
(event: ICursorPositionChangedEvent) => event,
);

const latestModelContentChangeObs = derivedHandleChanges(
{
owner: this,
createEmptyChangeSummary: () => ({ change: undefined }),
handleChange: (ctx, changeSummary: { change: IModelContentChangedEvent | undefined }) => {
// 如果只是改了光标则设置 change 为空,避免获取到缓存的 change
if (ctx.didChange(positionChangeObs)) {
changeSummary.change = undefined;
} else {
changeSummary.change = modelContentChangeObs.get();
}
return true;
},
},
(reader, changeSummary) => {
positionChangeObs.read(reader);
modelContentChangeObs.read(reader);
return changeSummary.change;
},
);

this.addDispose(recomputeInitiallyAndOnChange(latestModelContentChangeObs));

let lastModelContent: IModelContentChangedEvent | undefined;
this.addDispose(
this.monacoEditor.onDidChangeCursorPosition((event: ICursorPositionChangedEvent) => {
const currentPosition = event.position;
if (this.prePosition && this.prePosition.lineNumber !== currentPosition.lineNumber) {
this.doTrigger(currentPosition);
this.prePosition = currentPosition;
}
/**
* 由于 monaco 的 changeModelContent 事件比 changeCursorPosition 事件先触发,所以这里需要拿上一次的值进行消费
* 否则永远返回 undefined
*/
autorunDelta(latestModelContentChangeObs, ({ lastValue }) => {
lastModelContent = lastValue;
}),
);
return this;
}

protected doTrigger(position: Position) {
const isLineChangeEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsLineChange, false);
this.addDispose(
autorunDelta(positionChangeObs, ({ lastValue, newValue }) => {
const contentChange = lastModelContent;

if (!isLineChangeEnabled || !position) {
return;
}
const isLineChangeEnabled = this.preferenceService.getValid(
AINativeSettingSectionsId.CodeEditsLineChange,
false,
);
if (!isLineChangeEnabled) {
return false;
}

this.setBean({
typing: ECodeEditsSourceTyping.LineChange,
position,
data: {
preLineNumber: this.prePosition?.lineNumber,
currentLineNumber: position.lineNumber,
},
});
const prePosition = lastValue?.position;
const currentPosition = newValue?.position;
if (prePosition && prePosition.lineNumber !== currentPosition?.lineNumber) {
this.setBean({
typing: ECodeEditsSourceTyping.LineChange,
position: currentPosition,
data: {
preLineNumber: prePosition.lineNumber,
currentLineNumber: currentPosition.lineNumber,
change: contentChange,
},
});
}

// 消费完之后设置为 undefined,避免下次获取到缓存的值
lastModelContent = undefined;
}),
);

return this;
}
}
14 changes: 4 additions & 10 deletions packages/editor/src/browser/workbench-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ export class WorkbenchEditorServiceImpl extends WithEventBus implements Workbenc
this._onDidEditorGroupsChanged.fire();
}),
);

const editorRestorePromises = [];
const promise = this.topGrid
.deserialize(state, () => this.createEditorGroup(), editorRestorePromises)
Expand Down Expand Up @@ -2313,16 +2314,9 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
async restoreState(state: IEditorGroupState) {
this._restoringState = true;
this.previewURI = state.uris[state.previewIndex] ? new URI(state.uris[state.previewIndex]) : null;
await Promise.all(
state.uris.map(async (uri) => {
await this.doOpen(new URI(uri), {
disableNavigate: true,
backend: true,
preview: false,
deletedPolicy: 'skip',
});
}),
);
for (const uri of state.uris) {
await this.doOpen(new URI(uri), { disableNavigate: true, backend: true, preview: false, deletedPolicy: 'skip' });
}

let targetUri: URI | undefined;
if (state.current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,47 @@ export class ExpressFileServerContribution implements ServerAppContribution {
// 在允许的 contentType
contentType
) {
ctx.set('Content-Type', contentType);
const range = ctx.headers.range;

if (!fs.existsSync(filePath)) {
ctx.status = 404;
ctx.body = '文件未找到';
return;
}

if (this.appConfig.staticAllowOrigin) {
ctx.set('Access-Control-Allow-Origin', this.appConfig.staticAllowOrigin);
}

ctx.body = fs.createReadStream(filePath);
const stats = await fs.promises.stat(filePath);
const total = stats.size;

if (!range) {
ctx.status = 200;
ctx.set('Content-Type', contentType);
ctx.set('Content-Length', String(total));
ctx.body = fs.createReadStream(filePath);
return;
}

const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : total - 1;

if (start >= total || end >= total || start > end) {
ctx.status = 416; // Range Not Satisfiable
ctx.set('Content-Range', `bytes */${total}`);
return;
}

ctx.status = 206;
ctx.set('Content-Range', `bytes ${start}-${end}/${total}`);
ctx.set('Accept-Ranges', 'bytes');
ctx.set('Content-Length', String(end - start + 1));
ctx.set('Content-Type', contentType);

const stream = fs.createReadStream(filePath, { start, end });
ctx.body = stream;
} else {
ctx.status = 403;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/file-scheme/src/browser/preview.view.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { memo } from 'react';

import { Disposable, useInjectable } from '@opensumi/ide-core-browser';
import { StaticResourceService } from '@opensumi/ide-core-browser/lib/static-resource';
Expand All @@ -16,15 +16,14 @@ const useResource = (resource: IResource) => {
};
};

export const VideoPreview: ReactEditorComponent<null> = (props) => {
export const VideoPreview: ReactEditorComponent<null> = memo((props) => {
const { src } = useResource(props.resource);

return (
<div className={styles.kt_video_preview}>
<video autoPlay controls className={styles.kt_video} src={src}></video>
<video playsInline controls className={styles.kt_video} src={src} />
</div>
);
};
});

export const ImagePreview: ReactEditorComponent<null> = (props) => {
const imgRef = React.useRef<HTMLImageElement>();
Expand Down
8 changes: 6 additions & 2 deletions packages/file-scheme/src/browser/style.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@

.kt_video_preview {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
padding: 40px;
}

.kt_video {
width: 100%;
height: 100%;
max-width: 100%;
max-height: 100%;
}

.error-page {
Expand Down
5 changes: 4 additions & 1 deletion packages/notebook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"@opensumi/ide-outline": "workspace:*",
"@opensumi/ide-overlay": "workspace:*",
"@opensumi/ide-theme": "workspace:*",
"antd": "^5.21.4"
"@opensumi/ide-utils": "workspace:*",
"antd": "^5.21.4",
"diff": "^4.0.1",
"resize-observer-polyfill": "1.5.1"
}
}
33 changes: 33 additions & 0 deletions packages/notebook/src/browser/libro-preview.view.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Container, URI, ViewRender } from '@difizen/mana-app';
import React, { useEffect, useState } from 'react';

import { useInjectable } from '@opensumi/ide-core-browser';
import { ReactEditorComponent } from '@opensumi/ide-editor/lib/browser/types';

import { LibroVersionManager } from './libro/diff-view/libro-version-manager';
import { AIStudioLibroVersionView } from './libro/diff-view/libro-version-view';
import { ContentLoaderType, ManaContainer } from './mana';

export const LibroVersionPreview: ReactEditorComponent = ({ resource }) => {
const uri = resource.uri;
const originalUri = uri.scheme === 'diff' ? new URI(decodeURIComponent(uri.getParsedQuery().original)) : undefined;
const targetUri = uri.scheme === 'diff' ? new URI(decodeURIComponent(uri.getParsedQuery().modified)) : undefined;
const manaContainer = useInjectable<Container>(ManaContainer);
const libroVersionManager = manaContainer.get(LibroVersionManager);
const [versionView, setVersionView] = useState<AIStudioLibroVersionView>();

useEffect(() => {
libroVersionManager
.getOrCreateView({
resource: uri.toString(),
loadType: ContentLoaderType,
originalUri,
targetUri,
})
.then((view) => {
setVersionView(view);
});
}, [uri]);

return <div className='libro-version'>{versionView && <ViewRender view={versionView}></ViewRender>}</div>;
};
47 changes: 43 additions & 4 deletions packages/notebook/src/browser/libro.contribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ import { IThemeService, IconType } from '@opensumi/ide-theme/lib/common';

import { KERNEL_PANEL_ID, KernelPanel, initKernelPanelColorToken } from './kernel-panel';
import { LibroOpensumiModule } from './libro';
import { LibroDiffModule } from './libro/diff-view';
import { LibroOpener } from './libro-opener';
import { LibroVersionPreview } from './libro-preview.view';
import { initLibroColorToken } from './libro.color.tokens';
import { LIBRO_COMPONENTS_ID, LIBRO_COMPONENTS_SCHEME_ID } from './libro.protocol';
import { LIBRO_COMPONENTS_SCHEME_ID, LIBRO_COMPONENT_ID, LIBRO_PREVIEW_COMPONENT_ID } from './libro.protocol';
import { OpensumiLibroView } from './libro.view';
import { ManaContainer, initLibroOpensumi, manaContainer } from './mana/index';
import { NotebookDocumentContentProvider } from './notebook-document-content-provider';
Expand All @@ -62,7 +64,14 @@ const LayoutWrapper: React.FC<React.PropsWithChildren> = ({ children }) => {
return (
<ManaComponents.Application
context={{ container: manaContainer }}
modules={[ManaAppPreset, LibroJupyterNoEditorModule, LibroTOCModule, LibroOpensumiModule, LibroVariableModule]}
modules={[
ManaAppPreset,
LibroJupyterNoEditorModule,
LibroDiffModule,
LibroTOCModule,
LibroOpensumiModule,
LibroVariableModule,
]}
renderChildren
onReady={() => setIsReady(true)}
>
Expand Down Expand Up @@ -182,11 +191,17 @@ export class LibroContribution

registerEditorComponent(registry: EditorComponentRegistry) {
registry.registerEditorComponent({
uid: LIBRO_COMPONENTS_ID,
uid: LIBRO_COMPONENT_ID,
scheme: LIBRO_COMPONENTS_SCHEME_ID,
component: OpensumiLibroView,
});

registry.registerEditorComponent({
uid: LIBRO_PREVIEW_COMPONENT_ID,
scheme: LIBRO_COMPONENTS_SCHEME_ID,
component: LibroVersionPreview,
});

registry.registerEditorComponentResolver(Schemes.file, (resource, results) => {
if (resource.uri.path.ext === `.${LIBRO_COMPONENTS_SCHEME_ID}`) {
// 首次打开 notebook 文件时初始化 jupyter 服务连接
Expand All @@ -197,7 +212,31 @@ export class LibroContribution
}
results.push({
type: 'component',
componentId: LIBRO_COMPONENTS_ID,
componentId: LIBRO_COMPONENT_ID,
});
}
});

// git schema 的 notebook 资源,在 ide 中打开
registry.registerEditorComponentResolver('git', (resource, results) => {
if (resource.uri.path.ext === '.ipynb') {
results.push({
type: 'component',
componentId: LIBRO_PREVIEW_COMPONENT_ID,
});
}
});

registry.registerEditorComponentResolver('diff', (resource, results) => {
const { original, modified } = resource.uri.getParsedQuery();
if (
new URI(decodeURIComponent(modified)).path.ext === '.ipynb' ||
new URI(decodeURIComponent(original)).path.ext === '.ipynb'
) {
// TODO: 需要等 git 插件 ready,否则 git uri 无法解析
results.push({
type: 'component',
componentId: LIBRO_PREVIEW_COMPONENT_ID,
});
}
});
Expand Down
4 changes: 3 additions & 1 deletion packages/notebook/src/browser/libro.protocol.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export const LIBRO_COMPONENTS_ID = 'opensumi:libro';
export const LIBRO_COMPONENT_ID = 'opensumi:libro';
export const LIBRO_PREVIEW_COMPONENT_ID = 'opensumi:libro-preview';
export const LIBRO_DIFF_COMPONENT_ID = 'opensumi:libro-diff';
export const LIBRO_COMPONENTS_SCHEME_ID = 'ipynb';
Loading

0 comments on commit d305ec4

Please sign in to comment.