This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Load snippets via plugin resource #948
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b597c35
Load snippets via plugin resource
tsmaeder b0c4a5d
Resource file system
tsmaeder 5c95c5a
Intermediate state
tsmaeder fd994ee
Make remote and local resource accesses work
tsmaeder 65db992
Fix formatting
tsmaeder f68a6b3
Address PR comments
tsmaeder 0f3faf1
Update yarn.lock
tsmaeder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
extensions/eclipse-che-theia-plugin-remote/src/browser/che-plugin-file-system.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2021 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import { Disposable, Emitter, Event } from '@theia/core'; | ||
import { | ||
FileChange, | ||
FileDeleteOptions, | ||
FileOverwriteOptions, | ||
FileSystemProvider, | ||
FileSystemProviderCapabilities, | ||
FileType, | ||
FileWriteOptions, | ||
Stat, | ||
WatchOptions, | ||
} from '@theia/filesystem/lib/common/files'; | ||
import { FileService, FileServiceContribution } from '@theia/filesystem/lib/browser/file-service'; | ||
|
||
import { ChePluginUri } from '../common/che-plugin-uri'; | ||
import { Endpoint } from '@theia/core/lib/browser'; | ||
import URI from '@theia/core/lib/common/uri'; | ||
import { injectable } from 'inversify'; | ||
|
||
/** | ||
* A very basic file system provider that can read plugin resources via http | ||
*/ | ||
export class ChePluginFileSystem implements FileSystemProvider { | ||
private readonly _onDidChange = new Emitter<readonly FileChange[]>(); | ||
|
||
readonly onDidChangeFile: Event<readonly FileChange[]> = this._onDidChange.event; | ||
readonly onFileWatchError: Event<void> = new Emitter<void>().event; | ||
|
||
readonly capabilities: FileSystemProviderCapabilities; | ||
readonly onDidChangeCapabilities: Event<void> = Event.None; | ||
|
||
constructor() { | ||
this.capabilities = FileSystemProviderCapabilities.FileReadWrite + FileSystemProviderCapabilities.Readonly; | ||
} | ||
|
||
delete(resource: URI, opts: FileDeleteOptions): Promise<void> { | ||
throw new Error('Not implemented.'); | ||
} | ||
|
||
mkdir(resource: URI): Promise<void> { | ||
throw new Error('Not implemented.'); | ||
} | ||
|
||
private static getUri(path: string): URI { | ||
return new Endpoint({ | ||
path: path, | ||
}).getRestUrl(); | ||
} | ||
|
||
readFile(resource: URI): Promise<Uint8Array> { | ||
const uri = ChePluginFileSystem.getUri(resource.path.toString()); | ||
return new Promise((resolve, reject) => { | ||
const request = new XMLHttpRequest(); | ||
request.responseType = 'arraybuffer'; | ||
request.onreadystatechange = function (): void { | ||
if (this.readyState === XMLHttpRequest.DONE) { | ||
if (this.status === 200) { | ||
resolve(new Uint8Array(this.response)); | ||
} else { | ||
reject(new Error('Could not fetch plugin resource')); | ||
} | ||
} | ||
}; | ||
|
||
request.open('GET', uri.toString(), true); | ||
request.send(); | ||
}); | ||
} | ||
|
||
readdir(resource: URI): Promise<[string, FileType][]> { | ||
throw new Error('Not implemented.'); | ||
} | ||
|
||
rename(from: URI, to: URI, opts: FileOverwriteOptions): Promise<void> { | ||
throw new Error('Not implemented.'); | ||
} | ||
|
||
stat(resource: URI): Promise<Stat> { | ||
const uri = ChePluginFileSystem.getUri(resource.path.toString()) + '?request=stat'; | ||
return new Promise((resolve, reject) => { | ||
const request = new XMLHttpRequest(); | ||
request.responseType = 'json'; | ||
request.onreadystatechange = function (): void { | ||
if (this.readyState === XMLHttpRequest.DONE) { | ||
if (this.status === 200) { | ||
resolve(request.response); | ||
} else { | ||
reject(new Error('Could not fetch plugin resource')); | ||
} | ||
} | ||
}; | ||
|
||
request.open('GET', uri.toString(), true); | ||
request.send(); | ||
}); | ||
} | ||
|
||
watch(resource: URI, opts: WatchOptions): Disposable { | ||
// we treat the FS as read-only | ||
return Disposable.NULL; | ||
} | ||
|
||
writeFile(resource: URI, content: Uint8Array, opts: FileWriteOptions): Promise<void> { | ||
throw new Error('Not implemented.'); | ||
} | ||
} | ||
|
||
@injectable() | ||
export class ChePluginFileServiceContribution implements FileServiceContribution { | ||
registerFileSystemProviders(service: FileService): void { | ||
service.onWillActivateFileSystemProvider(event => { | ||
if (event.scheme === ChePluginUri.SCHEME) { | ||
event.waitUntil( | ||
(async () => { | ||
service.registerProvider(ChePluginUri.SCHEME, new ChePluginFileSystem()); | ||
})() | ||
); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
extensions/eclipse-che-theia-plugin-remote/src/common/che-plugin-uri.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2021 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import URI from '@theia/core/lib/common/uri'; | ||
|
||
/** | ||
* Share the definition of plugin resource uris between back end and | ||
* front end. | ||
*/ | ||
export namespace ChePluginUri { | ||
export const SCHEME = 'chepluginresource'; | ||
|
||
export function createUri(pluginId: string, relativePath?: string): URI { | ||
return new URI( | ||
`${SCHEME}:///hostedPlugin/${pluginId}/${ | ||
relativePath ? encodeURIComponent(relativePath.normalize().toString()) : '' | ||
}` | ||
); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
extensions/eclipse-che-theia-plugin-remote/src/node/che-plugin-uri-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2021 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import { PluginPackage, getPluginId } from '@theia/plugin-ext/lib/common/plugin-protocol'; | ||
|
||
import { ChePluginUri } from '../common/che-plugin-uri'; | ||
import { PluginUriFactory } from '@theia/plugin-ext/lib/hosted/node/scanners/plugin-uri-factory'; | ||
import URI from '@theia/core/lib/common/uri'; | ||
import { injectable } from 'inversify'; | ||
|
||
/** | ||
* Creates plugin resource URIs for plugin-relative files. | ||
*/ | ||
@injectable() | ||
export class ChePluginUriFactory implements PluginUriFactory { | ||
createUri(pkg: PluginPackage, pkgRelativePath?: string): URI { | ||
return ChePluginUri.createUri(getPluginId(pkg), pkgRelativePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
for readability maybe it would be easier to flag it as async method (and use await instead of chaining promises)
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.
I used to like the async/await syntax, but t.b.h these days I find it makes following the control flow rather confusing, in particular when debugging. I tend to only use async/await where it really improves readability. Here it does not, IMO: