This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Fall back to basic-code-intel after 500ms #140
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ import { URL as _URL, URLSearchParams as _URLSearchParams } from 'whatwg-url' | |||||
Object.assign(_URL, self.URL) | ||||||
Object.assign(self, { URL: _URL, URLSearchParams: _URLSearchParams }) | ||||||
|
||||||
import { activateBasicCodeIntel } from '@sourcegraph/basic-code-intel' | ||||||
import * as basicCodeIntel from '@sourcegraph/basic-code-intel' | ||||||
import { Tracer as LightstepTracer } from '@sourcegraph/lightstep-tracer-webworker' | ||||||
import { | ||||||
createMessageConnection, | ||||||
|
@@ -20,7 +20,7 @@ import { merge } from 'ix/asynciterable/index' | |||||
import { filter, map, scan, tap } from 'ix/asynciterable/pipe/index' | ||||||
import { fromPairs } from 'lodash' | ||||||
import { Span, Tracer } from 'opentracing' | ||||||
import { BehaviorSubject, from, fromEventPattern, Subscription } from 'rxjs' | ||||||
import { BehaviorSubject, from, fromEventPattern, Observable, ObservableInput, of, race, Subscription } from 'rxjs' | ||||||
import * as rxop from 'rxjs/operators' | ||||||
import * as sourcegraph from 'sourcegraph' | ||||||
import { Omit } from 'type-zoo' | ||||||
|
@@ -95,6 +95,58 @@ const documentSelector: sourcegraph.DocumentSelector = [{ language: 'typescript' | |||||
|
||||||
const logger: Logger = new RedactingLogger(console) | ||||||
|
||||||
/** | ||||||
* Emits from `fallback` after `delayMilliseconds`. Useful for falling back to | ||||||
* basic-code-intel while the language server is running. | ||||||
*/ | ||||||
function withFallback<T>({ | ||||||
main, | ||||||
fallback, | ||||||
delayMilliseconds, | ||||||
}: { | ||||||
main: ObservableInput<T> | ||||||
fallback: ObservableInput<T> | ||||||
delayMilliseconds: number | ||||||
}): Observable<T> { | ||||||
return race( | ||||||
of(null).pipe(rxop.switchMap(() => from(main))), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this the same?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's the same as If it gave you pause, I'll switch to |
||||||
of(null).pipe( | ||||||
rxop.delay(delayMilliseconds), | ||||||
rxop.switchMap(() => from(fallback)) | ||||||
) | ||||||
) | ||||||
} | ||||||
|
||||||
const basicCodeIntelHandlerArgs: basicCodeIntel.HandlerArgs = { | ||||||
sourcegraph, | ||||||
languageID: 'typescript', | ||||||
fileExts: ['ts', 'tsx', 'js', 'jsx'], | ||||||
commentStyle: { | ||||||
lineRegex: /\/\/\s?/, | ||||||
block: { | ||||||
startRegex: /\/\*\*?/, | ||||||
lineNoiseRegex: /(^\s*\*\s?)?/, | ||||||
endRegex: /\*\//, | ||||||
}, | ||||||
}, | ||||||
filterDefinitions: ({ filePath, fileContent, results }) => { | ||||||
const imports = fileContent | ||||||
.split('\n') | ||||||
.map(line => { | ||||||
// Matches the import at index 1 | ||||||
const match = /\bfrom ['"](.*)['"];?$/.exec(line) || /\brequire\(['"](.*)['"]\)/.exec(line) | ||||||
return match ? match[1] : undefined | ||||||
}) | ||||||
.filter((x): x is string => Boolean(x)) | ||||||
|
||||||
const filteredResults = results.filter(result => | ||||||
imports.some(i => path.join(path.dirname(filePath), i) === result.file.replace(/\.[^/.]+$/, '')) | ||||||
) | ||||||
|
||||||
return filteredResults.length === 0 ? results : filteredResults | ||||||
}, | ||||||
} | ||||||
|
||||||
export async function activate(ctx: sourcegraph.ExtensionContext): Promise<void> { | ||||||
// Cancel everything whene extension is deactivated | ||||||
const cancellationTokenSource = new CancellationTokenSource() | ||||||
|
@@ -104,37 +156,26 @@ export async function activate(ctx: sourcegraph.ExtensionContext): Promise<void> | |||||
const config = new BehaviorSubject(getConfig()) | ||||||
ctx.subscriptions.add(sourcegraph.configuration.subscribe(() => config.next(getConfig()))) | ||||||
|
||||||
const basicCodeIntelHandler = new basicCodeIntel.Handler(basicCodeIntelHandlerArgs) | ||||||
|
||||||
if (!config.value['typescript.serverUrl']) { | ||||||
logger.warn('No typescript.serverUrl configured, falling back to basic code intelligence') | ||||||
// Fall back to basic-code-intel behavior | ||||||
return activateBasicCodeIntel({ | ||||||
languageID: 'typescript', | ||||||
fileExts: ['ts', 'tsx', 'js', 'jsx'], | ||||||
commentStyle: { | ||||||
lineRegex: /\/\/\s?/, | ||||||
block: { | ||||||
startRegex: /\/\*\*?/, | ||||||
lineNoiseRegex: /(^\s*\*\s?)?/, | ||||||
endRegex: /\*\//, | ||||||
}, | ||||||
}, | ||||||
filterDefinitions: ({ filePath, fileContent, results }) => { | ||||||
const imports = fileContent | ||||||
.split('\n') | ||||||
.map(line => { | ||||||
// Matches the import at index 1 | ||||||
const match = /\bfrom ['"](.*)['"];?$/.exec(line) || /\brequire\(['"](.*)['"]\)/.exec(line) | ||||||
return match ? match[1] : undefined | ||||||
}) | ||||||
.filter((x): x is string => Boolean(x)) | ||||||
|
||||||
const filteredResults = results.filter(result => | ||||||
imports.some(i => path.join(path.dirname(filePath), i) === result.file.replace(/\.[^/.]+$/, '')) | ||||||
) | ||||||
|
||||||
return filteredResults.length === 0 ? results : filteredResults | ||||||
}, | ||||||
})(ctx) | ||||||
ctx.subscriptions.add( | ||||||
sourcegraph.languages.registerHoverProvider(documentSelector, { | ||||||
provideHover: (doc, pos) => basicCodeIntelHandler.hover(doc, pos), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if |
||||||
}) | ||||||
) | ||||||
ctx.subscriptions.add( | ||||||
sourcegraph.languages.registerDefinitionProvider(documentSelector, { | ||||||
provideDefinition: (doc, pos) => basicCodeIntelHandler.definition(doc, pos), | ||||||
}) | ||||||
) | ||||||
ctx.subscriptions.add( | ||||||
sourcegraph.languages.registerReferenceProvider(documentSelector, { | ||||||
provideReferences: (doc, pos) => basicCodeIntelHandler.references(doc, pos), | ||||||
}) | ||||||
) | ||||||
} | ||||||
|
||||||
const tracer: Tracer = config.value['lightstep.token'] | ||||||
|
@@ -508,7 +549,11 @@ export async function activate(ctx: sourcegraph.ExtensionContext): Promise<void> | |||||
providers.add( | ||||||
sourcegraph.languages.registerHoverProvider(documentSelector, { | ||||||
provideHover: distinctUntilChanged(areProviderParamsEqual, (textDocument, position) => | ||||||
observableFromAsyncIterable(provideHover(textDocument, position)) | ||||||
withFallback({ | ||||||
main: observableFromAsyncIterable(provideHover(textDocument, position)), | ||||||
fallback: basicCodeIntelHandler.hover(textDocument, position), | ||||||
delayMilliseconds: 500, | ||||||
}) | ||||||
), | ||||||
}) | ||||||
) | ||||||
|
@@ -544,7 +589,11 @@ export async function activate(ctx: sourcegraph.ExtensionContext): Promise<void> | |||||
providers.add( | ||||||
sourcegraph.languages.registerDefinitionProvider(documentSelector, { | ||||||
provideDefinition: distinctUntilChanged(areProviderParamsEqual, (textDocument, position) => | ||||||
observableFromAsyncIterable(provideDefinition(textDocument, position)) | ||||||
withFallback({ | ||||||
main: observableFromAsyncIterable(provideDefinition(textDocument, position)), | ||||||
fallback: basicCodeIntelHandler.definition(textDocument, position), | ||||||
delayMilliseconds: 500, | ||||||
}) | ||||||
), | ||||||
}) | ||||||
) | ||||||
|
@@ -736,7 +785,12 @@ export async function activate(ctx: sourcegraph.ExtensionContext): Promise<void> | |||||
}) | ||||||
providers.add( | ||||||
sourcegraph.languages.registerReferenceProvider(documentSelector, { | ||||||
provideReferences: (doc, pos, ctx) => observableFromAsyncIterable(provideReferences(doc, pos, ctx)), | ||||||
provideReferences: (doc, pos, ctx) => | ||||||
withFallback({ | ||||||
main: observableFromAsyncIterable(provideReferences(doc, pos, ctx)), | ||||||
fallback: basicCodeIntelHandler.references(doc, pos), | ||||||
delayMilliseconds: 2000, | ||||||
}), | ||||||
}) | ||||||
) | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
race()
gives the wrong behaviour here. As described in #107, I think the behaviour should be to show the basic code intel result while the language server hasn't returned yet, but (for hovers/references) replace it as soon as the language server has a result. The delay would just be there to avoid a flickering UI (rapidly changing from basic code intel to language server) and for definition, where we can't replace the results afterwards because we need to jump to the location.Without this, it's too easy for a hover or references call to take slightly longer than 500ms or 2s respectively and the user getting imprecise results (or maybe even no result at all).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, continuing conversation in #107