-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmorhun thanks for review There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, you may ensure the trailing slash for |
||
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]); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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 :)There was a problem hiding this comment.
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.