The Editor Service (IEditorService) is a workbench service that manages opening, closing, and interacting with editors across editor groups. It provides a high-level API for editor operations and tracks the active and visible editors.
The Editor Service is defined in src/vs/workbench/services/editor/common/editorService.ts:17 and serves as the primary interface for editor management.
Target group: ACTIVE_GROUP, SIDE_GROUP, AUX_WINDOW_GROUP, or group identifier
Copy
Ask AI
import { ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';// Open file in active groupawait editorService.openEditor({ resource: URI.file('/path/to/file.ts')});// Open with optionsawait editorService.openEditor({ resource: URI.file('/path/to/file.ts'), options: { selection: { startLineNumber: 10, startColumn: 1 }, pinned: true, preserveFocus: false, revealIfOpened: true }});// Open in side groupawait editorService.openEditor( { resource: URI.file('/path/to/file.ts') }, SIDE_GROUP);// Open in specific groupconst group = editorGroupService.getGroup(2);await editorService.openEditor( { resource: URI.file('/path/to/file.ts') }, group);
Group Constants
Copy
Ask AI
// Open in currently active groupexport const ACTIVE_GROUP = -1;// Open to the side of the active groupexport const SIDE_GROUP = -2;// Open in auxiliary windowexport const AUX_WINDOW_GROUP = -3;// Open in modal overlayexport const MODAL_GROUP = -4;
import { ICloseEditorOptions } from 'vs/workbench/services/editor/common/editorGroupsService';// Close specific editorconst editorToClose = editorService.findEditors(uri)[0];if (editorToClose) { await editorService.closeEditor(editorToClose);}// Close with optionsawait editorService.closeEditor(editorToClose, { preserveFocus: true, // Don't change focus skipConfirm: false // Still ask to save if dirty});// Close multiple editorsconst editorsToClose = editorService.findEditors(folderUri);await editorService.closeEditors(editorsToClose);
import { IEditorsChangeEvent } from 'vs/workbench/services/editor/common/editorService';// Listen to any editor change in any groupeditorService.onDidEditorsChange((event: IEditorsChangeEvent) => { console.log(`Change in group ${event.groupId}`); console.log('Event kind:', event.event.kind); if (event.event.editor) { console.log('Affected editor:', event.event.editor.getName()); }});
export class LayoutManager { constructor( @IEditorService private editorService: IEditorService ) {} async openSideBySide(leftUri: URI, rightUri: URI): Promise<void> { // Open first file in active group await this.editorService.openEditor( { resource: leftUri }, ACTIVE_GROUP ); // Open second file to the side await this.editorService.openEditor( { resource: rightUri }, SIDE_GROUP ); } async openMultipleFiles(uris: URI[]): Promise<void> { // Open all files at once const inputs = uris.map(uri => ({ resource: uri })); await this.editorService.openEditors(inputs, ACTIVE_GROUP); }}
Use Untyped Inputs: Prefer untyped editor inputs ({ resource: uri }) over creating EditorInput instances. This allows the editor resolver to select the appropriate editor.
Preserve Focus: When opening editors in the background, use preserveFocus: true to keep the user’s current focus.
Handle Missing Files: Always handle cases where files might not exist or fail to open. Check isOpened() before performing operations.
Group Management: Use SIDE_GROUP to open editors to the side, but consider using IEditorGroupsService for more complex layout requirements.
Event Disposal: Always dispose of event listeners when your component is disposed to prevent memory leaks.