Skip to content

Commit 02d3b49

Browse files
authored
Notebook extension recommendatation on open. (microsoft#188030)
1 parent 18136ed commit 02d3b49

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

src/vs/workbench/contrib/notebook/browser/notebookEditor.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { EnablementState } from 'vs/workbench/services/extensionManagement/commo
4646
import { IWorkingCopyBackupService } from 'vs/workbench/services/workingCopy/common/workingCopyBackup';
4747
import { streamToBuffer } from 'vs/base/common/buffer';
4848
import { ILogService } from 'vs/platform/log/common/log';
49+
import { INotebookEditorWorkerService } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerService';
4950

5051
const NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'NotebookEditorViewState';
5152

@@ -89,7 +90,8 @@ export class NotebookEditor extends EditorPane implements INotebookEditorPane {
8990
@INotebookService private readonly _notebookService: INotebookService,
9091
@IExtensionsWorkbenchService private readonly _extensionsWorkbenchService: IExtensionsWorkbenchService,
9192
@IWorkingCopyBackupService private readonly _workingCopyBackupService: IWorkingCopyBackupService,
92-
@ILogService private readonly logService: ILogService
93+
@ILogService private readonly logService: ILogService,
94+
@INotebookEditorWorkerService private readonly _notebookEditorWorkerService: INotebookEditorWorkerService,
9395
) {
9496
super(NotebookEditor.ID, telemetryService, themeService, storageService);
9597
this._editorMemento = this.getEditorMemento<INotebookEditorViewState>(_editorGroupService, configurationService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
@@ -318,6 +320,7 @@ export class NotebookEditor extends EditorPane implements INotebookEditorPane {
318320
}
319321

320322
this._handlePerfMark(perf, input);
323+
this._handlePromptRecommendations(model.notebook);
321324
} catch (e) {
322325
this.logService.warn('NotebookEditorWidget#setInput failed', e);
323326
if (isEditorOpenError(e)) {
@@ -425,6 +428,24 @@ export class NotebookEditor extends EditorPane implements INotebookEditorPane {
425428
});
426429
}
427430

431+
private _handlePromptRecommendations(model: NotebookTextModel) {
432+
this._notebookEditorWorkerService.canPromptRecommendation(model.uri).then(shouldPrompt => {
433+
type WorkbenchNotebookShouldPromptRecommendationClassification = {
434+
owner: 'rebornix';
435+
comment: 'The notebook file metrics. Used to get a better understanding of if we should prompt for notebook extension recommendations';
436+
shouldPrompt: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'Should we prompt for notebook extension recommendations' };
437+
};
438+
439+
type WorkbenchNotebookShouldPromptRecommendationEvent = {
440+
shouldPrompt: boolean;
441+
};
442+
443+
this.telemetryService.publicLog2<WorkbenchNotebookShouldPromptRecommendationEvent, WorkbenchNotebookShouldPromptRecommendationClassification>('notebook/shouldPromptRecommendation', {
444+
shouldPrompt: shouldPrompt
445+
});
446+
});
447+
}
448+
428449
override clearInput(): void {
429450
this._inputListener.clear();
430451

src/vs/workbench/contrib/notebook/browser/services/notebookWorkerServiceImpl.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export class NotebookEditorWorkerServiceImpl extends Disposable implements INote
3535
return client.computeDiff(original, modified);
3636
});
3737
}
38+
39+
canPromptRecommendation(model: URI): Promise<boolean> {
40+
return this._workerManager.withWorker().then(client => {
41+
return client.canPromptRecommendation(model);
42+
});
43+
}
3844
}
3945

4046
class WorkerManager extends Disposable {
@@ -218,6 +224,12 @@ class NotebookWorkerClient extends Disposable {
218224
});
219225
}
220226

227+
canPromptRecommendation(modelUri: URI) {
228+
return this._withSyncedResources([modelUri]).then(proxy => {
229+
return proxy.canPromptRecommendation(modelUri.toString());
230+
});
231+
}
232+
221233
private _getOrCreateModelManager(proxy: NotebookEditorSimpleWorker): NotebookEditorModelManager {
222234
if (!this._modelManager) {
223235
this._modelManager = this._register(new NotebookEditorModelManager(proxy, this._notebookService));

src/vs/workbench/contrib/notebook/common/services/notebookSimpleWorker.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { CellKind, ICellDto2, IMainCellDto, INotebookDiffResult, IOutputDto, Not
1313
import { Range } from 'vs/editor/common/core/range';
1414
import { INotebookWorkerHost } from 'vs/workbench/contrib/notebook/common/services/notebookWorkerHost';
1515
import { VSBuffer } from 'vs/base/common/buffer';
16+
import { SearchParams } from 'vs/editor/common/model/textModelSearch';
1617

1718
function bufferHash(buffer: VSBuffer): number {
1819
let initialHashVal = numberHash(104579, 0);
@@ -275,6 +276,35 @@ export class NotebookEditorSimpleWorker implements IRequestHandler, IDisposable
275276
};
276277
}
277278

279+
canPromptRecommendation(modelUrl: string): boolean {
280+
const model = this._getModel(modelUrl);
281+
const cells = model.cells;
282+
283+
for (let i = 0; i < cells.length; i++) {
284+
const cell = cells[i];
285+
if (cell.cellKind === CellKind.Markup) {
286+
continue;
287+
}
288+
289+
const lineCount = cell.textBuffer.getLineCount();
290+
const maxLineCount = Math.min(lineCount, 20);
291+
const range = new Range(1, 1, maxLineCount, cell.textBuffer.getLineLength(maxLineCount) + 1);
292+
const searchParams = new SearchParams('import\\s*pandas', true, false, null);
293+
const searchData = searchParams.parseSearchRequest();
294+
295+
if (!searchData) {
296+
continue;
297+
}
298+
299+
const cellMatches = cell.textBuffer.findMatchesLineByLine(range, searchData, true, 1);
300+
if (cellMatches.length > 0) {
301+
return true;
302+
}
303+
}
304+
305+
return false;
306+
}
307+
278308
protected _getModel(uri: string): MirrorNotebookDocument {
279309
return this._models[uri];
280310
}

src/vs/workbench/contrib/notebook/common/services/notebookWorkerService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export interface INotebookEditorWorkerService {
1515

1616
canComputeDiff(original: URI, modified: URI): boolean;
1717
computeDiff(original: URI, modified: URI): Promise<INotebookDiffResult>;
18+
canPromptRecommendation(model: URI): Promise<boolean>;
1819
}

0 commit comments

Comments
 (0)