@astrojs/language-server 0.9.2 → 0.12.0

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.
Files changed (97) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/bin/browserServer.js +3 -0
  3. package/bin/nodeServer.js +3 -0
  4. package/dist/browser.d.ts +1 -0
  5. package/dist/browser.js +8 -0
  6. package/dist/check.js +2 -5
  7. package/dist/core/DiagnosticsManager.d.ts +3 -3
  8. package/dist/core/DiagnosticsManager.js +2 -2
  9. package/dist/core/config/ConfigManager.d.ts +19 -164
  10. package/dist/core/config/ConfigManager.js +62 -101
  11. package/dist/core/config/index.d.ts +1 -0
  12. package/dist/core/config/index.js +6 -1
  13. package/dist/core/config/interfaces.d.ts +123 -0
  14. package/dist/core/config/interfaces.js +2 -0
  15. package/dist/core/documents/AstroDocument.d.ts +18 -0
  16. package/dist/core/documents/AstroDocument.js +37 -0
  17. package/dist/core/documents/DocumentBase.d.ts +10 -2
  18. package/dist/core/documents/DocumentBase.js +15 -50
  19. package/dist/core/documents/DocumentManager.d.ts +12 -16
  20. package/dist/core/documents/DocumentManager.js +32 -26
  21. package/dist/core/documents/DocumentMapper.js +3 -1
  22. package/dist/core/documents/index.d.ts +1 -1
  23. package/dist/core/documents/index.js +6 -2
  24. package/dist/core/documents/parseAstro.d.ts +2 -2
  25. package/dist/core/documents/parseHtml.d.ts +2 -2
  26. package/dist/core/documents/parseHtml.js +4 -1
  27. package/dist/core/documents/utils.d.ts +22 -26
  28. package/dist/core/documents/utils.js +96 -134
  29. package/dist/index.d.ts +0 -1
  30. package/dist/index.js +1 -3
  31. package/dist/node.d.ts +1 -0
  32. package/dist/node.js +29 -0
  33. package/dist/plugins/PluginHost.d.ts +8 -9
  34. package/dist/plugins/PluginHost.js +22 -36
  35. package/dist/plugins/astro/AstroPlugin.d.ts +11 -12
  36. package/dist/plugins/astro/AstroPlugin.js +17 -38
  37. package/dist/plugins/astro/features/{CompletionProvider.d.ts → CompletionsProvider.d.ts} +5 -5
  38. package/dist/plugins/astro/features/{CompletionProvider.js → CompletionsProvider.js} +33 -54
  39. package/dist/plugins/css/CSSDocument.d.ts +3 -3
  40. package/dist/plugins/css/CSSDocument.js +7 -16
  41. package/dist/plugins/css/CSSPlugin.d.ts +19 -11
  42. package/dist/plugins/css/CSSPlugin.js +63 -41
  43. package/dist/plugins/css/StyleAttributeDocument.d.ts +2 -2
  44. package/dist/plugins/css/StyleAttributeDocument.js +2 -2
  45. package/dist/plugins/css/features/astro-selectors.d.ts +2 -0
  46. package/dist/plugins/css/features/astro-selectors.js +16 -0
  47. package/dist/plugins/css/features/{getIdClassCompletion.d.ts → getIdClassCompletions.d.ts} +3 -3
  48. package/dist/plugins/css/features/{getIdClassCompletion.js → getIdClassCompletions.js} +10 -9
  49. package/dist/plugins/css/{service.d.ts → language-service.d.ts} +0 -0
  50. package/dist/plugins/css/{service.js → language-service.js} +2 -1
  51. package/dist/plugins/html/HTMLPlugin.d.ts +15 -17
  52. package/dist/plugins/html/HTMLPlugin.js +33 -41
  53. package/dist/plugins/index.d.ts +2 -2
  54. package/dist/plugins/index.js +7 -3
  55. package/dist/plugins/interfaces.d.ts +35 -48
  56. package/dist/plugins/typescript/LanguageServiceManager.d.ts +33 -14
  57. package/dist/plugins/typescript/LanguageServiceManager.js +57 -32
  58. package/dist/plugins/typescript/TypeScriptPlugin.d.ts +16 -23
  59. package/dist/plugins/typescript/TypeScriptPlugin.js +48 -56
  60. package/dist/plugins/typescript/astro-sys.d.ts +5 -3
  61. package/dist/plugins/typescript/astro-sys.js +24 -41
  62. package/dist/plugins/typescript/features/CompletionsProvider.d.ts +6 -6
  63. package/dist/plugins/typescript/features/CompletionsProvider.js +17 -33
  64. package/dist/plugins/typescript/features/DiagnosticsProvider.d.ts +4 -4
  65. package/dist/plugins/typescript/features/DiagnosticsProvider.js +22 -61
  66. package/dist/plugins/typescript/features/HoverProvider.d.ts +4 -5
  67. package/dist/plugins/typescript/features/HoverProvider.js +9 -10
  68. package/dist/plugins/typescript/features/SignatureHelpProvider.d.ts +4 -5
  69. package/dist/plugins/typescript/features/SignatureHelpProvider.js +15 -11
  70. package/dist/plugins/typescript/features/utils.d.ts +1 -12
  71. package/dist/plugins/typescript/features/utils.js +2 -22
  72. package/dist/plugins/typescript/language-service.d.ts +38 -0
  73. package/dist/plugins/typescript/language-service.js +227 -0
  74. package/dist/plugins/typescript/module-loader.d.ts +5 -8
  75. package/dist/plugins/typescript/module-loader.js +43 -23
  76. package/dist/plugins/typescript/{DocumentSnapshot.d.ts → snapshots/DocumentSnapshot.d.ts} +45 -42
  77. package/dist/plugins/typescript/snapshots/DocumentSnapshot.js +135 -0
  78. package/dist/plugins/typescript/snapshots/SnapshotManager.d.ts +42 -0
  79. package/dist/plugins/typescript/snapshots/SnapshotManager.js +197 -0
  80. package/dist/plugins/typescript/snapshots/utils.d.ts +28 -0
  81. package/dist/plugins/typescript/snapshots/utils.js +84 -0
  82. package/dist/plugins/typescript/utils.d.ts +10 -11
  83. package/dist/plugins/typescript/utils.js +122 -151
  84. package/dist/server.d.ts +2 -4
  85. package/dist/server.js +88 -54
  86. package/dist/utils.d.ts +16 -8
  87. package/dist/utils.js +29 -14
  88. package/package.json +19 -13
  89. package/bin/server.js +0 -7
  90. package/dist/core/documents/Document.d.ts +0 -51
  91. package/dist/core/documents/Document.js +0 -135
  92. package/dist/plugins/typescript/DocumentSnapshot.js +0 -202
  93. package/dist/plugins/typescript/SnapshotManager.d.ts +0 -24
  94. package/dist/plugins/typescript/SnapshotManager.js +0 -97
  95. package/dist/plugins/typescript/languageService.d.ts +0 -17
  96. package/dist/plugins/typescript/languageService.js +0 -169
  97. package/types/index.d.ts +0 -4
