Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

WIP: workspace file system API #120

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@sourcegraph/tslint-config": "^12.0.0",
"@types/minimatch": "^3.0.3",
"@types/mocha": "^5.2.5",
"@types/node": "^10.5.0",
"@types/node": "^10.12.2",
"concurrently": "^4.0.1",
"cpy-cli": "^2.0.0",
"husky": "^0.14.3",
Expand Down
29 changes: 29 additions & 0 deletions src/client/api/fileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FileSystem, FileType } from 'sourcegraph'
import { createProxyAndHandleRequests } from '../../common/proxy'
import { Connection } from '../../protocol/jsonrpc2/connection'
import { URI } from '../../shared/uri'

/** @internal */
export interface ClientFileSystemAPI {
$readDirectory(uri: string): Promise<[string, FileType][]>
$readFile(uri: string): Promise<Uint8Array>
}

/** @internal */
export class ClientFileSystem {
constructor(connection: Connection, private getFileSystem: (uri: string) => FileSystem) {
createProxyAndHandleRequests('fileSystem', connection, this)
}

public $readDirectory(uri: string): Promise<[string, FileType][]> {
return this.getFileSystem(uri).readDirectory(URI.parse(uri))
}

public $readFile(uri: string): Promise<Uint8Array> {
return this.getFileSystem(uri).readFile(URI.parse(uri))
}

public unsubscribe(): void {
// noop
}
}
25 changes: 25 additions & 0 deletions src/client/api/roots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Observable, Subscription } from 'rxjs'
import { createProxyAndHandleRequests } from '../../common/proxy'
import { ExtRootsAPI } from '../../extension/api/roots'
import { Connection } from '../../protocol/jsonrpc2/connection'
import { WorkspaceRoot } from '../../protocol/plainTypes'

/** @internal */
export class ClientRoots {
private subscriptions = new Subscription()
private proxy: ExtRootsAPI

constructor(connection: Connection, environmentRoots: Observable<WorkspaceRoot[] | null>) {
this.proxy = createProxyAndHandleRequests('roots', connection, this)

this.subscriptions.add(
environmentRoots.subscribe(roots => {
this.proxy.$acceptRoots(roots || [])
})
)
}

public unsubscribe(): void {
this.subscriptions.unsubscribe()
}
}
19 changes: 18 additions & 1 deletion src/client/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BehaviorSubject, Observable, Subject, Subscription, Unsubscribable } from 'rxjs'
import { distinctUntilChanged, map } from 'rxjs/operators'
import { ContextValues } from 'sourcegraph'
import { ContextValues, FileSystem } from 'sourcegraph'
import {
ConfigurationCascade,
ConfigurationUpdateParams,
Expand All @@ -18,7 +18,9 @@ import { ClientCommands } from './api/commands'
import { ClientConfiguration } from './api/configuration'
import { ClientContext } from './api/context'
import { ClientDocuments } from './api/documents'
import { ClientFileSystem } from './api/fileSystem'
import { ClientLanguageFeatures } from './api/languageFeatures'
import { ClientRoots } from './api/roots'
import { Search } from './api/search'
import { ClientViews } from './api/views'
import { ClientWindows } from './api/windows'
Expand Down Expand Up @@ -67,6 +69,11 @@ export interface ControllerOptions<X extends Extension, C extends ConfigurationC
* Called before applying the next environment in Controller#setEnvironment. It should have no side effects.
*/
environmentFilter?: (nextEnvironment: Environment<X, C>) => Environment<X, C>

/**
* Called to obtain a {@link module:sourcegraph.FileSystem} to access the file system for a URI.
*/
getFileSystem: (uri: string) => FileSystem
}

/**
Expand Down Expand Up @@ -273,6 +280,16 @@ export class Controller<X extends Extension, C extends ConfigurationCascade> imp
)
subscription.add(new Search(client, this.registries.queryTransformer))
subscription.add(new ClientCommands(client, this.registries.commands))
subscription.add(
new ClientRoots(
client,
this.environment.pipe(
map(({ roots }) => roots),
distinctUntilChanged()
)
)
)
subscription.add(new ClientFileSystem(client, this.options.getFileSystem))
}

public set trace(value: Trace) {
Expand Down
7 changes: 7 additions & 0 deletions src/client/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigurationCascade } from '../protocol'
import { WorkspaceRoot } from '../protocol/plainTypes'
import { Context, EMPTY_CONTEXT } from './context/context'
import { Extension } from './extension'
import { TextDocumentItem } from './types/textDocument'
Expand All @@ -13,6 +14,11 @@ import { TextDocumentItem } from './types/textDocument'
* @template C configuration cascade type
*/
export interface Environment<X extends Extension = Extension, C extends ConfigurationCascade = ConfigurationCascade> {
/**
* The currently open workspace roots (typically a single repository).
*/
readonly roots: WorkspaceRoot[] | null

/**
* The text documents that are currently visible. Each text document is represented to extensions as being
* in its own visible CodeEditor.
Expand All @@ -31,6 +37,7 @@ export interface Environment<X extends Extension = Extension, C extends Configur

/** An empty Sourcegraph extension client environment. */
export const EMPTY_ENVIRONMENT: Environment<any, any> = {
roots: null,
visibleTextDocuments: null,
extensions: null,
configuration: { merged: {} },
Expand Down
15 changes: 15 additions & 0 deletions src/extension/api/fileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FileType, URI } from 'sourcegraph'
import { ClientFileSystemAPI } from '../../client/api/fileSystem'

/** @internal */
export class ExtFileSystem {
constructor(private proxy: ClientFileSystemAPI) {}

public readDirectory(uri: URI): Promise<[string, FileType][]> {
return this.proxy.$readDirectory(uri.toString())
}

public readFile(uri: URI): Promise<Uint8Array> {
return this.proxy.$readFile(uri.toString())
}
}
33 changes: 33 additions & 0 deletions src/extension/api/roots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Observable, Subject } from 'rxjs'
import * as sourcegraph from 'sourcegraph'
import { WorkspaceRoot as PlainWorkspaceRoot } from '../../protocol/plainTypes'
import { URI } from '../../shared/uri'

