Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.

Commit 54b0358

Browse files
committed
.
1 parent dfe2969 commit 54b0358

File tree

1 file changed

+75
-37
lines changed

1 file changed

+75
-37
lines changed

src/lang-go.ts

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,43 @@ function positionParams(doc: sourcegraph.TextDocument, pos: sourcegraph.Position
565565
}
566566
}
567567

568+
/**
569+
* Automatically registers/deregisters a provider based on the given predicate of the settings.
570+
*/
571+
function registerWhile({
572+
register,
573+
settings,
574+
settingsPredicate,
575+
}: {
576+
register: () => sourcegraph.Unsubscribable
577+
settings: Observable<Settings>
578+
settingsPredicate: (settings: Settings) => boolean
579+
}): sourcegraph.Unsubscribable {
580+
let registration: sourcegraph.Unsubscribable | undefined
581+
return from(settings)
582+
.pipe(
583+
map(settingsPredicate),
584+
distinctUntilChanged(),
585+
map(enabled => {
586+
if (enabled) {
587+
registration = register()
588+
} else {
589+
if (registration) {
590+
registration.unsubscribe()
591+
registration = undefined
592+
}
593+
}
594+
}),
595+
finalize(() => {
596+
if (registration) {
597+
registration.unsubscribe()
598+
registration = undefined
599+
}
600+
})
601+
)
602+
.subscribe()
603+
}
604+
568605
/**
569606
* Uses WebSockets to communicate with a language server.
570607
*/
@@ -654,41 +691,6 @@ export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext)
654691
})
655692
)
656693

657-
/**
658-
* Automatically registers/deregisters a provider based on the given predicate of the settings.
659-
*/
660-
function registerWhile({
661-
register,
662-
settingsPredicate,
663-
}: {
664-
register: () => sourcegraph.Unsubscribable
665-
settingsPredicate: (settings: Settings) => boolean
666-
}): sourcegraph.Unsubscribable {
667-
let registration: sourcegraph.Unsubscribable | undefined
668-
return from(settings)
669-
.pipe(
670-
map(settingsPredicate),
671-
distinctUntilChanged(),
672-
map(enabled => {
673-
if (enabled) {
674-
registration = register()
675-
} else {
676-
if (registration) {
677-
registration.unsubscribe()
678-
registration = undefined
679-
}
680-
}
681-
}),
682-
finalize(() => {
683-
if (registration) {
684-
registration.unsubscribe()
685-
registration = undefined
686-
}
687-
})
688-
)
689-
.subscribe()
690-
}
691-
692694
ctx.subscriptions.add(
693695
registerWhile({
694696
register: () =>
@@ -703,6 +705,7 @@ export async function activateUsingWebSockets(ctx: sourcegraph.ExtensionContext)
703705
map(response => convert.xreferences({ references: response }))
704706
),
705707
}),
708+
settings,
706709
settingsPredicate: settings => Boolean(settings['go.showExternalReferences']),
707710
})
708711
)
@@ -742,6 +745,7 @@ const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } }
742745
export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
743746
async function afterActivate(): Promise<void> {
744747
if (!sourcegraph.configuration.get().get('lspclient')) {
748+
console.log('old')
745749
const address = sourcegraph.configuration.get<Settings>().get('go.serverUrl')
746750
if (address) {
747751
ctx.subscriptions.add(registerFeedbackButton({ languageID: 'go', sourcegraph, isPrecise: true }))
@@ -779,6 +783,8 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
779783
})(ctx)
780784
}
781785
} else {
786+
console.log('lspclient')
787+
782788
ctx.subscriptions.add(
783789
registerFeedbackButton({
784790
languageID: 'go',
@@ -808,7 +814,10 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
808814
},
809815
}),
810816
documentSelector: [{ language: 'go' }],
811-
clientToServerURI: (uri: URL) => new URL(`file:///${uri.hash.slice(1)}`),
817+
clientToServerURI: (uri: URL) => {
818+
return uri
819+
},
820+
// clientToServerURI: (uri: URL) => new URL(`file:///${uri.hash.slice(1)}`),
812821
serverToClientURI: (uri, currentRootURI) => {
813822
if (!currentRootURI) {
814823
return uri
@@ -826,7 +835,8 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
826835
const originalRootURI = rootURI.href
827836
rootURI.hash = ''
828837
return {
829-
originalRootURI,
838+
// originalRootURI,
839+
// TODO drop zipURL, keep zipURLTemplate (different PR)
830840
zipURL: constructZipURL({
831841
repoName: pathname(rootURI.href).replace(/^\/+/, ''),
832842
revision: rootURI.search.substr(1),
@@ -837,6 +847,34 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
837847
},
838848
})
839849
ctx.subscriptions.add(client)
850+
const settings: BehaviorSubject<Settings> = new BehaviorSubject<Settings>({})
851+
ctx.subscriptions.add(
852+
sourcegraph.configuration.subscribe(() => {
853+
settings.next(sourcegraph.configuration.get<Settings>().value)
854+
})
855+
)
856+
857+
ctx.subscriptions.add(
858+
registerWhile({
859+
register: () =>
860+
sourcegraph.languages.registerReferenceProvider([{ pattern: '*.go' }], {
861+
provideReferences: (doc: sourcegraph.TextDocument, pos: sourcegraph.Position) =>
862+
xrefs({
863+
doc,
864+
pos,
865+
sendRequest: ({ rootURI, requestType, request, useCache }) =>
866+
client.withConnection(rootURI, conn =>
867+
conn.sendRequest(requestType, request)
868+
),
869+
}).pipe(
870+
scan((acc: XRef[], curr: XRef) => [...acc, curr], [] as XRef[]),
871+
map(response => convert.xreferences({ references: response }))
872+
),
873+
}),
874+
settings,
875+
settingsPredicate: settings => Boolean(settings['go.showExternalReferences']),
876+
})
877+
)
840878
} else {
841879
// console.log('No go.serverUrl set')
842880
}

0 commit comments

Comments
 (0)