@@ -1,26 +1,11 @@
1
- import * as ts from 'typescript';
2
- import { TextDocumentContentChangeEvent, Position, Range } from 'vscode-languageserver';
3
- import { Document, DocumentMapper } from '../../core/documents';
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
- getFragment(): Promise<DocumentFragmentSnapshot>;
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
- export declare const createDocumentSnapshot: (filePath: string, currentText: string | null, createDocument?: ((_filePath: string, text: string, overrideText: boolean) => Document) | undefined) => DocumentSnapshot;
44
- export declare class DocumentFragmentSnapshot implements Omit<DocumentSnapshot, 'getFragment' | 'destroyFragment'>, SnapshotFragment {
45
- private mapper;
46
- private parent;
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
- filePath: string;
49
- url: string;
50
- text: string;
51
- parserError: null;
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): import("vscode-html-languageservice").Position;
60
- getLineContainingOffset(offset: number): string;
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
- export declare class TypeScriptDocumentSnapshot implements DocumentSnapshot {
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
- scriptInfo: null;
73
- parserError: null;
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): import("vscode-html-languageservice").Position;
84
+ positionAt(offset: number): Position;
81
85
  offsetAt(position: Position): number;
82
- getFragment(): Promise<DocumentFragmentSnapshot>;
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;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createFromFrameworkFilePath = exports.createFromAstroFilePath = exports.createFromTSFilePath = exports.createFromNonAstroFilePath = exports.createFromFilePath = exports.createFromDocument = void 0;
7
+ const typescript_1 = __importDefault(require("typescript"));
8
+ const astro2tsx_1 = __importDefault(require("../astro2tsx"));
9
+ const utils_1 = require("../utils");
10
+ const DocumentSnapshot_1 = require("./DocumentSnapshot");
11
+ const svelte_language_integration_1 = require("@astrojs/svelte-language-integration");
12
+ // Utilities to create Snapshots from different contexts
13
+ function createFromDocument(document) {
14
+ const { code } = (0, astro2tsx_1.default)(document.getText());
15
+ return new DocumentSnapshot_1.AstroSnapshot(document, code, typescript_1.default.ScriptKind.TSX);
16
+ }
17
+ exports.createFromDocument = createFromDocument;
18
+ /**
19
+ * Returns an Astro or Framework or a ts/js snapshot from a file path, depending on the file contents.
20
+ * @param filePath path to the file
21
+ * @param createDocument function that is used to create a document in case it's an Astro file
22
+ */
23
+ function createFromFilePath(filePath, createDocument) {
24
+ if ((0, utils_1.isAstroFilePath)(filePath)) {
25
+ return createFromAstroFilePath(filePath, createDocument);
26
+ }
27
+ else if ((0, utils_1.isFrameworkFilePath)(filePath)) {
28
+ const framework = (0, utils_1.getFrameworkFromFilePath)(filePath);
29
+ return createFromFrameworkFilePath(filePath, framework);
30
+ }
31
+ else {
32
+ return createFromTSFilePath(filePath);
33
+ }
34
+ }
35
+ exports.createFromFilePath = createFromFilePath;
36
+ /**
37
+ * Return a Framework or a TS snapshot from a file path, depending on the file contents
38
+ * Unlike createFromFilePath, this does not support creating an Astro snapshot
39
+ */
40
+ function createFromNonAstroFilePath(filePath) {
41
+ if ((0, utils_1.isFrameworkFilePath)(filePath)) {
42
+ const framework = (0, utils_1.getFrameworkFromFilePath)(filePath);
43
+ return createFromFrameworkFilePath(filePath, framework);
44
+ }
45
+ else {
46
+ return createFromTSFilePath(filePath);
47
+ }
48
+ }
49
+ exports.createFromNonAstroFilePath = createFromNonAstroFilePath;
50
+ /**
51
+ * Returns a ts/js snapshot from a file path.
52
+ * @param filePath path to the js/ts file
53
+ * @param options options that apply in case it's a svelte file
54
+ */
55
+ function createFromTSFilePath(filePath) {
56
+ var _a;
57
+ const originalText = (_a = typescript_1.default.sys.readFile(filePath)) !== null && _a !== void 0 ? _a : '';
58
+ return new DocumentSnapshot_1.TypeScriptDocumentSnapshot(0, filePath, originalText);
59
+ }
60
+ exports.createFromTSFilePath = createFromTSFilePath;
61
+ /**
62
+ * Returns an Astro snapshot from a file path.
63
+ * @param filePath path to the Astro file
64
+ * @param createDocument function that is used to create a document
65
+ */
66
+ function createFromAstroFilePath(filePath, createDocument) {
67
+ var _a;
68
+ const originalText = (_a = typescript_1.default.sys.readFile(filePath)) !== null && _a !== void 0 ? _a : '';
69
+ return createFromDocument(createDocument(filePath, originalText));
70
+ }
71
+ exports.createFromAstroFilePath = createFromAstroFilePath;
72
+ function createFromFrameworkFilePath(filePath, framework) {
73
+ var _a;
74
+ const originalText = (_a = typescript_1.default.sys.readFile(filePath)) !== null && _a !== void 0 ? _a : '';
75
+ let code = '';
76
+ if (framework === 'svelte') {
77
+ code = (0, svelte_language_integration_1.toTSX)(originalText);
78
+ }
79
+ else {
80
+ code = 'export default function() {}';
81
+ }
82
+ return new DocumentSnapshot_1.TypeScriptDocumentSnapshot(0, filePath, code, typescript_1.default.ScriptKind.TSX);
83
+ }
84
+ exports.createFromFrameworkFilePath = createFromFrameworkFilePath;
@@ -1,11 +1,13 @@
1
- import * as ts from 'typescript';
1
+ import ts from 'typescript';
2
2
  import { CompletionItemKind, DiagnosticSeverity, Position, Range } from 'vscode-languageserver';