/** @internal */
export interface ExtRootsAPI {
$acceptRoots(roots: PlainWorkspaceRoot[]): void
}

/** @internal */
export class ExtRoots implements ExtRootsAPI {
private roots: ReadonlyArray<sourcegraph.WorkspaceRoot> = []

/**
* Returns all workspace roots.
*
* @internal
*/
public getAll(): ReadonlyArray<sourcegraph.WorkspaceRoot> {
return this.roots
}

private changes = new Subject<void>()
public readonly onDidChange: Observable<void> = this.changes

public $acceptRoots(roots: PlainWorkspaceRoot[]): void {
this.roots = Object.freeze(
roots.map(plain => ({ ...plain, uri: URI.parse(plain.uri) } as sourcegraph.WorkspaceRoot))
)
this.changes.next()
}
}
23 changes: 21 additions & 2 deletions src/extension/extensionHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import * as sourcegraph from 'sourcegraph'
import { createProxy, handleRequests } from '../common/proxy'
import { Connection, createConnection, Logger, MessageTransports } from '../protocol/jsonrpc2/connection'
import { createWebWorkerMessageTransports } from '../protocol/jsonrpc2/transports/webWorker'
import { URI } from '../shared/uri'
import { ExtCommands } from './api/commands'
import { ExtConfiguration } from './api/configuration'
import { ExtContext } from './api/context'
import { ExtDocuments } from './api/documents'
import { ExtFileSystem } from './api/fileSystem'
import { ExtLanguageFeatures } from './api/languageFeatures'
import { ExtRoots } from './api/roots'
import { ExtSearch } from './api/search'
import { ExtViews } from './api/views'
import { ExtWindows } from './api/windows'
import { Location } from './types/location'
import { Position } from './types/position'
import { Range } from './types/range'
import { Selection } from './types/selection'
import { URI } from './types/uri'

const consoleLogger: Logger = {
error(message: string): void {
Expand Down Expand Up @@ -81,6 +83,12 @@ function createExtensionHandle(initData: InitData, connection: Connection): type
const documents = new ExtDocuments(sync)
handleRequests(connection, 'documents', documents)

const roots = new ExtRoots()
handleRequests(connection, 'roots', roots)

const fileSystem = new ExtFileSystem(proxy('fileSystem'))
handleRequests(connection, 'fileSystem', fileSystem)

const windows = new ExtWindows(proxy('windows'), proxy('codeEditor'), documents)
handleRequests(connection, 'windows', windows)

Expand Down Expand Up @@ -109,6 +117,12 @@ function createExtensionHandle(initData: InitData, connection: Connection): type
PlainText: sourcegraph.MarkupKind.PlainText,
Markdown: sourcegraph.MarkupKind.Markdown,
},
FileType: {
Unknown: sourcegraph.FileType.Unknown,
File: sourcegraph.FileType.File,
Directory: sourcegraph.FileType.Directory,
SymbolicLink: sourcegraph.FileType.SymbolicLink,
},

app: {
get activeWindow(): sourcegraph.Window | undefined {
Expand All @@ -125,6 +139,11 @@ function createExtensionHandle(initData: InitData, connection: Connection): type
return documents.getAll()
},
onDidOpenTextDocument: documents.onDidOpenTextDocument,
fileSystem,
get roots(): ReadonlyArray<sourcegraph.WorkspaceRoot> {
return roots.getAll()
},
onDidChangeRoots: roots.onDidChange,
},

configuration: {
Expand Down Expand Up @@ -156,7 +175,7 @@ function createExtensionHandle(initData: InitData, connection: Connection): type
internal: {
sync,
updateContext: updates => context.updateContext(updates),
sourcegraphURL: new URI(initData.sourcegraphURL),
sourcegraphURL: URI.parse(initData.sourcegraphURL),
clientApplication: initData.clientApplication,
},
}
Expand Down
10 changes: 5 additions & 5 deletions src/extension/types/location.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { URI } from '../../shared/uri'
import { assertToJSON } from './common.test'
import { Location } from './location'
import { Position } from './position'
import { Range } from './range'
import { URI } from './uri'

describe('Location', () => {
it('toJSON', () => {
assertToJSON(new Location(URI.file('u.ts'), new Position(3, 4)), {
uri: URI.parse('file://u.ts').toJSON(),
assertToJSON(new Location(URI.parse('file:///u.ts'), new Position(3, 4)), {
uri: URI.parse('file:///u.ts').toJSON(),
range: { start: { line: 3, character: 4 }, end: { line: 3, character: 4 } },
})
assertToJSON(new Location(URI.file('u.ts'), new Range(1, 2, 3, 4)), {
uri: URI.parse('file://u.ts').toJSON(),
assertToJSON(new Location(URI.parse('file:///u.ts'), new Range(1, 2, 3, 4)), {
uri: URI.parse('file:///u.ts').toJSON(),
range: { start: { line: 1, character: 2 }, end: { line: 3, character: 4 } },
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/extension/types/location.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as sourcegraph from 'sourcegraph'
import { URI } from '../../shared/uri'
import { Position } from './position'
import { Range } from './range'
import { URI } from './uri'

export class Location implements sourcegraph.Location {
public static isLocation(thing: any): thing is sourcegraph.Location {
Expand Down
25 changes: 0 additions & 25 deletions src/extension/types/uri.ts

This file was deleted.

Loading