Skip to content

Commit

Permalink
Re-register native completion providers when LSP completion is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Jan 17, 2024
1 parent 73ee344 commit a7c7576
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 34 deletions.
7 changes: 7 additions & 0 deletions atest/05_Features/Completion.robot
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ Works When Kernel Is Idle
${content} = Get Cell Editor Content 1
Should Contain ${content} TabError

Does Not Break Native Completions When Disabled
Configure JupyterLab Plugin {"disabled": true} plugin id=${COMPLETION PLUGIN ID}
Enter Cell Editor 1 line=2
Trigger Completer
Completer Should Suggest try
Completer Should Not Suggest test

Filters Completions In Case Sensitive Mode
[Documentation] Completions filtering is case-sensitive when caseSensitive is true
Configure JupyterLab Plugin {"caseSensitive": true} plugin id=${COMPLETION PLUGIN ID}
Expand Down
92 changes: 60 additions & 32 deletions packages/jupyterlab-lsp/src/features/completion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICompletionProviderManager } from '@jupyterlab/completer';
import {
ICompletionProviderManager,
ContextCompleterProvider,
KernelCompleterProvider
} from '@jupyterlab/completer';
import {
ILSPFeatureManager,
ILSPDocumentConnectionManager
Expand All @@ -24,6 +28,7 @@ import {
EnhancedKernelCompleterProvider
} from './overrides';
import { CompletionProvider } from './provider';
import { ICompletionFeature } from './tokens';

export const completionIcon = new LabIcon({
name: 'lsp:completion',
Expand Down Expand Up @@ -94,42 +99,65 @@ export namespace CompletionFeature {
export const id = PLUGIN_ID + ':completion';
}

export const COMPLETION_PLUGIN: JupyterFrontEndPlugin<void> = {
id: CompletionFeature.id,
requires: [
ILSPFeatureManager,
ISettingRegistry,
ICompletionProviderManager,
ILSPCompletionThemeManager,
IRenderMimeRegistry,
ILSPDocumentConnectionManager
],
export const COMPLETION_PLUGIN: JupyterFrontEndPlugin<ICompletionFeature | null> =
{
id: CompletionFeature.id,
requires: [
ILSPFeatureManager,
ISettingRegistry,
ICompletionProviderManager,
ILSPCompletionThemeManager,
IRenderMimeRegistry,
ILSPDocumentConnectionManager
],
autoStart: true,
activate: async (
app: JupyterFrontEnd,
featureManager: ILSPFeatureManager,
settingRegistry: ISettingRegistry,
completionProviderManager: ICompletionProviderManager,
iconsThemeManager: ILSPCompletionThemeManager,
renderMimeRegistry: IRenderMimeRegistry,
connectionManager: ILSPDocumentConnectionManager
): Promise<ICompletionFeature | null> => {
const settings = new FeatureSettings<LSPCompletionSettings>(
settingRegistry,
CompletionFeature.id
);
await settings.ready;
if (settings.composite.disable) {
return null;
}
const feature = new CompletionFeature({
settings,
connectionManager,
renderMimeRegistry,
iconsThemeManager,
completionProviderManager
});

featureManager.register(feature);
return { id: CompletionFeature.id };
}
};

export const COMPLETION_FALLBACK_PLUGIN: JupyterFrontEndPlugin<void> = {
id: CompletionFeature.id + '-fallback',
description:
'Plugin which restores the default completion provider when the LSP completion plugin is disabled',
requires: [ICompletionProviderManager],
optional: [ICompletionFeature],
autoStart: true,
activate: async (
app: JupyterFrontEnd,
featureManager: ILSPFeatureManager,
settingRegistry: ISettingRegistry,
completionProviderManager: ICompletionProviderManager,
iconsThemeManager: ILSPCompletionThemeManager,
renderMimeRegistry: IRenderMimeRegistry,
connectionManager: ILSPDocumentConnectionManager
completionFeature: ICompletionFeature | null
) => {
const settings = new FeatureSettings<LSPCompletionSettings>(
settingRegistry,
CompletionFeature.id
);
await settings.ready;
if (settings.composite.disable) {
return;
if (completionFeature == null) {
completionProviderManager.registerProvider(
new ContextCompleterProvider()
);
completionProviderManager.registerProvider(new KernelCompleterProvider());
}
const feature = new CompletionFeature({
settings,
connectionManager,
renderMimeRegistry,
iconsThemeManager,
completionProviderManager
});

featureManager.register(feature);
}
};
17 changes: 17 additions & 0 deletions packages/jupyterlab-lsp/src/features/completion/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Token } from '@lumino/coreutils';

import { PLUGIN_ID } from '../../tokens';

/**
* Token provided by the plugin which implements LSP completion.
* As of now no methods are exposed, but it still functions as
* an indicator for whether the LSP completion plugin is available,
* or whether it was disabled by the user (or failed to activate).
*/
export interface ICompletionFeature {
readonly id: string;
}

export const ICompletionFeature = new Token<ICompletionFeature>(
PLUGIN_ID + ':ICompletionFeature'
);
8 changes: 6 additions & 2 deletions packages/jupyterlab-lsp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import { LanguageServers } from './_plugin';
import { FILEEDITOR_ADAPTER_PLUGIN } from './adapters/fileeditor';
import { NOTEBOOK_ADAPTER_PLUGIN } from './adapters/notebook';
import { StatusButtonExtension } from './components/statusbar';
import { COMPLETION_PLUGIN } from './features/completion';
import {
COMPLETION_PLUGIN,
COMPLETION_FALLBACK_PLUGIN
} from './features/completion';
import { DIAGNOSTICS_PLUGIN } from './features/diagnostics';
import { HIGHLIGHTS_PLUGIN } from './features/highlights';
import { HOVER_PLUGIN } from './features/hover';
Expand Down Expand Up @@ -250,7 +253,8 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
FILEEDITOR_ADAPTER_PLUGIN,
plugin,
...DEFAULT_TRANSCLUSIONS,
...DEFAULT_FEATURES
...DEFAULT_FEATURES,
COMPLETION_FALLBACK_PLUGIN
];

/**
Expand Down

0 comments on commit a7c7576

Please sign in to comment.