@astrojs/language-server 0.28.3 → 0.29.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 (41) hide show
  1. package/dist/core/documents/DocumentMapper.d.ts +13 -7
  2. package/dist/core/documents/DocumentMapper.js +30 -12
  3. package/dist/core/worker/TSXService.d.ts +7 -0
  4. package/dist/core/worker/TSXService.js +12 -0
  5. package/dist/core/worker/TSXWorker.d.ts +1 -0
  6. package/dist/core/worker/TSXWorker.js +9 -0
  7. package/dist/plugins/astro/AstroPlugin.d.ts +3 -2
  8. package/dist/plugins/astro/AstroPlugin.js +5 -1
  9. package/dist/plugins/astro/features/CompletionsProvider.d.ts +2 -2
  10. package/dist/plugins/astro/features/DiagnosticsProvider.d.ts +10 -0
  11. package/dist/plugins/astro/features/DiagnosticsProvider.js +23 -0
  12. package/dist/plugins/typescript/TypeScriptPlugin.d.ts +2 -2
  13. package/dist/plugins/typescript/TypeScriptPlugin.js +8 -10
  14. package/dist/plugins/typescript/astro2tsx.d.ts +2 -4
  15. package/dist/plugins/typescript/astro2tsx.js +4 -91
  16. package/dist/plugins/typescript/features/CodeActionsProvider.js +18 -9
  17. package/dist/plugins/typescript/features/CompletionsProvider.d.ts +2 -2
  18. package/dist/plugins/typescript/features/CompletionsProvider.js +15 -15
  19. package/dist/plugins/typescript/features/DefinitionsProvider.d.ts +1 -1
  20. package/dist/plugins/typescript/features/DefinitionsProvider.js +13 -11
  21. package/dist/plugins/typescript/features/DiagnosticsProvider.d.ts +0 -1
  22. package/dist/plugins/typescript/features/DiagnosticsProvider.js +8 -24
  23. package/dist/plugins/typescript/features/DocumentSymbolsProvider.js +4 -7
  24. package/dist/plugins/typescript/features/FileReferencesProvider.js +4 -5
  25. package/dist/plugins/typescript/features/FoldingRangesProvider.js +6 -2
  26. package/dist/plugins/typescript/features/HoverProvider.js +4 -5
  27. package/dist/plugins/typescript/features/ImplementationsProvider.js +10 -8
  28. package/dist/plugins/typescript/features/InlayHintsProvider.js +3 -4
  29. package/dist/plugins/typescript/features/ReferencesProvider.js +9 -8
  30. package/dist/plugins/typescript/features/SemanticTokenProvider.js +8 -9
  31. package/dist/plugins/typescript/features/SignatureHelpProvider.js +1 -2
  32. package/dist/plugins/typescript/features/TypeDefinitionsProvider.js +7 -7
  33. package/dist/plugins/typescript/features/utils.d.ts +5 -16
  34. package/dist/plugins/typescript/features/utils.js +11 -18
  35. package/dist/plugins/typescript/snapshots/DocumentSnapshot.d.ts +30 -45
  36. package/dist/plugins/typescript/snapshots/DocumentSnapshot.js +50 -43
  37. package/dist/plugins/typescript/snapshots/SnapshotManager.js +0 -4
  38. package/dist/plugins/typescript/snapshots/utils.js +3 -2
  39. package/dist/plugins/typescript/utils.d.ts +2 -2
  40. package/dist/utils.js +3 -2
  41. package/package.json +5 -3
@@ -1,41 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findContainingNode = exports.SnapshotFragmentMap = exports.isPartOfImportStatement = void 0;
3
+ exports.findContainingNode = exports.SnapshotMap = exports.isPartOfImportStatement = void 0;
4
4
  const documents_1 = require("../../../core/documents");
