1
1
import '@babel/polyfill'
2
2
3
- import * as basicCodeIntel from '@sourcegraph/basic-code-intel'
3
+ import { activateBasicCodeIntel } from '@sourcegraph/basic-code-intel'
4
4
import * as wsrpc from '@sourcegraph/vscode-ws-jsonrpc'
5
5
import { ajax } from 'rxjs/ajax'
6
6
import * as sourcegraph from 'sourcegraph'
@@ -10,19 +10,7 @@ import * as convert from './convert-lsp-to-sea'
10
10
import * as lspext from './lspext'
11
11
12
12
import * as path from 'path'
13
-
14
- import {
15
- BehaviorSubject ,
16
- from ,
17
- Observable ,
18
- Observer ,
19
- of ,
20
- throwError ,
21
- race ,
22
- combineLatest ,
23
- ObservableInput ,
24
- merge ,
25
- } from 'rxjs'
13
+ import { BehaviorSubject , from , Observable , Observer , of , throwError } from 'rxjs'
26
14
import {
27
15
concatMap ,
28
16
distinctUntilChanged ,
@@ -33,20 +21,11 @@ import {
33
21
switchMap ,
34
22
take ,
35
23
finalize ,
36
- tap ,
37
- delay ,
38
- mapTo ,
39
- startWith ,
40
- takeUntil ,
41
- share ,
42
- filter ,
43
- endWith ,
44
24
} from 'rxjs/operators'
45
25
46
26
import { ConsoleLogger , createWebSocketConnection } from '@sourcegraph/vscode-ws-jsonrpc'
47
27
import gql from 'tagged-template-noop'
48
28
import { Settings } from './settings'
49
- import { documentSelector } from '@sourcegraph/basic-code-intel/lib/handler'
50
29
51
30
// If we can rid ourselves of file:// URIs, this type won't be necessary and we
52
31
// can use lspext.Xreference directly.
@@ -546,36 +525,10 @@ function positionParams(doc: sourcegraph.TextDocument, pos: sourcegraph.Position
546
525
}
547
526
}
548
527
549
- /**
550
- * Emits from `fallback` after `delayMilliseconds`. Useful for falling back to
551
- * basic-code-intel while the language server is running.
552
- */
553
- function withFallback < T > ( {
554
- main,
555
- fallback,
556
- delayMilliseconds,
557
- } : {
558
- main : ObservableInput < T >
559
- fallback : ObservableInput < T >
560
- delayMilliseconds : number
561
- } ) : Observable < T > {
562
- return from ( main )
563
- // return race(
564
- // of(null).pipe(switchMap(() => from(main))),
565
- // of(null).pipe(
566
- // delay(delayMilliseconds),
567
- // switchMap(() => from(fallback))
568
- // )
569
- // )
570
- }
571
-
572
528
/**
573
529
* Uses WebSockets to communicate with a language server.
574
530
*/
575
- export async function activateUsingWebSockets (
576
- ctx : sourcegraph . ExtensionContext ,
577
- basicCodeIntelHandler : basicCodeIntel . Handler
578
- ) : Promise < void > {
531
+ export async function activateUsingWebSockets ( ctx : sourcegraph . ExtensionContext ) : Promise < void > {
579
532
const accessToken = await getOrTryToCreateAccessToken ( )
580
533
const settings : BehaviorSubject < Settings > = new BehaviorSubject < Settings > ( { } )
581
534
ctx . subscriptions . add (
@@ -626,55 +579,38 @@ export async function activateUsingWebSockets(
626
579
627
580
ctx . subscriptions . add (
628
581
sourcegraph . languages . registerHoverProvider ( [ { pattern : '*.go' } ] , {
629
- provideHover : ( doc : sourcegraph . TextDocument , pos : sourcegraph . Position ) =>
630
- withFallback ( {
631
- main : sendDocPositionRequest ( { doc, pos, ty : lsp . HoverRequest . type , useCache : true } ) . then (
632
- convert . hover
633
- ) ,
634
- fallback : basicCodeIntelHandler . hover ( doc , pos ) ,
635
- delayMilliseconds : 500 ,
636
- } ) ,
582
+ provideHover : async ( doc : sourcegraph . TextDocument , pos : sourcegraph . Position ) => {
583
+ const response = await sendDocPositionRequest ( { doc, pos, ty : lsp . HoverRequest . type , useCache : true } )
584
+ return convert . hover ( response )
585
+ } ,
637
586
} )
638
587
)
639
588
640
589
ctx . subscriptions . add (
641
590
sourcegraph . languages . registerDefinitionProvider ( [ { pattern : '*.go' } ] , {
642
- provideDefinition : ( doc : sourcegraph . TextDocument , pos : sourcegraph . Position ) =>
643
- withFallback ( {
644
- main : sendDocPositionRequest ( {
645
- doc,
646
- pos,
647
- ty : new lsp . RequestType < any , any , any , void > ( 'textDocument/xdefinition' ) as any ,
648
- useCache : true ,
649
- } ) . then ( response => convert . xdefinition ( { currentDocURI : doc . uri , xdefinition : response } ) ) ,
650
- fallback : basicCodeIntelHandler . definition ( doc , pos ) ,
651
- delayMilliseconds : 500 ,
652
- } ) ,
591
+ provideDefinition : async ( doc : sourcegraph . TextDocument , pos : sourcegraph . Position ) => {
592
+ const response = await sendDocPositionRequest ( {
593
+ doc,
594
+ pos,
595
+ ty : new lsp . RequestType < any , any , any , void > ( 'textDocument/xdefinition' ) as any ,
596
+ useCache : true ,
597
+ } )
598
+ return convert . xdefinition ( { currentDocURI : doc . uri , xdefinition : response } )
599
+ } ,
653
600
} )
654
601
)
655
602
656
603
ctx . subscriptions . add (
657
604
sourcegraph . languages . registerReferenceProvider ( [ { pattern : '*.go' } ] , {
658
- provideReferences : ( doc : sourcegraph . TextDocument , pos : sourcegraph . Position ) =>
659
- withFallback ( {
660
- main : sendDocPositionRequest ( {
661
- doc,
662
- pos,
663
- ty : lsp . ReferencesRequest . type ,
664
- useCache : true ,
665
- } ) . then ( response => ( {
666
- kind : 'main' ,
667
- result : convert . references ( { currentDocURI : doc . uri , references : response } ) ,
668
- } ) ) ,
669
- fallback : basicCodeIntelHandler . references ( doc , pos ) . then ( result => ( { kind : 'fallback' , result } ) ) ,
670
- delayMilliseconds : 2000 ,
671
- } ) . pipe (
672
- // Indicate in the UI that the results are imprecise
673
- tap ( ( { kind } ) => {
674
- sourcegraph . internal . updateContext ( { isImprecise : kind === 'fallback' } )
675
- } ) ,
676
- map ( ( { result } ) => result )
677
- ) ,
605
+ provideReferences : async ( doc : sourcegraph . TextDocument , pos : sourcegraph . Position ) => {
606
+ const response = await sendDocPositionRequest ( {
607
+ doc,
608
+ pos,
609
+ ty : lsp . ReferencesRequest . type ,
610
+ useCache : true ,
611
+ } )
612
+ return convert . references ( { currentDocURI : doc . uri , references : response } )
613
+ } ,
678
614
} )
679
615
)
680
616
@@ -760,61 +696,45 @@ function pathname(url: string): string {
760
696
return pathname
761
697
}
762
698
763
- const basicCodeIntelHandlerArgs : basicCodeIntel . HandlerArgs = {
764
- sourcegraph,
765
- languageID : 'go' ,
766
- fileExts : [ 'go' ] ,
767
- filterDefinitions : ( { repo, filePath, fileContent, results } ) => {
768
- const currentFileImportedPaths = fileContent
769
- . split ( '\n' )
770
- . map ( line => {
771
- // Matches the import at index 3
772
- const match = / ^ ( i m p o r t | \t ) ( \w + | \. ) ? " ( .* ) " $ / . exec ( line )
773
- return match ? match [ 3 ] : undefined
774
- } )
775
- . filter ( ( x ) : x is string => Boolean ( x ) )
776
-
777
- const currentFileImportPath = repo + '/' + path . dirname ( filePath )
778
-
779
- const filteredResults = results . filter ( result => {
780
- const resultImportPath = result . repo + '/' + path . dirname ( result . file )
781
- return [ ...currentFileImportedPaths , currentFileImportPath ] . some ( i => resultImportPath === i )
782
- } )
783
-
784
- return filteredResults . length === 0 ? results : filteredResults
785
- } ,
786
- commentStyle : {
787
- lineRegex : / \/ \/ \s ? / ,
788
- } ,
789
- }
790
-
791
699
// No-op for Sourcegraph versions prior to 3.0.
792
700
const DUMMY_CTX = { subscriptions : { add : ( _unsubscribable : any ) => void 0 } }
793
701
794
702
export function activate ( ctx : sourcegraph . ExtensionContext = DUMMY_CTX ) : void {
795
703
async function afterActivate ( ) : Promise < void > {
796
- const basicCodeIntelHandler = new basicCodeIntel . Handler ( basicCodeIntelHandlerArgs )
797
704
const address = sourcegraph . configuration . get < Settings > ( ) . get ( 'go.serverUrl' )
798
705
if ( address ) {
799
- await activateUsingWebSockets ( ctx , basicCodeIntelHandler )
706
+ await activateUsingWebSockets ( ctx )
800
707
} else {
801
- sourcegraph . internal . updateContext ( { isImprecise : true } )
708
+ activateBasicCodeIntel ( {
709
+ sourcegraph,
710
+ languageID : 'go' ,
711
+ fileExts : [ 'go' ] ,
712
+ filterDefinitions : ( { repo, filePath, pos, fileContent, results } ) => {
713
+ const currentFileImportedPaths = fileContent
714
+ . split ( '\n' )
715
+ . map ( line => {
716
+ // Matches the import at index 3
717
+ const match = / ^ ( i m p o r t | \t ) ( \w + | \. ) ? " ( .* ) " $ / . exec ( line )
718
+ return match ? match [ 3 ] : undefined
719
+ } )
720
+ . filter ( ( x ) : x is string => Boolean ( x ) )
721
+
722
+ const currentFileImportPath = repo + '/' + path . dirname ( filePath )
723
+
724
+ const filteredResults = results . filter ( result => {
725
+ const resultImportPath = result . repo + '/' + path . dirname ( result . file )
726
+ return (
727
+ currentFileImportedPaths . some ( i => resultImportPath . includes ( i ) ) ||
728
+ resultImportPath === currentFileImportPath
729
+ )
730
+ } )
802
731
803
- ctx . subscriptions . add (
804
- sourcegraph . languages . registerHoverProvider ( documentSelector ( basicCodeIntelHandler . fileExts ) , {
805
- provideHover : ( doc , pos ) => basicCodeIntelHandler . hover ( doc , pos ) ,
806
- } )
807
- )
808
- ctx . subscriptions . add (
809
- sourcegraph . languages . registerDefinitionProvider ( documentSelector ( basicCodeIntelHandler . fileExts ) , {
810
- provideDefinition : ( doc , pos ) => basicCodeIntelHandler . definition ( doc , pos ) ,
811
- } )
812
- )
813
- ctx . subscriptions . add (
814
- sourcegraph . languages . registerReferenceProvider ( documentSelector ( basicCodeIntelHandler . fileExts ) , {
815
- provideReferences : ( doc , pos ) => basicCodeIntelHandler . references ( doc , pos ) ,
816
- } )
817
- )
732
+ return filteredResults . length === 0 ? results : filteredResults
733
+ } ,
734
+ commentStyle : {
735
+ lineRegex : / \/ \/ \s ? / ,
736
+ } ,
737
+ } ) ( ctx )
818
738
}
819
739
}
820
740
setTimeout ( afterActivate , 100 )
0 commit comments