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

fix: apply latest Volar.js version to docs in "Guides - Your First Volar Language Server" #20

Open
tsukuha opened this issue Dec 15, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@tsukuha
Copy link
Contributor

tsukuha commented Dec 15, 2024

About

Thanks for your watching.
I found to fix point in the doc packages/server/src/languagePlugin.ts existing on first-server.mdx and so on aren't applied
latest Volar.js version.

Reproduction

  • package.json
{
  "name": "languge-tools-startup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "languge-tools-startup": "link:",
    "typescript": "^5.7.2"
  }
}
  • tsconfig.base.json
{
  "compilerOptions": {
    "module": "nodenext"
  }
}
  • packages/server/package.json
{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "bin": {
    "html-language-server": "./bin/html1-language-server.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@volar/language-core": "^2.4.11",
    "@volar/language-server": "^2.4.11",
    "@volar/language-service": "^2.4.11",
    "volar-service-css": "^0.0.62",
    "volar-service-html": "^0.0.62",
    "vscode-html-languageservice": "^5.3.1",
    "vscode-uri": "^3.0.8"
  }
}
  • packages/server/tsconfig.json
{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "out",
    "rootDir": "src",
  },
  "include": ["src"],
}
code error1 error2
スクリーンショット 2024-12-15 9 49 55 スクリーンショット 2024-12-15 9 50 17 スクリーンショット 2024-12-15 9 50 17

Proposal to fix

  • packages/server/src/languagePlugin.ts
+import type { LanguagePlugin, VirtualCode, CodeMapping } from "@volar/language-core";
-import type { LanguagePlugin, VirtualCode } from "@volar/language-core";
import type { URI } from "vscode-uri";
import type * as ts from 'typescript';

export const language = {
  getLanguageId(uri) {
    if (uri.path.endsWith('.html1')) {
      return 'html1';
    }
  },
+ createVirtualCode(scriptId, languageId, snapshot, ctx) {
- createVirtualCode(uri, languageId, snapshot) {
    if (languageId === "html1") {
      return new Html1Code(snapshot);
    }
  },
+ updateVirtualCode(scriptId, virtualCode, newSnapshot, ctx) {
- updateVirtualCode(uri, languageCode, snapshot) {
+  virtualCode.snapshot = newSnapshot;
-  languageCode.update(snapshot);
+  return virtualCode;
-  return languageCode;
  },
} satisfies LanguagePlugin<URI>;

export class Html1Code implements VirtualCode {
  id = "root";
  languageId = "html1";
  embeddedCodes: VirtualCode[] = [];
+ mappings: CodeMapping[] = [];

  constructor(public snapshot: ts.IScriptSnapshot) {
    this.onSnapshotUpdated();
  }

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

  public onSnapshotUpdated() {
    // Do something with the snapshot
  }
}
@johnsoncodehk johnsoncodehk added the documentation Improvements or additions to documentation label Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants