提交 a0bfd49a 编写于 作者: B Benjamin Pasero

use replaceEditors() for BaseSaveFileAction

上级 5fcf4f07
......@@ -44,6 +44,11 @@ export interface IResourceInput {
*/
mime?: string;
/**
* The encoding of the text input if known.
*/
encoding?: string;
/**
* Optional options to use when opening the text input.
*/
......
......@@ -187,6 +187,11 @@ export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
* Sets the absolute file resource URI this input is about.
*/
setResource(resource: URI): void;
/**
* Sets the preferred encodingt to use for this input.
*/
setPreferredEncoding(encoding: string): void;
}
/**
......
......@@ -43,7 +43,7 @@ export class FileEditorInput extends CommonFileEditorInput {
private verboseDescription: string;
/**
* An editor input whos contents are retrieved from file services.
* An editor input who's contents are retrieved from file services.
*/
constructor(
resource: URI,
......@@ -89,6 +89,10 @@ export class FileEditorInput extends CommonFileEditorInput {
this.mime = mime;
}
public setPreferredEncoding(encoding: string): void {
this.preferredEncoding = encoding;
}
public getEncoding(): string {
let textModel = CACHE.get(this.resource);
if (textModel) {
......
......@@ -28,7 +28,7 @@ import {EventType as WorkbenchEventType, EditorEvent} from 'vs/workbench/common/
import {LocalFileChangeEvent, VIEWLET_ID, ITextFileService, TextFileChangeEvent, EventType as FileEventType} from 'vs/workbench/parts/files/common/files';
import {IFileService, IFileStat, IImportResult} from 'vs/platform/files/common/files';
import {DiffEditorInput, toDiffLabel} from 'vs/workbench/common/editor/diffEditorInput';
import {asFileEditorInput, getUntitledOrFileResource, TextEditorOptions, EditorOptions, UntitledEditorInput, ConfirmResult} from 'vs/workbench/common/editor';
import {asFileEditorInput, getUntitledOrFileResource, EditorOptions, UntitledEditorInput, ConfirmResult} from 'vs/workbench/common/editor';
import {FileEditorInput} from 'vs/workbench/parts/files/browser/editors/fileEditorInput';
import {FileStat, NewStatPlaceholder} from 'vs/workbench/parts/files/common/explorerViewModel';
import {ExplorerView} from 'vs/workbench/parts/files/browser/views/explorerView';
......@@ -40,7 +40,7 @@ import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/edito
import {IQuickOpenService} from 'vs/workbench/services/quickopen/common/quickOpenService';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IStorageService} from 'vs/platform/storage/common/storage';
import {Position} from 'vs/platform/editor/common/editor';
import {Position, IResourceInput} from 'vs/platform/editor/common/editor';
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService, IConstructorSignature2} from 'vs/platform/instantiation/common/instantiation';
import {IMessageService, IMessageWithAction, IConfirmation, Severity, CancelAction} from 'vs/platform/message/common/message';
......@@ -1404,8 +1404,6 @@ export abstract class BaseSaveFileAction extends BaseActionWithErrorReporting {
// Save As (or Save untitled with associated path)
if (this.isSaveAs() || source.scheme === 'untitled') {
let positionsOfSource = findSaveAsPositions(this.editorService, source);
let mimeOfSource: string;
if (source.scheme === 'untitled') {
let selectedMime = this.untitledEditorService.get(source).getMime();
......@@ -1423,9 +1421,10 @@ export abstract class BaseSaveFileAction extends BaseActionWithErrorReporting {
}
let selectionOfSource: Selection;
if (positionsOfSource.length) {
const activeEditor = this.editorService.getActiveEditor();
if (activeEditor instanceof BaseTextEditor && positionsOfSource.indexOf(activeEditor.position) >= 0) {
if (activeEditor instanceof BaseTextEditor) {
const activeResource = getUntitledOrFileResource(activeEditor.input);
if (activeResource.toString() === source.toString()) {
selectionOfSource = <Selection>activeEditor.getSelection();
}
}
......@@ -1448,31 +1447,24 @@ export abstract class BaseSaveFileAction extends BaseActionWithErrorReporting {
}
return savePromise.then((target) => {
if (!target) {
return;
if (!target || target.toString() === source.toString()) {
return; // save canceled or same resource used
}
// Reopen editors for the resource based on the positions
let reopenPromise = TPromise.as(null);
if (target.toString() !== source.toString() && positionsOfSource.length) {
let targetInput = this.instantiationService.createInstance(FileEditorInput, target, mimeOfSource, encodingOfSource);
let options = new TextEditorOptions();
options.pinned = true;
if (selectionOfSource) {
options.selection(selectionOfSource.startLineNumber, selectionOfSource.startColumn, selectionOfSource.endLineNumber, selectionOfSource.endColumn);
const replaceWith: IResourceInput = {
resource: target,
mime: mimeOfSource,
encoding: encodingOfSource,
options: {
pinned: true,
selection: selectionOfSource
}
reopenPromise = this.editorService.openEditors(positionsOfSource.map(p => {
return {
position: p,
input: targetInput,
options
};
}));
}
return reopenPromise;
return this.editorService.replaceEditors([{
toReplace: { resource: source },
replaceWith: replaceWith
}]).then(() => true);
});
}
......
......@@ -50,6 +50,8 @@ export abstract class FileEditorInput extends EditorInput implements IFileEditor
public abstract getMime(): string;
public abstract setPreferredEncoding(encoding: string): void;
public abstract setEncoding(encoding: string, mode: EncodingMode): void;
public abstract getEncoding(): string;
......
......@@ -288,7 +288,7 @@ export class WorkbenchEditorService implements IWorkbenchEditorService {
// Base Text Editor Support for file resources
else if (this.fileInputDescriptor && resourceInput.resource instanceof URI && resourceInput.resource.scheme === network.Schemas.file) {
return this.createFileInput(resourceInput.resource, resourceInput.mime);
return this.createFileInput(resourceInput.resource, resourceInput.mime, resourceInput.encoding);
}
// Treat an URI as ResourceEditorInput
......@@ -302,10 +302,11 @@ export class WorkbenchEditorService implements IWorkbenchEditorService {
return TPromise.as<EditorInput>(null);
}
private createFileInput(resource: URI, mime?: string): TPromise<IFileEditorInput> {
private createFileInput(resource: URI, mime?: string, encoding?: string): TPromise<IFileEditorInput> {
return this.instantiationService.createInstance(this.fileInputDescriptor).then((typedFileInput) => {
typedFileInput.setResource(resource);
typedFileInput.setMime(mime || guessMimeTypes(resource.fsPath).join(', '));
typedFileInput.setPreferredEncoding(encoding);
return typedFileInput;
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册