Skip to content

Commit 70df03f

Browse files
paulzrugpanov
andauthored
Allow opting out of automatic Jupyter cell-marker configuration (#2239)
## Changes Opening a Databricks project can add Jupyter cell-marker settings to a shared `.vscode/settings.json`, leaving the repository modified even when notebook integration is not used. This adds `databricks.notebooks.configureJupyterCellMarkers`, enabled by default to preserve existing notebook behavior. Users can opt out of both automatic cell-marker updates in their **User settings**: ```json "databricks.notebooks.configureJupyterCellMarkers": false ``` Reload the window after changing the setting. Existing Jupyter settings are left untouched; previously generated entries can be removed manually. Fixes #1729. Co-authored-by: Grigory Panov <gripanov@gmail.com>
1 parent a6adfac commit 70df03f

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎packages/databricks-vscode/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,11 @@
18991899
"uniqueItems": true,
19001900
"description": "Opt into experimental features."
19011901
},
1902+
"databricks.notebooks.configureJupyterCellMarkers": {
1903+
"type": "boolean",
1904+
"default": true,
1905+
"description": "Automatically configure Jupyter cell markers in workspace settings for Databricks Python notebooks. Disable in User settings to leave Jupyter cell markers unchanged. Reload the window after changing this setting. Previously written settings are not removed."
1906+
},
19021907
"databricks.wsfs.rearrangeCells": {
19031908
"type": "boolean",
19041909
"default": true,
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import {randomUUID} from "crypto";
2+
import os from "os";
3+
import path from "path";
4+
import {ConfigurationTarget, Uri, workspace} from "vscode";
5+
import type {WorkspaceConfiguration} from "vscode";
6+
import {instance, mock, reset, spy, verify, when} from "ts-mockito";
7+
import {ConnectionManager} from "../../configuration/ConnectionManager";
8+
import {WorkspaceFolderManager} from "../../vscode-objs/WorkspaceFolderManager";
9+
import {setDbnbCellLimits} from "./DatabricksNbCellLimits";
10+
11+
describe("Databricks notebook cell markers", () => {
12+
let workspaceSpy: typeof workspace;
13+
let databricksConfig: WorkspaceConfiguration;
14+
let jupyterConfig: WorkspaceConfiguration;
15+
let workspaceFolderManager: WorkspaceFolderManager;
16+
let connectionManager: ConnectionManager;
17+
18+
beforeEach(() => {
19+
workspaceSpy = spy(workspace);
20+
databricksConfig = mock<WorkspaceConfiguration>();
21+
jupyterConfig = mock<WorkspaceConfiguration>();
22+
when(workspaceSpy.getConfiguration("databricks")).thenReturn(
23+
instance(databricksConfig)
24+
);
25+
when(workspaceSpy.getConfiguration("jupyter")).thenReturn(
26+
instance(jupyterConfig)
27+
);
28+
when(
29+
jupyterConfig.get<string>("interactiveWindow.cellMarker.codeRegex")
30+
).thenReturn("^# custom");
31+
workspaceFolderManager = mock(WorkspaceFolderManager);
32+
when(workspaceFolderManager.activeProjectUri).thenReturn(
33+
Uri.file(path.join(os.tmpdir(), randomUUID()))
34+
);
35+
connectionManager = mock(ConnectionManager);
36+
when(connectionManager.waitForConnect()).thenResolve();
37+
});
38+
39+
afterEach(() => {
40+
reset(workspaceSpy);
41+
});
42+
43+
it("leaves Jupyter settings untouched when automatic configuration is disabled", async () => {
44+
when(
45+
databricksConfig.get<boolean>(
46+
"notebooks.configureJupyterCellMarkers"
47+
)
48+
).thenReturn(false);
49+
50+
await setDbnbCellLimits(
51+
instance(workspaceFolderManager),
52+
instance(connectionManager)
53+
);
54+
55+
verify(workspaceSpy.getConfiguration("jupyter")).never();
56+
});
57+
58+
for (const enabled of [undefined, true]) {
59+
it(`preserves custom markers when automatic configuration is ${
60+
enabled === undefined ? "unset" : "enabled"
61+
}`, async () => {
62+
when(
63+
databricksConfig.get<boolean>(
64+
"notebooks.configureJupyterCellMarkers"
65+
)
66+
).thenReturn(enabled);
67+
68+
await setDbnbCellLimits(
69+
instance(workspaceFolderManager),
70+
instance(connectionManager)
71+
);
72+
73+
verify(
74+
jupyterConfig.update(
75+
"interactiveWindow.cellMarker.codeRegex",
76+
"^# COMMAND ----------|^# Databricks notebook source|^# custom",
77+
ConfigurationTarget.Workspace
78+
)
79+
).once();
80+
verify(
81+
jupyterConfig.update(
82+
"interactiveWindow.cellMarker.default",
83+
"# COMMAND ----------",
84+
ConfigurationTarget.Workspace
85+
)
86+
).once();
87+
});
88+
}
89+
});

‎packages/databricks-vscode/src/language/notebooks/DatabricksNbCellLimits.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export async function setDbnbCellLimits(
1111
workspaceFolderManager.activeProjectUri,
1212
connectionManager
1313
);
14+
if (!workspaceConfigs.configureJupyterCellMarkers) {
15+
return;
16+
}
17+
1418
if (workspaceConfigs.jupyterCellMarkerRegex === undefined) {
1519
workspaceConfigs.jupyterCellMarkerRegex =
1620
"^(# Databricks notebook source|# COMMAND ----------)";

‎packages/databricks-vscode/src/vscode-objs/WorkspaceConfigs.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ export const workspaceConfigs = {
9898
return workspace.getConfiguration("python").get<string>("envFile");
9999
},
100100

101+
get configureJupyterCellMarkers(): boolean {
102+
return (
103+
workspace
104+
.getConfiguration("databricks")
105+
.get<boolean>("notebooks.configureJupyterCellMarkers") ?? true
106+
);
107+
},
108+
101109
get jupyterCellMarkerRegex(): string | undefined {
102110
return workspace
103111
.getConfiguration("jupyter")

0 commit comments

Comments
 (0)