Skip to content

Commit

Permalink
feat: location opened editor when open file
Browse files Browse the repository at this point in the history
  • Loading branch information
pipiiiiii committed Jun 29, 2023
1 parent dc8c4e5 commit bf53536
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions packages/core-browser/src/common/common.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ export namespace OPEN_EDITORS_COMMANDS {
id: 'opened.editors.copyPath',
category: CATEGORY,
};

export const LOCATION: Command = {
id: 'opened.editors.location',
category: CATEGORY,
};
}

export namespace COMMON_COMMANDS {
Expand Down
32 changes: 22 additions & 10 deletions packages/editor/src/browser/workbench-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { observable } from 'mobx';
import { Injectable, Autowired, Injector, INJECTOR_TOKEN } from '@opensumi/di';
import {
FILE_COMMANDS,
OPEN_EDITORS_COMMANDS,
ResizeEvent,
getSlotLocation,
AppConfig,
Expand Down Expand Up @@ -1340,11 +1341,8 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
this.currentEditor?.monacoEditor.revealRangeInCenter(options.range as monaco.IRange, 0);
}, 0);
}
if ((options && options.disableNavigate) || (options && options.backend)) {
// no-op
} else {
this.locateInFileTree(uri);
}
// 执行定位逻辑
this.locationInTree(options, uri);
this.notifyTabChanged();
return {
group: this,
Expand Down Expand Up @@ -1434,11 +1432,8 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
resource,
}),
);
if ((options && options.disableNavigate) || (options && options.backend)) {
// no-op
} else {
this.locateInFileTree(uri);
}
// 执行定位逻辑
this.locationInTree(options, uri);
this.eventBus.fire(
new EditorGroupChangeEvent({
group: this,
Expand All @@ -1464,12 +1459,29 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
}
}

private locationInTree(options: IResourceOpenOptions, uri: URI) {
if (!options?.backend) {
if (!options?.disableNavigate) {
this.locateInFileTree(uri);
}
if (!options.disableNavigateOnOpendEditor) {
this.locateInOpenedEditor(uri);
}
}
}

private locateInFileTree(uri: URI) {
if (this.explorerAutoRevealConfig) {
this.commands.tryExecuteCommand(FILE_COMMANDS.LOCATION.id, uri);
}
}

private locateInOpenedEditor(uri: URI) {
if (this.explorerAutoRevealConfig) {
this.commands.tryExecuteCommand(OPEN_EDITORS_COMMANDS.LOCATION.id, uri);
}
}

async openUris(uris: URI[]): Promise<void> {
for (const uri of uris) {
await this.open(uri);
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,11 @@ export interface IResourceOpenOptions {
*/
disableNavigate?: boolean;

/**
* 不尝试在已打开的编辑器中的 uri 进行定位
*/
disableNavigateOnOpendEditor?: boolean;

/**
* 是否使用preview模式
* 如果是undefined,使用editor.previewMode配置作为默认值
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CommandService,
FILE_COMMANDS,
EDITOR_COMMANDS,
URI,
} from '@opensumi/ide-core-browser';
import { ClientAppContribution } from '@opensumi/ide-core-browser';
import { ToolbarRegistry, TabBarToolbarContribution } from '@opensumi/ide-core-browser/lib/layout';
Expand Down Expand Up @@ -131,6 +132,12 @@ export class OpenedEditorContribution
this.commandService.executeCommand(FILE_COMMANDS.COPY_RELATIVE_PATH.id, node.uri, [node.uri]);
},
});

commands.registerCommand(OPEN_EDITORS_COMMANDS.LOCATION, {
execute: (uri: URI) => {
this.openedEditorModelService.location(uri);
},
});
}

registerToolbarItems(registry: ToolbarRegistry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ export class OpenedEditorModelService {
if (node.parent && EditorFileGroup.is(node.parent as EditorFileGroup)) {
groupIndex = (node.parent as EditorFileGroup).group.index;
}
this.commandService.executeCommand(EDITOR_COMMANDS.OPEN_RESOURCE.id, node.uri, { groupIndex, preserveFocus: true });
this.commandService.executeCommand(EDITOR_COMMANDS.OPEN_RESOURCE.id, node.uri, { groupIndex, preserveFocus: true, disableNavigateOnOpendEditor: true });
};

public closeFile = (node: EditorFile) => {
Expand Down

0 comments on commit bf53536

Please sign in to comment.