3
- import { SnapshotFragment } from './DocumentSnapshot';
3
+ import { SnapshotFragment } from './snapshots/DocumentSnapshot';
4
4
  export declare function scriptElementKindToCompletionItemKind(kind: ts.ScriptElementKind): CompletionItemKind;
5
5
  export declare function getCommitCharactersForScriptElement(kind: ts.ScriptElementKind): string[] | undefined;
6
- export declare function mapSeverity(category: ts.DiagnosticCategory): DiagnosticSeverity;
7
- export declare function getScriptKindFromFileName(fileName: string): ts.ScriptKind;
8
6
  export declare function getExtensionFromScriptKind(kind: ts.ScriptKind | undefined): ts.Extension;
7
+ export declare function findTsConfigPath(fileName: string, rootUris: string[]): string;
8
+ export declare function isSubPath(uri: string, possibleSubPath: string): boolean;
9
+ export declare function getScriptKindFromFileName(fileName: string): ts.ScriptKind;
10
+ export declare function mapSeverity(category: ts.DiagnosticCategory): DiagnosticSeverity;
9
11
  export declare function convertRange(document: {
10
12
  positionAt: (offset: number) => Position;
11
13
  }, range: {
@@ -13,22 +15,19 @@ export declare function convertRange(document: {
13
15
  length?: number;
14
16
  }): Range;
15
17
  export declare function convertToLocationRange(defDoc: SnapshotFragment, textSpan: ts.TextSpan): Range;
16
- declare type FrameworkExt = 'astro' | 'vue' | 'jsx' | 'tsx' | 'svelte';
18
+ export declare type FrameworkExt = 'astro' | 'vue' | 'jsx' | 'tsx' | 'svelte';
17
19
  declare type FrameworkVirtualExt = 'ts' | 'tsx';
20
+ export declare function getFrameworkFromFilePath(filePath: string): FrameworkExt;
18
21
  export declare function isVirtualFrameworkFilePath(ext: FrameworkExt, virtualExt: FrameworkVirtualExt, filePath: string): boolean;
19
22
  export declare function isAstroFilePath(filePath: string): boolean;
23
+ export declare function isFrameworkFilePath(filePath: string): boolean;
20
24
  export declare function isVirtualAstroFilePath(filePath: string): boolean;
21
25
  export declare function isVirtualVueFilePath(filePath: string): boolean;
22
- export declare function isVirtualJsxFilePath(filePath: string): boolean;
23
26
  export declare function isVirtualSvelteFilePath(filePath: string): boolean;
24
27
  export declare function isVirtualFilePath(filePath: string): boolean;
25
28
  export declare function toVirtualAstroFilePath(filePath: string): string;
29
+ export declare function toVirtualFilePath(filePath: string): string;
26
30
  export declare function toRealAstroFilePath(filePath: string): string;
27
31
  export declare function ensureRealAstroFilePath(filePath: string): string;
28
32
  export declare function ensureRealFilePath(filePath: string): string;
29
- export declare function findTsConfigPath(fileName: string, rootUris: string[]): string;
30
- /** */
31
- export declare function isSubPath(uri: string, possibleSubPath: string): boolean;
32
- /** Substitutes */
33
- export declare function substituteWithWhitespace(result: string, start: number, end: number, oldContent: string, before: string, after: string): string;
34
33
  export {};