Skip to content

Commit

Permalink
follow up 💄 on #12448
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Mar 16, 2017
1 parent 6159f87 commit e134f42
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 58 deletions.
7 changes: 7 additions & 0 deletions src/vs/base/common/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ export class ResourceMap<T> {
this.map.forEach(clb);
}

public values(): T[] {
const values: T[] = [];
this.map.forEach(value => values.push(value));

return values;
}

private toKey(resource: URI): string {
let key: string;

Expand Down
16 changes: 16 additions & 0 deletions src/vs/base/test/common/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ suite('Map', () => {
map.set(resource2, '2');
map.set(resource3, true);

const values = map.values();
assert.equal(values[0], 1);
assert.equal(values[1], '2');
assert.equal(values[2], true);

let counter = 0;
map.forEach(value => {
assert.equal(value, values[counter++]);
});

const obj = Object.create(null);
map.set(resource4, obj);

Expand Down Expand Up @@ -384,6 +394,12 @@ suite('Map', () => {
assert.ok(!map.get(resource2));
assert.ok(!map.get(resource3));
assert.ok(!map.has(resource1));

map.set(resource1, false);
map.set(resource2, 0);

assert.ok(map.has(resource1));
assert.ok(map.has(resource2));
});

test('ResourceMap - files', function () {
Expand Down
41 changes: 37 additions & 4 deletions src/vs/platform/files/common/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import events = require('vs/base/common/events');
import { isLinux } from 'vs/base/common/platform';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import Event from 'vs/base/common/event';
import { Schemas } from 'vs/base/common/network';

export const IFileService = createDecorator<IFileService>('fileService');

Expand Down Expand Up @@ -290,17 +291,49 @@ export class FileChangesEvent extends events.Event {
}
}

export function isEqual(path1: string, path2: string): boolean {
const identityEquals = (path1 === path2);
export function isEqual(resourceA: URI, resourceB: URI): boolean;
export function isEqual(pathA: string, pathB: string): boolean;
export function isEqual(resourceOrPathA: string | URI, resourceOrPathB: string | URI): boolean {
const identityEquals = (resourceOrPathA === resourceOrPathB);
if (identityEquals) {
return true;
}

if (!resourceOrPathA || !resourceOrPathB) {
return false;
}

// Compare by URI
if (typeof resourceOrPathA !== 'string') {
const resourceA = resourceOrPathA;
const resourceB = resourceOrPathB as URI;

if (resourceA.scheme !== resourceB.scheme) {
return false;
}

// File URIs compare by fsPath
if (resourceA.scheme === Schemas.file) {
return isEqual(resourceA.fsPath, resourceB.fsPath);
}

// Non-file URIs compare by full string
return resourceA.toString() === resourceB.toString();
}

// Compare by Path
const pathA = resourceOrPathA;
const pathB = resourceOrPathB as string;

if (isLinux || identityEquals) {
return identityEquals;
}

if (path1.length !== path2.length) {
if (pathA.length !== pathB.length) {
return false;
}

return path1.toLowerCase() === path2.toLowerCase();
return pathA.toLowerCase() === pathB.toLowerCase();
}

export function isParent(path: string, candidate: string): boolean {
Expand Down
12 changes: 10 additions & 2 deletions src/vs/platform/files/test/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ suite('Files', () => {

test('isEqual', function () {
assert.ok(isEqual('/some/path', '/some/path'));
assert.ok(isEqual(URI.file('/some/path'), URI.file('/some/path')));
assert.ok(isEqual('c:\\some\\path', 'c:\\some\\path'));
assert.ok(isEqual(URI.file('c:\\some\\path'), URI.file('c:\\some\\path')));
assert.ok(!isEqual('/some/path', '/some/other/path'));
assert.ok(!isEqual(URI.file('/some/path'), URI.file('/some/other/path')));
assert.ok(!isEqual('c:\\some\\path', 'c:\\some\\other\\path'));
assert.ok(!isEqual(URI.file('c:\\some\\path'), URI.file('c:\\some\\other\\path')));

if (isLinux) {
assert.ok(!isEqual('/some/path', '/some/PATH'));
assert.ok(!isEqual(URI.file('/some/path'), URI.file('/some/PATH')));
} else {
assert.ok(isEqual('/some/path', '/some/PATH'));
assert.ok(isEqual('c:\\some\\path', 'c:\\some\\PATH'));
assert.ok(isEqual(URI.file('/some/path'), URI.file('/some/PATH')));
assert.ok(isEqual(URI.file('c:\\some\\path'), URI.file('c:\\some\\PATH')));
}

assert.ok(isEqual(URI.parse('some://cool/uri'), URI.parse('some://cool/uri')));
assert.ok(!isEqual(URI.parse('some://cool/uri'), URI.parse('some://other/uri')));
});

test('isParent', function () {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/browser/parts/editor/editorStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { IEditor as IBaseEditor, IEditorInput } from 'vs/platform/editor/common/
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IQuickOpenService, IPickOpenEntry, IFilePickOpenEntry } from 'vs/platform/quickOpen/common/quickOpen';
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
import { IFilesConfiguration, SUPPORTED_ENCODINGS } from 'vs/platform/files/common/files';
import { IFilesConfiguration, SUPPORTED_ENCODINGS, isEqual } from 'vs/platform/files/common/files';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IModelService } from 'vs/editor/common/services/modelService';
Expand Down Expand Up @@ -649,7 +649,7 @@ export class EditorStatus implements IStatusbarItem {
const activeEditor = this.editorService.getActiveEditor();
if (activeEditor) {
const activeResource = toResource(activeEditor.input, { supportSideBySide: true, filter: ['file', 'untitled'] });
if (activeResource && activeResource.toString() === resource.toString()) {
if (isEqual(activeResource, resource)) {
return this.onEncodingChange(<IBaseEditor>activeEditor); // only update if the encoding changed for the active resource
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/vs/workbench/common/editor/editorStacksModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { Registry } from 'vs/platform/platform';
import { Position, Direction } from 'vs/platform/editor/common/editor';
import { Schemas } from 'vs/base/common/network';
import { isEqual } from 'vs/platform/files/common/files';
import { ResourceMap } from 'vs/base/common/map';

Expand Down Expand Up @@ -200,10 +199,8 @@ export class EditorGroup implements IEditorGroup {
for (let i = 0; i < this.editors.length; i++) {
const editor = this.editors[i];
const editorResource = toResource(editor, { supportSideBySide: true });
if (editorResource && editorResource.scheme === resource.scheme) {
if (resource.scheme === Schemas.file && isEqual(editorResource.fsPath, resource.fsPath) || (resource.scheme !== Schemas.file && editorResource.toString() === resource.toString())) {
return editor;
}
if (isEqual(editorResource, resource)) {
return editor;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { CommandsRegistry, ICommandHandler } from 'vs/platform/commands/common/c
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { explorerItemToFileResource, ExplorerFocusCondition, FilesExplorerFocusCondition } from 'vs/workbench/parts/files/common/files';
import { isEqual } from 'vs/platform/files/common/files';

class FilesViewerActionContributor extends ActionBarContributor {

Expand Down Expand Up @@ -90,7 +91,7 @@ class FilesViewerActionContributor extends ActionBarContributor {
}

const workspace = this.contextService.getWorkspace();
const isRoot = workspace && stat.resource.toString() === workspace.resource.toString();
const isRoot = workspace && isEqual(stat.resource.fsPath, workspace.resource.fsPath);

// Copy File/Folder
if (!isRoot) {
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/parts/files/browser/fileActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ export class PasteFileAction extends BaseFileAction {
}

// Check if target is ancestor of pasted folder
if (this.element.resource.toString() !== fileToCopy.resource.toString() && (isEqual(this.element.resource.fsPath, fileToCopy.resource.fsPath) || isParent(this.element.resource.fsPath, fileToCopy.resource.fsPath))) {
if (!isEqual(this.element.resource.fsPath, fileToCopy.resource.fsPath) && (isEqual(this.element.resource.fsPath, fileToCopy.resource.fsPath) || isParent(this.element.resource.fsPath, fileToCopy.resource.fsPath))) {
return false;
}

Expand All @@ -1007,7 +1007,7 @@ export class PasteFileAction extends BaseFileAction {

// Find target
let target: FileStat;
if (this.element.resource.toString() === fileToCopy.resource.toString()) {
if (isEqual(this.element.resource.fsPath, fileToCopy.resource.fsPath)) {
target = this.element.parent;
} else {
target = this.element.isDirectory ? this.element : this.element.parent;
Expand Down Expand Up @@ -1313,7 +1313,7 @@ export class CompareResourcesAction extends Action {
}

// Check if target is identical to source
if (this.resource.toString() === globalResourceToCompare.toString()) {
if (isEqual(this.resource.fsPath, globalResourceToCompare.fsPath)) {
return false;
}

Expand Down Expand Up @@ -1408,7 +1408,7 @@ export abstract class BaseSaveFileAction extends BaseActionWithErrorReporting {
const editor = getCodeEditor(activeEditor);
if (editor) {
const activeResource = toResource(activeEditor.input, { supportSideBySide: true, filter: ['file', 'untitled'] });
if (activeResource && activeResource.toString() === source.toString()) {
if (activeResource && isEqual(activeResource.fsPath, source.fsPath)) {
viewStateOfSource = editor.saveViewState();
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/vs/workbench/parts/files/browser/saveErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textF
import { ITextModelResolverService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService';
import { IModel } from 'vs/editor/common/editorCommon';
import { toResource } from 'vs/workbench/common/editor';
import { ResourceMap } from 'vs/base/common/map';

export const CONFLICT_RESOLUTION_SCHEME = 'conflictResolution';

// A handler for save error happening with conflict resolution actions
export class SaveErrorHandler implements ISaveErrorHandler, IWorkbenchContribution, ITextModelContentProvider {
private messages: { [resource: string]: () => void };
private messages: ResourceMap<() => void>;
private toUnbind: IDisposable[];

constructor(
Expand All @@ -43,7 +44,7 @@ export class SaveErrorHandler implements ISaveErrorHandler, IWorkbenchContributi
@IModeService private modeService: IModeService,
@IInstantiationService private instantiationService: IInstantiationService
) {
this.messages = Object.create(null);
this.messages = new ResourceMap<() => void>();
this.toUnbind = [];

// Register as text model content provider that supports to load a resource as it actually
Expand Down Expand Up @@ -82,10 +83,10 @@ export class SaveErrorHandler implements ISaveErrorHandler, IWorkbenchContributi
}

private onFileSavedOrReverted(resource: URI): void {
const hideMessage = this.messages[resource.toString()];
const hideMessage = this.messages.get(resource);
if (hideMessage) {
hideMessage();
this.messages[resource.toString()] = void 0;
this.messages.delete(resource);
}
}

Expand Down Expand Up @@ -157,11 +158,13 @@ export class SaveErrorHandler implements ISaveErrorHandler, IWorkbenchContributi
}

// Show message and keep function to hide in case the file gets saved/reverted
this.messages[model.getResource().toString()] = this.messageService.show(Severity.Error, message);
this.messages.set(model.getResource(), this.messageService.show(Severity.Error, message));
}

public dispose(): void {
this.toUnbind = dispose(this.toUnbind);

this.messages.clear();
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/vs/workbench/parts/files/browser/views/explorerViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export class FileDataSource implements IDataSource {
}

// Return if root reached
if (this.contextService.hasWorkspace() && stat.resource.toString() === this.contextService.getWorkspace().resource.toString()) {
const workspace = this.contextService.getWorkspace();
if (workspace && isEqual(stat.resource.fsPath, workspace.resource.fsPath)) {
return TPromise.as(null);
}

Expand Down Expand Up @@ -411,7 +412,8 @@ export class FileController extends DefaultController {
}

// Handle root
if (this.contextService.getWorkspace() && stat.resource.toString() === this.contextService.getWorkspace().resource.toString()) {
const workspace = this.contextService.getWorkspace();
if (workspace && isEqual(stat.resource.fsPath, workspace.resource.fsPath)) {
tree.clearFocus(payload);
tree.clearSelection(payload);

Expand Down Expand Up @@ -687,7 +689,7 @@ export class FileDragAndDrop implements IDragAndDrop {
return true; // NewStatPlaceholders can not be moved
}

if (source.resource.toString() === target.resource.toString()) {
if (isEqual(source.resource.fsPath, target.resource.fsPath)) {
return true; // Can not move anything onto itself
}

Expand All @@ -710,7 +712,8 @@ export class FileDragAndDrop implements IDragAndDrop {
return fromDesktop || isCopy ? DRAG_OVER_ACCEPT_BUBBLE_DOWN_COPY(true) : DRAG_OVER_ACCEPT_BUBBLE_DOWN(true);
}

if (target.resource.toString() !== this.contextService.getWorkspace().resource.toString()) {
const workspace = this.contextService.getWorkspace();
if (workspace && !isEqual(target.resource.fsPath, workspace.resource.fsPath)) {
return fromDesktop || isCopy ? DRAG_OVER_ACCEPT_BUBBLE_UP_COPY : DRAG_OVER_ACCEPT_BUBBLE_UP;
}

Expand Down
11 changes: 6 additions & 5 deletions src/vs/workbench/parts/files/common/explorerViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IFileStat, isEqual, isParent } from 'vs/platform/files/common/files';
import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput';
import { IEditorInput } from 'vs/platform/editor/common/editor';
import { IEditorGroup, toResource } from 'vs/workbench/common/editor';
import { ResourceMap } from 'vs/base/common/map';

export enum StatType {
FILE,
Expand Down Expand Up @@ -104,17 +105,17 @@ export class FileStat implements IFileStat {
if (mergingDirectories && disk.isDirectoryResolved) {

// Map resource => stat
const oldLocalChildren: { [resource: string]: FileStat; } = Object.create(null);
const oldLocalChildren = new ResourceMap<FileStat>();
local.children.forEach((localChild: FileStat) => {
oldLocalChildren[localChild.resource.toString()] = localChild;
oldLocalChildren.set(localChild.resource, localChild);
});

// Clear current children
local.children = [];

// Merge received children
disk.children.forEach((diskChild: FileStat) => {
const formerLocalChild = oldLocalChildren[diskChild.resource.toString()];
const formerLocalChild = oldLocalChildren.get(diskChild.resource);

// Existing child: merge
if (formerLocalChild) {
Expand Down Expand Up @@ -177,7 +178,7 @@ export class FileStat implements IFileStat {
*/
public removeChild(child: FileStat): void {
for (let i = 0; i < this.children.length; i++) {
if (this.children[i].resource.toString() === child.resource.toString()) {
if (isEqual(this.children[i].resource.fsPath, child.resource.fsPath)) {
this.children.splice(i, 1);
break;
}
Expand Down Expand Up @@ -291,7 +292,7 @@ export class NewStatPlaceholder extends FileStat {
}

public getId(): string {
return 'new-stat-placeholder:' + this.id + ':' + this.parent.resource.toString();
return `new-stat-placeholder:${this.id}:${this.parent.resource.toString()}`;
}

public isDirectoryPlaceholder(): boolean {
Expand Down
7 changes: 4 additions & 3 deletions src/vs/workbench/parts/markers/browser/markersPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ContributableActionProvider } from 'vs/workbench/browser/actionBarRegis
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IListService } from 'vs/platform/list/browser/listService';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { isEqual } from 'vs/platform/files/common/files';

export class MarkersPanel extends Panel {

Expand Down Expand Up @@ -246,7 +247,7 @@ export class MarkersPanel extends Panel {
if (resourceForCurrentActiveFile) {
return false;
}
return changedResources.some(r => r.toString() === this.currentActiveFile.toString());
return changedResources.some(r => isEqual(r, this.currentActiveFile));
}

private onEditorsChanged(): void {
Expand Down Expand Up @@ -333,7 +334,7 @@ export class MarkersPanel extends Panel {
private getResourceForCurrentActiveFile(): Resource {
if (this.currentActiveFile) {
let resources = this.markersModel.filteredResources.filter((resource): boolean => {
return this.currentActiveFile.toString() === resource.uri.toString();
return isEqual(this.currentActiveFile, resource.uri);
});
return resources.length > 0 ? resources[0] : null;
}
Expand All @@ -344,7 +345,7 @@ export class MarkersPanel extends Panel {
let selectedElement = this.tree.getSelection();
if (selectedElement && selectedElement.length > 0) {
if (selectedElement[0] instanceof Marker) {
if (resource.uri.toString() === selectedElement[0].marker.resource.toString()) {
if (isEqual(resource.uri, selectedElement[0].marker.resource)) {
return true;
}
}
Expand Down
Loading

0 comments on commit e134f42

Please sign in to comment.