Skip to content

Commit

Permalink
Merge pull request #44 from mizdra/update-volar
Browse files Browse the repository at this point in the history
Update volar
  • Loading branch information
mizdra authored Dec 8, 2024
2 parents 4eaf04f + 1e7c167 commit 0faf264
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "ZixuanChen.vitest-explorer"]
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "vitest.explorer"]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"typescript.preferences.importModuleSpecifier": "relative",
"javascript.preferences.importModuleSpecifier": "relative",
Expand Down
37 changes: 19 additions & 18 deletions examples/nextjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"next": "13.4.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "^5.3.0"
"typescript": "^5.7.2"
}
}
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"dist"
],
"dependencies": {
"@volar/language-core": "~2.0.2",
"@volar/typescript": "~2.0.2",
"@volar/language-core": "~2.4.10",
"@volar/typescript": "~2.4.10",
"change-case": "^4.1.2"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export = createLanguageServicePlugin((ts, info) => {

if (!info.project.fileExists(assetPluginOptions.tsConfigPath)) {
// project name not a tsconfig path, this is a inferred project
return [];
return { languagePlugins: [] };
}

const assetLanguage = createAssetLanguage(ts.sys, assetPluginOptions);

return [assetLanguage];
return { languagePlugins: [assetLanguage] };
});
82 changes: 44 additions & 38 deletions src/language-service/language.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
import path from 'node:path';
import type { LanguagePlugin } from '@volar/language-core';
import type * as ts from 'typescript/lib/tsserverlibrary';
import type {} from '@volar/typescript';
import ts from 'typescript/lib/tsserverlibrary';
import { getDtsContent } from '../dts';
import { AssetPluginOptions } from '../option';

export function createAssetLanguage(sys: ts.System, assetPluginOptions: AssetPluginOptions): LanguagePlugin {
export function createAssetLanguage(sys: ts.System, assetPluginOptions: AssetPluginOptions): LanguagePlugin<string> {
return {
createVirtualCode(fileId) {
const fileName = fileId.includes('://') ? fileId.split('://')[1] ?? '' : fileId;
if (
isMatchFile(fileName, assetPluginOptions.extensions, assetPluginOptions.exclude, assetPluginOptions.include)
) {
const dtsContent = getDtsContent(
fileName,
assetPluginOptions.exportedNameCase,
assetPluginOptions.exportedNamePrefix,
);
return {
id: 'main',
mappings: [],
embeddedCodes: [],
languageId: 'typescript',
snapshot: {
getText: (start, end) => dtsContent.substring(start, end),
getLength: () => dtsContent.length,
getChangeRange: () => undefined,
},
};
}
getLanguageId(scriptId) {
if (isMatchFile(scriptId)) return 'asset';
return undefined;
},
updateVirtualCode(_fileId, virtualCode) {
return virtualCode; // asset file content update does not affect virtual code
createVirtualCode(scriptId, languageId) {
if (languageId !== 'asset') return undefined;
const dtsContent = getDtsContent(
scriptId,
assetPluginOptions.exportedNameCase,
assetPluginOptions.exportedNamePrefix,
);
return {
id: 'main',
languageId: 'asset',
snapshot: {
getText: (start, end) => dtsContent.substring(start, end),
getLength: () => dtsContent.length,
getChangeRange: () => undefined,
},
mappings: [],
};
},
typescript: {
extraFileExtensions: assetPluginOptions.extensions.map((ext) => ({
extension: ext.slice(1),
isMixedContent: true,
scriptKind: 7,
})),
getScript(virtualCode) {
extraFileExtensions: assetPluginOptions.extensions.map((ext) => {
return {
extension: ext.slice(1),
isMixedContent: true,
scriptKind: ts.ScriptKind.TS,
};
}),
getServiceScript(root) {
return {
code: virtualCode,
extension: '.ts',
scriptKind: 3,
code: root,
extension: ts.Extension.Ts,
scriptKind: ts.ScriptKind.TS,
};
},
},
};

function isMatchFile(fileName: string, extensions: string[], exclude: string[], include: string[]): boolean {
if (!extensions.includes(path.extname(fileName))) return false;
return sys.readDirectory(path.dirname(fileName), extensions, exclude, include, 1).includes(fileName);
function isMatchFile(fileName: string): boolean {
if (!assetPluginOptions.extensions.includes(path.extname(fileName))) return false;
return sys
.readDirectory(
path.dirname(fileName),
assetPluginOptions.extensions,
assetPluginOptions.exclude,
assetPluginOptions.include,
1,
)
.includes(fileName);
}
}

0 comments on commit 0faf264

Please sign in to comment.