@marimo-team/islands 0.23.12-dev9 → 0.23.13-dev0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/{chat-ui-BEOvjkmJ.js → chat-ui-CsPewo4h.js} +2 -2
  2. package/dist/{code-visibility-w2yZTVwB.js → code-visibility-D9IipVFG.js} +714 -707
  3. package/dist/{html-to-image-Di0mtt6O.js → html-to-image-DXwLcQ6l.js} +22 -15
  4. package/dist/main.js +1160 -1027
  5. package/dist/{process-output-BLd4KuwX.js → process-output-C6_e1pT_.js} +1 -1
  6. package/dist/{reveal-component-CuqTvwmg.js → reveal-component-Dk32fyu2.js} +13 -13
  7. package/dist/style.css +1 -1
  8. package/package.json +1 -1
  9. package/src/components/data-table/TableBottomBar.tsx +4 -1
  10. package/src/components/data-table/data-table.tsx +26 -17
  11. package/src/components/data-table/utils.ts +1 -4
  12. package/src/components/editor/ai/__tests__/completion-utils.test.ts +48 -2
  13. package/src/components/editor/ai/completion-utils.ts +54 -36
  14. package/src/components/editor/app-container.tsx +3 -1
  15. package/src/components/editor/output/ImageOutput.tsx +12 -3
  16. package/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx +2 -2
  17. package/src/core/codemirror/go-to-definition/__tests__/commands.test.ts +67 -0
  18. package/src/core/codemirror/go-to-definition/__tests__/utils.test.ts +47 -0
  19. package/src/core/codemirror/go-to-definition/commands.ts +47 -30
  20. package/src/core/codemirror/go-to-definition/utils.ts +0 -1
  21. package/src/core/codemirror/reactive-references/__tests__/analyzer.test.ts +54 -0
  22. package/src/core/codemirror/reactive-references/analyzer.ts +44 -35
  23. package/src/core/islands/__tests__/bridge.test.ts +25 -0
  24. package/src/core/islands/__tests__/parse.test.ts +585 -1
  25. package/src/core/islands/__tests__/test-utils.tsx +10 -1
  26. package/src/core/islands/bridge.ts +6 -1
  27. package/src/core/islands/constants.ts +2 -0
  28. package/src/core/islands/parse.ts +290 -13
  29. package/src/plugins/impl/DataTablePlugin.tsx +20 -1
  30. package/src/plugins/impl/__tests__/DataTablePlugin.test.tsx +141 -1
  31. package/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx +54 -4
  32. package/src/plugins/impl/anywidget/__tests__/AnyWidgetPlugin.test.tsx +104 -1
  33. package/src/plugins/impl/anywidget/__tests__/model.test.ts +19 -0
  34. package/src/plugins/impl/anywidget/model.ts +15 -0
  35. package/src/utils/__tests__/records.test.ts +27 -0
  36. package/src/utils/records.ts +12 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marimo-team/islands",
3
- "version": "0.23.12-dev9",
3
+ "version": "0.23.13-dev0",
4
4
  "main": "dist/main.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -3,6 +3,7 @@
3
3
 
4
4
  import type { RowSelectionState, Table } from "@tanstack/react-table";
5
5
  import { useLocale } from "react-aria";
6
+ import { isStaticNotebook } from "@/core/static/static-state";
6
7
  import type { GetRowIds } from "@/plugins/impl/DataTablePlugin";
7
8
  import { cn } from "@/utils/cn";
8
9
  import { Events } from "@/utils/events";
