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

Commit c3e771a

Browse files
committed
refactor: move old self-executable extension API into 'old' dir
This makes room for the new extension API, where extensions merely export an `activate` function instead of being self-executing and being bundled with the extension side of the Sourcegraph extension API JSON-RPC protocol. BREAKING CHANGE: The import paths for `activateExtension`, `SourcegraphExtensionAPI`, and other extension API types are now in `sourcegraph/module/extension/old/**`.
1 parent 6d9934f commit c3e771a

14 files changed

+83
-78
lines changed

src/extension/features/context.ts

-17
This file was deleted.

src/extension/api.ts src/extension/old/api.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Subscription } from 'rxjs'
2-
import { Context } from '../environment/context/context'
3-
import { MessageConnection } from '../jsonrpc2/connection'
4-
import { InitializeParams, Settings, TextDocumentDecoration } from '../protocol'
5-
import { TextDocumentIdentifier } from '../types/textDocument'
6-
import { URI } from '../types/uri'
2+
import * as sourcegraph from 'sourcegraph'
3+
import { Context } from '../../environment/context/context'
4+
import { MessageConnection } from '../../jsonrpc2/connection'
5+
import { InitializeParams, Settings, TextDocumentDecoration } from '../../protocol'
6+
import { TextDocumentIdentifier } from '../../types/textDocument'
77

88
/**
99
* The Sourcegraph extension API, which extensions use to interact with the client.
@@ -188,7 +188,7 @@ export interface Component {
188188
/**
189189
* The URI of the resource (such as a file) that this component is displaying, or null if there is none.
190190
*/
191-
resource: URI | null
191+
resource: sourcegraph.URI | null
192192
}
193193

