summaryrefslogtreecommitdiff
path: root/tools/astro-languageserver/src
diff options
context:
space:
mode:
Diffstat (limited to 'tools/astro-languageserver/src')
-rw-r--r--tools/astro-languageserver/src/index.ts2
-rw-r--r--tools/astro-languageserver/src/plugins/typescript/features/CompletionsProvider.ts13
-rw-r--r--tools/astro-languageserver/src/plugins/typescript/languageService.ts2
-rw-r--r--tools/astro-languageserver/src/plugins/typescript/module-loader.ts172
-rw-r--r--tools/astro-languageserver/src/plugins/typescript/utils.ts22
5 files changed, 95 insertions, 116 deletions
diff --git a/tools/astro-languageserver/src/index.ts b/tools/astro-languageserver/src/index.ts
index 41f04d11a..bc25f9475 100644
--- a/tools/astro-languageserver/src/index.ts
+++ b/tools/astro-languageserver/src/index.ts
@@ -71,7 +71,7 @@ export function startServer() {
connection.onDidCloseTextDocument((evt) => docManager.closeDocument(evt.textDocument.uri));
connection.onDidChangeTextDocument((evt) => {
- docManager.updateDocument(evt.textDocument.uri, evt.contentChanges)
+ docManager.updateDocument(evt.textDocument.uri, evt.contentChanges);
});
connection.onDidChangeWatchedFiles((evt) => {
diff --git a/tools/astro-languageserver/src/plugins/typescript/features/CompletionsProvider.ts b/tools/astro-languageserver/src/plugins/typescript/features/CompletionsProvider.ts
index e56902e6e..cb4826af2 100644
--- a/tools/astro-languageserver/src/plugins/typescript/features/CompletionsProvider.ts
+++ b/tools/astro-languageserver/src/plugins/typescript/features/CompletionsProvider.ts
@@ -26,11 +26,12 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
const fragment = await tsDoc.getFragment();
const offset = document.offsetAt(position);
- const entries = lang.getCompletionsAtPosition(fragment.filePath, offset, {
- importModuleSpecifierPreference: 'relative',
- importModuleSpecifierEnding: 'auto',
- quotePreference: 'single'
- })?.entries || [];
+ const entries =
+ lang.getCompletionsAtPosition(fragment.filePath, offset, {
+ importModuleSpecifierPreference: 'relative',
+ importModuleSpecifierEnding: 'auto',
+ quotePreference: 'single',
+ })?.entries || [];
const completionItems = entries
.map((entry: ts.CompletionEntry) => this.toCompletionItem(fragment, entry, document.uri, position, new Set()))
@@ -49,7 +50,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
return completionItem;
}
- if(filePath.endsWith('.astro')) {
+ if (filePath.endsWith('.astro')) {
filePath = filePath + '.ts';
}
diff --git a/tools/astro-languageserver/src/plugins/typescript/languageService.ts b/tools/astro-languageserver/src/plugins/typescript/languageService.ts
index ddd8cb3cd..8406b8ee0 100644
--- a/tools/astro-languageserver/src/plugins/typescript/languageService.ts
+++ b/tools/astro-languageserver/src/plugins/typescript/languageService.ts
@@ -72,7 +72,7 @@ async function createLanguageService(tsconfigPath: string, workspaceRoot: string
let projectVersion = 0;
const snapshotManager = new SnapshotManager(project.fileNames, { exclude: ['node_modules', 'dist'], include: ['astro'] }, workspaceRoot || process.cwd());
-
+
const astroModuleLoader = createAstroModuleLoader(getScriptSnapshot, {});
const host: ts.LanguageServiceHost = {
diff --git a/tools/astro-languageserver/src/plugins/typescript/module-loader.ts b/tools/astro-languageserver/src/plugins/typescript/module-loader.ts
index 6bed70ac3..2bcb206e7 100644
--- a/tools/astro-languageserver/src/plugins/typescript/module-loader.ts
+++ b/tools/astro-languageserver/src/plugins/typescript/module-loader.ts
@@ -1,50 +1,46 @@
import ts from 'typescript';
import type { DocumentSnapshot } from './SnapshotManager';
-import {
- isVirtualAstroFilePath,
- ensureRealAstroFilePath,
- getExtensionFromScriptKind
-} from './utils';
+import { isVirtualAstroFilePath, ensureRealAstroFilePath, getExtensionFromScriptKind } from './utils';
import { createAstroSys } from './astro-sys';
/**
* Caches resolved modules.
*/
class ModuleResolutionCache {
- private cache = new Map<string, ts.ResolvedModule>();
+ private cache = new Map<string, ts.ResolvedModule>();
- /**
- * Tries to get a cached module.
- */
- get(moduleName: string, containingFile: string): ts.ResolvedModule | undefined {
- return this.cache.get(this.getKey(moduleName, containingFile));
- }
+ /**
+ * Tries to get a cached module.
+ */
+ get(moduleName: string, containingFile: string): ts.ResolvedModule | undefined {
+ return this.cache.get(this.getKey(moduleName, containingFile));
+ }
- /**
- * Caches resolved module, if it is not undefined.
- */
- set(moduleName: string, containingFile: string, resolvedModule: ts.ResolvedModule | undefined) {
- if (!resolvedModule) {
- return;
- }
- this.cache.set(this.getKey(moduleName, containingFile), resolvedModule);
+ /**
+ * Caches resolved module, if it is not undefined.
+ */
+ set(moduleName: string, containingFile: string, resolvedModule: ts.ResolvedModule | undefined) {
+ if (!resolvedModule) {
+ return;
}
+ this.cache.set(this.getKey(moduleName, containingFile), resolvedModule);
+ }
- /**
- * Deletes module from cache. Call this if a file was deleted.
- * @param resolvedModuleName full path of the module
- */
- delete(resolvedModuleName: string): void {
- this.cache.forEach((val, key) => {
- if (val.resolvedFileName === resolvedModuleName) {
- this.cache.delete(key);
- }
- });
- }
+ /**
+ * Deletes module from cache. Call this if a file was deleted.
+ * @param resolvedModuleName full path of the module
+ */
+ delete(resolvedModuleName: string): void {
+ this.cache.forEach((val, key) => {
+ if (val.resolvedFileName === resolvedModuleName) {
+ this.cache.delete(key);
+ }
+ });
+ }
- private getKey(moduleName: string, containingFile: string) {
- return containingFile + ':::' + ensureRealAstroFilePath(moduleName);
- }
+ private getKey(moduleName: string, containingFile: string) {
+ return containingFile + ':::' + ensureRealAstroFilePath(moduleName);
+ }
}
/**
@@ -59,74 +55,56 @@ class ModuleResolutionCache {
* @param getSnapshot A function which returns a (in case of astro file fully preprocessed) typescript/javascript snapshot
* @param compilerOptions The typescript compiler options
*/
-export function createAstroModuleLoader(
- getSnapshot: (fileName: string) => DocumentSnapshot,
- compilerOptions: ts.CompilerOptions
-) {
- const astroSys = createAstroSys(getSnapshot);
- const moduleCache = new ModuleResolutionCache();
+export function createAstroModuleLoader(getSnapshot: (fileName: string) => DocumentSnapshot, compilerOptions: ts.CompilerOptions) {
+ const astroSys = createAstroSys(getSnapshot);
+ const moduleCache = new ModuleResolutionCache();
- return {
- fileExists: astroSys.fileExists,
- readFile: astroSys.readFile,
- writeFile: astroSys.writeFile,
- readDirectory: astroSys.readDirectory,
- directoryExists: astroSys.directoryExists,
- getDirectories: astroSys.getDirectories,
- realpath: astroSys.realpath,
- deleteFromModuleCache: (path: string) => moduleCache.delete(path),
- resolveModuleNames
- };
+ return {
+ fileExists: astroSys.fileExists,
+ readFile: astroSys.readFile,
+ writeFile: astroSys.writeFile,
+ readDirectory: astroSys.readDirectory,
+ directoryExists: astroSys.directoryExists,
+ getDirectories: astroSys.getDirectories,
+ realpath: astroSys.realpath,
+ deleteFromModuleCache: (path: string) => moduleCache.delete(path),
+ resolveModuleNames,
+ };
- function resolveModuleNames(
- moduleNames: string[],
- containingFile: string
- ): Array<ts.ResolvedModule | undefined> {
- return moduleNames.map((moduleName) => {
- const cachedModule = moduleCache.get(moduleName, containingFile);
- if (cachedModule) {
- return cachedModule;
- }
+ function resolveModuleNames(moduleNames: string[], containingFile: string): Array<ts.ResolvedModule | undefined> {
+ return moduleNames.map((moduleName) => {
+ const cachedModule = moduleCache.get(moduleName, containingFile);
+ if (cachedModule) {
+ return cachedModule;
+ }
- const resolvedModule = resolveModuleName(moduleName, containingFile);
- moduleCache.set(moduleName, containingFile, resolvedModule);
- return resolvedModule;
- });
- }
+ const resolvedModule = resolveModuleName(moduleName, containingFile);
+ moduleCache.set(moduleName, containingFile, resolvedModule);
+ return resolvedModule;
+ });
+ }
- function resolveModuleName(
- name: string,
- containingFile: string
- ): ts.ResolvedModule | undefined {
- // Delegate to the TS resolver first.
- // If that does not bring up anything, try the Astro Module loader
- // which is able to deal with .astro files.
- const tsResolvedModule = ts.resolveModuleName(name, containingFile, compilerOptions, ts.sys)
- .resolvedModule;
- if (tsResolvedModule && !isVirtualAstroFilePath(tsResolvedModule.resolvedFileName)) {
- return tsResolvedModule;
- }
+ function resolveModuleName(name: string, containingFile: string): ts.ResolvedModule | undefined {
+ // Delegate to the TS resolver first.
+ // If that does not bring up anything, try the Astro Module loader
+ // which is able to deal with .astro files.
+ const tsResolvedModule = ts.resolveModuleName(name, containingFile, compilerOptions, ts.sys).resolvedModule;
+ if (tsResolvedModule && !isVirtualAstroFilePath(tsResolvedModule.resolvedFileName)) {
+ return tsResolvedModule;
+ }
- const astroResolvedModule = ts.resolveModuleName(
- name,
- containingFile,
- compilerOptions,
- astroSys
- ).resolvedModule;
- if (
- !astroResolvedModule ||
- !isVirtualAstroFilePath(astroResolvedModule.resolvedFileName)
- ) {
- return astroResolvedModule;
- }
+ const astroResolvedModule = ts.resolveModuleName(name, containingFile, compilerOptions, astroSys).resolvedModule;
+ if (!astroResolvedModule || !isVirtualAstroFilePath(astroResolvedModule.resolvedFileName)) {
+ return astroResolvedModule;
+ }
- const resolvedFileName = ensureRealAstroFilePath(astroResolvedModule.resolvedFileName);
- const snapshot = getSnapshot(resolvedFileName);
+ const resolvedFileName = ensureRealAstroFilePath(astroResolvedModule.resolvedFileName);
+ const snapshot = getSnapshot(resolvedFileName);
- const resolvedastroModule: ts.ResolvedModuleFull = {
- extension: getExtensionFromScriptKind(snapshot && snapshot.scriptKind),
- resolvedFileName
- };
- return resolvedastroModule;
- }
+ const resolvedastroModule: ts.ResolvedModuleFull = {
+ extension: getExtensionFromScriptKind(snapshot && snapshot.scriptKind),
+ resolvedFileName,
+ };
+ return resolvedastroModule;
+ }
}
diff --git a/tools/astro-languageserver/src/plugins/typescript/utils.ts b/tools/astro-languageserver/src/plugins/typescript/utils.ts
index 3c43e56d5..da4e37c84 100644
--- a/tools/astro-languageserver/src/plugins/typescript/utils.ts
+++ b/tools/astro-languageserver/src/plugins/typescript/utils.ts
@@ -113,17 +113,17 @@ export function getScriptKindFromFileName(fileName: string): ts.ScriptKind {
export function getExtensionFromScriptKind(kind: ts.ScriptKind | undefined): ts.Extension {
switch (kind) {
- case ts.ScriptKind.JSX:
- return ts.Extension.Jsx;
- case ts.ScriptKind.TS:
- return ts.Extension.Ts;
- case ts.ScriptKind.TSX:
- return ts.Extension.Tsx;
- case ts.ScriptKind.JSON:
- return ts.Extension.Json;
- case ts.ScriptKind.JS:
- default:
- return ts.Extension.Js;
+ case ts.ScriptKind.JSX:
+ return ts.Extension.Jsx;
+ case ts.ScriptKind.TS:
+ return ts.Extension.Ts;
+ case ts.ScriptKind.TSX:
+ return ts.Extension.Tsx;
+ case ts.ScriptKind.JSON:
+ return ts.Extension.Json;
+ case ts.ScriptKind.JS:
+ default:
+ return ts.Extension.Js;
}
}