@@ -40,6 +41,8 @@ export const TableBottomBar = <TData,>({
40
41
  className,
41
42
  }: TableBottomBarProps<TData>) => {
42
43
  const { locale } = useLocale();
44
+ // Pagination fetches each page via a kernel RPC, absent in static exports.
45
+ const isStatic = isStaticNotebook();
43
46
  const handleSelectAllRows = (value: boolean) => {
44
47
  if (!onRowSelectionChange) {
45
48
  return;
@@ -171,7 +174,7 @@ export const TableBottomBar = <TData,>({
171
174
  <CellSelectionStats table={table} className="lg:hidden" />
172
175
  </div>
173
176
  <div className="ml-auto lg:ml-0 lg:justify-self-center flex items-center shrink-0">
174
- {pagination && (
177
+ {pagination && !isStatic && (
175
178
  <DataTablePagination
176
179
  table={table}
177
180
  tableLoading={tableLoading}
@@ -25,6 +25,7 @@ import { useLocale } from "react-aria";
25
25
 
26
26
  import { Button } from "@/components/ui/button";
27
27
  import { Table } from "@/components/ui/table";
28
+ import { isStaticNotebook } from "@/core/static/static-state";
28
29
  import { Banner } from "@/plugins/impl/common/error-banner";
29
30
  import type {
30
31
  CalculateTopKRows,
@@ -182,6 +183,11 @@ const DataTableInternal = <TData,>({
182
183
  onViewedRowChange,
183
184
  renderTableExplorerPanel,
184
185
  }: DataTableProps<TData>) => {
186
+ // The top bar's controls (search, filters, column explorer, chart builder)
187
+ // all require a live kernel, which static exports don't have.
188
+ const isStatic = isStaticNotebook();
189
+ const showTableTopBar = !isStatic;
190
+
185
191
  const [showLoadingBar, setShowLoadingBar] = React.useState<boolean>(false);
186
192
  const { locale } = useLocale();
187
193
 
@@ -267,11 +273,12 @@ const DataTableInternal = <TData,>({
267
273
  }
268
274
  : {}),
269
275
  manualSorting: manualSorting,
276
+ enableSorting: !isStatic,
270
277
  enableMultiSort: true,
271
278
  getSortedRowModel: getSortedRowModel(),
272
279
  // filtering
273
280
  manualFiltering: true,
274
- enableColumnFilters: showFilters,
281
+ enableColumnFilters: showFilters && !isStatic,
275
282
  getFilteredRowModel: getFilteredRowModel(),
276
283
  onColumnFiltersChange: onFiltersChange,
277
284
  // selection
@@ -355,22 +362,24 @@ const DataTableInternal = <TData,>({
355
362
  part="table-wrapper"
356
363
  className={cn(className || "rounded-md border overflow-hidden")}
357
364
  >
358
- <TableTopBar
359
- table={table}
360
- showSearch={showSearch}
361
- searchQuery={searchQuery}
362
- onSearchQueryChange={onSearchQueryChange}
363
- reloading={reloading}
364
- showChartBuilder={showChartBuilder}
365
- isChartBuilderOpen={isChartBuilderOpen}
366
- toggleDisplayHeader={toggleDisplayHeader}
367
- showTableExplorer={showTableExplorer}
368
- togglePanel={togglePanel}
369
- isAnyPanelOpen={isAnyPanelOpen}
370
- downloadAs={downloadAs}
371
- sizeBytes={sizeBytes}
372
- sizeBytesIsLoading={sizeBytesIsLoading}
373
- />
365
+ {showTableTopBar && (
366
+ <TableTopBar
367
+ table={table}
368
+ showSearch={showSearch}
369
+ searchQuery={searchQuery}
370
+ onSearchQueryChange={onSearchQueryChange}
371
+ reloading={reloading}
372
+ showChartBuilder={showChartBuilder}
373
+ isChartBuilderOpen={isChartBuilderOpen}
374
+ toggleDisplayHeader={toggleDisplayHeader}
375
+ showTableExplorer={showTableExplorer}
376
+ togglePanel={togglePanel}
377
+ isAnyPanelOpen={isAnyPanelOpen}
378
+ downloadAs={downloadAs}
379
+ sizeBytes={sizeBytes}
380
+ sizeBytesIsLoading={sizeBytesIsLoading}
381
+ />
382
+ )}
374
383
  {allUserColumnsHidden && (
375
384
  <Banner className="mb-1 mx-2 rounded flex items-center justify-between">
376
385
  <span>All columns are hidden.</span>
@@ -4,6 +4,7 @@ import type { Table } from "@tanstack/react-table";
4
4
  import type { TableData } from "@/plugins/impl/DataTablePlugin";
5
5
  import { vegaLoadData } from "@/plugins/impl/vega/loader";
6
6
  import { jsonParseWithSpecialChar } from "@/utils/json/json-parser";
7
+ import { isRecord } from "@/utils/records";
7
8
  import { getMimeValues } from "./mime-cell";
8
9
  import type { DataType } from "@/core/kernel/messages";
9
10
  import {
@@ -232,10 +233,6 @@ function stripHtml(html: string): string {
232
233
 
233
234
  const HTML_MIMETYPES = new Set(["text/html", "text/markdown"]);
234
235
 
235
- function isRecord(value: unknown): value is Record<string, unknown> {
236
- return value !== null && typeof value === "object" && !Array.isArray(value);
237
- }
238
-
239
236
  /**
240
237
  * Get clipboard-ready text and optional HTML for a cell.
241
238
  *
@@ -1,6 +1,15 @@
1
1
  /* Copyright 2026 Marimo. All rights reserved. */
2
- import { beforeEach, describe, expect, it, type Mock, vi } from "vitest";
2
+ import {
3
+ afterEach,
4
+ beforeEach,
5
+ describe,
6
+ expect,
7
+ it,
8
+ type Mock,
9
+ vi,
10
+ } from "vitest";
3
11
  import { variableName } from "@/__tests__/branded";
12
+ import * as aiContext from "@/core/ai/context/context";
4
13
  import { getCodes } from "@/core/codemirror/copilot/getCodes";
5
14
  import { dataSourceConnectionsAtom } from "@/core/datasets/data-source-connections";
6
15
  import { DUCKDB_ENGINE } from "@/core/datasets/engines";
@@ -8,10 +17,11 @@ import { datasetsAtom } from "@/core/datasets/state";
8
17
  import type { DatasetsState } from "@/core/datasets/types";
9
18
  import { store } from "@/core/state/jotai";
10
19
  import { variablesAtom } from "@/core/variables/state";
11
- import type { UIMessage } from "ai";
20
+ import type { FileUIPart, UIMessage } from "ai";
12
21
  import {
13
22
  codeToCells,
14
23
  getAICompletionBody,
24
+ getAICompletionBodyWithAttachments,
15
25
  isContextAttachment,
16
26
  MARIMO_CONTEXT_PART_TYPE,
17
27
  resolveChatContext,
@@ -440,6 +450,42 @@ describe("isContextAttachment", () => {
440
450
  });
441
451
  });
442
452
 
453
+ describe("context attachment stamping", () => {
454
+ const rawAttachment: FileUIPart = {
455
+ type: "file",
456
+ mediaType: "image/png",
457
+ url: "data:image/png;base64,abc",
458
+ };
459
+
460
+ beforeEach(() => {
461
+ vi.spyOn(aiContext, "getAIContextRegistry").mockReturnValue({
462
+ parseAllContextIds: () => ["data://t1"],
463
+ formatContextForAI: () => '<data name="t1" />',
464
+ getAttachmentsForContext: async () => [rawAttachment],
465
+ } as unknown as ReturnType<typeof aiContext.getAIContextRegistry>);
466
+ });
467
+
468
+ afterEach(() => {
469
+ vi.restoreAllMocks();
470
+ });
471
+
472
+ it("stamps chat attachments as context-derived", async () => {
473
+ const { attachments } = await resolveChatContext("see @data://t1");
474
+ expect(attachments).toHaveLength(1);
475
+ expect(isContextAttachment(attachments[0])).toBe(true);
476
+ // The original attachment is left untouched (we return a stamped copy).
477
+ expect(rawAttachment.providerMetadata).toBeUndefined();
478
+ });
479
+
480
+ it("stamps completion attachments the same way as chat", async () => {
481
+ const { attachments } = await getAICompletionBodyWithAttachments({
482
+ input: "see @data://t1",
483
+ });
484
+ expect(attachments).toHaveLength(1);
485
+ expect(isContextAttachment(attachments[0])).toBe(true);
486
+ });
487
+ });
488
+
443
489
  describe("codeToCells", () => {
444
490
  it("should return empty array for empty string", () => {
445
491
  const code = "";
@@ -9,6 +9,7 @@ import {
9
9
  import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";
10
10
  import type { DataUIPart, FileUIPart, UIMessage } from "ai";
11
11
  import { getAIContextRegistry } from "@/core/ai/context/context";
12
+ import type { ContextLocatorId } from "@/core/ai/context/registry";
12
13
  import { getCodes } from "@/core/codemirror/copilot/getCodes";
13
14
  import type { LanguageAdapterType } from "@/core/codemirror/language/types";
14
15
  import type { AiCompletionRequest } from "@/core/network/types";
@@ -89,20 +90,51 @@ export function isContextAttachment(part: UIMessage["parts"][number]): boolean {
89
90
  }
90
91
 
91
92
  /**
92
- * Resolve @-context for messages. They represent referenced
93
- * datasets, variables, or other context from the user's prompt.
93
+ * Stamp a context-derived attachment with a provenance marker.
94
+ *
95
+ * Some @-mentions resolve to file attachments (e.g. a cell's image output),
96
+ * which get appended to the user message right alongside files the user
97
+ * uploaded by hand. Once they're in the message the two are indistinguishable,
98
+ * so we mark the context-derived ones. This matters on message edit: we
99
+ * re-resolve context from the edited text, and `isContextAttachment` lets us
100
+ * drop only the stale context attachments while preserving the user's own
101
+ * uploads
94
102
  */
95
- export async function resolveChatContext(
103
+ function stampContextAttachment(attachment: FileUIPart): FileUIPart {
104
+ return {
105
+ ...attachment,
106
+ providerMetadata: {
107
+ ...attachment.providerMetadata,
108
+ // Merge within the `marimo` namespace so we don't clobber any other
109
+ // marimo metadata a provider may have already set.
110
+ marimo: {
111
+ ...attachment.providerMetadata?.marimo,
112
+ ...CONTEXT_ATTACHMENT_METADATA.marimo,
113
+ },
114
+ },
115
+ };
116
+ }
117
+
118
+ interface ResolvedContext {
119
+ plainText: string;
120
+ contextIds: ContextLocatorId[];
121
+ attachments: FileUIPart[];
122
+ }
123
+
124
+ /**
125
+ * Parse @-context for messages
126
+ */
127
+ async function resolveContextAttachments(
96
128
  input: string,
97
- ): Promise<ResolvedChatContext> {
129
+ ): Promise<ResolvedContext> {
98
130
  if (!input.includes(CONTEXT_TRIGGER)) {
99
- return { contextPart: null, attachments: [] };
131
+ return { plainText: "", contextIds: [], attachments: [] };
100
132
  }
101
133
 
102
134
  const registry = getAIContextRegistry(store);
103
135
  const contextIds = registry.parseAllContextIds(input);
104
136
  if (contextIds.length === 0) {
105
- return { contextPart: null, attachments: [] };
137
+ return { plainText: "", contextIds: [], attachments: [] };
106
138
  }
107
139
 
108
140
  const plainText = registry.formatContextForAI(contextIds);
@@ -110,20 +142,24 @@ export async function resolveChatContext(
110
142
  let attachments: FileUIPart[] = [];
111
143
  try {
112
144
  const resolved = await registry.getAttachmentsForContext(contextIds);
113
- attachments = resolved.map((attachment) => ({
114
- ...attachment,
115
- providerMetadata: {
116
- ...attachment.providerMetadata,
117
- marimo: {
118
- ...attachment.providerMetadata?.marimo,
119
- ...CONTEXT_ATTACHMENT_METADATA.marimo,
120
- },
121
- },
122
- }));
145
+ attachments = resolved.map(stampContextAttachment);
123
146
  } catch (error) {
124
147
  Logger.error("Error getting attachments:", error);
125
148
  }
126
149
 
150
+ return { plainText, contextIds, attachments };
151
+ }
152
+
153
+ /**
154
+ * Resolve @-context for messages. They represent referenced
155
+ * datasets, variables, or other context from the user's prompt.
156
+ */
157
+ export async function resolveChatContext(
158
+ input: string,
159
+ ): Promise<ResolvedChatContext> {
160
+ const { plainText, contextIds, attachments } =
161
+ await resolveContextAttachments(input);
162
+
127
163
  let contextPart: MarimoContextUIPart | null = null;
128
164
  if (plainText.trim()) {
129
165
  contextPart = {
@@ -141,31 +177,13 @@ export async function resolveChatContext(
141
177
  export async function getAICompletionBodyWithAttachments({
142
178
  input,
143
179
  }: Opts): Promise<AICompletionBodyWithAttachments> {
144
- let contextString = "";
145
- let attachments: FileUIPart[] = [];
146
-
147
- // Skip if no '@' in the input
148
- if (input.includes("@")) {
149
- const registry = getAIContextRegistry(store);
150
- const contextIds = registry.parseAllContextIds(input);
151
-
152
- // Get context string
153
- contextString = registry.formatContextForAI(contextIds);
154
-
155
- // Get attachments
156
- try {
157
- attachments = await registry.getAttachmentsForContext(contextIds);
158
- Logger.debug("Included attachments", attachments.length);
159
- } catch (error) {
160
- Logger.error("Error getting attachments:", error);
161
- }
162
- }
180
+ const { plainText, attachments } = await resolveContextAttachments(input);
163
181
 
164
182
  return {
165
183
  body: {
166
184
  includeOtherCode: getCodes(""),
167
185
  context: {
168
- plainText: contextString,
186
+ plainText,
169
187
  schema: [],
170
188
  variables: [],
171
189
  },
@@ -48,7 +48,9 @@ export const AppContainer: React.FC<PropsWithChildren<Props>> = ({
48
48
  "bg-background w-full h-full text-textColor",
49
49
  "flex flex-col overflow-y-auto",
50
50
  width === "full" && "config-width-full",
51
- width === "columns" ? "overflow-x-auto" : "overflow-x-hidden",
51
+ width === "columns"
52
+ ? "overflow-x-auto"
53
+ : "overflow-x-auto sm:overflow-x-hidden",
52
54
  "print:height-fit",
53
55
  )}
54
56
  >
@@ -5,8 +5,8 @@ import type { JSX } from "react";
5
5
  interface Props {
6
6
  src: string;
7
7
  alt?: string;
8
- width?: number;
9
- height?: number;
8
+ width?: number | string;
9
+ height?: number | string;
10
10
  className?: string;
11
11
  }
12
12
 
@@ -17,9 +17,18 @@ export const ImageOutput = ({
17
17
  height,
18
18
  className,
19
19
  }: Props): JSX.Element => {
20
+ // Convert numeric values to pixel strings, pass string values (like "100%") as-is
21
+ const style: React.CSSProperties = {};
22
+ if (width !== undefined) {
23
+ style.width = typeof width === "number" ? `${width}px` : width;
24
+ }
25
+ if (height !== undefined) {
26
+ style.height = typeof height === "number" ? `${height}px` : height;
27
+ }
28
+
20
29
  return (
21
30
  <span className={className}>
22
- <img src={src} alt={alt} width={width} height={height} />
31
+ <img src={src} alt={alt} style={style} />
23
32
  </span>
24
33
  );
25
34
  };
@@ -32,9 +32,9 @@ export const VerticalLayoutWrapper: React.FC<PropsWithChildren<Props>> = ({
32
32
  // This padding needs to be the same from above to be correctly applied
33
33
  "pb-24 sm:pb-12",
34
34
  appConfig.width === "compact" &&
35
- "max-w-(--content-width) min-w-[400px]",
35
+ "max-w-(--content-width) sm:min-w-[400px]",
36
36
  appConfig.width === "medium" &&
37
- "max-w-(--content-width-medium) min-w-[400px]",
37
+ "max-w-(--content-width-medium) sm:min-w-[400px]",
38
38
  appConfig.width === "columns" && "w-fit",
39
39
  appConfig.width === "full" && "max-w-full",
40
40
  // Hide the cells for a fake loading effect, to avoid flickering
@@ -253,6 +253,73 @@ a = 10`;
253
253
  `);
254
254
  });
255
255
 
256
+ test("from-import alias is the binding, not the imported name", async () => {
257
+ const code = `\
258
+ from math import sin as my_sin
259
+ print(my_sin)`;
260
+ view = createEditor(code);
261
+ const usagePosition = code.lastIndexOf("my_sin");
262
+ const result = goToVariableDefinition(view, "my_sin", usagePosition);
263
+
264
+ expect(result).toBe(true);
265
+ await tick();
266
+ // The alias `my_sin` (after `as`) is the real binding.
267
+ expect(renderEditorView(view)).toMatchInlineSnapshot(`
268
+ "
269
+ from math import sin as my_sin
270
+ ^
271
+ print(my_sin)
272
+ "
273
+ `);
274
+ });
275
+
276
+ test("module path in from-import is not a local definition", async () => {
277
+ const code = `\
278
+ from math import sin
279
+ print(math)`;
280
+ view = createEditor(code);
281
+ const usagePosition = code.lastIndexOf("math");
282
+ // `math` is a module reference in the from-clause, not a binding in this
283
+ // cell, so the scoped resolver should return false and let the caller fall
284
+ // through to cross-cell resolution.
285
+ const result = goToVariableDefinition(view, "math", usagePosition);
286
+
287
+ expect(result).toBe(false);
288
+ expect(view.state.selection.main.head).toBe(0);
289
+ });
290
+
291
+ test("imported name without `as` is a local definition", async () => {
292
+ const code = `\
293
+ from math import sin
294
+ print(sin)`;
295
+ view = createEditor(code);
296
+ const usagePosition = code.lastIndexOf("sin");
297
+ const result = goToVariableDefinition(view, "sin", usagePosition);
298
+
299
+ expect(result).toBe(true);
300
+ await tick();
301
+ expect(renderEditorView(view)).toMatchInlineSnapshot(`
302
+ "
303
+ from math import sin
304
+ ^
305
+ print(sin)
306
+ "
307
+ `);
308
+ });
309
+
310
+ test("imported name shadowed by `as` is not a binding", async () => {
311
+ const code = `\
312
+ from math import sin as my_sin
313
+ print(sin)`;
314
+ view = createEditor(code);
315
+ const usagePosition = code.lastIndexOf("sin");
316
+ // `sin` here refers to nothing in this cell (it was renamed to `my_sin`),
317
+ // so the scoped resolver should return false.
318
+ const result = goToVariableDefinition(view, "sin", usagePosition);
319
+
320
+ expect(result).toBe(false);
321
+ });
322
+
256
323
  test("selects outer-scope function declaration", async () => {
257
324
  view = createEditor(`\
258
325
  def x():
@@ -133,4 +133,51 @@ output = _x + 10`;
133
133
  await tick();
134
134
  expect(view.state.selection.main.head).toBe(code.indexOf("_x = 10"));
135
135
  });
136
+
137
+ test("falls through to cross-cell when in-cell occurrence is only a module path in a from-import", async () => {
138
+ // Regression: ImportStatement used to register every VariableName child
139
+ // (the module path and pre-`as` names) as in-cell declarations, so the
140
+ // local-first short-circuit would steal F12 from cross-cell resolution.
141
+ const moduleCell = cellId("module-cell");
142
+ const usageCell = cellId("usage-cell");
143
+ const moduleCode = `mymodule = 100`;
144
+ const usageCode = `\
145
+ from mymodule import something
146
+ print(mymodule)`;
147
+
148
+ const moduleView = createEditor(moduleCode, moduleCode.length);
149
+ const usageView = createEditor(
150
+ usageCode,
151
+ usageCode.lastIndexOf("mymodule"),
152
+ );
153
+ views.push(moduleView, usageView);
154
+
155
+ const notebook = initialNotebookState();
156
+ notebook.cellHandles[moduleCell] = {
157
+ current: { editorView: moduleView, editorViewOrNull: moduleView },
158
+ } as never;
159
+ notebook.cellHandles[usageCell] = {
160
+ current: { editorView: usageView, editorViewOrNull: usageView },
161
+ } as never;
162
+
163
+ store.set(notebookAtom, notebook);
164
+ store.set(variablesAtom, {
165
+ [variableName("mymodule")]: {
166
+ dataType: "int",
167
+ declaredBy: [moduleCell],
168
+ name: variableName("mymodule"),
169
+ usedBy: [usageCell],
170
+ value: "100",
171
+ },
172
+ });
173
+
174
+ const result = goToDefinitionAtCursorPosition(usageView);
175
+
176
+ expect(result).toBe(true);
177
+ await tick();
178
+ // Cross-cell jump: moduleView's cursor should land on `mymodule = 100`.
179
+ expect(moduleView.state.selection.main.head).toBe(
180
+ moduleCode.indexOf("mymodule"),
181
+ );
182
+ });
136
183
  });
@@ -307,33 +307,52 @@ function collectMatchingDeclarations(
307
307
  break;
308
308
  }
309
309
  case "ImportStatement": {
310
+ // The grammar emits one ImportStatement for both `import x [as y]` and
311
+ // `from m import x [as y], ...`. Direct children include the keywords
312
+ // (`from`/`import`/`as`), commas, dots, and every VariableName from the
313
+ // module path AND the import list. We only want the names that actually
314
+ // bind in the current scope: the post-`as` alias if present, otherwise
315
+ // the imported name itself. Names before `import` (the from-path) and
316
+ // the original name when an alias follows it are NOT bindings.
310
317
  const subCursor = node.cursor();
311
318
  subCursor.firstChild();
312
- do {
313
- if (
314
- subCursor.name === "VariableName" &&
315
- state.doc.sliceString(subCursor.from, subCursor.to) === variableName
316
- ) {
317
- addDeclaration(declarations, currentScope, subCursor.from);
319
+ let pastImport = false;
320
+ // Buffer the most recent post-`import` VariableName so we can defer
321
+ // committing it until we know whether `as` follows.
322
+ let pending: { from: number; matches: boolean } | null = null;
323
+ const commit = () => {
324
+ if (pending?.matches) {
325
+ addDeclaration(declarations, currentScope, pending.from);
318
326
  }
319
- } while (subCursor.nextSibling());
320
- break;
321
- }
322
- case "ImportFromStatement": {
323
- const subCursor = node.cursor();
324
- subCursor.firstChild();
325
- let foundImport = false;
327
+ pending = null;
328
+ };
326
329
  do {
327
330
  if (subCursor.name === "import") {
328
- foundImport = true;
329
- } else if (
330
- foundImport &&
331
- subCursor.name === "VariableName" &&
332
- state.doc.sliceString(subCursor.from, subCursor.to) === variableName
333
- ) {
334
- addDeclaration(declarations, currentScope, subCursor.from);
331
+ pastImport = true;
332
+ continue;
333
+ }
334
+ if (!pastImport) {
335
+ continue;
336
+ }
337
+ if (subCursor.name === "as") {
338
+ // Next VariableName is the alias and replaces `pending`.
339
+ pending = null;
340
+ continue;
341
+ }
342
+ if (subCursor.name === "VariableName") {
343
+ // Flush any previous pending name (no `as` followed it).
344
+ commit();
345
+ pending = {
346
+ from: subCursor.from,
347
+ matches:
348
+ state.doc.sliceString(subCursor.from, subCursor.to) ===
349
+ variableName,
350
+ };
351
+ } else if (subCursor.name === ",") {
352
+ commit();
335
353
  }
336
354
  } while (subCursor.nextSibling());
355
+ commit();
337
356
  break;
338
357
  }
339
358
  case "TryStatement":
@@ -410,23 +429,21 @@ function findScopedDefinitionPosition(
410
429
  * @param view The editor view which contains the variable name.
411
430
  * @param variableName The name of the variable to select, if found in the editor.
412
431
  * @param usagePosition The position of the variable usage, if available.
413
- * @param fallbackToFirstMatch Whether to fall back to the first matching
414
- * variable name when no scoped definition is found. Defaults to true.
415
432
  */
416
433
  export function goToVariableDefinition(
417
434
  view: EditorView,
418
435
  variableName: string,
419
436
  usagePosition?: number,
420
- fallbackToFirstMatch = true,
421
437
  ): boolean {
422
438
  const { state } = view;
423
- let from: number | null = null;
424
- if (usagePosition !== undefined) {
425
- from = findScopedDefinitionPosition(state, variableName, usagePosition);
426
- }
427
- if (from === null && fallbackToFirstMatch) {
428
- from = findFirstMatchingVariable(state, variableName);
429
- }
439
+ // When the caller knows the usage position, trust the scoped lookup. Falling
440
+ // back to first-match would defeat the local-vs-cross-cell decision in
441
+ // goToDefinition: if the symbol only appears as a module path in an import,
442
+ // scoped resolution returns null and we want the caller to try other cells.
443
+ const from =
444
+ usagePosition !== undefined
445
+ ? findScopedDefinitionPosition(state, variableName, usagePosition)
446
+ : findFirstMatchingVariable(state, variableName);
430
447
 
431
448
  if (from === null) {
432
449
  return false;
@@ -82,7 +82,6 @@ export function goToDefinition(
82
82
  view,
83
83
  variableName,
84
84
  usagePosition,
85
- false,
86
85
  );
87
86
  if (foundLocally) {
88
87
  return true;