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 12
Merged
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 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
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,45 +1,142 @@ | ||
| import * as sourcegraph from 'sourcegraph' | ||
| import { Handler, Config } from './handler' | ||
| import { from, Observable } from 'rxjs' | ||
| import { first, map, distinctUntilChanged, finalize } from 'rxjs/operators' | ||
| import { Handler, Settings, DOCUMENT_SELECTOR } from './handler' | ||
|
|
||
| export async function activate(): Promise<void> { | ||
| // No-op for Sourcegraph versions prior to 3.0-preview | ||
| const DUMMY_CTX = { subscriptions: { add: (_unsubscribable: any) => void 0 } } | ||
|
|
||
| export function activate(ctx: sourcegraph.ExtensionContext = DUMMY_CTX): void { | ||
| const h = new Handler() | ||
|
|
||
| sourcegraph.commands.registerCommand('basicCodeIntel.toggle', () => { | ||
| // Toggle between 2 states: | ||
| // | ||
| // Enabled: basicCodeIntel.enabled = true and extensions.langserver/* = false | ||
| // | ||
| // Disabled: basicCodeIntel.enabled = false and extensions.langserver/* = true | ||
| // | ||
| // These 2 states are not inverses of each other. Enabling and disabling basic code | ||
| // intel might enable or disable langserver extensions in a way that the user does not | ||
| // expect or desire. | ||
| const config = sourcegraph.configuration.get< | ||
| Config & { extensions: { [id: string]: boolean } } | ||
| >() | ||
|
|
||
| const newEnabled = !config.get('basicCodeIntel.enabled') | ||
| config | ||
| .update('basicCodeIntel.enabled', newEnabled) | ||
| .then(async () => { | ||
| const extensions = { ...(config.get('extensions') || {}) } | ||
| for (const extensionID of Object.keys(extensions)) { | ||
| if ( | ||
| extensionID.startsWith('langserver/') || | ||
| extensionID.includes('/langserver') | ||
| ) { | ||
| extensions[extensionID] = !newEnabled | ||
| } | ||
| ctx.subscriptions.add( | ||
| sourcegraph.commands.registerCommand( | ||
| 'basicCodeIntel.old.togglePreciseFuzzy', | ||
| () => { | ||
| // Toggle between 2 states: | ||
| // | ||
| // Enabled: basicCodeIntel.enabled = true and extensions.langserver/* = false | ||
| // | ||
| // Disabled: basicCodeIntel.enabled = false and extensions.langserver/* = true | ||
| // | ||
| // These 2 states are not inverses of each other. Enabling and disabling basic code | ||
| // intel might enable or disable langserver extensions in a way that the user does not | ||
| // expect or desire. | ||
| const config = sourcegraph.configuration.get< | ||
| Settings & { extensions: { [id: string]: boolean } } | ||
| >() | ||
|
|
||
| const newEnabled = !config.get('basicCodeIntel.enabled') | ||
| config | ||
| .update('basicCodeIntel.enabled', newEnabled) | ||
| .then(async () => { | ||
| const extensions = { | ||
| ...(config.get('extensions') || {}), | ||
| } | ||
| for (const extensionID of Object.keys(extensions)) { | ||
| if ( | ||
| extensionID.startsWith('langserver/') || | ||
| extensionID.includes('/langserver') | ||
| ) { | ||
| extensions[extensionID] = !newEnabled | ||
| } | ||
| } | ||
| await config.update('extensions', extensions) | ||
| }) | ||
| .catch(err => console.error(err)) | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| ctx.subscriptions.add( | ||
| reregisterWhenEnablementChanges(() => | ||
| sourcegraph.languages.registerDefinitionProvider( | ||
| DOCUMENT_SELECTOR, | ||
| { | ||
| provideDefinition: (doc, pos) => | ||
| enabledOrNull(() => | ||
| observableOrPromiseCompat(h.definition(doc, pos)) | ||
| ), | ||
| } | ||
| await config.update('extensions', extensions) | ||
| ) | ||
| ) | ||
| ) | ||
| ctx.subscriptions.add( | ||
| reregisterWhenEnablementChanges(() => | ||
| sourcegraph.languages.registerReferenceProvider(DOCUMENT_SELECTOR, { | ||
| provideReferences: (doc, pos) => | ||
| enabledOrNull(() => | ||
| observableOrPromiseCompat(h.references(doc, pos)) | ||
| ), | ||
| }) | ||
| .catch(err => console.error(err)) | ||
| }) | ||
|
|
||
| sourcegraph.languages.registerDefinitionProvider(['*'], { | ||
| provideDefinition: (doc, pos) => h.definition(doc, pos), | ||
| }) | ||
| sourcegraph.languages.registerReferenceProvider(['*'], { | ||
| provideReferences: (doc, pos) => h.references(doc, pos), | ||
| }) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| const settingsSubscribable = new Observable<Settings>(sub => { | ||
| sub.next(sourcegraph.configuration.get().value) | ||
| return sourcegraph.configuration.subscribe(() => | ||
| sub.next(sourcegraph.configuration.get().value) | ||
| ) | ||
| }) | ||
|
|
||
| function enabledOrNull<T>(provider: () => T): T | null { | ||
| if ( | ||
| !sourcegraph.configuration.get<Settings>().value[ | ||
| 'basicCodeIntel.enabled' | ||
| ] | ||
| ) { | ||
| return null | ||
| } | ||
| return provider() | ||
| } | ||
|
|
||
| /** | ||
| * This makes it so that basicCodeIntel.toggle (the "Show/hide fuzzy matches" button) immediately takes effect and | ||
| * changes the locations that are currently being displayed in the panel. | ||
| * | ||
| * If we used an observable instead, it would always show the loading indicator. | ||
| */ | ||
| function reregisterWhenEnablementChanges( | ||
| register: () => sourcegraph.Unsubscribable | ||
| ): sourcegraph.Unsubscribable { | ||
| let registration: sourcegraph.Unsubscribable | undefined | ||
| return from(settingsSubscribable) | ||
| .pipe( | ||
| distinctUntilChanged( | ||
| (a, b) => | ||
| Boolean(a['basicCodeIntel.enabled']) === | ||
| Boolean(b['basicCodeIntel.enabled']) && | ||
| a['basicCodeIntel.definition.symbols'] === | ||
| b['basicCodeIntel.definition.symbols'] | ||
| ), | ||
| map(() => { | ||
| if (registration) { | ||
| registration.unsubscribe() | ||
| } | ||
| registration = register() | ||
| }), | ||
| finalize(() => { | ||
| if (registration) { | ||
| registration.unsubscribe() | ||
| registration = undefined | ||
| } | ||
| }) | ||
| ) | ||
| .subscribe() | ||
| } | ||
|
|
||
| function observableOrPromiseCompat<T>( | ||
| result: Observable<T> | Promise<T> | ||
| ): sourcegraph.ProviderResult<T> { | ||
| // HACK: Earlier extension API versions did not support providers returning observables. We can detect whether | ||
| // the extension API version is compatible by checking for the presence of registerLocationProvider, which was | ||
| // added around the same time. | ||
| const supportsProvidersReturningObservables = !!sourcegraph.languages | ||
| .registerLocationProvider | ||
| return supportsProvidersReturningObservables | ||
| ? from(result) | ||
| : from(result) | ||
| .pipe(first()) | ||
| .toPromise() | ||
| } | ||
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
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.
We should plan to handle the issue of extension API breakage differently - IMO there should be no backwards compatibility handling in extension code, it should be handled at the registry/extension host level.
eg.:
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.
👍 Moved to https://github.com/sourcegraph/sourcegraph/issues/1255