This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Activation refactor #198
Merged
Merged
Activation refactor #198
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
437e391
WIP.
efritz d5f8bba
WIP.
efritz 568d230
Add todo notes.
efritz 5b1f89e
Bump sourcegraph version.
efritz 1f577ca
Bump target.
efritz 087e76f
Add missing type annotations.
efritz 07e9485
Run prettier.
efritz 15cc456
Rename abortion.ts.
efritz db6a2f7
Deduplicate yarn.lock.
efritz 4af2541
Remove abortPrevious.
efritz 4c608cf
Merge branch 'master' into activation-refactor
efritz 987cf79
Deduplicate yarn.lock.
efritz a82c86e
Remove duplicate entry.
efritz b3bfc48
Undo the locations provider stuff.
efritz 8092359
Reorganize package code (#201)
efritz f787f16
Merge branch 'master' into activation-refactor
efritz 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export interface AbortError extends Error { | ||
name: 'AbortError' | ||
} | ||
|
||
/** | ||
* Creates an Error with name "AbortError" | ||
*/ | ||
export const createAbortError = (): AbortError => | ||
Object.assign(new Error('Aborted'), { name: 'AbortError' as const }) | ||
|
||
/** | ||
* Returns true if the given value is an AbortError | ||
*/ | ||
export const isAbortError = (err: any): err is AbortError => | ||
typeof err === 'object' && err !== null && err.name === 'AbortError' | ||
|
||
export function throwIfAbortError(err: unknown): void { | ||
if (isAbortError(err)) { | ||
throw err | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,258 @@ | ||
import * as sourcegraph from 'sourcegraph' | ||
import { HandlerArgs, Handler } from './search/handler' | ||
import { initLSIF } from './lsif/activation' | ||
import { impreciseBadge } from './badges' | ||
import { shareReplay } from 'rxjs/operators' | ||
import { Observable, Observer } from 'rxjs' | ||
import { createAbortError } from './abort' | ||
import { LSPProviders } from './lsp/providers' | ||
import { LSIFProviders } from './lsif/providers' | ||
import { SearchProviders } from './search/providers' | ||
|
||
export function activateCodeIntel( | ||
ctx: sourcegraph.ExtensionContext, | ||
selector: sourcegraph.DocumentSelector, | ||
handlerArgs: HandlerArgs, | ||
lspProviders?: LSPProviders | ||
): void { | ||
const lsifProviders = initLSIF() | ||
const searchProviders = new Handler(handlerArgs) | ||
|
||
ctx.subscriptions.add( | ||
sourcegraph.languages.registerDefinitionProvider( | ||
selector, | ||
createDefinitionProvider( | ||
lsifProviders, | ||
searchProviders, | ||
lspProviders | ||
) | ||
) | ||
) | ||
ctx.subscriptions.add( | ||
sourcegraph.languages.registerReferenceProvider( | ||
selector, | ||
createReferencesProvider( | ||
lsifProviders, | ||
searchProviders, | ||
lspProviders | ||
) | ||
) | ||
) | ||
ctx.subscriptions.add( | ||
sourcegraph.languages.registerHoverProvider( | ||
selector, | ||
createHoverProvider(lsifProviders, searchProviders, lspProviders) | ||
) | ||
) | ||
} | ||
|
||
function createDefinitionProvider( | ||
lsifProviders: LSIFProviders, | ||
searchProviders: SearchProviders, | ||
lspProviders?: LSPProviders | ||
): sourcegraph.DefinitionProvider { | ||
async function* provideDefinition( | ||
doc: sourcegraph.TextDocument, | ||
pos: sourcegraph.Position | ||
): AsyncGenerator<sourcegraph.Definition | undefined, void, undefined> { | ||
const lsifResult = await lsifProviders.definition(doc, pos) | ||
if (lsifResult) { | ||
yield lsifResult | ||
return | ||
} | ||
|
||
if (lspProviders) { | ||
yield* lspProviders.definition(doc, pos) | ||
return | ||
} | ||
|
||
let searchResult = await searchProviders.definition(doc, pos) | ||
if (!searchResult) { | ||
yield undefined | ||
return | ||
} | ||
|
||
if (!Array.isArray(searchResult)) { | ||
const badged = { ...searchResult, badge: impreciseBadge } | ||
yield badged | ||
return | ||
} | ||
|
||
yield searchResult.map(v => ({ ...v, badge: impreciseBadge })) | ||
} | ||
|
||
return { | ||
provideDefinition: wrap(areProviderParamsEqual, provideDefinition), | ||
} | ||
} | ||
|
||
function createReferencesProvider( | ||
lsifProviders: LSIFProviders, | ||
searchProviders: SearchProviders, | ||
lspProviders?: LSPProviders | ||
): sourcegraph.ReferenceProvider { | ||
// Gets an opaque value that is the same for all locations | ||
// within a file but different from other files. | ||
const file = (loc: sourcegraph.Location) => | ||
`${loc.uri.host} ${loc.uri.pathname} ${loc.uri.hash}` | ||
|
||
async function* provideReferences( | ||
doc: sourcegraph.TextDocument, | ||
pos: sourcegraph.Position, | ||
ctx: sourcegraph.ReferenceContext | ||
): AsyncGenerator<sourcegraph.Location[] | null, void, undefined> { | ||
if (lspProviders) { | ||
yield* lspProviders.references(doc, pos, ctx) | ||
return | ||
} | ||
|
||
// Get and extract LSIF results | ||
const lsifResult = await lsifProviders.references(doc, pos) | ||
const lsifReferences = lsifResult || [] | ||
const lsifFiles = new Set(lsifReferences.map(file)) | ||
|
||
// Unconditionally get search references and append them with | ||
// precise results because LSIF data might be sparse. Remove any | ||
// search-based result that occurs in a file with an LSIF result. | ||
const searchResults = ( | ||
(await searchProviders.references(doc, pos)) || [] | ||
).filter(fuzzyRef => !lsifFiles.has(file(fuzzyRef))) | ||
|
||
yield [ | ||
...lsifReferences, | ||
...searchResults.map(v => ({ | ||
...v, | ||
badge: impreciseBadge, | ||
})), | ||
] | ||
} | ||
|
||
return { | ||
provideReferences: wrap( | ||
areProviderParamsContextEqual, | ||
provideReferences | ||
), | ||
} | ||
} | ||
|
||
function createHoverProvider( | ||
lsifProviders: LSIFProviders, | ||
searchProviders: SearchProviders, | ||
lspProviders?: LSPProviders | ||
): sourcegraph.HoverProvider { | ||
async function* provideHover( | ||
doc: sourcegraph.TextDocument, | ||
pos: sourcegraph.Position | ||
): AsyncGenerator< | ||
sourcegraph.Badged<sourcegraph.Hover> | null | undefined, | ||
void, | ||
undefined | ||
> { | ||
const lsifResult = await lsifProviders.hover(doc, pos) | ||
if (lsifResult) { | ||
yield lsifResult | ||
return | ||
} | ||
|
||
if (lspProviders) { | ||
yield* lspProviders.hover(doc, pos) | ||
return | ||
} | ||
|
||
const searchResult = await searchProviders.hover(doc, pos) | ||
if (!searchResult) { | ||
yield undefined | ||
return | ||
} | ||
|
||
yield { ...searchResult, badge: impreciseBadge } | ||
} | ||
|
||
return { | ||
provideHover: wrap(areProviderParamsEqual, provideHover), | ||
} | ||
} | ||
|
||
// | ||
// | ||
// | ||
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. The following code was lifted from the typescript extension. |
||
|
||
const wrap = <P extends any[], R>( | ||
compare: (a: P, b: P) => boolean, | ||
fn: (...args: P) => AsyncGenerator<R, void, void> | ||
): ((...args: P) => Observable<R>) => | ||
memoizePrevious(compare, (...args) => | ||
observableFromAsyncGenerator(() => fn(...args)).pipe(shareReplay(1)) | ||
) | ||
|
||
const areProviderParamsEqual = ( | ||
[doc1, pos1]: [sourcegraph.TextDocument, sourcegraph.Position], | ||
[doc2, pos2]: [sourcegraph.TextDocument, sourcegraph.Position] | ||
): boolean => doc1.uri === doc2.uri && pos1.isEqual(pos2) | ||
|
||
const areProviderParamsContextEqual = ( | ||
[doc1, pos1]: [ | ||
sourcegraph.TextDocument, | ||
sourcegraph.Position, | ||
sourcegraph.ReferenceContext | ||
], | ||
[doc2, pos2]: [ | ||
sourcegraph.TextDocument, | ||
sourcegraph.Position, | ||
sourcegraph.ReferenceContext | ||
] | ||
): boolean => areProviderParamsEqual([doc1, pos1], [doc2, pos2]) | ||
|
||
const observableFromAsyncGenerator = <T>( | ||
generator: () => AsyncGenerator<T, unknown, void> | ||
): Observable<T> => | ||
new Observable((observer: Observer<T>) => { | ||
const iterator = generator() | ||
let unsubscribed = false | ||
let iteratorDone = false | ||
function next(): void { | ||
iterator.next().then( | ||
result => { | ||
if (unsubscribed) { | ||
return | ||
} | ||
if (result.done) { | ||
iteratorDone = true | ||
observer.complete() | ||
} else { | ||
observer.next(result.value) | ||
next() | ||
} | ||
}, | ||
err => { | ||
observer.error(err) | ||
} | ||
) | ||
} | ||
next() | ||
return () => { | ||
unsubscribed = true | ||
if (!iteratorDone && iterator.throw) { | ||
iterator.throw(createAbortError()).catch(() => { | ||
// ignore | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
/** Workaround for https://github.com/sourcegraph/sourcegraph/issues/1321 */ | ||
function memoizePrevious<P extends any[], R>( | ||
compare: (a: P, b: P) => boolean, | ||
fn: (...args: P) => R | ||
): (...args: P) => R { | ||
let previousResult: R | ||
let previousArgs: P | ||
return (...args) => { | ||
if (previousArgs && compare(previousArgs, args)) { | ||
return previousResult | ||
} | ||
previousArgs = args | ||
previousResult = fn(...args) | ||
return previousResult | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,17 +1,10 @@ | ||
export { impreciseBadge } from './badges' | ||
export { Handler, HandlerArgs } from './handler' | ||
export { activateCodeIntel } from './activation' | ||
export { HandlerArgs } from './search/handler' | ||
export { CommentStyle, BlockCommentStyle } from './search/comments' | ||
export { LSPProviders } from './lsp/providers' | ||
export { | ||
initLSIF, | ||
asyncFirst, | ||
asyncWhen, | ||
when, | ||
wrapMaybe, | ||
Maybe, | ||
MaybeProviders, | ||
noopMaybeProviders, | ||
mkIsLSIFAvailable, | ||
hover, | ||
definition, | ||
references, | ||
Providers, | ||
} from './lsif' | ||
AbortError, | ||
createAbortError, | ||
isAbortError, | ||
throwIfAbortError, | ||
} from './abort' |
Oops, something went wrong.
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.
Note: this logic isn't great but it's what currently exists for Go and Typescript. We should favor fast LSIF results first, then LSP results, then search results (if LSP is provided). This is possible now that we've updated our providers to be AsyncGenerators.