@astrojs/language-server 0.9.3 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/bin/browserServer.js +3 -0
- package/bin/nodeServer.js +3 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +8 -0
- package/dist/check.js +2 -5
- package/dist/core/DiagnosticsManager.d.ts +3 -3
- package/dist/core/DiagnosticsManager.js +2 -2
- package/dist/core/config/ConfigManager.d.ts +19 -164
- package/dist/core/config/ConfigManager.js +62 -101
- package/dist/core/config/index.d.ts +1 -0
- package/dist/core/config/index.js +6 -1
- package/dist/core/config/interfaces.d.ts +123 -0
- package/dist/core/config/interfaces.js +2 -0
- package/dist/core/documents/AstroDocument.d.ts +18 -0
- package/dist/core/documents/AstroDocument.js +37 -0
- package/dist/core/documents/DocumentBase.d.ts +10 -2
- package/dist/core/documents/DocumentBase.js +15 -50
- package/dist/core/documents/DocumentManager.d.ts +12 -16
- package/dist/core/documents/DocumentManager.js +32 -26
- package/dist/core/documents/DocumentMapper.js +3 -1
- package/dist/core/documents/index.d.ts +1 -1
- package/dist/core/documents/index.js +6 -2
- package/dist/core/documents/parseAstro.d.ts +2 -2
- package/dist/core/documents/parseHtml.d.ts +2 -2
- package/dist/core/documents/parseHtml.js +4 -1
- package/dist/core/documents/utils.d.ts +22 -26
- package/dist/core/documents/utils.js +96 -134
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -3
- package/dist/node.d.ts +1 -0
- package/dist/node.js +29 -0
- package/dist/plugins/PluginHost.d.ts +10 -9
- package/dist/plugins/PluginHost.js +30 -36
- package/dist/plugins/astro/AstroPlugin.d.ts +11 -12
- package/dist/plugins/astro/AstroPlugin.js +17 -38
- package/dist/plugins/astro/features/{CompletionProvider.d.ts → CompletionsProvider.d.ts} +5 -5
- package/dist/plugins/astro/features/{CompletionProvider.js → CompletionsProvider.js} +33 -54
- package/dist/plugins/css/CSSDocument.d.ts +3 -3
- package/dist/plugins/css/CSSDocument.js +7 -16
- package/dist/plugins/css/CSSPlugin.d.ts +22 -12
- package/dist/plugins/css/CSSPlugin.js +112 -41
- package/dist/plugins/css/StyleAttributeDocument.d.ts +2 -2
- package/dist/plugins/css/StyleAttributeDocument.js +2 -2
- package/dist/plugins/css/features/astro-selectors.d.ts +2 -0
- package/dist/plugins/css/features/astro-selectors.js +16 -0
- package/dist/plugins/css/features/{getIdClassCompletion.d.ts → getIdClassCompletions.d.ts} +3 -3
- package/dist/plugins/css/features/{getIdClassCompletion.js → getIdClassCompletions.js} +10 -9
- package/dist/plugins/css/{service.d.ts → language-service.d.ts} +0 -0
- package/dist/plugins/css/{service.js → language-service.js} +2 -1
- package/dist/plugins/html/HTMLPlugin.d.ts +15 -17
- package/dist/plugins/html/HTMLPlugin.js +33 -41
- package/dist/plugins/index.d.ts +2 -2
- package/dist/plugins/index.js +7 -3
- package/dist/plugins/interfaces.d.ts +35 -48
- package/dist/plugins/typescript/LanguageServiceManager.d.ts +33 -14
- package/dist/plugins/typescript/LanguageServiceManager.js +57 -32
- package/dist/plugins/typescript/TypeScriptPlugin.d.ts +16 -23
- package/dist/plugins/typescript/TypeScriptPlugin.js +48 -56
- package/dist/plugins/typescript/astro-sys.d.ts +5 -3
- package/dist/plugins/typescript/astro-sys.js +24 -41
- package/dist/plugins/typescript/features/CompletionsProvider.d.ts +6 -6
- package/dist/plugins/typescript/features/CompletionsProvider.js +17 -33
- package/dist/plugins/typescript/features/DiagnosticsProvider.d.ts +4 -4
- package/dist/plugins/typescript/features/DiagnosticsProvider.js +22 -61
- package/dist/plugins/typescript/features/HoverProvider.d.ts +4 -5
- package/dist/plugins/typescript/features/HoverProvider.js +9 -10
- package/dist/plugins/typescript/features/SignatureHelpProvider.d.ts +4 -5
- package/dist/plugins/typescript/features/SignatureHelpProvider.js +15 -11
- package/dist/plugins/typescript/features/utils.d.ts +1 -12
- package/dist/plugins/typescript/features/utils.js +2 -22
- package/dist/plugins/typescript/language-service.d.ts +38 -0
- package/dist/plugins/typescript/language-service.js +222 -0
- package/dist/plugins/typescript/module-loader.d.ts +5 -8
- package/dist/plugins/typescript/module-loader.js +43 -23
- package/dist/plugins/typescript/{DocumentSnapshot.d.ts → snapshots/DocumentSnapshot.d.ts} +45 -42
- package/dist/plugins/typescript/snapshots/DocumentSnapshot.js +135 -0
- package/dist/plugins/typescript/snapshots/SnapshotManager.d.ts +42 -0
- package/dist/plugins/typescript/snapshots/SnapshotManager.js +197 -0
- package/dist/plugins/typescript/snapshots/utils.d.ts +28 -0
- package/dist/plugins/typescript/snapshots/utils.js +84 -0
- package/dist/plugins/typescript/utils.d.ts +10 -11
- package/dist/plugins/typescript/utils.js +122 -151
- package/dist/server.d.ts +2 -4
- package/dist/server.js +91 -54
- package/dist/utils.d.ts +16 -8
- package/dist/utils.js +29 -14
- package/package.json +19 -13
- package/bin/server.js +0 -7
- package/dist/core/documents/Document.d.ts +0 -51
- package/dist/core/documents/Document.js +0 -135
- package/dist/plugins/typescript/DocumentSnapshot.js +0 -202
- package/dist/plugins/typescript/SnapshotManager.d.ts +0 -24
- package/dist/plugins/typescript/SnapshotManager.js +0 -97
- package/dist/plugins/typescript/languageService.d.ts +0 -17
- package/dist/plugins/typescript/languageService.js +0 -169
- package/types/index.d.ts +0 -4
|
@@ -7,6 +7,7 @@ exports.createAstroModuleLoader = void 0;
|
|
|
7
7
|
const typescript_1 = __importDefault(require("typescript"));
|
|
8
8
|
const utils_1 = require("./utils");
|
|
9
9
|
const astro_sys_1 = require("./astro-sys");
|
|
10
|
+
const utils_2 = require("../../utils");
|
|
10
11
|
/**
|
|
11
12
|
* Caches resolved modules.
|
|
12
13
|
*/
|
|
@@ -21,12 +22,15 @@ class ModuleResolutionCache {
|
|
|
21
22
|
return this.cache.get(this.getKey(moduleName, containingFile));
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
|
-
*
|
|
25
|
+
* Checks if has cached module.
|
|
26
|
+
*/
|
|
27
|
+
has(moduleName, containingFile) {
|
|
28
|
+
return this.cache.has(this.getKey(moduleName, containingFile));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Caches resolved module (or undefined).
|
|
25
32
|
*/
|
|
26
33
|
set(moduleName, containingFile, resolvedModule) {
|
|
27
|
-
if (!resolvedModule) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
34
|
this.cache.set(this.getKey(moduleName, containingFile), resolvedModule);
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
@@ -35,21 +39,34 @@ class ModuleResolutionCache {
|
|
|
35
39
|
*/
|
|
36
40
|
delete(resolvedModuleName) {
|
|
37
41
|
this.cache.forEach((val, key) => {
|
|
38
|
-
if (val.resolvedFileName === resolvedModuleName) {
|
|
42
|
+
if ((val === null || val === void 0 ? void 0 : val.resolvedFileName) === resolvedModuleName) {
|
|
43
|
+
this.cache.delete(key);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Deletes everything from cache that resolved to `undefined`
|
|
49
|
+
* and which might match the path.
|
|
50
|
+
*/
|
|
51
|
+
deleteUnresolvedResolutionsFromCache(path) {
|
|
52
|
+
const fileNameWithoutEnding = (0, utils_2.getLastPartOfPath)(path).split('.').shift() || '';
|
|
53
|
+
this.cache.forEach((val, key) => {
|
|
54
|
+
const moduleName = key.split(':::').pop() || '';
|
|
55
|
+
if (!val && moduleName.includes(fileNameWithoutEnding)) {
|
|
39
56
|
this.cache.delete(key);
|
|
40
57
|
}
|
|
41
58
|
});
|
|
42
59
|
}
|
|
43
60
|
getKey(moduleName, containingFile) {
|
|
44
|
-
return containingFile + ':::' + (0, utils_1.
|
|
61
|
+
return containingFile + ':::' + (0, utils_1.ensureRealFilePath)(moduleName);
|
|
45
62
|
}
|
|
46
63
|
}
|
|
47
64
|
/**
|
|
48
|
-
* Creates a module loader specifically for `.astro` files.
|
|
65
|
+
* Creates a module loader specifically for `.astro` and other frameworks files.
|
|
49
66
|
*
|
|
50
67
|
* The typescript language service tries to look up other files that are referenced in the currently open astro file.
|
|
51
|
-
* For `.ts`/`.js` files this works, for `.astro` files it does not by default.
|
|
52
|
-
* Reason: The typescript language service does not know about
|
|
68
|
+
* For `.ts`/`.js` files this works, for `.astro` and frameworks files it does not by default.
|
|
69
|
+
* Reason: The typescript language service does not know about those file endings,
|
|
53
70
|
* so it assumes it's a normal typescript file and searches for files like `../Component.astro.ts`, which is wrong.
|
|
54
71
|
* In order to fix this, we need to wrap typescript's module resolution and reroute all `.astro.ts` file lookups to .astro.
|
|
55
72
|
*
|
|
@@ -62,19 +79,21 @@ function createAstroModuleLoader(getSnapshot, compilerOptions) {
|
|
|
62
79
|
return {
|
|
63
80
|
fileExists: astroSys.fileExists,
|
|
64
81
|
readFile: astroSys.readFile,
|
|
65
|
-
writeFile: astroSys.writeFile,
|
|
66
82
|
readDirectory: astroSys.readDirectory,
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
deleteFromModuleCache: (path) => {
|
|
84
|
+
astroSys.deleteFromCache(path);
|
|
85
|
+
moduleCache.delete(path);
|
|
86
|
+
},
|
|
87
|
+
deleteUnresolvedResolutionsFromCache: (path) => {
|
|
88
|
+
astroSys.deleteFromCache(path);
|
|
89
|
+
moduleCache.deleteUnresolvedResolutionsFromCache(path);
|
|
90
|
+
},
|
|
71
91
|
resolveModuleNames,
|
|
72
92
|
};
|
|
73
93
|
function resolveModuleNames(moduleNames, containingFile) {
|
|
74
94
|
return moduleNames.map((moduleName) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return cachedModule;
|
|
95
|
+
if (moduleCache.has(moduleName, containingFile)) {
|
|
96
|
+
return moduleCache.get(moduleName, containingFile);
|
|
78
97
|
}
|
|
79
98
|
const resolvedModule = resolveModuleName(moduleName, containingFile);
|
|
80
99
|
moduleCache.set(moduleName, containingFile, resolvedModule);
|
|
@@ -84,22 +103,23 @@ function createAstroModuleLoader(getSnapshot, compilerOptions) {
|
|
|
84
103
|
function resolveModuleName(name, containingFile) {
|
|
85
104
|
// Delegate to the TS resolver first.
|
|
86
105
|
// If that does not bring up anything, try the Astro Module loader
|
|
87
|
-
// which is able to deal with .astro files.
|
|
106
|
+
// which is able to deal with .astro and other frameworks files.
|
|
88
107
|
const tsResolvedModule = typescript_1.default.resolveModuleName(name, containingFile, compilerOptions, typescript_1.default.sys).resolvedModule;
|
|
89
|
-
if (tsResolvedModule && !(0, utils_1.
|
|
108
|
+
if (tsResolvedModule && !(0, utils_1.isVirtualFilePath)(tsResolvedModule.resolvedFileName)) {
|
|
90
109
|
return tsResolvedModule;
|
|
91
110
|
}
|
|
92
111
|
const astroResolvedModule = typescript_1.default.resolveModuleName(name, containingFile, compilerOptions, astroSys).resolvedModule;
|
|
93
|
-
if (!astroResolvedModule || !(0, utils_1.
|
|
112
|
+
if (!astroResolvedModule || !(0, utils_1.isVirtualFilePath)(astroResolvedModule.resolvedFileName)) {
|
|
94
113
|
return astroResolvedModule;
|
|
95
114
|
}
|
|
96
|
-
const resolvedFileName = (0, utils_1.
|
|
115
|
+
const resolvedFileName = (0, utils_1.ensureRealFilePath)(astroResolvedModule.resolvedFileName);
|
|
97
116
|
const snapshot = getSnapshot(resolvedFileName);
|
|
98
|
-
const
|
|
117
|
+
const resolvedAstroModule = {
|
|
99
118
|
extension: (0, utils_1.getExtensionFromScriptKind)(snapshot && snapshot.scriptKind),
|
|
100
119
|
resolvedFileName,
|
|
120
|
+
isExternalLibraryImport: astroResolvedModule.isExternalLibraryImport,
|
|
101
121
|
};
|
|
102
|
-
return
|
|
122
|
+
return resolvedAstroModule;
|
|
103
123
|
}
|
|
104
124
|
}
|
|
105
125
|
exports.createAstroModuleLoader = createAstroModuleLoader;
|
|
@@ -1,26 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
* The mapper to get from original snapshot positions to generated and vice versa.
|
|
6
|
-
*/
|
|
7
|
-
export interface SnapshotFragment extends DocumentMapper {
|
|
8
|
-
positionAt(offset: number): Position;
|
|
9
|
-
offsetAt(position: Position): number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* An error which occured while trying to parse/preprocess the Astro file contents.
|
|
13
|
-
*/
|
|
14
|
-
export interface ParserError {
|
|
15
|
-
message: string;
|
|
16
|
-
range: Range;
|
|
17
|
-
code: number;
|
|
18
|
-
}
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { Position, TextDocumentContentChangeEvent } from 'vscode-languageserver';
|
|
3
|
+
import { AstroDocument, DocumentMapper, IdentityMapper } from '../../../core/documents';
|
|
4
|
+
import { FrameworkExt } from '../utils';
|
|
19
5
|
export interface DocumentSnapshot extends ts.IScriptSnapshot {
|
|
20
6
|
version: number;
|
|
21
7
|
filePath: string;
|
|
22
8
|
scriptKind: ts.ScriptKind;
|
|
23
|
-
parserError: ParserError | null;
|
|
24
9
|
positionAt(offset: number): Position;
|
|
25
10
|
/**
|
|
26
11
|
* Instantiates a source mapper.
|
|
@@ -28,7 +13,7 @@ export interface DocumentSnapshot extends ts.IScriptSnapshot {
|
|
|
28
13
|
* it's no longer needed / the class should be cleaned up
|
|
29
14
|
* in order to prevent memory leaks.
|
|
30
15
|
*/
|
|
31
|
-
|
|
16
|
+
createFragment(): Promise<SnapshotFragment>;
|
|
32
17
|
/**
|
|
33
18
|
* Needs to be called when source mapper
|
|
34
19
|
* is no longer needed / the class should be cleaned up
|
|
@@ -40,48 +25,66 @@ export interface DocumentSnapshot extends ts.IScriptSnapshot {
|
|
|
40
25
|
*/
|
|
41
26
|
getFullText(): string;
|
|
42
27
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The mapper to get from original snapshot positions to generated and vice versa.
|
|
30
|
+
*/
|
|
31
|
+
export interface SnapshotFragment extends DocumentMapper {
|
|
32
|
+
positionAt(offset: number): Position;
|
|
33
|
+
offsetAt(position: Position): number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Snapshots used for Astro files
|
|
37
|
+
*/
|
|
38
|
+
export declare class AstroSnapshot implements DocumentSnapshot {
|
|
39
|
+
private readonly parent;
|
|
40
|
+
private readonly text;
|
|
41
|
+
readonly scriptKind: ts.ScriptKind;
|
|
42
|
+
private fragment?;
|
|
47
43
|
version: number;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
scriptKind: ts.ScriptKind;
|
|
53
|
-
scriptInfo: null;
|
|
54
|
-
constructor(mapper: any, parent: Document);
|
|
44
|
+
constructor(parent: AstroDocument, text: string, scriptKind: ts.ScriptKind);
|
|
45
|
+
createFragment(): Promise<AstroSnapshotFragment>;
|
|
46
|
+
destroyFragment(): null;
|
|
47
|
+
get filePath(): string;
|
|
55
48
|
getText(start: number, end: number): string;
|
|
56
49
|
getLength(): number;
|
|
57
50
|
getFullText(): string;
|
|
58
51
|
getChangeRange(): undefined;
|
|
59
|
-
positionAt(offset: number):
|
|
60
|
-
|
|
52
|
+
positionAt(offset: number): Position;
|
|
53
|
+
}
|
|
54
|
+
export declare class AstroSnapshotFragment implements SnapshotFragment {
|
|
55
|
+
private readonly mapper;
|
|
56
|
+
readonly parent: AstroDocument;
|
|
57
|
+
readonly text: string;
|
|
58
|
+
private readonly url;
|
|
59
|
+
private lineOffsets;
|
|
60
|
+
constructor(mapper: DocumentMapper, parent: AstroDocument, text: string, url: string);
|
|
61
|
+
positionAt(offset: number): Position;
|
|
61
62
|
offsetAt(position: Position): number;
|
|
62
63
|
getOriginalPosition(pos: Position): Position;
|
|
63
64
|
getGeneratedPosition(pos: Position): Position;
|
|
64
65
|
isInGenerated(pos: Position): boolean;
|
|
65
66
|
getURL(): string;
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Snapshot used for anything that is not an Astro file
|
|
70
|
+
* It's both used for .js(x)/.ts(x) files and .svelte/.vue files
|
|
71
|
+
*/
|
|
72
|
+
export declare class TypeScriptDocumentSnapshot extends IdentityMapper implements DocumentSnapshot, SnapshotFragment {
|
|
68
73
|
version: number;
|
|
69
74
|
readonly filePath: string;
|
|
70
75
|
private text;
|
|
76
|
+
readonly framework?: FrameworkExt | undefined;
|
|
71
77
|
scriptKind: ts.ScriptKind;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
url: string;
|
|
75
|
-
constructor(version: number, filePath: string, text: string);
|
|
78
|
+
private lineOffsets?;
|
|
79
|
+
constructor(version: number, filePath: string, text: string, scriptKind?: ts.ScriptKind, framework?: FrameworkExt | undefined);
|
|
76
80
|
getText(start: number, end: number): string;
|
|
77
81
|
getLength(): number;
|
|
78
82
|
getFullText(): string;
|
|
79
83
|
getChangeRange(): undefined;
|
|
80
|
-
positionAt(offset: number):
|
|
84
|
+
positionAt(offset: number): Position;
|
|
81
85
|
offsetAt(position: Position): number;
|
|
82
|
-
|
|
83
|
-
getOriginalPosition(pos: Position): Position;
|
|
86
|
+
createFragment(): Promise<this>;
|
|
84
87
|
destroyFragment(): void;
|
|
85
|
-
getLineContainingOffset(offset: number): string;
|
|
86
88
|
update(changes: TextDocumentContentChangeEvent[]): void;
|
|
89
|
+
private getLineOffsets;
|
|
87
90
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypeScriptDocumentSnapshot = exports.AstroSnapshotFragment = exports.AstroSnapshot = void 0;
|
|
4
|
+
const documents_1 = require("../../../core/documents");
|
|
5
|
+
const utils_1 = require("../../../utils");
|
|
6
|
+
const utils_2 = require("../utils");
|
|
7
|
+
/**
|
|
8
|
+
* Snapshots used for Astro files
|
|
9
|
+
*/
|
|
10
|
+
class AstroSnapshot {
|
|
11
|
+
constructor(parent, text, scriptKind) {
|
|
12
|
+
this.parent = parent;
|
|
13
|
+
this.text = text;
|
|
14
|
+
this.scriptKind = scriptKind;
|
|
15
|
+
this.version = this.parent.version;
|
|
16
|
+
}
|
|
17
|
+
async createFragment() {
|
|
18
|
+
if (!this.fragment) {
|
|
19
|
+
const uri = (0, utils_1.pathToUrl)(this.filePath);
|
|
20
|
+
this.fragment = new AstroSnapshotFragment(new documents_1.IdentityMapper(uri), this.parent, this.text, uri);
|
|
21
|
+
}
|
|
22
|
+
return this.fragment;
|
|
23
|
+
}
|
|
24
|
+
destroyFragment() {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
get filePath() {
|
|
28
|
+
return this.parent.getFilePath() || '';
|
|
29
|
+
}
|
|
30
|
+
getText(start, end) {
|
|
31
|
+
return this.text.substring(start, end);
|
|
32
|
+
}
|
|
33
|
+
getLength() {
|
|
34
|
+
return this.text.length;
|
|
35
|
+
}
|
|
36
|
+
getFullText() {
|
|
37
|
+
return this.text;
|
|
38
|
+
}
|
|
39
|
+
getChangeRange() {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
positionAt(offset) {
|
|
43
|
+
return (0, documents_1.positionAt)(offset, this.text);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.AstroSnapshot = AstroSnapshot;
|
|
47
|
+
class AstroSnapshotFragment {
|
|
48
|
+
constructor(mapper, parent, text, url) {
|
|
49
|
+
this.mapper = mapper;
|
|
50
|
+
this.parent = parent;
|
|
51
|
+
this.text = text;
|
|
52
|
+
this.url = url;
|
|
53
|
+
this.lineOffsets = (0, documents_1.getLineOffsets)(this.text);
|
|
54
|
+
}
|
|
55
|
+
positionAt(offset) {
|
|
56
|
+
return (0, documents_1.positionAt)(offset, this.text, this.lineOffsets);
|
|
57
|
+
}
|
|
58
|
+
offsetAt(position) {
|
|
59
|
+
return (0, documents_1.offsetAt)(position, this.text, this.lineOffsets);
|
|
60
|
+
}
|
|
61
|
+
getOriginalPosition(pos) {
|
|
62
|
+
return this.mapper.getOriginalPosition(pos);
|
|
63
|
+
}
|
|
64
|
+
getGeneratedPosition(pos) {
|
|
65
|
+
return this.mapper.getGeneratedPosition(pos);
|
|
66
|
+
}
|
|
67
|
+
isInGenerated(pos) {
|
|
68
|
+
throw new Error('Method not implemented.');
|
|
69
|
+
}
|
|
70
|
+
getURL() {
|
|
71
|
+
return this.url;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.AstroSnapshotFragment = AstroSnapshotFragment;
|
|
75
|
+
/**
|
|
76
|
+
* Snapshot used for anything that is not an Astro file
|
|
77
|
+
* It's both used for .js(x)/.ts(x) files and .svelte/.vue files
|
|
78
|
+
*/
|
|
79
|
+
class TypeScriptDocumentSnapshot extends documents_1.IdentityMapper {
|
|
80
|
+
constructor(version, filePath, text, scriptKind, framework) {
|
|
81
|
+
super((0, utils_1.pathToUrl)(filePath));
|
|
82
|
+
this.version = version;
|
|
83
|
+
this.filePath = filePath;
|
|
84
|
+
this.text = text;
|
|
85
|
+
this.framework = framework;
|
|
86
|
+
scriptKind ? (this.scriptKind = scriptKind) : (this.scriptKind = (0, utils_2.getScriptKindFromFileName)(filePath));
|
|
87
|
+
}
|
|
88
|
+
getText(start, end) {
|
|
89
|
+
return this.text.substring(start, end);
|
|
90
|
+
}
|
|
91
|
+
getLength() {
|
|
92
|
+
return this.text.length;
|
|
93
|
+
}
|
|
94
|
+
getFullText() {
|
|
95
|
+
return this.text;
|
|
96
|
+
}
|
|
97
|
+
getChangeRange() {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
positionAt(offset) {
|
|
101
|
+
return (0, documents_1.positionAt)(offset, this.text, this.getLineOffsets());
|
|
102
|
+
}
|
|
103
|
+
offsetAt(position) {
|
|
104
|
+
return (0, documents_1.offsetAt)(position, this.text, this.getLineOffsets());
|
|
105
|
+
}
|
|
106
|
+
async createFragment() {
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
destroyFragment() {
|
|
110
|
+
// nothing to clean up
|
|
111
|
+
}
|
|
112
|
+
update(changes) {
|
|
113
|
+
for (const change of changes) {
|
|
114
|
+
let start = 0;
|
|
115
|
+
let end = 0;
|
|
116
|
+
if ('range' in change) {
|
|
117
|
+
start = this.offsetAt(change.range.start);
|
|
118
|
+
end = this.offsetAt(change.range.end);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
end = this.getLength();
|
|
122
|
+
}
|
|
123
|
+
this.text = this.text.slice(0, start) + change.text + this.text.slice(end);
|
|
124
|
+
}
|
|
125
|
+
this.version++;
|
|
126
|
+
this.lineOffsets = undefined;
|
|
127
|
+
}
|
|
128
|
+
getLineOffsets() {
|
|
129
|
+
if (!this.lineOffsets) {
|
|
130
|
+
this.lineOffsets = (0, documents_1.getLineOffsets)(this.text);
|
|
131
|
+
}
|
|
132
|
+
return this.lineOffsets;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
exports.TypeScriptDocumentSnapshot = TypeScriptDocumentSnapshot;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { DocumentSnapshot, TypeScriptDocumentSnapshot } from './DocumentSnapshot';
|
|
2
|
+
import { TextDocumentContentChangeEvent } from 'vscode-languageserver';
|
|
3
|
+
/**
|
|
4
|
+
* Every snapshot corresponds to a unique file on disk.
|
|
5
|
+
* A snapshot can be part of multiple projects, but for a given file path
|
|
6
|
+
* there can be only one snapshot.
|
|
7
|
+
*/
|
|
8
|
+
export declare class GlobalSnapshotManager {
|
|
9
|
+
private emitter;
|
|
10
|
+
private documents;
|
|
11
|
+
get(fileName: string): DocumentSnapshot | undefined;
|
|
12
|
+
set(fileName: string, document: DocumentSnapshot): void;
|
|
13
|
+
delete(fileName: string): void;
|
|
14
|
+
updateNonAstroFile(fileName: string, changes?: TextDocumentContentChangeEvent[]): TypeScriptDocumentSnapshot | undefined;
|
|
15
|
+
onChange(listener: (fileName: string, newDocument: DocumentSnapshot | undefined) => void): void;
|
|
16
|
+
}
|
|
17
|
+
export interface TsFilesSpec {
|
|
18
|
+
include?: readonly string[];
|
|
19
|
+
exclude?: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Should only be used by `language-service.ts`
|
|
23
|
+
*/
|
|
24
|
+
export declare class SnapshotManager {
|
|
25
|
+
private globalSnapshotsManager;
|
|
26
|
+
private projectFiles;
|
|
27
|
+
private fileSpec;
|
|
28
|
+
private workspaceRoot;
|
|
29
|
+
private documents;
|
|
30
|
+
private lastLogged;
|
|
31
|
+
private readonly watchExtensions;
|
|
32
|
+
constructor(globalSnapshotsManager: GlobalSnapshotManager, projectFiles: string[], fileSpec: TsFilesSpec, workspaceRoot: string);
|
|
33
|
+
updateProjectFiles(): void;
|
|
34
|
+
updateNonAstroFile(fileName: string, changes?: TextDocumentContentChangeEvent[]): void;
|
|
35
|
+
has(fileName: string): boolean;
|
|
36
|
+
set(fileName: string, snapshot: DocumentSnapshot): void;
|
|
37
|
+
get(fileName: string): DocumentSnapshot | undefined;
|
|
38
|
+
delete(fileName: string): void;
|
|
39
|
+
getFileNames(): string[];
|
|
40
|
+
getProjectFileNames(): string[];
|
|
41
|
+
private logStatistics;
|
|
42
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.SnapshotManager = exports.GlobalSnapshotManager = void 0;
|
|
30
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
31
|
+
const DocumentSnapshot_1 = require("./DocumentSnapshot");
|
|
32
|
+
const utils_1 = require("../../../utils");
|
|
33
|
+
const events_1 = require("events");
|
|
34
|
+
const DocumentSnapshotUtils = __importStar(require("./utils"));
|
|
35
|
+
const utils_2 = require("../utils");
|
|
36
|
+
/**
|
|
37
|
+
* Every snapshot corresponds to a unique file on disk.
|
|
38
|
+
* A snapshot can be part of multiple projects, but for a given file path
|
|
39
|
+
* there can be only one snapshot.
|
|
40
|
+
*/
|
|
41
|
+
class GlobalSnapshotManager {
|
|
42
|
+
constructor() {
|
|
43
|
+
this.emitter = new events_1.EventEmitter();
|
|
44
|
+
this.documents = new Map();
|
|
45
|
+
}
|
|
46
|
+
get(fileName) {
|
|
47
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
48
|
+
return this.documents.get(fileName);
|
|
49
|
+
}
|
|
50
|
+
set(fileName, document) {
|
|
51
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
52
|
+
const prev = this.get(fileName);
|
|
53
|
+
if (prev) {
|
|
54
|
+
prev.destroyFragment();
|
|
55
|
+
}
|
|
56
|
+
this.documents.set(fileName, document);
|
|
57
|
+
this.emitter.emit('change', fileName, document);
|
|
58
|
+
}
|
|
59
|
+
delete(fileName) {
|
|
60
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
61
|
+
this.documents.delete(fileName);
|
|
62
|
+
this.emitter.emit('change', fileName, undefined);
|
|
63
|
+
}
|
|
64
|
+
updateNonAstroFile(fileName, changes) {
|
|
65
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
66
|
+
const previousSnapshot = this.get(fileName);
|
|
67
|
+
if (changes) {
|
|
68
|
+
// We don't support incremental changes for Framework files, as they need to be rebuilt completely on every change
|
|
69
|
+
if (!(previousSnapshot instanceof DocumentSnapshot_1.TypeScriptDocumentSnapshot) || previousSnapshot.framework) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
previousSnapshot.update(changes);
|
|
73
|
+
this.emitter.emit('change', fileName, previousSnapshot);
|
|
74
|
+
return previousSnapshot;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const newSnapshot = DocumentSnapshotUtils.createFromNonAstroFilePath(fileName);
|
|
78
|
+
if (previousSnapshot) {
|
|
79
|
+
newSnapshot.version = previousSnapshot.version + 1;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// ensure it's greater than initial version
|
|
83
|
+
// so that ts server picks up the change
|
|
84
|
+
newSnapshot.version += 1;
|
|
85
|
+
}
|
|
86
|
+
this.set(fileName, newSnapshot);
|
|
87
|
+
return newSnapshot;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
onChange(listener) {
|
|
91
|
+
this.emitter.on('change', listener);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.GlobalSnapshotManager = GlobalSnapshotManager;
|
|
95
|
+
/**
|
|
96
|
+
* Should only be used by `language-service.ts`
|
|
97
|
+
*/
|
|
98
|
+
class SnapshotManager {
|
|
99
|
+
constructor(globalSnapshotsManager, projectFiles, fileSpec, workspaceRoot) {
|
|
100
|
+
this.globalSnapshotsManager = globalSnapshotsManager;
|
|
101
|
+
this.projectFiles = projectFiles;
|
|
102
|
+
this.fileSpec = fileSpec;
|
|
103
|
+
this.workspaceRoot = workspaceRoot;
|
|
104
|
+
this.documents = new Map();
|
|
105
|
+
this.lastLogged = new Date(new Date().getTime() - 60001);
|
|
106
|
+
this.watchExtensions = [
|
|
107
|
+
typescript_1.default.Extension.Dts,
|
|
108
|
+
typescript_1.default.Extension.Js,
|
|
109
|
+
typescript_1.default.Extension.Jsx,
|
|
110
|
+
typescript_1.default.Extension.Ts,
|
|
111
|
+
typescript_1.default.Extension.Tsx,
|
|
112
|
+
typescript_1.default.Extension.Json,
|
|
113
|
+
];
|
|
114
|
+
this.globalSnapshotsManager.onChange((fileName, document) => {
|
|
115
|
+
// Only delete/update snapshots, don't add new ones,
|
|
116
|
+
// as they could be from another TS service and this
|
|
117
|
+
// snapshot manager can't reach this file.
|
|
118
|
+
// For these, instead wait on a `get` method invocation
|
|
119
|
+
// and set them "manually" in the set/update methods.
|
|
120
|
+
if (!document) {
|
|
121
|
+
this.documents.delete(fileName);
|
|
122
|
+
}
|
|
123
|
+
else if (this.documents.has(fileName)) {
|
|
124
|
+
this.documents.set(fileName, document);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
updateProjectFiles() {
|
|
129
|
+
const { include, exclude } = this.fileSpec;
|
|
130
|
+
// Since we default to not include anything,
|
|
131
|
+
// just don't waste time on this
|
|
132
|
+
if ((include === null || include === void 0 ? void 0 : include.length) === 0) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const projectFiles = typescript_1.default.sys
|
|
136
|
+
.readDirectory(this.workspaceRoot, this.watchExtensions, exclude, include)
|
|
137
|
+
.map(utils_1.normalizePath);
|
|
138
|
+
this.projectFiles = Array.from(new Set([...this.projectFiles, ...projectFiles]));
|
|
139
|
+
}
|
|
140
|
+
updateNonAstroFile(fileName, changes) {
|
|
141
|
+
const snapshot = this.globalSnapshotsManager.updateNonAstroFile(fileName, changes);
|
|
142
|
+
// This isn't duplicated logic to the listener, because this could
|
|
143
|
+
// be a new snapshot which the listener wouldn't add.
|
|
144
|
+
if (snapshot) {
|
|
145
|
+
this.documents.set((0, utils_1.normalizePath)(fileName), snapshot);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
has(fileName) {
|
|
149
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
150
|
+
return this.projectFiles.includes(fileName) || this.getFileNames().includes(fileName);
|
|
151
|
+
}
|
|
152
|
+
set(fileName, snapshot) {
|
|
153
|
+
this.globalSnapshotsManager.set(fileName, snapshot);
|
|
154
|
+
// This isn't duplicated logic to the listener, because this could
|
|
155
|
+
// be a new snapshot which the listener wouldn't add.
|
|
156
|
+
this.documents.set((0, utils_1.normalizePath)(fileName), snapshot);
|
|
157
|
+
}
|
|
158
|
+
get(fileName) {
|
|
159
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
160
|
+
let snapshot = this.documents.get(fileName);
|
|
161
|
+
if (!snapshot) {
|
|
162
|
+
snapshot = this.globalSnapshotsManager.get(fileName);
|
|
163
|
+
if (snapshot) {
|
|
164
|
+
this.documents.set(fileName, snapshot);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return snapshot;
|
|
168
|
+
}
|
|
169
|
+
delete(fileName) {
|
|
170
|
+
fileName = (0, utils_1.normalizePath)(fileName);
|
|
171
|
+
this.projectFiles = this.projectFiles.filter((s) => s !== fileName);
|
|
172
|
+
this.globalSnapshotsManager.delete(fileName);
|
|
173
|
+
}
|
|
174
|
+
getFileNames() {
|
|
175
|
+
return Array.from(this.documents.keys()).map((fileName) => (0, utils_2.toVirtualFilePath)(fileName));
|
|
176
|
+
}
|
|
177
|
+
getProjectFileNames() {
|
|
178
|
+
return this.projectFiles.map((file) => {
|
|
179
|
+
return (0, utils_2.toVirtualFilePath)(file);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
logStatistics() {
|
|
183
|
+
const date = new Date();
|
|
184
|
+
// Don't use setInterval because that will keep tests running forever
|
|
185
|
+
if (date.getTime() - this.lastLogged.getTime() > 60000) {
|
|
186
|
+
this.lastLogged = date;
|
|
187
|
+
const projectFiles = this.getProjectFileNames();
|
|
188
|
+
const allFiles = Array.from(new Set([...projectFiles, ...this.getFileNames()]));
|
|
189
|
+
console.log('SnapshotManager File Statistics:\n' +
|
|
190
|
+
`Project files: ${projectFiles.length}\n` +
|
|
191
|
+
`Astro files: ${allFiles.filter((name) => name.endsWith('.astro')).length}\n` +
|
|
192
|
+
`From node_modules: ${allFiles.filter((name) => name.includes('node_modules')).length}\n` +
|
|
193
|
+
`Total: ${allFiles.length}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.SnapshotManager = SnapshotManager;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AstroDocument } from '../../../core/documents';
|
|
2
|
+
import { FrameworkExt } from '../utils';
|
|
3
|
+
import { AstroSnapshot, TypeScriptDocumentSnapshot } from './DocumentSnapshot';
|
|
4
|
+
export declare function createFromDocument(document: AstroDocument): AstroSnapshot;
|
|
5
|
+
/**
|
|
6
|
+
* Returns an Astro or Framework or a ts/js snapshot from a file path, depending on the file contents.
|
|
7
|
+
* @param filePath path to the file
|
|
8
|
+
* @param createDocument function that is used to create a document in case it's an Astro file
|
|
9
|
+
*/
|
|
10
|
+
export declare function createFromFilePath(filePath: string, createDocument: (filePath: string, text: string) => AstroDocument): AstroSnapshot | TypeScriptDocumentSnapshot;
|
|
11
|
+
/**
|
|
12
|
+
* Return a Framework or a TS snapshot from a file path, depending on the file contents
|
|
13
|
+
* Unlike createFromFilePath, this does not support creating an Astro snapshot
|
|
14
|
+
*/
|
|
15
|
+
export declare function createFromNonAstroFilePath(filePath: string): TypeScriptDocumentSnapshot;
|
|
16
|
+
/**
|
|
17
|
+
* Returns a ts/js snapshot from a file path.
|
|
18
|
+
* @param filePath path to the js/ts file
|
|
19
|
+
* @param options options that apply in case it's a svelte file
|
|
20
|
+
*/
|
|
21
|
+
export declare function createFromTSFilePath(filePath: string): TypeScriptDocumentSnapshot;
|
|
22
|
+
/**
|
|
23
|
+
* Returns an Astro snapshot from a file path.
|
|
24
|
+
* @param filePath path to the Astro file
|
|
25
|
+
* @param createDocument function that is used to create a document
|
|
26
|
+
*/
|
|
27
|
+
export declare function createFromAstroFilePath(filePath: string, createDocument: (filePath: string, text: string) => AstroDocument): AstroSnapshot;
|
|
28
|
+
export declare function createFromFrameworkFilePath(filePath: string, framework: FrameworkExt): TypeScriptDocumentSnapshot;
|