Skip to content

Step 7 WI-1: Scratchpad IntelliSense — Shell API type definitions + TS Server Plugin#543

Open
tnaum-ms wants to merge 9 commits intofeature/shell-integrationfrom
dev/tnaum/7-scratchpad-autcomplete
Open

Step 7 WI-1: Scratchpad IntelliSense — Shell API type definitions + TS Server Plugin#543
tnaum-ms wants to merge 9 commits intofeature/shell-integrationfrom
dev/tnaum/7-scratchpad-autcomplete

Conversation

@tnaum-ms
Copy link
Copy Markdown
Collaborator

Goal

Add IntelliSense (autocompletion, hover docs, signature help) to DocumentDB scratchpad (.documentdb.js) files.

This is Work Item 1 (Foundation) of Step 7: Scratchpad CompletionItemProvider.

What's included

  • Phase 1.1: DocumentDB shell API .d.ts type definitions (854 lines, original JSDoc)
  • Phase 1.2: TypeScript Server Plugin that injects the .d.ts into scratchpad files
  • Phase 1.3: Language configuration, .documentdb.js as sole extension, TS activation

What it enables

When a user opens a .documentdb.js scratchpad file, they get:

  • db. method completions (getCollection, runCommand, aggregate, etc.)
  • db.users. collection method completions (find, insertOne, updateOne, etc.)
  • Cursor method chains (.limit(), .sort(), .toArray(), etc.)
  • BSON constructor completions (ObjectId(), ISODate(), UUID(), etc.)
  • JSDoc-powered hover documentation on all methods
  • Signature help for method parameters

…ions

Create the DocumentDB-scoped .d.ts file that declares the shell API
surface available in scratchpad (.documentdb) files. This provides:

- BSON type classes (ObjectId, UUID, Int32, Long, Decimal128, etc.)
- BSON constructor functions (ObjectId(), ISODate(), NumberInt(), etc.)
- Shell globals (db, use, help, print, sleep, version, EJSON)
- Database interface with 14 methods (getCollection, runCommand, etc.)
- Collection interface with 25 methods (find, insertOne, aggregate, etc.)
- FindCursor interface with 18 chainable methods (limit, sort, etc.)
- AggregationCursor interface with 8 methods (toArray, explain, etc.)
- Result types (InsertOneResult, UpdateResult, DeleteResult, etc.)

All JSDoc content is original writing — not copied from MongoDB docs.
Only DocumentDB-supported methods are included.
Add a TypeScript server plugin that injects the DocumentDB shell API
type definitions into the TypeScript project context for scratchpad
files (language ID: documentdb-scratchpad).

The plugin uses the getExternalFiles() API to tell the TS server to
include our .d.ts file, enabling:
- db. method chain completions
- Cursor method completions (.limit(), .sort(), etc.)
- BSON constructor completions (ObjectId(), ISODate(), etc.)
- JSDoc-powered hover documentation
- Signature help for method parameters

Implementation:
- src/documentdb/scratchpad/tsPlugin/index.ts: Plugin module
- webpack.config.ext.js: New entry point + CopyWebpackPlugin for .d.ts
- package.json: typescriptServerPlugins contribution point
…activation

- configurationDefaults for documentdb-scratchpad: enable string literal
  completions (editor.quickSuggestions.strings: true) without global changes
- Use .documentdb.js as sole file extension (drop bare .documentdb — never shipped)
- Force-activate VS Code TypeScript extension on scratchpad file open
  (our custom language ID isn't in TS auto-activation list)
@tnaum-ms tnaum-ms requested a review from a team as a code owner March 28, 2026 11:31
Add a custom vscode.CompletionItemProvider for scratchpad files that
provides DocumentDB-specific completions the TS service can't handle:

- scratchpadContextDetector.ts: Two-stage cursor context detection
  Stage 1: JS-level (top-level, db., collection., cursor chain, method arg)
  Stage 2: Query-object (reuses cursorContext.ts from collection view)

- completionRegistry.ts: Static catalog of shell globals (S1),
  database methods (S2), collection methods (S3), find cursor
  methods (S4), and aggregation cursor methods (S5)

- ScratchpadCompletionItemProvider.ts: Main provider class
  - Top-level: globals + BSON constructors
  - db.*: database methods + dynamic collection names from SchemaStore
  - db.<collection>.*: collection methods with frequency-based sorting
  - Cursor chains: find cursor or aggregation cursor methods
  - Method arguments: query operators, field names, type-aware sorting
  - String literals: collection names inside getCollection()/use()

Registered in ClustersExtension.ts alongside CodeLens and block highlighter.
Add 80 TDD behavior tests covering:
- Context detection (18 tests): S1-S6 cursor positions + method arg detection
- Completion registry (62 tests): S1-S5 required entries, exclusions, sort
  ordering, snippet quality, and description completeness

Tests follow the same TDD contract pattern as the collection view's
completionBehavior.test.ts — tests define the behavior specification
and must not be auto-fixed on failure.
Fixes from L1/L2/N test results:

1. Fix L1.4/L1.5: .d.ts index signature changed from
   'DocumentDBCollection | ((...args) => unknown)' to just
   'DocumentDBCollection'. The union type caused TS to resolve
   db.users.find() as Array.find instead of our collection method,
   and hover showed 'any' instead of JSDoc.

2. Fix L2.2: Double-dollar ($$regex) when selecting $-prefixed operators.
   Added replaceRange computation that extends back 1 character when
   preceded by '$', matching the same fix from the collection view.

3. Fix L2.3: Unquoted dotted field names (e.g. 'address.city' inserted
   without quotes, causing syntax errors). Fields containing dots,
   spaces, or hyphens are now auto-quoted.

4. Fix L2.3: db.getCollection('restaurants').find({}) didn't provide
   field completions. Added resolveCollectionName() that extracts the
   collection name from the getCollection('name') string argument.

5. Fix L2.5: String literal completions inside getCollection('|') now
   work. Added checkStringLiteralContext() that detects string position
   BEFORE the method argument handler runs.

6. Fix lint: remove useless escape in regex character class.
The typescriptServerPlugins contribution point resolves the 'name' field
as an npm package name from <extensionPath>/node_modules/ (as documented
by VS Code). Previous attempts used relative file paths which don't work
because extensionPath varies between dev host (dist/) and VSIX (root/).

Fix: Create a package.json stub at dist/node_modules/documentdb-scratchpad-ts-plugin/
with 'main' pointing to ../../scratchpadTsPlugin.js. This allows the TS
server to resolve our plugin via standard npm package resolution:
  require('documentdb-scratchpad-ts-plugin')
  → dist/node_modules/documentdb-scratchpad-ts-plugin/package.json
  → main: ../../scratchpadTsPlugin.js
  → dist/scratchpadTsPlugin.js

The stub is copied to dist/ by CopyWebpackPlugin during build.
Verified: included in VSIX, resolves correctly with TS's resolveJSModule().

Also: cleaned up debug logging from ClustersExtension and tsPlugin.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant