Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] JSONDocument.getNodeFromOffset is not a function at runtime #4576

Open
2 tasks done
TimonVS opened this issue Jun 25, 2024 · 0 comments
Open
2 tasks done

[Bug] JSONDocument.getNodeFromOffset is not a function at runtime #4576

TimonVS opened this issue Jun 25, 2024 · 0 comments

Comments

@TimonVS
Copy link

TimonVS commented Jun 25, 2024

Reproducible in vscode.dev or in VS Code Desktop?

  • Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.50.0#XQAAAAJQBQAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscw83J8RgmSaumWUF-TBOsVJ4S4FwfCDQb5-2uUHXTVAefbVSd9IzaZ_vv9VR9KugsKiuGc2v5CrpC-28kzerv7FIINy73ctGSXrXyLSJzMzDkuKaa3tMg-UK4lhIYaP9NgpLNPfl7xF4BFMGqe-EJcVnNjGP6HeT9D9pWb93Wo4ZQgXuxOwtwin2u1xyz2Gx2slmhr8HXj7qdLwdlI27k25rc_v3AgJaKUIY-HvvR-F0sYfn_RN_g0QEv9MxSIEaFssFLtvkiFbazxmuPXPkHE0d_5xmqDVrtxkmN5uPUH9JDZF7Yd1uWIv8GMpep5xZVFvSw2-cIz0w28dQsPRy09JtpdmqY_n8RDaTrTESu5TYMgqSUFAK5NLKs_SnqA-TxUA5t2LxcbDcESk_xWw1TvxfXMdIYn2kDAktHQZpw_WFa0VsjWzI5VGXfodLhidItlPOQvpLzdH4Ubvi9ri3HxQM--aLPv4u380HU3Ut-LoNAsG1DNderr5zcICxXC9wfSh-3lGctsMGComlylPMCAu147wsBYYxTuk4rrCNAvpTYvdvkDtNYbuaz6tKxtSbM86ty-wmMi4B45WXyoykT741zQSpdgyfhF67hv6JUWVhA8cL88H1mCrWtUsyiLkYFp0dHOfje430WShbPDsEICuuNK4F_jM1Px4NL713M2FRudga9BWP5wCjpUHsE_dhg3U0HKbDsr-6QaXG-UGmWOcfuVFjEfOuM94FkVYH78P9jQRx7xr6bgf6XjoZiGKyFyKrxRVvkCwRJfIsplC3WSQAaGLXEEothGIRxm7NjXTdFCUwN1wUH_-Exwyg

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: #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

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

No branches or pull requests

1 participant