1+ import * as lspClient from '@sourcegraph/lsp-client'
12import '@babel/polyfill'
23
34import { activateBasicCodeIntel , registerFeedbackButton } from '@sourcegraph/basic-code-intel'
@@ -26,6 +27,7 @@ import {
2627import { ConsoleLogger , createWebSocketConnection } from '@sourcegraph/vscode-ws-jsonrpc'
2728import gql from 'tagged-template-noop'
2829import { Settings } from './settings'
30+ import { TextDocument } from 'sourcegraph'
2931
3032// If we can rid ourselves of file:// URIs, this type won't be necessary and we
3133// can use lspext.Xreference directly.
@@ -739,11 +741,43 @@ const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } }
739741
740742export function activate ( ctx : sourcegraph . ExtensionContext = DUMMY_CTX ) : void {
741743 async function afterActivate ( ) : Promise < void > {
742- const address = sourcegraph . configuration . get < Settings > ( ) . get ( 'go.serverUrl' )
743- if ( address ) {
744- ctx . subscriptions . add ( registerFeedbackButton ( { languageID : 'go' , sourcegraph, isPrecise : true } ) )
744+ if ( Math . random ( ) < 0 ) {
745+ const address = sourcegraph . configuration . get < Settings > ( ) . get ( 'go.serverUrl' )
746+ if ( address ) {
747+ ctx . subscriptions . add ( registerFeedbackButton ( { languageID : 'go' , sourcegraph, isPrecise : true } ) )
748+ await activateUsingWebSockets ( ctx )
749+ } else {
750+ activateBasicCodeIntel ( {
751+ sourcegraph,
752+ languageID : 'go' ,
753+ fileExts : [ 'go' ] ,
754+ filterDefinitions : ( { repo, filePath, pos, fileContent, results } ) => {
755+ const currentFileImportedPaths = fileContent
756+ . split ( '\n' )
757+ . map ( line => {
758+ // Matches the import at index 3
759+ const match = / ^ ( i m p o r t | \t ) ( \w + | \. ) ? " ( .* ) " $ / . exec ( line )
760+ return match ? match [ 3 ] : undefined
761+ } )
762+ . filter ( ( x ) : x is string => Boolean ( x ) )
763+
764+ const currentFileImportPath = repo + '/' + path . dirname ( filePath )
765+
766+ const filteredResults = results . filter ( result => {
767+ const resultImportPath = result . repo + '/' + path . dirname ( result . file )
768+ return (
769+ currentFileImportedPaths . some ( i => resultImportPath . includes ( i ) ) ||
770+ resultImportPath === currentFileImportPath
771+ )
772+ } )
745773
746- await activateUsingWebSockets ( ctx )
774+ return filteredResults . length === 0 ? results : filteredResults
775+ } ,
776+ commentStyle : {
777+ lineRegex : / \/ \/ \s ? / ,
778+ } ,
779+ } ) ( ctx )
780+ }
747781 } else {
748782 ctx . subscriptions . add (
749783 registerFeedbackButton ( {
@@ -753,36 +787,45 @@ export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
753787 } )
754788 )
755789
756- activateBasicCodeIntel ( {
757- sourcegraph,
758- languageID : 'go' ,
759- fileExts : [ 'go' ] ,
760- filterDefinitions : ( { repo, filePath, pos, fileContent, results } ) => {
761- const currentFileImportedPaths = fileContent
762- . split ( '\n' )
763- . map ( line => {
764- // Matches the import at index 3
765- const match = / ^ ( i m p o r t | \t ) ( \w + | \. ) ? " ( .* ) " $ / . exec ( line )
766- return match ? match [ 3 ] : undefined
767- } )
768- . filter ( ( x ) : x is string => Boolean ( x ) )
769-
770- const currentFileImportPath = repo + '/' + path . dirname ( filePath )
771-
772- const filteredResults = results . filter ( result => {
773- const resultImportPath = result . repo + '/' + path . dirname ( result . file )
774- return (
775- currentFileImportedPaths . some ( i => resultImportPath . includes ( i ) ) ||
776- resultImportPath === currentFileImportPath
777- )
790+ const token = await getOrTryToCreateAccessToken ( )
791+ if ( ! token ) {
792+ // console.log('No token')
793+ } else {
794+ // tslint:disable-next-line: no-empty
795+ const noop = ( ) => { }
796+
797+ const serverURL = sourcegraph . configuration . get < Settings > ( ) . value [ 'go.serverUrl' ]
798+ if ( serverURL ) {
799+ const client = await lspClient . register ( {
800+ sourcegraph,
801+ transport : lspClient . webSocketTransport ( {
802+ serverUrl : serverURL ,
803+ logger : {
804+ error : noop ,
805+ warn : noop ,
806+ info : noop ,
807+ log : noop ,
808+ } ,
809+ } ) ,
810+ documentSelector : [ { language : 'go' } ] ,
811+ // clientToServerURI: uri => ..., // optional
812+ // serverToClientURI: uri => ..., // optional
813+ initializationOptions : ( url : URL ) => {
814+ url . hash = ''
815+ return {
816+ zipURL : constructZipURL ( {
817+ repoName : pathname ( url . href ) . replace ( / ^ \/ + / , '' ) ,
818+ revision : url . search . substr ( 1 ) ,
819+ token,
820+ } ) ,
821+ }
822+ } ,
778823 } )
779-
780- return filteredResults . length === 0 ? results : filteredResults
781- } ,
782- commentStyle : {
783- lineRegex : / \/ \/ \s ? / ,
784- } ,
785- } ) ( ctx )
824+ ctx . subscriptions . add ( client )
825+ } else {
826+ // console.log('No go.serverUrl set')
827+ }
828+ }
786829 }
787830 }
788831 setTimeout ( afterActivate , 100 )
0 commit comments