Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement plugin API to get workspace folder by file URI #3718

Merged
merged 1 commit into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/plugin-ext/src/main/browser/workspace-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export class WorkspaceMainImpl implements WorkspaceMain {

notifyWorkspaceFoldersChanged(): void {
if (this.roots && this.roots.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not part of your change - this.roots won't be null or undefined, and therefore checking it is not necessary :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elaihau Thanks for the review,
I'm going to open another PR where this checking is removed.

const folders = this.roots.map(root => {
const folders = this.roots.map((root: FileStat, index: number) => {
const uri = Uri.parse(root.uri);
const path = new Path(uri.path);
return {
uri: uri,
name: path.base,
index: 0
index: index
};
});

Expand Down
7 changes: 5 additions & 2 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export function createAPIFactory(
return editors.onDidChangeTextEditorVisibleRanges(listener, thisArg, disposables);
},
async showTextDocument(documentArg: theia.TextDocument | Uri,
optionsArg?: theia.TextDocumentShowOptions | theia.ViewColumn,
preserveFocus?: boolean
optionsArg?: theia.TextDocumentShowOptions | theia.ViewColumn,
preserveFocus?: boolean
): Promise<theia.TextEditor> {
let documentOptions: theia.TextDocumentShowOptions | undefined;
const uri: Uri = documentArg instanceof Uri ? documentArg : documentArg.uri;
Expand Down Expand Up @@ -352,6 +352,9 @@ export function createAPIFactory(
registerFileSystemProvider(scheme: string, provider: theia.FileSystemProvider, options?: { isCaseSensitive?: boolean, isReadonly?: boolean }): theia.Disposable {
// FIXME: to implement
return new Disposable(() => { });
},
getWorkspaceFolder(uri: Uri): theia.WorkspaceFolder | Uri | undefined {
return workspaceExt.getWorkspaceFolder(uri);
}
};

Expand Down
31 changes: 31 additions & 0 deletions packages/plugin-ext/src/plugin/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,35 @@ export class WorkspaceExtImpl implements WorkspaceExt {
return undefined;
}

getWorkspaceFolder(uri: URI): theia.WorkspaceFolder | URI | undefined {
if (!this.folders || !this.folders.length) {
return undefined;
}

const resourcePath = uri.toString();

let workspaceFolder: theia.WorkspaceFolder | undefined;
for (let i = 0; i < this.folders.length; i++) {
const folder = this.folders[i];
const folderPath = folder.uri.toString();

if (resourcePath === folderPath) {
// return the input when the given uri is a workspace folder itself
return uri;
}

if (this.matchPaths(resourcePath, folderPath) && (!workspaceFolder || folderPath.length > workspaceFolder.uri.toString().length)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may go with something like this:

 if (folderPath.startsWith(resourcePath) && (!workspaceFolder || folderPath.length > workspaceFolder.uri.toString().length)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmorhun thanks for review
startsWith doesn't fit here, because in this case file /home/user/project_2/src/main.js matches the workspace folder /home/user/project, which is not correct.

Copy link
Contributor

@mmorhun mmorhun Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you may ensure the trailing slash for resourcePath.
But up to you, I do not force)

workspaceFolder = folder;
}
}
return workspaceFolder;
}

private matchPaths(resourcePath: string, folderPath: string): boolean {
const resourcePathCrumbs = resourcePath.split('/');
const folderPathCrumbs = folderPath.split('/');

return folderPathCrumbs.every((crumb, index) => crumb === resourcePathCrumbs[index]);
}

}
Loading