volarjs / volar.js

💙🌊
https://volarjs.dev/
MIT License
963 stars 47 forks source link

Can't read Virtual Codes via Volar Labs extension #201

Closed RonBis closed 2 months ago

RonBis commented 2 months ago

Getting an error whenever I try to read VirtualCode from Volar Labs sidebar icon

[error] CodeExpectedError: cannot open volar_virtual_code://my_ls/root.html#file%253A%252F%252F%252Fd%25253A%252FCode%252FMisc%252FExtension%252520Dev%252Fmy-ls%252Fsample%252Ftest2.html. Detail: Unable to resolve text model content for resource volar_virtual_code://my_ls/root.html#file%253A%252F%252F%252Fd%25253A%252FCode%252FMisc%252FExtension%252520Dev%252Fmy-ls%252Fsample%252Ftest2.html at s.$tryOpenDocument (vscode-file://vscode-app/c:/Users/xxx/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/out/vs/workbench/workbench.desktop.main.js:2196:101536)

Here is the reference code:

connection.listen();

connection.onInitialize((params) => { return server.initialize( params, createSimpleProject([languagePlugin]), [] ); });

connection.onInitialized(server.initialized); connection.onShutdown(server.shutdown);


- plugin.ts (Server)
```js
export const languagePlugin: LanguagePlugin<URI> = {
  getLanguageId(uri) {
    if (uri.path.endsWith(".html")) {
      return "html";
    }
  },
  createVirtualCode(uri, languageId, snapshot, ctx) {
    if (languageId === "html") {
      return new MyVirtualCode(snapshot);
    }
  },
};

export class MyVirtualCode implements VirtualCode {
  id = "root";
  languageId = "html";
  mappings: CodeMapping[];
  embeddedCodes?: VirtualCode[];

  constructor(public snapshot: IScriptSnapshot) {
    this.mappings = [
      {
        sourceOffsets: [0],
        generatedOffsets: [0],
        lengths: [snapshot.getLength()],
        data: {
          completion: true,
          format: true,
          navigation: true,
          semantic: true,
          structure: true,
          verification: true,
        },
      },
    ];
    this.onSnapshotUpdated();
  }

  public update(newSnapshot: IScriptSnapshot) {
    this.snapshot = newSnapshot;
    this.onSnapshotUpdated();
  }

  public onSnapshotUpdated() {
    const snapshotContent = this.snapshot.getText(0, this.snapshot.getLength());
  }
}

Is it a bug or am I doing something wrong here?

johnsoncodehk commented 2 months ago

I can't see the problem simply, can you share your repo?

RonBis commented 2 months ago

Yes, here is the link. https://github.com/RonBis/vscode-better-alpine

johnsoncodehk commented 2 months ago

This is a Volar Labs bug, I just released Volar Labs 2.3.0 to fix this problem, please try the new version, thanks.