microsoft / microsoft/monaco-editor
[Bug] `JSONDocument.getNodeFromOffset` is not a function at runtime
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 46.8k
- Forks
- 4.1k
- Avg merge
- 17h 58m
- Merged PRs (30d)
- 1
Description
Reproducible in vscode.dev or in VS Code Desktop?
- Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
- Not reproducible in the monaco editor playground
Monaco Editor Playground Link
Monaco Editor Playground Code
const value = /* set from `myEditor.getModel()`: */ `{
}`;
// Hover on each property to see its docs!
const myEditor = monaco.editor.create(document.getElementById("container"), {
value,
language: "json",
automaticLayout: true,
});
// Register dynamic completions based on AST
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: async (model, position, context) => {
console.log(monaco.languages.json);
const jsonWorker = await (
await monaco.languages.json.getWorker()
)(model.uri);
const jsonDocument = await jsonWorker.parseJSONDocument(
model.uri.toString()
);
console.log(jsonDocument);
// This works just fine in Monaco Editor Playground
const node = jsonDocument?.getNodeFromOffset(model.getOffsetAt(position));
console.log(node);
const word = model.getWordUntilPosition(position);
return {
suggestions: [
{
kind: monaco.languages.CompletionItemKind.Text,
insertText: '"foo"',
label: 'Foo',
range: {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn,
},
},
],
};
},
});
Reproduction Steps
I'm trying to create dynamic completions based on the JSON AST. For that I'm utilizing the functionality exposed with this PR: https://github.com/microsoft/monaco-editor/pull/4299, specifically JSONDocument.getNodeFromOffset. TypeScript tells me the method is there, but I'm getting a runtime error: Uncaught Error: jsonDocument?.getNodeFromOffset is not a function.
Reproduction environment with Vite: https://stackblitz.com/edit/vitejs-vite-bhgqgc?file=src%2Fmain.ts
Actual (Problematic) Behavior
Runtime error: Uncaught Error: jsonDocument?.getNodeFromOffset is not a function
Expected Behavior
jsonDocument?.getNodeFromOffset should work as it does in the Monaco Editor Playground.
Additional Context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Vite reproduction and compare its JSON worker behavior with the Monaco Editor Playground example at v0.50.0. Check why the runtime JSONDocument lacks getNodeFromOffset even though TypeScript exposes it; done means the documented call works without a runtime error in the reproduction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100