Skip to content

Commit

Permalink
fix(diff-viewer): use slash as sep (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored Sep 13, 2024
1 parent 457d5db commit 0cff95e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 95 deletions.
171 changes: 83 additions & 88 deletions packages/core/src/api/renderDiffViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SlotRenderer,
} from '@opensumi/ide-core-browser';
import merge from 'lodash/merge';
import React, { useEffect } from 'react';
import React, { useEffect, useLayoutEffect, useMemo } from 'react';
import { IDiffViewerProps } from '../core/diff-viewer';
import { DiffViewerModule } from '../core/diff-viewer/module';
import { BoxPanel, SplitPanel } from '../editor';
Expand Down Expand Up @@ -52,105 +52,100 @@ export function DiffViewerLayoutComponent(): React.ReactElement {
}

export const DiffViewerRenderer = (props: IDiffViewerProps) => {
const mergedProps = merge({
appConfig: {},
runtimeConfig: {
onWillApplyTheme: props.onWillApplyTheme,
tabBarRightExtraContent: props.tabBarRightExtraContent,
} as RuntimeConfig,
}, props) as IAppRendererProps;
const workspaceDir = useConstant(() => props?.appConfig?.workspaceDir || 'workspace-' + randomString(8));

if (!mergedProps.appConfig.injector) {
mergedProps.appConfig.injector = new Injector();
}

const injector = mergedProps.appConfig.injector;

injector.addProviders({
token: IDiffViewerProps,
useValue: mergedProps,
});

const appConfig = mergedProps.appConfig;

let appModules: ModuleConstructor[] = appConfig?.modules || [];
if (!appModules.includes(DiffViewerModule)) {
appModules = [
DiffViewerModule,
...appModules,
];
}

delete appConfig?.modules;

const workspaceDir = appConfig?.workspaceDir || 'workspace-' + randomString(8);
delete (appConfig as Partial<IAppRendererProps['appConfig']>)?.workspaceDir;

const layoutConfig = appConfig?.layoutConfig || defaultLayoutConfig;
delete appConfig?.layoutConfig;

const layoutComponent = appConfig?.layoutComponent || DiffViewerLayoutComponent;
delete appConfig?.layoutComponent;

useEffect(() => {
const prefix = randomString(8);
registerLocalStorageProvider(GeneralSettingsId.Theme, workspaceDir, prefix);
useLayoutEffect(() => {
registerLocalStorageProvider(GeneralSettingsId.Theme, workspaceDir, workspaceDir);
return () => {
for (var i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i)!;
if (key.startsWith(prefix)) {
if (key.startsWith(workspaceDir)) {
localStorage.removeItem(key);
}
}
};
}, []);

const diffViewerAppConfig: IAppRendererProps = merge<IAppRendererProps, Partial<IAppRendererProps>>({
appConfig: {
modules: appModules,
workspaceDir,
layoutComponent,
layoutConfig,
disableRestoreEditorGroupState: true,
extensionMetadata,
defaultPreferences: {
'general.theme': 'opensumi-light',
'editor.minimap': false,
'ai.native.inlineDiff.preview.mode': 'inlineLive',
'editor.showActionWhenGroupEmpty': true,
'editor.autoSave': 'afterDelay',
'application.confirmExit': 'never',
'editor.guides.bracketPairs': false,
'editor.quickSuggestionsDelay': 10,
'editor.previewMode': false,
'editor.autoSaveDelay': 1000, // one second
'editor.fixedOverflowWidgets': true, // widget editor 默认改为 fixed
'editor.unicodeHighlight.ambiguousCharacters': false,
'editor.preventScrollAfterFocused': true,
'files.exclude': {
...FILES_DEFAULTS.filesExclude,
// browserfs OverlayFS 用来记录删除的文件
[`**${deletionLogPath}`]: true,
}, [workspaceDir]);

const diffViewerAppConfig: IAppRendererProps = useMemo(() => {
const mergedProps = merge({
appConfig: {},
runtimeConfig: {
onWillApplyTheme: props.onWillApplyTheme,
tabBarRightExtraContent: props.tabBarRightExtraContent,
} as RuntimeConfig,
}, props) as IAppRendererProps;

if (!mergedProps.appConfig.injector) {
mergedProps.appConfig.injector = new Injector();
}

const injector = mergedProps.appConfig.injector;

injector.addProviders({
token: IDiffViewerProps,
useValue: mergedProps,
});

const appConfig = mergedProps.appConfig;

let appModules: ModuleConstructor[] = appConfig?.modules || [];
if (!appModules.includes(DiffViewerModule)) {
appModules = [
DiffViewerModule,
...appModules,
];
}

delete props?.appConfig?.modules;
delete props?.appConfig?.workspaceDir;

return merge<IAppRendererProps, Partial<IAppRendererProps>>({
appConfig: {
modules: appModules,
workspaceDir,
layoutComponent: DiffViewerLayoutComponent,
layoutConfig: defaultLayoutConfig,
disableRestoreEditorGroupState: true,
extensionMetadata,
defaultPreferences: {
'general.theme': 'opensumi-light',
'editor.minimap': false,
'ai.native.inlineDiff.preview.mode': 'inlineLive',
'editor.showActionWhenGroupEmpty': true,
'editor.autoSave': 'afterDelay',
'application.confirmExit': 'never',
'editor.guides.bracketPairs': false,
'editor.quickSuggestionsDelay': 10,
'editor.previewMode': false,
'editor.autoSaveDelay': 1000, // one second
'editor.fixedOverflowWidgets': true, // widget editor 默认改为 fixed
'editor.unicodeHighlight.ambiguousCharacters': false,
'editor.preventScrollAfterFocused': true,
'files.exclude': {
...FILES_DEFAULTS.filesExclude,
// browserfs OverlayFS 用来记录删除的文件
[`**${deletionLogPath}`]: true,
},
},
},
},
runtimeConfig: ({
aiNative: {
enable: true,
capabilities: {
supportsInlineChat: true,
runtimeConfig: ({
aiNative: {
enable: true,
capabilities: {
// 必须要开,否则采纳按钮不可用
supportsInlineChat: true,
},
},
},
startupEditor: 'none',
workspace: {
filesystem: {
fs: 'InMemory',
options: {},
startupEditor: 'none',
workspace: {
filesystem: {
fs: 'InMemory',
options: {},
},
},
},
}),
}, mergedProps);

}),
}, mergedProps);
}, [props]);
return (
<AppRenderer
{...diffViewerAppConfig}
Expand Down
20 changes: 13 additions & 7 deletions packages/core/src/core/diff-viewer/internal/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
IChatProgress,
ILogger,
isArray,
isWindows,
MaybePromise,
Sequencer,
URI,
} from '@opensumi/ide-core-common';
import { IResourceOpenOptions, WorkbenchEditorService } from '@opensumi/ide-editor';
import { Selection, SelectionDirection } from '@opensumi/ide-monaco';
import { toSlashes } from '@opensumi/ide-utils/lib/path';

import { Autowired } from '@opensumi/di';
import { InlineChatController } from '@opensumi/ide-ai-native/lib/browser/widget/inline-chat/inline-chat-controller';
Expand Down Expand Up @@ -85,8 +87,12 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
return URI.file(this.getFullPath(filePath));
}

stripDirectory(filePath: string) {
const result = removeStart(filePath, this.appConfig.workspaceDir);
normalizePath(filePath: string) {
let result = removeStart(filePath, this.appConfig.workspaceDir);
if (isWindows) {
result = toSlashes(result);
}

if (result.startsWith('/')) {
return result.slice(1);
}
Expand Down Expand Up @@ -239,7 +245,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
};

getFilePathForEditor = (editor: IEditor) => {
return this.stripDirectory(editor.currentUri!.codeUri.fsPath);
return this.normalizePath(editor.currentUri!.codeUri.fsPath);
};

getAllTabs = (): IDiffViewerTab[] => {
Expand All @@ -248,12 +254,12 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri

return resources.map((editor, idx) => ({
index: idx,
filePath: this.stripDirectory(editor.uri.codeUri.fsPath),
filePath: this.normalizePath(editor.uri.codeUri.fsPath),
}));
};

getFileIndex = (filePath: string) => {
const aPath = this.stripDirectory(filePath);
const aPath = this.normalizePath(filePath);
return this.getAllTabs().findIndex((tab) => tab.filePath === aPath);
};

Expand Down Expand Up @@ -359,7 +365,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
const fsPath = e.uri.fsPath;

this._onPartialEditEvent.fire({
filePath: this.stripDirectory(fsPath),
filePath: this.normalizePath(fsPath),
...e,
});
}));
Expand All @@ -370,7 +376,7 @@ export class DiffViewerContribution implements ClientAppContribution, MenuContri
let currentIndex = -1;
if (newPath) {
currentIndex = this.getFileIndex(newPath);
newPath = this.stripDirectory(newPath);
newPath = this.normalizePath(newPath);
}

const event = {
Expand Down

0 comments on commit 0cff95e

Please sign in to comment.