Skip to content

Commit

Permalink
Display correct file system path for Windows (eclipse-theia#11180)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored May 25, 2022
1 parent 0ec8075 commit 9ec8835
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 30 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/browser/label-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { expect } from 'chai';
import { DefaultUriLabelProviderContribution, URIIconReference } from './label-provider';
import URI from '../common/uri';
import { OS } from '../common/os';

describe('DefaultUriLabelProviderContribution', function (): void {

Expand All @@ -31,7 +32,11 @@ describe('DefaultUriLabelProviderContribution', function (): void {
const prov = new DefaultUriLabelProviderContribution();
const longName = prov.getLongName(new URI('file:///tmp/hello/you.txt'));

expect(longName).eq('/tmp/hello/you.txt');
if (OS.backend.isWindows) {
expect(longName).eq('\\tmp\\hello\\you.txt');
} else {
expect(longName).eq('/tmp/hello/you.txt');
}
});

it('should return icon class for something that seems to be a file', function (): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/browser/label-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class DefaultUriLabelProviderContribution implements LabelProviderContrib
return this.formatUri(uri, formatting);
}
}
return uri && uri.path.toString();
return uri && uri.path.fsPath();
}

protected getUri(element: URI | URIIconReference): URI | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class URI {
return base;
}
if (this.path.isRoot) {
return this.path.toString();
return this.path.fsPath();
}
return '';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/debug/src/browser/model/debug-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class DebugSource extends DebugSourceData {

get name(): string {
if (this.inMemory) {
return this.raw.name || this.uri.path.base || this.uri.path.toString();
return this.raw.name || this.uri.path.base || this.uri.path.fsPath();
}
return this.labelProvider.getName(this.uri);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/browser/editor-widget-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class EditorWidgetFactory implements WidgetFactory {
}

private setLabels(editor: EditorWidget, uri: URI): void {
editor.title.caption = uri.path.toString();
editor.title.caption = uri.path.fsPath();
const icon = this.labelProvider.getIcon(uri);
editor.title.label = this.labelProvider.getName(uri);
editor.title.iconClass = icon + ' file-icon';
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/browser/quick-editor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class QuickEditorService implements QuickAccessContribution, QuickAccessP
label: this.labelProvider.getName(uri),
description: description,
iconClasses,
ariaLabel: uri.path.toString(),
ariaLabel: uri.path.fsPath(),
alwaysShow: true,
execute: () => this.openFile(uri)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class LocationListRenderer extends ReactRenderer {
protected renderTextInput(): React.ReactNode {
return (
<input className={'theia-select ' + LocationListRenderer.Styles.LOCATION_TEXT_INPUT_CLASS}
defaultValue={this.service.location?.path.toString()}
defaultValue={this.service.location?.path.fsPath()}
onBlur={this.handleTextInputOnBlur}
onChange={this.handleTextInputOnChange}
onKeyDown={this.handleTextInputKeyDown}
Expand Down Expand Up @@ -278,7 +278,7 @@ export class LocationListRenderer extends ReactRenderer {
protected renderLocation(location: LocationListRenderer.Location): React.ReactNode {
const { uri, isDrive } = location;
const value = uri.toString();
return <option value={value} key={uri.toString()}>{isDrive ? uri.path.toString() : uri.displayName}</option>;
return <option value={value} key={uri.toString()}>{isDrive ? uri.path.fsPath() : uri.displayName}</option>;
}

protected onLocationChanged(e: React.ChangeEvent<HTMLSelectElement>): void {
Expand Down
14 changes: 12 additions & 2 deletions packages/markers/src/browser/marker-tree-label-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { FileStat } from '@theia/filesystem/lib/common/files';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { MockEnvVariablesServerImpl } from '@theia/core/lib/browser/test/mock-env-variables-server';
import { FileUri } from '@theia/core/lib/node';
import { OS } from '@theia/core/lib/common/os';
import * as temp from 'temp';

disableJSDOM();
Expand Down Expand Up @@ -131,7 +132,11 @@ describe('Marker Tree Label Provider', () => {
const label = markerTreeLabelProvider.getLongName(
createMarkerInfoNode('file:///home/b/foo.ts')
);
expect(label).equals('/home/b');
if (OS.backend.isWindows) {
expect(label).eq('\\home\\b');
} else {
expect(label).eq('/home/b');
}
});
});
describe('multi-root workspace', () => {
Expand Down Expand Up @@ -165,7 +170,12 @@ describe('Marker Tree Label Provider', () => {
const label = markerTreeLabelProvider.getLongName(
createMarkerInfoNode('file:///home/a/b/foo.ts')
);
expect(label).equals('/home/a/b');

if (OS.backend.isWindows) {
expect(label).eq('\\home\\a\\b');
} else {
expect(label).eq('/home/a/b');
}
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/markers/src/browser/problem/problem-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ export class ProblemDecorator implements TreeDecorator {
if (parentWorkspace) {
const relativeDirFromWorkspace = parentWorkspace.relative(nodeURIDir);
workspacePrefixString = workspaceRoots.length > 1 ? this.labelProvider.getName(parentWorkspace) : '';
filePathString = relativeDirFromWorkspace?.toString() ?? '';
filePathString = relativeDirFromWorkspace?.fsPath() ?? '';
separator = filePathString && workspacePrefixString ? ' \u2022 ' : ''; // add a bullet point between workspace and path
} else {
workspacePrefixString = nodeURIDir.path.toString();
workspacePrefixString = nodeURIDir.path.fsPath();
}
return `${workspacePrefixString}${separator}${filePathString}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ export class OpenEditorsWidget extends FileTreeWidget {
if (parentWorkspace) {
const relativeDirFromWorkspace = parentWorkspace.relative(nodeURIDir);
workspacePrefixString = workspaceRoots.length > 1 ? this.labelProvider.getName(parentWorkspace) : '';
filePathString = relativeDirFromWorkspace?.toString() ?? '';
filePathString = relativeDirFromWorkspace?.fsPath() ?? '';
separator = filePathString && workspacePrefixString ? ' \u2022 ' : ''; // add a bullet point between workspace and path
} else {
workspacePrefixString = nodeURIDir.path.toString();
workspacePrefixString = nodeURIDir.path.fsPath();
}
return [{
fontData: { color },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class PreferencesContribution extends AbstractViewContribution<Preference
} else {
const items: QuickPickItem[] = roots.map(root => ({
label: root.name,
description: root.resource.path.toString(),
description: root.resource.path.fsPath(),
execute: () => callback(root)
}));
this.quickInputService?.showQuickPick(items, { placeholder: 'Select workspace folder' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class PreferenceSingleFilePathInputRenderer extends PreferenceStringInput
const title = selectionProps?.title ?? selectionProps?.canSelectFolders ? WorkspaceCommands.OPEN_FOLDER.dialogLabel : WorkspaceCommands.OPEN_FILE.dialogLabel;
const selection = await this.fileDialogService.showOpenDialog({ title, ...selectionProps });
if (selection) {
this.setPreferenceImmediately(selection.path.toString());
this.setPreferenceImmediately(selection.path.fsPath());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class ResourcePropertyViewTreeWidget extends TreeWidget implements Proper
}

protected getLocationString(fileStat: FileStat): string {
return fileStat.resource.path.toString();
return fileStat.resource.path.fsPath();
}

protected getFileName(fileStat: FileStat): string {
Expand Down
8 changes: 4 additions & 4 deletions packages/scm/src/browser/scm-tree-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ export class ScmResourceComponent extends ScmElement<ScmResourceComponent.Props>
const letter = decoration && decoration.letter || '';
const tooltip = decoration && decoration.tooltip || '';
const relativePath = parentPath.relative(resourceUri.parent);
const path = relativePath ? relativePath.toString() : labelProvider.getLongName(resourceUri.parent);
const path = relativePath ? relativePath.fsPath() : labelProvider.getLongName(resourceUri.parent);
const title = tooltip.length !== 0
? `${resourceUri.path.toString()}${tooltip}`
: resourceUri.path.toString();
? `${resourceUri.path.fsPath()}${tooltip}`
: resourceUri.path.fsPath();

return <div key={sourceUri}
className={`scmItem ${TREE_NODE_SEGMENT_CLASS} ${TREE_NODE_SEGMENT_GROW_CLASS}`}
Expand Down Expand Up @@ -684,7 +684,7 @@ export class ScmResourceFolderElement extends ScmElement<ScmResourceFolderElemen
const { model, treeNode, sourceUri, labelProvider, commands, menus, contextKeys, caption } = this.props;
const sourceFileStat: FileStat = { uri: sourceUri, isDirectory: true, lastModification: 0 };
const icon = labelProvider.getIcon(sourceFileStat);
const title = new URI(sourceUri).path.toString();
const title = new URI(sourceUri).path.fsPath();

return <div key={sourceUri}
className={`scmItem ${TREE_NODE_SEGMENT_CLASS} ${TREE_NODE_SEGMENT_GROW_CLASS} ${ScmTreeWidget.Styles.NO_SELECT}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
const collapseValue: string = this.searchInWorkspacePreferences['search.collapseResults'];
let path: string;
if (result.root === this.defaultRootName) {
path = new URI(result.fileUri).path.dir.toString();
path = new URI(result.fileUri).path.dir.fsPath();
} else {
path = this.filenameAndPath(result.root, result.fileUri).path;
}
Expand Down Expand Up @@ -662,7 +662,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
const uri = new URI(rootUri);
return {
selected: false,
path: uri.path.toString(),
path: uri.path.fsPath(),
folderUri: rootUri,
uri: new URI(rootUri),
children: [],
Expand Down Expand Up @@ -718,7 +718,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
const relativePath = new URI(rootUriStr).relative(uri.parent);
return {
name: this.labelProvider.getName(uri),
path: relativePath ? relativePath.toString() : ''
path: relativePath ? relativePath.fsPath() : ''
};
}

Expand Down Expand Up @@ -980,7 +980,7 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
return <div className='result'>
<div className='result-head'>
<div className={`result-head-info noWrapInfo noselect ${node.selected ? 'selected' : ''}`}
title={new URI(node.fileUri).path.toString()}>
title={new URI(node.fileUri).path.fsPath()}>
<span className={`file-icon ${this.toNodeIcon(node)}`}></span>
<div className='noWrapInfo'>
<span className={'file-name'}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export class WorkspaceFrontendContribution implements CommandContribution, Keybi
await this.workspaceService.save(selected);
return true;
} catch {
this.messageService.error(nls.localizeByDefault("Unable to save workspace '{0}'", selected.path.toString()));
this.messageService.error(nls.localizeByDefault("Unable to save workspace '{0}'", selected.path.fsPath()));
}
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/browser/workspace-input-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class WorkspaceInputDialog extends SingleTextInputDialog {
icon.style.marginRight = '0.5em';
icon.style.verticalAlign = 'middle';
element.style.verticalAlign = 'middle';
element.title = this.props.parentUri.path.toString();
element.title = this.props.parentUri.path.fsPath();
element.appendChild(icon);
element.appendChild(document.createTextNode(label));
// Add the path and icon div before the `inputField`.
Expand Down
22 changes: 19 additions & 3 deletions packages/workspace/src/browser/workspace-uri-contribution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { FileStat } from '@theia/filesystem/lib/common/files';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { MockEnvVariablesServerImpl } from '@theia/core/lib/browser/test/mock-env-variables-server';
import { FileUri } from '@theia/core/lib/node';
import { OS } from '@theia/core/lib/common/os';
import * as temp from 'temp';

after(() => disableJSDOM());
Expand Down Expand Up @@ -155,20 +156,35 @@ describe('WorkspaceUriLabelProviderContribution class', () => {
it('should return the absolute path of a file from the file\'s URI if the file is not in the workspace', () => {
const file = new URI('file:///tmp/prout.txt');
const longName = labelProvider.getLongName(file);
expect(longName).eq('/tmp/prout.txt');

if (OS.backend.isWindows) {
expect(longName).eq('\\tmp\\prout.txt');
} else {
expect(longName).eq('/tmp/prout.txt');
}
});

it('should return the absolute path of a file from the file\'s FileStat if the file is not in the workspace', () => {
const file: FileStat = FileStat.file('file:///tmp/prout.txt');
const longName = labelProvider.getLongName(file);
expect(longName).eq('/tmp/prout.txt');

if (OS.backend.isWindows) {
expect(longName).eq('\\tmp\\prout.txt');
} else {
expect(longName).eq('/tmp/prout.txt');
}
});

it('should return the path of a file if WorkspaceService returns no roots', () => {
roots = [];
const file = new URI('file:///tmp/prout.txt');
const longName = labelProvider.getLongName(file);
expect(longName).eq('/tmp/prout.txt');

if (OS.backend.isWindows) {
expect(longName).eq('\\tmp\\prout.txt');
} else {
expect(longName).eq('/tmp/prout.txt');
}
});
});

Expand Down

0 comments on commit 9ec8835

Please sign in to comment.