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

Commit 76a17bb

Browse files
authored
Revert fallback to basic-code-intel (#63)
1 parent 5d35c20 commit 76a17bb

File tree

3 files changed

+60
-146
lines changed

3 files changed

+60
-146
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@
121121
"typescript": "^3.1.6"
122122
},
123123
"dependencies": {
124-
"@sourcegraph/basic-code-intel": "6.0.14",
124+
"@sourcegraph/basic-code-intel": "6.0.15",
125125
"@sourcegraph/vscode-ws-jsonrpc": "0.0.3-fork",
126-
"path-browserify": "^1.0.0",
127126
"prettier": "^1.16.4",
128127
"rxjs": "^6.3.3",
129128
"sourcegraph-langserver-http": "https://github.com/sourcegraph/sourcegraph-langserver-http#0b0173feef37d1f4f68d881c95ac3f4c97bfedb3",

src/lang-go.ts

+55-135
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@babel/polyfill'
22

3-
import * as basicCodeIntel from '@sourcegraph/basic-code-intel'
3+
import { activateBasicCodeIntel } from '@sourcegraph/basic-code-intel'
44
import * as wsrpc from '@sourcegraph/vscode-ws-jsonrpc'
55
import { ajax } from 'rxjs/ajax'
66
import * as sourcegraph from 'sourcegraph'
@@ -10,19 +10,7 @@ import * as convert from './convert-lsp-to-sea'
1010
import * as lspext from './lspext'
1111

1212
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'
2614
import {
2715
concatMap,
2816
distinctUntilChanged,
@@ -33,20 +21,11 @@ import {
3321
switchMap,
3422
take,
3523
finalize,
36-
tap,
37-
delay,
38-
mapTo,
39-
startWith,
40-
takeUntil,
41-
share,
42-
filter,
43-
endWith,
4424
} from 'rxjs/operators'
4525

4626
import { ConsoleLogger, createWebSocketConnection } from '@sourcegraph/vscode-ws-jsonrpc'
4727
import gql from 'tagged-template-noop'
4828
import { Settings } from './settings'
49-
import { documentSelector } from '@sourcegraph/basic-code-intel/lib/handler'
5029

5130
// If we can rid ourselves of file:// URIs, this type won't be necessary and we
5231
// can use lspext.Xreference directly.
@@ -546,36 +525,10 @@ function positionParams(doc: sourcegraph.TextDocument, pos: sourcegraph.Position
546525
}
547526
}
548527

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-
572528
/**
573529
* Uses WebSockets to communicate with a language server.
574530
*/
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> {
579532
const accessToken = await getOrTryToCreateAccessToken()
580533
const settings: BehaviorSubject<Settings> = new BehaviorSubject<Settings>({})
581534
ctx.subscriptions.add(
@@ -626,55 +579,38 @@ export async function activateUsingWebSockets(
626579

627580
ctx.subscriptions.add(
628581
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+
},
637586
})
638587
)
639588

640589
ctx.subscriptions.add(
641590
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+
},
653600
})
654601
)
655602

656603
ctx.subscriptions.add(
657604
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+
},
678614
})
679615
)
680616

@@ -760,61 +696,45 @@ function pathname(url: string): string {
760696
return pathname
761697
}
762698

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 = /^(import |\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-
791699
// No-op for Sourcegraph versions prior to 3.0.
792700
const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } }
793701

794702
export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void {
795703
async function afterActivate(): Promise<void> {
796-
const basicCodeIntelHandler = new basicCodeIntel.Handler(basicCodeIntelHandlerArgs)
797704
const address = sourcegraph.configuration.get<Settings>().get('go.serverUrl')
798705
if (address) {
799-
await activateUsingWebSockets(ctx, basicCodeIntelHandler)
706+
await activateUsingWebSockets(ctx)
800707
} 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 = /^(import |\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+
})
802731

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)
818738
}
819739
}
820740
setTimeout(afterActivate, 100)

yarn.lock

+4-9
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,10 @@
700700
resolved "https://registry.yarnpkg.com/@sindresorhus/is/-/is-0.7.0.tgz#9a06f4f137ee84d7df0460c1fdb1135ffa6c50fd"
701701
integrity sha512-ONhaKPIufzzrlNbqtWFFd+jlnemX6lJAgq9ZeiZtS7I1PIf/la7CW4m83rTXRnVnsMbW2k56pGYu7AUFJD9Pow==
702702

703-
"@sourcegraph/basic-code-intel@6.0.14":
704-
version "6.0.14"
705-
resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.14.tgz#898216e8966d1b902c5e49199a2a33d255ed9de4"
706-
integrity sha512-SekYreQAgpmeE4oNWh+Dke6ChXwudLyr+5qU79DX6lSQY1UQLg55T5cmwSbcIBLKwQzvyyUJ7YLvtQhXOn7QwA==
703+
"@sourcegraph/basic-code-intel@6.0.15":
704+
version "6.0.15"
705+
resolved "https://registry.yarnpkg.com/@sourcegraph/basic-code-intel/-/basic-code-intel-6.0.15.tgz#b7c0baa894d89244fc5155a08839543cdbcca4ea"
706+
integrity sha512-oMNbyAar7Sy4gnX5hLXQZhmLFy19zFtKndkJ4BlLCvRsmuJozMgWT32sUnhpDrco32NJQf6Irgl7a7PFX1Gb5w==
707707
dependencies:
708708
lodash "^4.17.11"
709709
rxjs "^6.3.3"
@@ -3833,11 +3833,6 @@ path-browserify@0.0.0:
38333833
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.0.tgz#a0b870729aae214005b7d5032ec2cbbb0fb4451a"
38343834
integrity sha1-oLhwcpquIUAFt9UDLsLLuw+0RRo=
38353835

3836-
path-browserify@^1.0.0:
3837-
version "1.0.0"
3838-
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.0.tgz#40702a97af46ae00b0ea6fa8998c0b03c0af160d"
3839-
integrity sha512-Hkavx/nY4/plImrZPHRk2CL9vpOymZLgEbMNX1U0bjcBL7QN9wODxyx0yaMZURSQaUtSEvDrfAvxa9oPb0at9g==
3840-
38413836
path-dirname@^1.0.0:
38423837
version "1.0.2"
38433838
resolved "https://registry.yarnpkg.com/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0"

0 commit comments

Comments
 (0)