194194
/**

src/extension/extensionHost.test.ts src/extension/old/extensionHost.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as assert from 'assert'
2-
import { createConnection as createClientConnection } from '../client/connection'
2+
import { createConnection as createClientConnection } from '../../client/connection'
33
import {
44
InitializedNotification,
55
InitializeParams,
66
InitializeRequest,
77
InitializeResult,
88
RegistrationRequest,
9-
} from '../protocol'
10-
import { createMessageTransports } from '../test/integration/helpers'
9+
} from '../../protocol'
10+
import { createMessageTransports } from '../../test/integration/helpers'
1111
import { activateExtension } from './extensionHost'
1212

13-
describe('activateExtension', () => {
13+
describe('activateExtension (old)', () => {
1414
it('initialize request parameters and result', async () => {
1515
const [clientTransports, serverTransports] = createMessageTransports()
1616
const clientConnection = createClientConnection(clientTransports)

src/extension/extensionHost.ts src/extension/old/extensionHost.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Subscription } from 'rxjs'
2-
import { createMessageConnection, Logger, MessageConnection, MessageTransports } from '../jsonrpc2/connection'
3-
import { createWebWorkerMessageTransports } from '../jsonrpc2/transports/webWorker'
2+
import { createMessageConnection, Logger, MessageConnection, MessageTransports } from '../../jsonrpc2/connection'
3+
import { createWebWorkerMessageTransports } from '../../jsonrpc2/transports/webWorker'
44
import {
55
ConfigurationCascade,
66
InitializedNotification,
@@ -9,7 +9,7 @@ import {
99
InitializeResult,
1010
RegistrationParams,
1111
RegistrationRequest,
12-
} from '../protocol'
12+
} from '../../protocol'
1313
import { Commands, Configuration, ExtensionContext, Observable, SourcegraphExtensionAPI, Window, Windows } from './api'
1414
import { createExtCommands } from './features/commands'
1515
import { createExtConfiguration } from './features/configuration'
@@ -31,12 +31,12 @@ class ExtensionHandle<C> implements SourcegraphExtensionAPI<C> {
3131
this.subscription.add(this.rawConnection)
3232

3333
this.configuration = createExtConfiguration<C>(
34-
this,
34+
this.rawConnection,
3535
initializeParams.configurationCascade as ConfigurationCascade<C>
3636
)
37-
this._windows = new ExtWindows(this)
38-
this.commands = createExtCommands(this)
39-
this.context = createExtContext(this)
37+
this._windows = new ExtWindows(this.rawConnection)
38+
this.commands = createExtCommands(this.rawConnection)
39+
this.context = createExtContext(this.rawConnection)
4040
}
4141

4242
public get activeWindow(): Window | null {

src/extension/features/commands.test.ts src/extension/old/features/commands.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert'
2-
import { MockMessageConnection } from '../../jsonrpc2/test/mockMessageConnection'
2+
import { MockMessageConnection } from '../../../jsonrpc2/test/mockMessageConnection'
33
import {
44
ExecuteCommandParams,
55
ExecuteCommandRegistrationOptions,
@@ -8,7 +8,7 @@ import {
88
RegistrationRequest,
99
UnregistrationParams,
1010
UnregistrationRequest,
11-
} from '../../protocol'
11+
} from '../../../protocol'
1212
import { Commands } from '../api'
1313
import { createExtCommands } from './commands'
1414

@@ -18,7 +18,7 @@ describe('ExtCommands', () => {
1818
mockConnection: MockMessageConnection
1919
} {
2020
const mockConnection = new MockMessageConnection()
21-
const extCommands = createExtCommands({ rawConnection: mockConnection })
21+
const extCommands = createExtCommands(mockConnection)
2222
return { extCommands, mockConnection }
2323
}
2424

src/extension/features/commands.ts src/extension/old/features/commands.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
import { Subscription } from 'rxjs'
22
import uuidv4 from 'uuid/v4'
3-
import { CommandRegistry } from '../../environment/providers/command'
3+
import { CommandRegistry } from '../../../environment/providers/command'
4+
import { MessageConnection } from '../../../jsonrpc2/connection'
45
import {
56
ExecuteCommandRegistrationOptions,
67
ExecuteCommandRequest,
78
RegistrationParams,
89
RegistrationRequest,
910
UnregistrationParams,
1011
UnregistrationRequest,
11-
} from '../../protocol'
12-
import { Commands, SourcegraphExtensionAPI } from '../api'
12+
} from '../../../protocol'
13+
import { Commands } from '../api'
1314

1415
/**
1516
* Creates the Sourcegraph extension API's {@link SourcegraphExtensionAPI#commands} value.
1617
*
17-
* @param ext The Sourcegraph extension API handle.
18+
* @param rawConnection The connection to the Sourcegraph API client.
1819
* @return The {@link Creates the Sourcegraph extension API extension API's#commands} value.
1920
*/
20-
export function createExtCommands(ext: Pick<SourcegraphExtensionAPI<any>, 'rawConnection'>): Commands {
21+
export function createExtCommands(rawConnection: MessageConnection): Commands {
2122
// TODO: move CommandRegistry to somewhere general since it's now used by the controller AND extension
2223
const commandRegistry = new CommandRegistry()
23-
ext.rawConnection.onRequest(ExecuteCommandRequest.type, params => commandRegistry.executeCommand(params))
24+
rawConnection.onRequest(ExecuteCommandRequest.type, params => commandRegistry.executeCommand(params))
2425
return {
2526
register: (command: string, run: (...args: any[]) => Promise<any>): Subscription => {
2627
const subscription = new Subscription()
2728

2829
const id = uuidv4()
2930
subscription.add(commandRegistry.registerCommand({ command, run }))
30-
ext.rawConnection
31+
rawConnection
3132
.sendRequest(RegistrationRequest.type, {
3233
registrations: [
3334
{
@@ -40,7 +41,7 @@ export function createExtCommands(ext: Pick<SourcegraphExtensionAPI<any>, 'rawCo
4041
.catch(err => console.error(err))
4142

4243
subscription.add(() => {
43-
ext.rawConnection
44+
rawConnection
4445
.sendRequest(UnregistrationRequest.type, {
4546
unregisterations: [{ id, method: ExecuteCommandRequest.type.method }],
4647
} as UnregistrationParams)

src/extension/features/configuration.test.ts src/extension/old/features/configuration.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import assert from 'assert'
2-
import { MockMessageConnection } from '../../jsonrpc2/test/mockMessageConnection'
2+
import { MockMessageConnection } from '../../../jsonrpc2/test/mockMessageConnection'
33
import {
44
ConfigurationCascade,
55
ConfigurationUpdateParams,
66
ConfigurationUpdateRequest,
77
DidChangeConfigurationNotification,
88
DidChangeConfigurationParams,
9-
} from '../../protocol'
9+
} from '../../../protocol'
1010
import { Configuration, Observable } from '../api'
1111
import { observableValue } from '../util'
1212
import { createExtConfiguration, setValueAtKeyPath } from './configuration'
@@ -23,7 +23,7 @@ describe('ExtConfiguration', () => {
2323
mockConnection: MockMessageConnection
2424
} {
2525
const mockConnection = new MockMessageConnection()
26-
const extConfiguration = createExtConfiguration({ rawConnection: mockConnection }, initial)
26+
const extConfiguration = createExtConfiguration(mockConnection, initial)
2727
return { extConfiguration, mockConnection }
2828
}
2929

src/extension/features/configuration.ts src/extension/old/features/configuration.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { BehaviorSubject } from 'rxjs'
22
import { distinctUntilChanged } from 'rxjs/operators'
3+
import { MessageConnection } from '../../../jsonrpc2/connection'
34
import {
45
ConfigurationCascade,
56
ConfigurationUpdateParams,
67
ConfigurationUpdateRequest,
78
DidChangeConfigurationNotification,
89
KeyPath,
9-
} from '../../protocol'
10-
import { isEqual } from '../../util'
11-
import { Configuration, Observable, SourcegraphExtensionAPI } from '../api'
10+
} from '../../../protocol'
11+
import { isEqual } from '../../../util'
12+
import { Configuration, Observable } from '../api'
1213

1314
class ExtConfiguration<C> extends BehaviorSubject<C> implements Configuration<C>, Observable<C> {
14-
constructor(private ext: Pick<SourcegraphExtensionAPI<C>, 'rawConnection'>, initial: ConfigurationCascade<C>) {
15+
constructor(private rawConnection: MessageConnection, initial: ConfigurationCascade<C>) {
1516
super(initial.merged as C)
1617

17-
ext.rawConnection.onNotification(DidChangeConfigurationNotification.type, params => {
18+
rawConnection.onNotification(DidChangeConfigurationNotification.type, params => {
1819
this.next(params.configurationCascade.merged as C)
1920
})
2021
}
@@ -35,7 +36,7 @@ class ExtConfiguration<C> extends BehaviorSubject<C> implements Configuration<C>
3536
const cur = this.value
3637
this.next(setValueAtKeyPath(cur, path, value))
3738

38-
return this.ext.rawConnection.sendRequest(ConfigurationUpdateRequest.type, {
39+
return this.rawConnection.sendRequest(ConfigurationUpdateRequest.type, {
3940
path,
4041
value,
4142
} as ConfigurationUpdateParams)
@@ -107,12 +108,12 @@ export function setValueAtKeyPath(source: any, path: KeyPath, value: any): any {
107108
/**
108109
* Creates the Sourcegraph extension API's {@link SourcegraphExtensionAPI#configuration} value.
109110
*
110-
* @param ext The Sourcegraph extension API handle.
111+
* @param rawConnection The connection to the Sourcegraph API client.
111112
* @return The {@link SourcegraphExtensionAPI#configuration} value.
112113
*/
113114
export function createExtConfiguration<C>(
114-
ext: Pick<SourcegraphExtensionAPI<C>, 'rawConnection'>,
115+
rawConnection: MessageConnection,
115116
initial: ConfigurationCascade<C>
116117
): Configuration<C> & Observable<C> {
117-
return new ExtConfiguration(ext, initial)
118+
return new ExtConfiguration(rawConnection, initial)
118119
}

src/extension/features/context.test.ts src/extension/old/features/context.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assert from 'assert'
2-
import { MockMessageConnection } from '../../jsonrpc2/test/mockMessageConnection'
3-
import { ContextUpdateNotification, ContextUpdateParams } from '../../protocol/context'
2+
import { MockMessageConnection } from '../../../jsonrpc2/test/mockMessageConnection'
3+
import { ContextUpdateNotification, ContextUpdateParams } from '../../../protocol/context'
44
import { ExtensionContext } from '../api'
55
import { createExtContext } from './context'
66

77
describe('ExtContext', () => {
88
function create(): { extContext: ExtensionContext; mockConnection: MockMessageConnection } {
99
const mockConnection = new MockMessageConnection()
10-
const extContext = createExtContext({ rawConnection: mockConnection })
10+
const extContext = createExtContext(mockConnection)
1111
return { extContext, mockConnection }
1212
}
1313

src/extension/old/features/context.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Context } from '../../../environment/context/context'
2+
import { MessageConnection } from '../../../jsonrpc2/connection'
3+
import { ContextUpdateNotification, ContextUpdateParams } from '../../../protocol/context'
4+
import { ExtensionContext } from '../api'
5+
6+
/**
7+
* Creates the Sourcegraph extension API's {@link SourcegraphExtensionAPI#context} value.
8+
*
9+
* @param rawConnection The connection to the Sourcegraph API client.
10+
* @return The {@link SourcegraphExtensionAPI#context} value.
11+
*/
12+
export function createExtContext(rawConnection: MessageConnection): ExtensionContext {
13+
return {
14+
updateContext: (updates: Context): void => {
15+
rawConnection.sendNotification(ContextUpdateNotification.type, { updates } as ContextUpdateParams)
16+
},
17+
}
18+
}

src/extension/features/windows.test.ts src/extension/old/features/windows.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import assert from 'assert'
2-
import { MockMessageConnection } from '../../jsonrpc2/test/mockMessageConnection'
2+
import { MockMessageConnection } from '../../../jsonrpc2/test/mockMessageConnection'
33
import {
44
DidCloseTextDocumentNotification,
55
DidCloseTextDocumentParams,
66
DidOpenTextDocumentNotification,
77
DidOpenTextDocumentParams,
88
ShowInputParams,
99
ShowInputRequest,
10-
} from '../../protocol'
10+
} from '../../../protocol'
11+
import { URI } from '../../../types/uri'
1112
import { Window } from '../api'
1213
import { observableValue } from '../util'
1314
import { ExtWindows } from './windows'
1415

1516
describe('ExtWindows', () => {
1617
function create(): { extWindows: ExtWindows; mockConnection: MockMessageConnection } {
1718
const mockConnection = new MockMessageConnection()
18-
const extWindows = new ExtWindows({ rawConnection: mockConnection })
19+
const extWindows = new ExtWindows(mockConnection)
1920
return { extWindows, mockConnection }
2021
}
2122

@@ -32,7 +33,7 @@ describe('ExtWindows', () => {
3233
textDocument: { uri: 'file:///a', languageId: 'l', text: 't', version: 1 },
3334
} as DidOpenTextDocumentParams)
3435
const expectedWindows: Window[] = [
35-
{ isActive: true, activeComponent: { isActive: true, resource: 'file:///a' } },
36+
{ isActive: true, activeComponent: { isActive: true, resource: URI.parse('file:///a') } },
3637
]
3738
assert.deepStrictEqual(observableValue(extWindows), expectedWindows)
3839
assert.deepStrictEqual(extWindows.activeWindow, expectedWindows[0])
@@ -58,7 +59,7 @@ describe('ExtWindows', () => {
5859
textDocument: { uri: 'file:///b' },
5960
} as DidCloseTextDocumentParams)
6061
assert.deepStrictEqual(observableValue(extWindows), [
61-
{ isActive: true, activeComponent: { isActive: true, resource: 'file:///a' } },
62+
{ isActive: true, activeComponent: { isActive: true, resource: URI.parse('file:///a') } },
6263
] as Window[])
6364
})
6465
})

src/extension/features/windows.ts src/extension/old/features/windows.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { BehaviorSubject } from 'rxjs'
2+
import { MessageConnection } from '../../../jsonrpc2/connection'
23
import {
34
DidCloseTextDocumentNotification,
45
DidOpenTextDocumentNotification,
56
ShowInputRequest,
67
TextDocumentDecoration,
78
TextDocumentPublishDecorationsNotification,
89
TextDocumentPublishDecorationsParams,
9-
} from '../../protocol'
10-
import { TextDocumentIdentifier } from '../../types/textDocument'
11-
import { Observable, SourcegraphExtensionAPI, Window, Windows } from '../api'
10+
} from '../../../protocol'
11+
import { TextDocumentIdentifier } from '../../../types/textDocument'
12+
import { URI } from '../../../types/uri'
13+
import { Observable, Window, Windows } from '../api'
1214

1315
/**
1416
* Implements the Sourcegraph extension API's {@link SourcegraphExtensionAPI#windows} value.
@@ -17,7 +19,7 @@ import { Observable, SourcegraphExtensionAPI, Window, Windows } from '../api'
1719
* @return The {@link SourcegraphExtensionAPI#windows} value.
1820
*/
1921
export class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Observable<Window[]> {
20-
constructor(private ext: Pick<SourcegraphExtensionAPI<any>, 'rawConnection'>) {
22+
constructor(private rawConnection: MessageConnection) {
2123
super([
2224
{
2325
isActive: true,
@@ -26,15 +28,17 @@ export class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Ob
2628
])
2729

2830
// Track last-opened text document.
29-
ext.rawConnection.onNotification(DidOpenTextDocumentNotification.type, params => {
30-
this.next([{ ...this.value[0], activeComponent: { isActive: true, resource: params.textDocument.uri } }])
31+
rawConnection.onNotification(DidOpenTextDocumentNotification.type, params => {
32+
this.next([
33+
{ ...this.value[0], activeComponent: { isActive: true, resource: URI.parse(params.textDocument.uri) } },
34+
])
3135
})
32-
ext.rawConnection.onNotification(DidCloseTextDocumentNotification.type, params => {
36+
rawConnection.onNotification(DidCloseTextDocumentNotification.type, params => {
3337
if (
3438
this.activeWindow &&
3539
this.activeWindow.activeComponent &&
3640
this.activeWindow.activeComponent.resource &&
37-
this.activeWindow.activeComponent.resource === params.textDocument.uri
41+
this.activeWindow.activeComponent.resource.toString() === params.textDocument.uri
3842
) {
3943
this.next([{ ...this.value[0], activeComponent: null }])
4044
}
@@ -46,11 +50,11 @@ export class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Ob
4650
}
4751

4852
public showInputBox(message: string, defaultValue?: string): Promise<string | null> {
49-
return this.ext.rawConnection.sendRequest(ShowInputRequest.type, { message, defaultValue })
53+
return this.rawConnection.sendRequest(ShowInputRequest.type, { message, defaultValue })
5054
}
5155

5256
public setDecorations(resource: TextDocumentIdentifier, decorations: TextDocumentDecoration[]): void {
53-
return this.ext.rawConnection.sendNotification(TextDocumentPublishDecorationsNotification.type, {
57+
return this.rawConnection.sendNotification(TextDocumentPublishDecorationsNotification.type, {
5458
textDocument: resource,
5559
decorations,
5660
} as TextDocumentPublishDecorationsParams)
File renamed without changes.

src/index.ts

-3
This file was deleted.

0 commit comments

Comments
 (0)