From a5bfd57ae91391a128542a248b391eac9fcb8792 Mon Sep 17 00:00:00 2001 From: Omar Sadat Date: Sat, 29 May 2021 08:40:05 -0400 Subject: [PATCH] Enhancement : cursor/selection preserved The cursor or the selection is now preserved when we close an editor and when we use ctrlcmd + p to reopen it. It stays at the location where we left it and doesn't end up at the beginning of the file like before. --- packages/file-search/src/browser/quick-file-open.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/file-search/src/browser/quick-file-open.ts b/packages/file-search/src/browser/quick-file-open.ts index ba99d9014d820..dceed53d9f84d 100644 --- a/packages/file-search/src/browser/quick-file-open.ts +++ b/packages/file-search/src/browser/quick-file-open.ts @@ -30,7 +30,7 @@ import { NavigationLocationService } from '@theia/editor/lib/browser/navigation/ import * as fuzzy from '@theia/core/shared/fuzzy'; import { MessageService } from '@theia/core/lib/common/message-service'; import { FileSystemPreferences } from '@theia/filesystem/lib/browser'; -import { EditorOpenerOptions, Position, Range } from '@theia/editor/lib/browser'; +import { EditorOpenerOptions, EditorWidget, Position, Range } from '@theia/editor/lib/browser'; export const quickFileOpen: Command = { id: 'file-search.openFile', @@ -361,9 +361,15 @@ export class QuickFileOpenService implements QuickOpenModel, QuickOpenHandler { openFile(uri: URI): void { const options = this.buildOpenerOptions(); - const resolvedOpener = this.openerService.getOpener(uri, options); - resolvedOpener + const closedEditor = this.navigationLocationService.closedEditorsStack.find(editor => editor.uri.path.toString() === uri.path.toString()); + this.openerService.getOpener(uri, options) .then(opener => opener.open(uri, options)) + .then(widget => { + // Attempt to restore the editor state if it exists, and no selection is explicitly requested. + if (widget instanceof EditorWidget && closedEditor && !options.selection) { + widget.editor.restoreViewState(closedEditor.viewState); + } + }) .catch(error => this.messageService.error(error)); }