Skip to content

Commit

Permalink
feat: support configuration (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfbecker committed Apr 24, 2019
1 parent 24d3c99 commit 8432004
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
46 changes: 32 additions & 14 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,39 @@ describe('createStubSourcegraphAPI()', () => {
const stub = createStubSourcegraphAPI()
assert(stub)
})
it('should support creating unique decorationTypes', () => {
const stub = createStubSourcegraphAPI()
assert.deepStrictEqual(stub.app.createDecorationType(), { key: 'decorationType0' })
assert.deepStrictEqual(stub.app.createDecorationType(), { key: 'decorationType1' })
assert.deepStrictEqual(stub.app.createDecorationType(), { key: 'decorationType2' })
describe('app', () => {
it('should support creating unique decorationTypes', () => {
const stub = createStubSourcegraphAPI()
assert.deepStrictEqual(stub.app.createDecorationType(), { key: 'decorationType0' })
assert.deepStrictEqual(stub.app.createDecorationType(), { key: 'decorationType1' })
assert.deepStrictEqual(stub.app.createDecorationType(), { key: 'decorationType2' })
})
})

// TODO fix Position/Range classes (publish as package) and MarkupKind enum assignability
// it('should allow registering a hover provider', () => {
// const stub = createStubSourcegraphAPI()
// stub.languages.registerHoverProvider(['*.ts'], {
// provideHover: (doc, pos) => ({
// contents: { kind: stub.MarkupKind.Markdown, value: doc.uri },
// range: new stub.Range(new stub.Position(1, 2), new stubs.Position(3, 4)),
// }),
describe('configuration', () => {
it('should support reading configuration', async () => {
const stub = createStubSourcegraphAPI()
assert.deepStrictEqual(stub.configuration.get().value, {})
const listener = sinon.spy()
stub.configuration.subscribe(listener)
sinon.assert.calledOnce(listener)
assert.deepStrictEqual(listener.args[0][0], undefined)
await stub.configuration.get().update('foo', 'bar')
assert.deepStrictEqual(stub.configuration.get().value, { foo: 'bar' })
assert.deepStrictEqual(stub.configuration.get().get('foo'), 'bar')
sinon.assert.calledTwice(listener)
assert.deepStrictEqual(listener.args[1][0], undefined)
})
})
// describe('languages', () => {
// // TODO fix Position/Range classes (publish as package) and MarkupKind enum assignability
// it('should allow registering a hover provider', () => {
// const stub = createStubSourcegraphAPI()
// stub.languages.registerHoverProvider(['*.ts'], {
// provideHover: (doc, pos) => ({
// contents: { kind: stub.MarkupKind.Markdown, value: doc.uri },
// range: new stub.Range(new stub.Position(1, 2), new stubs.Position(3, 4)),
// }),
// })
// })
// })
})
Expand Down
19 changes: 17 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Subject, Subscription } from 'rxjs'
import { BehaviorSubject, Subject, Subscription } from 'rxjs'
import { mapTo } from 'rxjs/operators'
import * as sinon from 'sinon'
import * as sourcegraph from 'sourcegraph'
import { MarkupKind } from 'vscode-languageserver-types'
Expand Down Expand Up @@ -27,6 +28,7 @@ let decorationTypeCounter = 0
* with all methods being Sinon spys and all Subscribables being Subjects.
*/
export const createStubSourcegraphAPI = () => {
const configSubject = new BehaviorSubject<any>({})
const rootChanges = new Subject<void>()
const openedTextDocuments = new Subject<sourcegraph.TextDocument>()
// const shims: typeof import('sourcegraph') = {
Expand Down Expand Up @@ -73,7 +75,20 @@ export const createStubSourcegraphAPI = () => {
app: {
createDecorationType: () => ({ key: 'decorationType' + decorationTypeCounter++ }),
},
configuration: {}, // TODO
configuration: {
get: <C extends object = { [key: string]: any }>() => ({
get: <K extends keyof C>(key: K): Readonly<C[K]> | undefined => configSubject.value[key],

update: async <K extends keyof C>(key: K, value: C[K] | undefined): Promise<void> => {
configSubject.next({ ...configSubject.value, [key]: value })
},

get value(): Readonly<C> {
return configSubject.value
},
}),
subscribe: (next: () => void) => configSubject.pipe(mapTo(undefined)).subscribe(next),
},
search: {}, // TODO
commands: {}, // TODO
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@sourcegraph/tsconfig",
"compilerOptions": {
"target": "es2016",
"target": "es2018",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4711,7 +4711,7 @@ resolve-url@^0.2.1:
resolved "https://registry.npmjs.org/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=

resolve@^1.10.0, resolve@^1.3.2, resolve@^1.4.0:
resolve@^1.3.2, resolve@^1.4.0:
version "1.10.1"
resolved "https://registry.npmjs.org/resolve/-/resolve-1.10.1.tgz#664842ac960795bbe758221cdccda61fb64b5f18"
integrity sha512-KuIe4mf++td/eFb6wkaPbMDnP6kObCaEtIDuHOUED6MNUo4K670KZUHuuvYPZDxNF0WVLw49n06M2m2dXphEzA==
Expand Down

0 comments on commit 8432004

Please sign in to comment.