mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 11:08:51 +01:00
debt - merge getResource, asFileEditorInput and asFileOrUntitledEditorInput into one helper method
This commit is contained in:
@@ -16,7 +16,6 @@ import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace
|
||||
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
|
||||
import { SyncDescriptor, AsyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
|
||||
import { IInstantiationService, IConstructorSignature0 } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { telemetryURIDescriptor } from 'vs/platform/telemetry/common/telemetry';
|
||||
|
||||
export enum ConfirmResult {
|
||||
SAVE,
|
||||
@@ -336,28 +335,6 @@ export interface IFileEditorInput extends IEditorInput, IEncodingSupport {
|
||||
setPreferredEncoding(encoding: string): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* The base class of untitled editor inputs in the workbench.
|
||||
*/
|
||||
export abstract class UntitledEditorInput extends EditorInput implements IEncodingSupport {
|
||||
|
||||
abstract getResource(): URI;
|
||||
|
||||
abstract isDirty(): boolean;
|
||||
|
||||
abstract suggestFileName(): string;
|
||||
|
||||
abstract getEncoding(): string;
|
||||
|
||||
abstract setEncoding(encoding: string, mode: EncodingMode): void;
|
||||
|
||||
public getTelemetryDescriptor(): { [key: string]: any; } {
|
||||
const descriptor = super.getTelemetryDescriptor();
|
||||
descriptor['resource'] = telemetryURIDescriptor(this.getResource());
|
||||
return descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Side by side editor inputs that have a master and details side.
|
||||
*/
|
||||
@@ -815,37 +792,6 @@ export class TextDiffEditorOptions extends TextEditorOptions {
|
||||
public autoRevealFirstChange: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an input, tries to get the associated URI for it (either file or untitled scheme).
|
||||
*/
|
||||
export function getUntitledOrFileResource(input: IEditorInput, supportDiff?: boolean): URI {
|
||||
if (!input) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Untitled
|
||||
if (input instanceof UntitledEditorInput) {
|
||||
return input.getResource();
|
||||
}
|
||||
|
||||
// File
|
||||
const fileInput = asFileEditorInput(input, supportDiff);
|
||||
|
||||
return fileInput && fileInput.getResource();
|
||||
}
|
||||
|
||||
// TODO@Ben every editor should have an associated resource
|
||||
export function getResource(input: IEditorInput): URI {
|
||||
if (input instanceof EditorInput && typeof (<any>input).getResource === 'function') {
|
||||
const candidate = (<any>input).getResource();
|
||||
if (candidate instanceof URI) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return getUntitledOrFileResource(input, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to return all opened editors with resources not belonging to the currently opened workspace.
|
||||
*/
|
||||
@@ -855,9 +801,9 @@ export function getOutOfWorkspaceEditorResources(editorGroupService: IEditorGrou
|
||||
editorGroupService.getStacksModel().groups.forEach(group => {
|
||||
const editors = group.getEditors();
|
||||
editors.forEach(editor => {
|
||||
const fileInput = asFileEditorInput(editor, true);
|
||||
if (fileInput && !contextService.isInsideWorkspace(fileInput.getResource())) {
|
||||
resources.push(fileInput.getResource());
|
||||
const fileResource = toResource(editor, { supportSideBySide: true, filter: 'file' });
|
||||
if (fileResource && !contextService.isInsideWorkspace(fileResource)) {
|
||||
resources.push(fileResource);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -865,24 +811,6 @@ export function getOutOfWorkspaceEditorResources(editorGroupService: IEditorGrou
|
||||
return resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object as IFileEditorInput only if it matches the signature.
|
||||
*/
|
||||
export function asFileEditorInput(obj: any, supportSideBySide?: boolean): IFileEditorInput {
|
||||
if (!obj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check for side by side if we are asked to
|
||||
if (supportSideBySide && obj instanceof SideBySideEditorInput) {
|
||||
obj = (<SideBySideEditorInput>obj).master;
|
||||
}
|
||||
|
||||
const i = <IFileEditorInput>obj;
|
||||
|
||||
return i instanceof EditorInput && types.areFunctions(i.setResource, i.setEncoding, i.getEncoding, i.getResource, i.setPreferredEncoding) ? i : null;
|
||||
}
|
||||
|
||||
export interface IStacksModelChangeEvent {
|
||||
group: IEditorGroup;
|
||||
editor?: IEditorInput;
|
||||
@@ -991,6 +919,67 @@ export interface ActiveEditorMoveArguments {
|
||||
value?: number;
|
||||
}
|
||||
|
||||
export var EditorCommands = {
|
||||
export const EditorCommands = {
|
||||
MoveActiveEditor: 'moveActiveEditor'
|
||||
};
|
||||
};
|
||||
|
||||
export interface IResourceOptions {
|
||||
supportSideBySide?: boolean;
|
||||
filter?: 'file' | 'untitled' | ['file', 'untitled'] | ['untitled', 'file'];
|
||||
}
|
||||
|
||||
export function hasResource(editor: IEditorInput, options?: IResourceOptions): boolean {
|
||||
return !!toResource(editor, options);
|
||||
}
|
||||
|
||||
export function toResource(editor: IEditorInput, options?: IResourceOptions): URI {
|
||||
if (!editor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check for side by side if we are asked to
|
||||
if (options && options.supportSideBySide && editor instanceof SideBySideEditorInput) {
|
||||
editor = editor.master;
|
||||
}
|
||||
|
||||
const resource = doGetEditorResource(editor);
|
||||
if (!options || !options.filter) {
|
||||
return resource; // return early if no filter is specified
|
||||
}
|
||||
|
||||
if (!resource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let includeFiles: boolean;
|
||||
let includeUntitled: boolean;
|
||||
if (Array.isArray(options.filter)) {
|
||||
includeFiles = (options.filter.indexOf('file') >= 0);
|
||||
includeUntitled = (options.filter.indexOf('untitled') >= 0);
|
||||
} else {
|
||||
includeFiles = (options.filter === 'file');
|
||||
includeUntitled = (options.filter === 'untitled');
|
||||
}
|
||||
|
||||
if (includeFiles && resource.scheme === 'file') {
|
||||
return resource;
|
||||
}
|
||||
|
||||
if (includeUntitled && resource.scheme === 'untitled') {
|
||||
return resource;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO@Ben every editor should have an associated resource
|
||||
function doGetEditorResource(editor: IEditorInput): URI {
|
||||
if (editor instanceof EditorInput && typeof (<any>editor).getResource === 'function') {
|
||||
const candidate = (<any>editor).getResource();
|
||||
if (candidate instanceof URI) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
'use strict';
|
||||
|
||||
import Event, { Emitter, once } from 'vs/base/common/event';
|
||||
import { IEditorRegistry, Extensions, EditorInput, getResource, IEditorStacksModel, IEditorGroup, IEditorIdentifier, IGroupEvent, GroupIdentifier, IStacksModelChangeEvent, IWorkbenchEditorConfiguration, EditorOpenPositioning, SideBySideEditorInput } from 'vs/workbench/common/editor';
|
||||
import { IEditorRegistry, Extensions, EditorInput, toResource, IEditorStacksModel, IEditorGroup, IEditorIdentifier, IGroupEvent, GroupIdentifier, IStacksModelChangeEvent, IWorkbenchEditorConfiguration, EditorOpenPositioning, SideBySideEditorInput } from 'vs/workbench/common/editor';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
@@ -196,7 +196,7 @@ export class EditorGroup implements IEditorGroup {
|
||||
|
||||
for (let i = 0; i < this.editors.length; i++) {
|
||||
const editor = this.editors[i];
|
||||
const editorResource = getResource(editor);
|
||||
const editorResource = toResource(editor, { supportSideBySide: true });
|
||||
if (editorResource && editorResource.toString() === resource.toString()) {
|
||||
return editor;
|
||||
}
|
||||
@@ -553,7 +553,7 @@ export class EditorGroup implements IEditorGroup {
|
||||
}
|
||||
|
||||
private updateResourceMap(editor: EditorInput, remove: boolean): void {
|
||||
const resource = getResource(editor);
|
||||
const resource = toResource(editor, { supportSideBySide: true });
|
||||
if (resource) {
|
||||
|
||||
// It is possible to have the same resource opened twice (once as normal input and once as diff input)
|
||||
|
||||
@@ -7,7 +7,7 @@ import { IDisposable } from 'vs/base/common/lifecycle';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import * as editorCommon from 'vs/editor/common/editorCommon';
|
||||
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
|
||||
import { asFileEditorInput } from 'vs/workbench/common/editor';
|
||||
import { toResource } from 'vs/workbench/common/editor';
|
||||
|
||||
export interface IRangeHighlightDecoration {
|
||||
resource: URI;
|
||||
@@ -47,9 +47,9 @@ export class RangeHighlightDecorations implements IDisposable {
|
||||
}
|
||||
|
||||
private getEditor(resourceRange: IRangeHighlightDecoration): editorCommon.ICommonCodeEditor {
|
||||
const editorInput = asFileEditorInput(this.editorService.getActiveEditorInput());
|
||||
if (editorInput) {
|
||||
if (editorInput.getResource().fsPath === resourceRange.resource.fsPath) {
|
||||
const fileResource = toResource(this.editorService.getActiveEditorInput(), { filter: 'file' });
|
||||
if (fileResource) {
|
||||
if (fileResource.fsPath === resourceRange.resource.fsPath) {
|
||||
return <editorCommon.ICommonCodeEditor>this.editorService.getActiveEditor().getControl();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,18 +10,19 @@ import { suggestFilename } from 'vs/base/common/mime';
|
||||
import labels = require('vs/base/common/labels');
|
||||
import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry';
|
||||
import paths = require('vs/base/common/paths');
|
||||
import { UntitledEditorInput as AbstractUntitledEditorInput, EncodingMode, ConfirmResult } from 'vs/workbench/common/editor';
|
||||
import { EditorInput, IEncodingSupport, EncodingMode, ConfirmResult } from 'vs/workbench/common/editor';
|
||||
import { UntitledEditorModel } from 'vs/workbench/common/editor/untitledEditorModel';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
|
||||
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
|
||||
import Event, { Emitter } from 'vs/base/common/event';
|
||||
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
|
||||
import { telemetryURIDescriptor } from 'vs/platform/telemetry/common/telemetry';
|
||||
|
||||
/**
|
||||
* An editor input to be used for untitled text buffers.
|
||||
*/
|
||||
export class UntitledEditorInput extends AbstractUntitledEditorInput {
|
||||
export class UntitledEditorInput extends EditorInput implements IEncodingSupport {
|
||||
|
||||
public static ID: string = 'workbench.editors.untitledEditorInput';
|
||||
public static SCHEMA: string = 'untitled';
|
||||
@@ -175,6 +176,13 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
|
||||
return model;
|
||||
}
|
||||
|
||||
public getTelemetryDescriptor(): { [key: string]: any; } {
|
||||
const descriptor = super.getTelemetryDescriptor();
|
||||
descriptor['resource'] = telemetryURIDescriptor(this.getResource());
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
public matches(otherInput: any): boolean {
|
||||
if (super.matches(otherInput) === true) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user