5
5
  function isPartOfImportStatement(text, position) {
6
6
  const line = (0, documents_1.getLineAtPosition)(position, text);
7
7
  return /\s*from\s+["'][^"']*/.test(line.slice(0, position.character));
8
8
  }
9
9
  exports.isPartOfImportStatement = isPartOfImportStatement;
10
- class SnapshotFragmentMap {
10
+ class SnapshotMap {
11
11
  constructor(languageServiceManager) {
12
12
  this.languageServiceManager = languageServiceManager;
13
13
  this.map = new Map();
14
14
  }
15
- set(fileName, content) {
16
- this.map.set(fileName, content);
15
+ set(fileName, snapshot) {
16
+ this.map.set(fileName, snapshot);
17
17
  }
18
18
  get(fileName) {
19
19
  return this.map.get(fileName);
20
20
  }
21
- getFragment(fileName) {
22
- return this.map.get(fileName)?.fragment;
23
- }
24
21
  async retrieve(fileName) {
25
- let snapshotFragment = this.get(fileName);
26
- if (!snapshotFragment) {
27
- const snapshot = await this.languageServiceManager.getSnapshot(fileName);
28
- const fragment = await snapshot.createFragment();
29
- snapshotFragment = { fragment, snapshot };
30
- this.set(fileName, snapshotFragment);
22
+ let snapshot = this.get(fileName);
23
+ if (!snapshot) {
24
+ const snap = await this.languageServiceManager.getSnapshot(fileName);
25
+ this.set(fileName, snap);
26
+ snapshot = snap;
31
27
  }
32
- return snapshotFragment;
33
- }
34
- async retrieveFragment(fileName) {
35
- return (await this.retrieve(fileName)).fragment;
28
+ return snapshot;
36
29
  }
37
30
  }
38
- exports.SnapshotFragmentMap = SnapshotFragmentMap;
31
+ exports.SnapshotMap = SnapshotMap;
39
32
  function findContainingNode(node, textSpan, predicate) {
40
33
  const children = node.getChildren();
41
34
  const end = textSpan.start + textSpan.length;
@@ -1,70 +1,51 @@
1
- import type { Position, TextDocumentContentChangeEvent } from 'vscode-languageserver';
2
- import { AstroDocument, DocumentMapper, FragmentMapper, IdentityMapper, TagInformation } from '../../../core/documents';
3
- export interface DocumentSnapshot extends ts.IScriptSnapshot {
1
+ import type { DiagnosticMessage } from '@astrojs/compiler/shared/types';
2
+ import { EncodedSourceMap, TraceMap } from '@jridgewell/trace-mapping';
3
+ import { Position, TextDocumentContentChangeEvent } from 'vscode-languageserver';
4
+ import { AstroDocument, DocumentMapper, FragmentMapper, IdentityMapper, SourceMapDocumentMapper, TagInformation } from '../../../core/documents';
5
+ export interface DocumentSnapshot extends ts.IScriptSnapshot, DocumentMapper {
4
6
  version: number;
5
7
  filePath: string;
6
8
  scriptKind: ts.ScriptKind;
7
9
  positionAt(offset: number): Position;
8
- /**
9
- * Instantiates a source mapper.
10
- * `destroyFragment` needs to be called when
11
- * it's no longer needed / the class should be cleaned up
12
- * in order to prevent memory leaks.
13
- */
14
- createFragment(): Promise<SnapshotFragment>;
15
- /**
16
- * Needs to be called when source mapper
17
- * is no longer needed / the class should be cleaned up
18
- * in order to prevent memory leaks.
19
- */
20
- destroyFragment(): void;
10
+ offsetAt(position: Position): number;
21
11
  /**
22
12
  * Convenience function for getText(0, getLength())
23
13
  */
24
14
  getFullText(): string;
25
15
  }
26
- /**
27
- * The mapper to get from original snapshot positions to generated and vice versa.
28
- */
29
- export interface SnapshotFragment extends DocumentMapper {
30
- positionAt(offset: number): Position;
31
- offsetAt(position: Position): number;
32
- }
33
16
  /**
34
17
  * Snapshots used for Astro files
35
18
  */
36
19
  export declare class AstroSnapshot implements DocumentSnapshot {
37
20
  readonly parent: AstroDocument;
38
21
  private readonly text;
22
+ private readonly tsxMap;
39
23
  readonly scriptKind: ts.ScriptKind;
40
- private fragment?;
41
- version: number;
24
+ readonly compilerDiagnostics: DiagnosticMessage[];
25
+ private mapper?;
26
+ private lineOffsets?;
27
+ private url;
42
28
  scriptTagSnapshots: ScriptTagDocumentSnapshot[];
43
- constructor(parent: AstroDocument, text: string, scriptKind: ts.ScriptKind);
44
- createFragment(): Promise<AstroSnapshotFragment>;
45
- destroyFragment(): null;
29
+ version: number;
30
+ constructor(parent: AstroDocument, text: string, tsxMap: EncodedSourceMap, scriptKind: ts.ScriptKind, compilerDiagnostics: DiagnosticMessage[]);
31
+ isInGenerated(pos: Position): boolean;
32
+ getURL(): string;
46
33
  get filePath(): string;
47
34
  getText(start: number, end: number): string;
48
35
  getLength(): number;
49
36
  getFullText(): string;
50
37
  getChangeRange(): undefined;
51
38
  positionAt(offset: number): Position;
52
- }
53
- export declare class AstroSnapshotFragment implements SnapshotFragment {
54
- private readonly mapper;
55
- readonly parent: AstroDocument;
56
- readonly text: string;
57
- private readonly url;
58
- private lineOffsets;
59
- constructor(mapper: DocumentMapper, parent: AstroDocument, text: string, url: string);
60
- positionAt(offset: number): Position;
61
39
  offsetAt(position: Position): number;
62
40
  getOriginalPosition(pos: Position): Position;
63
41
  getGeneratedPosition(pos: Position): Position;
64
- isInGenerated(pos: Position): boolean;
65
- getURL(): string;
42
+ private getLineOffsets;
43
+ private getMapper;
66
44
  }
67
- export declare class ScriptTagDocumentSnapshot extends FragmentMapper implements DocumentSnapshot, SnapshotFragment {
45
+ /**
46
+ * Snapshots used for script tags inside Astro files
47
+ */
48
+ export declare class ScriptTagDocumentSnapshot extends FragmentMapper implements DocumentSnapshot {
68
49
  scriptTag: TagInformation;
69
50
  private readonly parent;
70
51
  filePath: string;
@@ -75,8 +56,6 @@ export declare class ScriptTagDocumentSnapshot extends FragmentMapper implements
75
56
  constructor(scriptTag: TagInformation, parent: AstroDocument, filePath: string, scriptKind: ts.ScriptKind);
76
57
  positionAt(offset: number): Position;
77
58
  offsetAt(position: Position): number;
78
- createFragment(): Promise<SnapshotFragment>;
79
- destroyFragment(): void;
80
59
  getText(start: number, end: number): string;
81
60
  getLength(): number;
82
61
  getFullText(): string;
@@ -87,7 +66,7 @@ export declare class ScriptTagDocumentSnapshot extends FragmentMapper implements
87
66
  * Snapshot used for anything that is not an Astro file
88
67
  * It's both used for .js(x)/.ts(x) files and .svelte/.vue files
89
68
  */
90
- export declare class TypeScriptDocumentSnapshot extends IdentityMapper implements DocumentSnapshot, SnapshotFragment {
69
+ export declare class TypeScriptDocumentSnapshot extends IdentityMapper implements DocumentSnapshot {
91
70
  version: number;
92
71
  readonly filePath: string;
93
72
  private text;
@@ -101,8 +80,14 @@ export declare class TypeScriptDocumentSnapshot extends IdentityMapper implement
101
80
  getChangeRange(): undefined;
102
81
  positionAt(offset: number): Position;
103
82
  offsetAt(position: Position): number;
104
- createFragment(): Promise<this>;
105
- destroyFragment(): void;
83
+ createFragment(): this;
106
84
  update(changes: TextDocumentContentChangeEvent[]): void;
107
85
  private getLineOffsets;
108
86
  }
87
+ export declare class ConsumerDocumentMapper extends SourceMapDocumentMapper {
88
+ private nrPrependesLines;
89
+ constructor(traceMap: TraceMap, sourceUri: string, nrPrependesLines: number);
90
+ getOriginalPosition(generatedPosition: Position): Position;
91
+ getGeneratedPosition(originalPosition: Position): Position;
92
+ isInGenerated(): boolean;
93
+ }
@@ -1,28 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypeScriptDocumentSnapshot = exports.ScriptTagDocumentSnapshot = exports.AstroSnapshotFragment = exports.AstroSnapshot = void 0;
3
+ exports.ConsumerDocumentMapper = exports.TypeScriptDocumentSnapshot = exports.ScriptTagDocumentSnapshot = exports.AstroSnapshot = void 0;
4
+ const trace_mapping_1 = require("@jridgewell/trace-mapping");
5
+ const vscode_languageserver_1 = require("vscode-languageserver");
4
6
  const documents_1 = require("../../../core/documents");
5
7
  const utils_1 = require("../../../utils");
6
8
  /**
7
9
  * Snapshots used for Astro files
8
10
  */
9
11
  class AstroSnapshot {
10
- constructor(parent, text, scriptKind) {
12
+ constructor(parent, text, tsxMap, scriptKind, compilerDiagnostics) {
11
13
  this.parent = parent;
12
14
  this.text = text;
15
+ this.tsxMap = tsxMap;
13
16
  this.scriptKind = scriptKind;
14
- this.version = this.parent.version;
17
+ this.compilerDiagnostics = compilerDiagnostics;
18
+ this.url = (0, utils_1.pathToUrl)(this.filePath);
15
19
  this.scriptTagSnapshots = [];
20
+ this.version = this.parent.version;
16
21
  }
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;
22
+ isInGenerated(pos) {
23
+ throw new Error('Method not implemented.');
23
24
  }
24
- destroyFragment() {
25
- return null;
25
+ getURL() {
26
+ return this.url;
26
27
  }
27
28
  get filePath() {
28
29
  return this.parent.getFilePath() || '';
@@ -40,38 +41,34 @@ class AstroSnapshot {
40
41
  return undefined;
41
42
  }
42
43
  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);
44
+ return (0, documents_1.positionAt)(offset, this.text, this.getLineOffsets());
57
45
  }
58
46
  offsetAt(position) {
59
- return (0, documents_1.offsetAt)(position, this.text, this.lineOffsets);
47
+ return (0, documents_1.offsetAt)(position, this.text, this.getLineOffsets());
60
48
  }
61
49
  getOriginalPosition(pos) {
62
- return this.mapper.getOriginalPosition(pos);
50
+ return this.getMapper().getOriginalPosition(pos);
63
51
  }
64
52
  getGeneratedPosition(pos) {
65
- return this.mapper.getGeneratedPosition(pos);
53
+ return this.getMapper().getGeneratedPosition(pos);
66
54
  }
67
- isInGenerated(pos) {
68
- throw new Error('Method not implemented.');
55
+ getLineOffsets() {
56
+ if (!this.lineOffsets) {
57
+ this.lineOffsets = (0, documents_1.getLineOffsets)(this.text);
58
+ }
59
+ return this.lineOffsets;
69
60
  }
70
- getURL() {
71
- return this.url;
61
+ getMapper() {
62
+ if (!this.mapper) {
63
+ this.mapper = new ConsumerDocumentMapper(new trace_mapping_1.TraceMap(this.tsxMap), this.url, 0);
64
+ }
65
+ return this.mapper;
72
66
  }
73
67
  }
74
- exports.AstroSnapshotFragment = AstroSnapshotFragment;
68
+ exports.AstroSnapshot = AstroSnapshot;
69
+ /**
70
+ * Snapshots used for script tags inside Astro files
71
+ */
75
72
  class ScriptTagDocumentSnapshot extends documents_1.FragmentMapper {
76
73
  constructor(scriptTag, parent, filePath, scriptKind) {
77
74
  super(parent.getText(), scriptTag, filePath);
@@ -88,12 +85,6 @@ class ScriptTagDocumentSnapshot extends documents_1.FragmentMapper {
88
85
  offsetAt(position) {
89
86
  return (0, documents_1.offsetAt)(position, this.text, this.getLineOffsets());
90
87
  }
91
- async createFragment() {
92
- return this;
93
- }
94
- destroyFragment() {
95
- //
96
- }
97
88
  getText(start, end) {
98
89
  return this.text.substring(start, end);
99
90
  }
@@ -145,12 +136,9 @@ class TypeScriptDocumentSnapshot extends documents_1.IdentityMapper {
145
136
  offsetAt(position) {
146
137
  return (0, documents_1.offsetAt)(position, this.text, this.getLineOffsets());
147
138
  }
148
- async createFragment() {
139
+ createFragment() {
149
140
  return this;
150
141
  }
151
- destroyFragment() {
152
- // nothing to clean up
153
- }
154
142
  update(changes) {
155
143
  for (const change of changes) {
156
144
  let start = 0;
@@ -175,3 +163,22 @@ class TypeScriptDocumentSnapshot extends documents_1.IdentityMapper {
175
163
  }
176
164
  }
177
165
  exports.TypeScriptDocumentSnapshot = TypeScriptDocumentSnapshot;
166
+ class ConsumerDocumentMapper extends documents_1.SourceMapDocumentMapper {
167
+ constructor(traceMap, sourceUri, nrPrependesLines) {
168
+ super(traceMap, sourceUri);
169
+ this.nrPrependesLines = nrPrependesLines;
170
+ }
171
+ getOriginalPosition(generatedPosition) {
172
+ return super.getOriginalPosition(vscode_languageserver_1.Position.create(generatedPosition.line - this.nrPrependesLines, generatedPosition.character));
173
+ }
174
+ getGeneratedPosition(originalPosition) {
175
+ const result = super.getGeneratedPosition(originalPosition);
176
+ result.line += this.nrPrependesLines;
177
+ return result;
178
+ }
179
+ isInGenerated() {
180
+ // always return true and map outliers case by case
181
+ return true;
182
+ }
183
+ }
184
+ exports.ConsumerDocumentMapper = ConsumerDocumentMapper;
@@ -45,10 +45,6 @@ class GlobalSnapshotManager {
45
45
  }
46
46
  set(fileName, document) {
47
47
  fileName = (0, utils_1.normalizePath)(fileName);
48
- const prev = this.get(fileName);
49
- if (prev) {
50
- prev.destroyFragment();
51
- }
52
48
  this.documents.set(fileName, document);
53
49
  this.emitter.emit('change', fileName, document);
54
50
  }
@@ -12,8 +12,9 @@ const utils_2 = require("../utils");
12
12
  const DocumentSnapshot_1 = require("./DocumentSnapshot");
13
13
  // Utilities to create Snapshots from different contexts
14
14
  function createFromDocument(document, ts) {
15
- const { code } = (0, astro2tsx_1.default)(document.getText(), classNameFromFilename(document.getURL()));
16
- return new DocumentSnapshot_1.AstroSnapshot(document, code, ts.ScriptKind.TSX);
15
+ const { code, map, diagnostics } = (0, astro2tsx_1.default)(document.getText(), document.getURL());
16
+ const sourceMap = map;
17
+ return new DocumentSnapshot_1.AstroSnapshot(document, code, sourceMap, ts.ScriptKind.TSX, diagnostics);
17
18
  }
18
19
  exports.createFromDocument = createFromDocument;
19
20
  /**
@@ -1,7 +1,7 @@
1
1
  import type { Node } from 'vscode-html-languageservice';
2
2
  import { CompletionItemKind, Position, Range, SemanticTokensLegend, SymbolKind } from 'vscode-languageserver';
3
3
  import { AstroDocument, TagInformation } from '../../core/documents';
4
- import type { AstroSnapshot, ScriptTagDocumentSnapshot, SnapshotFragment } from './snapshots/DocumentSnapshot';
4
+ import type { AstroSnapshot, DocumentSnapshot, ScriptTagDocumentSnapshot } from './snapshots/DocumentSnapshot';
5
5
  export declare const enum TokenType {
6
6
  class = 0,
7
7
  enum = 1,
@@ -38,7 +38,7 @@ export declare function convertRange(document: {
38
38
  start?: number;
39
39
  length?: number;
40
40
  }): Range;
41
- export declare function convertToLocationRange(defDoc: SnapshotFragment, textSpan: ts.TextSpan): Range;
41
+ export declare function convertToLocationRange(defDoc: DocumentSnapshot, textSpan: ts.TextSpan): Range;
42
42
  export declare function ensureFrontmatterInsert(resultRange: Range, document: AstroDocument): Range;
43
43
  export declare function checkEndOfFileCodeInsert(resultRange: Range, document: AstroDocument): Range;
44
44
  export declare function removeAstroComponentSuffix(name: string): string;
package/dist/utils.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAstroInstall = exports.isAstroWorkspace = exports.debounceThrottle = exports.debounceSameArg = exports.getRegExpMatches = exports.regexLastIndexOf = exports.isBeforeOrEqualToPosition = exports.isInRange = exports.isNotNullOrUndefined = exports.clamp = exports.modifyLines = exports.toPascalCase = exports.mergeDeep = exports.get = exports.getLastPartOfPath = exports.pathToUrl = exports.urlToPath = exports.normalizePath = exports.normalizeUri = void 0;
4
+ const path_1 = require("path");
4
5
  const vscode_uri_1 = require("vscode-uri");
5
6
  const importPackage_1 = require("./importPackage");
6
7
  /** Normalizes a document URI */
@@ -223,7 +224,7 @@ function getAstroInstall(basePaths) {
223
224
  if (!path) {
224
225
  throw Error;
225
226
  }
226
- version = require(path + '/package.json').version;
227
+ version = require((0, path_1.resolve)(path, 'package.json')).version;
227
228
  }
228
229
  catch {
229
230
  // If we couldn't find it inside the workspace's node_modules, it might means we're in the monorepo
@@ -232,7 +233,7 @@ function getAstroInstall(basePaths) {
232
233
  if (!path) {
233
234
  throw Error;
234
235
  }
235
- version = require(path + '/package.json').version;
236
+ version = require((0, path_1.resolve)(path, 'package.json')).version;
236
237
  }
237
238
  catch (e) {
238
239
  // If we still couldn't find it, it probably just doesn't exist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "0.28.3",
3
+ "version": "0.29.0",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -14,11 +14,13 @@
14
14
  "types"
15
15
  ],
16
16
  "dependencies": {
17
+ "@astrojs/compiler": "^0.29.15",
18
+ "@jridgewell/trace-mapping": "^0.3.14",
17
19
  "@vscode/emmet-helper": "^2.8.4",
18
20
  "events": "^3.3.0",
19
21
  "prettier": "^2.7.1",
20
22
  "prettier-plugin-astro": "^0.7.0",
21
- "source-map": "^0.7.3",
23
+ "synckit": "^0.7.2",
22
24
  "vscode-css-languageservice": "^6.0.1",
23
25
  "vscode-html-languageservice": "^5.0.0",
24
26
  "vscode-languageserver": "^8.0.1",
@@ -35,7 +37,7 @@
35
37
  "@types/node": "^16.11.58",
36
38
  "@types/prettier": "^2.7.0",
37
39
  "@types/sinon": "^10.0.11",
38
- "astro": "^1.1.3",
40
+ "astro": "^1.6.2",
39
41
  "astro-scripts": "*",
40
42
  "chai": "^4.3.6",
41
43
  "cross-env": "^7.0.3",