@astrojs/language-server 1.0.4 → 2.0.0-next.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 (163) hide show
  1. package/LICENSE +3 -5
  2. package/bin/nodeServer.js +7 -3
  3. package/dist/core/astro2tsx.d.ts +6 -0
  4. package/dist/core/astro2tsx.js +119 -0
  5. package/dist/core/compilerUtils.d.ts +27 -0
  6. package/dist/core/compilerUtils.js +52 -0
  7. package/dist/core/index.d.ts +27 -0
  8. package/dist/core/index.js +119 -0
  9. package/dist/core/parseAstro.d.ts +27 -0
  10. package/dist/core/parseAstro.js +47 -0
  11. package/dist/core/parseCSS.d.ts +7 -0
  12. package/dist/core/parseCSS.js +116 -0
  13. package/dist/core/parseHTML.d.ts +7 -0
  14. package/dist/core/parseHTML.js +113 -0
  15. package/dist/core/parseJS.d.ts +7 -0
  16. package/dist/core/parseJS.js +236 -0
  17. package/dist/core/svelte.d.ts +18 -0
  18. package/dist/core/svelte.js +45 -0
  19. package/dist/core/utils.d.ts +14 -0
  20. package/dist/core/utils.js +138 -0
  21. package/dist/core/vue.d.ts +18 -0
  22. package/dist/core/vue.js +45 -0
  23. package/dist/importPackage.d.ts +1 -1
  24. package/dist/importPackage.js +4 -3
  25. package/dist/index.d.ts +1 -2
  26. package/dist/index.js +26 -6
  27. package/dist/languageServerPlugin.d.ts +2 -0
  28. package/dist/languageServerPlugin.js +76 -0
  29. package/dist/nodeServer.js +6 -0
  30. package/dist/plugins/astro.d.ts +3 -0
  31. package/dist/plugins/astro.js +123 -0
  32. package/dist/plugins/{html/features/astro-attributes.js → html-data.js} +1 -0
  33. package/dist/plugins/html.d.ts +3 -0
  34. package/dist/plugins/html.js +33 -0
  35. package/dist/plugins/typescript/codeActions.d.ts +4 -0
  36. package/dist/plugins/typescript/codeActions.js +49 -0
  37. package/dist/plugins/typescript/completions.d.ts +3 -0
  38. package/dist/plugins/typescript/completions.js +81 -0
  39. package/dist/plugins/typescript/diagnostics.d.ts +12 -0
  40. package/dist/plugins/typescript/diagnostics.js +99 -0
  41. package/dist/plugins/typescript/index.d.ts +3 -0
  42. package/dist/plugins/typescript/index.js +57 -0
  43. package/dist/utils.d.ts +19 -69
  44. package/dist/utils.js +66 -239
  45. package/package.json +37 -38
  46. package/types/astro-jsx.d.ts +49 -5
  47. package/bin/browserServer.js +0 -3
  48. package/dist/browser.js +0 -15
  49. package/dist/check.d.ts +0 -23
  50. package/dist/check.js +0 -60
  51. package/dist/core/DiagnosticsManager.d.ts +0 -13
  52. package/dist/core/DiagnosticsManager.js +0 -29
  53. package/dist/core/config/ConfigManager.d.ts +0 -43
  54. package/dist/core/config/ConfigManager.js +0 -226
  55. package/dist/core/config/index.d.ts +0 -2
  56. package/dist/core/config/index.js +0 -18
  57. package/dist/core/config/interfaces.d.ts +0 -77
  58. package/dist/core/config/interfaces.js +0 -2
  59. package/dist/core/documents/AstroDocument.d.ts +0 -19
  60. package/dist/core/documents/AstroDocument.js +0 -43
  61. package/dist/core/documents/DocumentBase.d.ts +0 -68
  62. package/dist/core/documents/DocumentBase.js +0 -75
  63. package/dist/core/documents/DocumentManager.d.ts +0 -23
  64. package/dist/core/documents/DocumentManager.js +0 -100
  65. package/dist/core/documents/DocumentMapper.d.ts +0 -94
  66. package/dist/core/documents/DocumentMapper.js +0 -264
  67. package/dist/core/documents/index.d.ts +0 -5
  68. package/dist/core/documents/index.js +0 -21
  69. package/dist/core/documents/parseAstro.d.ts +0 -15
  70. package/dist/core/documents/parseAstro.js +0 -63
  71. package/dist/core/documents/parseHtml.d.ts +0 -13
  72. package/dist/core/documents/parseHtml.js +0 -126
  73. package/dist/core/documents/utils.d.ts +0 -63
  74. package/dist/core/documents/utils.js +0 -223
  75. package/dist/node.d.ts +0 -1
  76. package/dist/node.js +0 -43
  77. package/dist/plugins/PluginHost.d.ts +0 -42
  78. package/dist/plugins/PluginHost.js +0 -206
  79. package/dist/plugins/astro/AstroPlugin.d.ts +0 -17
  80. package/dist/plugins/astro/AstroPlugin.js +0 -90
  81. package/dist/plugins/astro/features/CompletionsProvider.d.ts +0 -17
  82. package/dist/plugins/astro/features/CompletionsProvider.js +0 -242
  83. package/dist/plugins/astro/features/DiagnosticsProvider.d.ts +0 -10
  84. package/dist/plugins/astro/features/DiagnosticsProvider.js +0 -23
  85. package/dist/plugins/css/CSSDocument.d.ts +0 -45
  86. package/dist/plugins/css/CSSDocument.js +0 -68
  87. package/dist/plugins/css/CSSPlugin.d.ts +0 -39
  88. package/dist/plugins/css/CSSPlugin.js +0 -274
  89. package/dist/plugins/css/StyleAttributeDocument.d.ts +0 -40
  90. package/dist/plugins/css/StyleAttributeDocument.js +0 -64
  91. package/dist/plugins/css/features/astro-selectors.d.ts +0 -2
  92. package/dist/plugins/css/features/astro-selectors.js +0 -15
  93. package/dist/plugins/css/features/getIdClassCompletions.d.ts +0 -19
  94. package/dist/plugins/css/features/getIdClassCompletions.js +0 -57
  95. package/dist/plugins/css/language-service.d.ts +0 -3
  96. package/dist/plugins/css/language-service.js +0 -47
  97. package/dist/plugins/html/HTMLPlugin.d.ts +0 -33
  98. package/dist/plugins/html/HTMLPlugin.js +0 -198
  99. package/dist/plugins/html/utils.d.ts +0 -6
  100. package/dist/plugins/html/utils.js +0 -11
  101. package/dist/plugins/index.d.ts +0 -6
  102. package/dist/plugins/index.js +0 -22
  103. package/dist/plugins/interfaces.d.ts +0 -101
  104. package/dist/plugins/interfaces.js +0 -2
  105. package/dist/plugins/typescript/LanguageServiceManager.d.ts +0 -47
  106. package/dist/plugins/typescript/LanguageServiceManager.js +0 -97
  107. package/dist/plugins/typescript/TypeScriptPlugin.d.ts +0 -54
  108. package/dist/plugins/typescript/TypeScriptPlugin.js +0 -152
  109. package/dist/plugins/typescript/astro-sys.d.ts +0 -8
  110. package/dist/plugins/typescript/astro-sys.js +0 -46
  111. package/dist/plugins/typescript/astro2tsx.d.ts +0 -2
  112. package/dist/plugins/typescript/astro2tsx.js +0 -26
  113. package/dist/plugins/typescript/features/CodeActionsProvider.d.ts +0 -17
  114. package/dist/plugins/typescript/features/CodeActionsProvider.js +0 -212
  115. package/dist/plugins/typescript/features/CompletionsProvider.d.ts +0 -31
  116. package/dist/plugins/typescript/features/CompletionsProvider.js +0 -294
  117. package/dist/plugins/typescript/features/DefinitionsProvider.d.ts +0 -9
  118. package/dist/plugins/typescript/features/DefinitionsProvider.js +0 -58
  119. package/dist/plugins/typescript/features/DiagnosticsProvider.d.ts +0 -25
  120. package/dist/plugins/typescript/features/DiagnosticsProvider.js +0 -249
  121. package/dist/plugins/typescript/features/DocumentSymbolsProvider.d.ts +0 -11
  122. package/dist/plugins/typescript/features/DocumentSymbolsProvider.js +0 -67
  123. package/dist/plugins/typescript/features/FileReferencesProvider.d.ts +0 -9
  124. package/dist/plugins/typescript/features/FileReferencesProvider.js +0 -27
  125. package/dist/plugins/typescript/features/FoldingRangesProvider.d.ts +0 -12
  126. package/dist/plugins/typescript/features/FoldingRangesProvider.js +0 -71
  127. package/dist/plugins/typescript/features/HoverProvider.d.ts +0 -10
  128. package/dist/plugins/typescript/features/HoverProvider.js +0 -50
  129. package/dist/plugins/typescript/features/ImplementationsProvider.d.ts +0 -9
  130. package/dist/plugins/typescript/features/ImplementationsProvider.js +0 -53
  131. package/dist/plugins/typescript/features/InlayHintsProvider.d.ts +0 -13
  132. package/dist/plugins/typescript/features/InlayHintsProvider.js +0 -30
  133. package/dist/plugins/typescript/features/ReferencesProvider.d.ts +0 -9
  134. package/dist/plugins/typescript/features/ReferencesProvider.js +0 -55
  135. package/dist/plugins/typescript/features/RenameProvider.d.ts +0 -13
  136. package/dist/plugins/typescript/features/RenameProvider.js +0 -60
  137. package/dist/plugins/typescript/features/SemanticTokenProvider.d.ts +0 -16
  138. package/dist/plugins/typescript/features/SemanticTokenProvider.js +0 -75
  139. package/dist/plugins/typescript/features/SignatureHelpProvider.d.ts +0 -22
  140. package/dist/plugins/typescript/features/SignatureHelpProvider.js +0 -111
  141. package/dist/plugins/typescript/features/TypeDefinitionsProvider.d.ts +0 -9
  142. package/dist/plugins/typescript/features/TypeDefinitionsProvider.js +0 -54
  143. package/dist/plugins/typescript/features/utils.d.ts +0 -14
  144. package/dist/plugins/typescript/features/utils.js +0 -48
  145. package/dist/plugins/typescript/language-service.d.ts +0 -42
  146. package/dist/plugins/typescript/language-service.js +0 -277
  147. package/dist/plugins/typescript/module-loader.d.ts +0 -23
  148. package/dist/plugins/typescript/module-loader.js +0 -155
  149. package/dist/plugins/typescript/previewer.d.ts +0 -7
  150. package/dist/plugins/typescript/previewer.js +0 -108
  151. package/dist/plugins/typescript/snapshots/DocumentSnapshot.d.ts +0 -95
  152. package/dist/plugins/typescript/snapshots/DocumentSnapshot.js +0 -187
  153. package/dist/plugins/typescript/snapshots/SnapshotManager.d.ts +0 -44
  154. package/dist/plugins/typescript/snapshots/SnapshotManager.js +0 -179
  155. package/dist/plugins/typescript/snapshots/utils.d.ts +0 -29
  156. package/dist/plugins/typescript/snapshots/utils.js +0 -106
  157. package/dist/plugins/typescript/utils.d.ts +0 -75
  158. package/dist/plugins/typescript/utils.js +0 -366
  159. package/dist/server.d.ts +0 -6
  160. package/dist/server.js +0 -275
  161. package/types/README.md +0 -5
  162. /package/dist/{browser.d.ts → nodeServer.d.ts} +0 -0
  163. /package/dist/plugins/{html/features/astro-attributes.d.ts → html-data.d.ts} +0 -0
@@ -1,152 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypeScriptPlugin = void 0;
4
- const vscode_languageserver_1 = require("vscode-languageserver");
5
- const astro2tsx_1 = require("./astro2tsx");
6
- const CodeActionsProvider_1 = require("./features/CodeActionsProvider");
7
- const CompletionsProvider_1 = require("./features/CompletionsProvider");
8
- const DefinitionsProvider_1 = require("./features/DefinitionsProvider");
9
- const DiagnosticsProvider_1 = require("./features/DiagnosticsProvider");
10
- const DocumentSymbolsProvider_1 = require("./features/DocumentSymbolsProvider");
11
- const FileReferencesProvider_1 = require("./features/FileReferencesProvider");
12
- const FoldingRangesProvider_1 = require("./features/FoldingRangesProvider");
13
- const HoverProvider_1 = require("./features/HoverProvider");
14
- const ImplementationsProvider_1 = require("./features/ImplementationsProvider");
15
- const InlayHintsProvider_1 = require("./features/InlayHintsProvider");
16
- const ReferencesProvider_1 = require("./features/ReferencesProvider");
17
- const RenameProvider_1 = require("./features/RenameProvider");
18
- const SemanticTokenProvider_1 = require("./features/SemanticTokenProvider");
19
- const SignatureHelpProvider_1 = require("./features/SignatureHelpProvider");
20
- const TypeDefinitionsProvider_1 = require("./features/TypeDefinitionsProvider");
21
- const utils_1 = require("./utils");
22
- class TypeScriptPlugin {
23
- constructor(configManager, languageServiceManager) {
24
- this.__name = 'typescript';
25
- this.configManager = configManager;
26
- this.languageServiceManager = languageServiceManager;
27
- this.ts = languageServiceManager.docContext.ts;
28
- this.codeActionsProvider = new CodeActionsProvider_1.CodeActionsProviderImpl(this.languageServiceManager, this.configManager);
29
- this.completionProvider = new CompletionsProvider_1.CompletionsProviderImpl(this.languageServiceManager, this.configManager);
30
- this.hoverProvider = new HoverProvider_1.HoverProviderImpl(this.languageServiceManager);
31
- this.fileReferencesProvider = new FileReferencesProvider_1.FileReferencesProviderImpl(this.languageServiceManager);
32
- this.definitionsProvider = new DefinitionsProvider_1.DefinitionsProviderImpl(this.languageServiceManager);
33
- this.typeDefinitionsProvider = new TypeDefinitionsProvider_1.TypeDefinitionsProviderImpl(this.languageServiceManager);
34
- this.implementationsProvider = new ImplementationsProvider_1.ImplementationsProviderImpl(this.languageServiceManager);
35
- this.referencesProvider = new ReferencesProvider_1.FindReferencesProviderImpl(this.languageServiceManager);
36
- this.signatureHelpProvider = new SignatureHelpProvider_1.SignatureHelpProviderImpl(this.languageServiceManager);
37
- this.diagnosticsProvider = new DiagnosticsProvider_1.DiagnosticsProviderImpl(this.languageServiceManager);
38
- this.documentSymbolsProvider = new DocumentSymbolsProvider_1.DocumentSymbolsProviderImpl(this.languageServiceManager);
39
- this.semanticTokensProvider = new SemanticTokenProvider_1.SemanticTokensProviderImpl(this.languageServiceManager);
40
- this.inlayHintsProvider = new InlayHintsProvider_1.InlayHintsProviderImpl(this.languageServiceManager, this.configManager);
41
- this.foldingRangesProvider = new FoldingRangesProvider_1.FoldingRangesProviderImpl(this.languageServiceManager);
42
- this.renameProvider = new RenameProvider_1.RenameProviderImpl(this.languageServiceManager, this.configManager);
43
- }
44
- async doHover(document, position) {
45
- if (!(await this.featureEnabled(document, 'hover'))) {
46
- return null;
47
- }
48
- return this.hoverProvider.doHover(document, position);
49
- }
50
- async prepareRename(document, position) {
51
- return this.renameProvider.prepareRename(document, position);
52
- }
53
- async rename(document, position, newName) {
54
- return this.renameProvider.rename(document, position, newName);
55
- }
56
- async getFoldingRanges(document) {
57
- return this.foldingRangesProvider.getFoldingRanges(document);
58
- }
59
- async getSemanticTokens(document, range, cancellationToken) {
60
- if (!(await this.featureEnabled(document, 'semanticTokens'))) {
61
- return null;
62
- }
63
- return this.semanticTokensProvider.getSemanticTokens(document, range, cancellationToken);
64
- }
65
- async getDocumentSymbols(document) {
66
- if (!(await this.featureEnabled(document, 'documentSymbols'))) {
67
- return [];
68
- }
69
- const symbols = await this.documentSymbolsProvider.getDocumentSymbols(document);
70
- return symbols;
71
- }
72
- async getCodeActions(document, range, context, cancellationToken) {
73
- if (!(await this.featureEnabled(document, 'codeActions'))) {
74
- return [];
75
- }
76
- return this.codeActionsProvider.getCodeActions(document, range, context, cancellationToken);
77
- }
78
- async getCompletions(document, position, completionContext, cancellationToken) {
79
- if (!(await this.featureEnabled(document, 'completions'))) {
80
- return null;
81
- }
82
- const completions = await this.completionProvider.getCompletions(document, position, completionContext, cancellationToken);
83
- return completions;
84
- }
85
- async resolveCompletion(document, completionItem, cancellationToken) {
86
- return this.completionProvider.resolveCompletion(document, completionItem, cancellationToken);
87
- }
88
- async getInlayHints(document, range) {
89
- return this.inlayHintsProvider.getInlayHints(document, range);
90
- }
91
- async fileReferences(document) {
92
- return this.fileReferencesProvider.fileReferences(document);
93
- }
94
- async getDefinitions(document, position) {
95
- return this.definitionsProvider.getDefinitions(document, position);
96
- }
97
- async getTypeDefinitions(document, position) {
98
- return this.typeDefinitionsProvider.getTypeDefinitions(document, position);
99
- }
100
- async getImplementation(document, position) {
101
- return this.implementationsProvider.getImplementation(document, position);
102
- }
103
- async findReferences(document, position, context) {
104
- return this.referencesProvider.findReferences(document, position, context);
105
- }
106
- async getDiagnostics(document, cancellationToken) {
107
- if (!(await this.featureEnabled(document, 'diagnostics'))) {
108
- return [];
109
- }
110
- return this.diagnosticsProvider.getDiagnostics(document, cancellationToken);
111
- }
112
- async onWatchFileChanges(onWatchFileChangesParas) {
113
- let doneUpdateProjectFiles = false;
114
- for (const { fileName, changeType } of onWatchFileChangesParas) {
115
- const scriptKind = (0, utils_1.getScriptKindFromFileName)(fileName, this.ts);
116
- if (scriptKind === this.ts.ScriptKind.Unknown && !(0, utils_1.isFrameworkFilePath)(fileName) && !(0, utils_1.isAstroFilePath)(fileName)) {
117
- continue;
118
- }
119
- if (changeType === vscode_languageserver_1.FileChangeType.Created && !doneUpdateProjectFiles) {
120
- doneUpdateProjectFiles = true;
121
- await this.languageServiceManager.updateProjectFiles();
122
- }
123
- else if (changeType === vscode_languageserver_1.FileChangeType.Deleted) {
124
- await this.languageServiceManager.deleteSnapshot(fileName);
125
- }
126
- else if (!(0, utils_1.isAstroFilePath)(fileName)) {
127
- // Content updates for Astro files are handled through the documentManager and the 'documentChange' event
128
- await this.languageServiceManager.updateExistingNonAstroFile(fileName);
129
- }
130
- }
131
- }
132
- async updateNonAstroFile(fileName, changes, text) {
133
- await this.languageServiceManager.updateExistingNonAstroFile(fileName, changes, text);
134
- }
135
- async getSignatureHelp(document, position, context, cancellationToken) {
136
- return this.signatureHelpProvider.getSignatureHelp(document, position, context, cancellationToken);
137
- }
138
- getTSXForDocument(document) {
139
- return (0, astro2tsx_1.astro2tsx)(document.getText(), document.getURL());
140
- }
141
- /**
142
- * @internal Public for tests only
143
- */
144
- getSnapshotManager(fileName) {
145
- return this.languageServiceManager.getSnapshotManager(fileName);
146
- }
147
- async featureEnabled(document, feature) {
148
- return ((await this.configManager.isEnabled(document, 'typescript')) &&
149
- (await this.configManager.isEnabled(document, 'typescript', feature)));
150
- }
151
- }
152
- exports.TypeScriptPlugin = TypeScriptPlugin;
@@ -1,8 +0,0 @@
1
- import type ts from 'typescript';
2
- import type { DocumentSnapshot } from './snapshots/DocumentSnapshot';
3
- /**
4
- * This should only be accessed by TS Astro module resolution.
5
- */
6
- export declare function createAstroSys(getSnapshot: (fileName: string) => DocumentSnapshot, ts: typeof import('typescript/lib/tsserverlibrary')): ts.System & {
7
- deleteFromCache: (path: string) => void;
8
- };
@@ -1,46 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createAstroSys = void 0;
4
- const utils_1 = require("./utils");
5
- /**
6
- * This should only be accessed by TS Astro module resolution.
7
- */
8
- function createAstroSys(getSnapshot, ts) {
9
- const fileExistsCache = new Map();
10
- const AstroSys = {
11
- ...ts.sys,
12
- fileExists(path) {
13
- path = (0, utils_1.ensureRealFilePath)(path);
14
- const exists = fileExistsCache.get(path) ?? ts.sys.fileExists(path);
15
- fileExistsCache.set(path, exists);
16
- return exists;
17
- },
18
- readFile(path) {
19
- const snapshot = getSnapshot(path);
20
- return snapshot.getText(0, snapshot.getLength());
21
- },
22
- readDirectory(path, extensions, exclude, include, depth) {
23
- const extensionsWithAstro = (extensions ?? []).concat(...['.astro', '.svelte', '.vue', '.md', '.mdx', '.html']);
24
- const result = ts.sys.readDirectory(path, extensionsWithAstro, exclude, include, depth);
25
- return result;
26
- },
27
- deleteFile(path) {
28
- fileExistsCache.delete((0, utils_1.ensureRealFilePath)(path));
29
- return ts.sys.deleteFile?.(path);
30
- },
31
- deleteFromCache(path) {
32
- fileExistsCache.delete((0, utils_1.ensureRealFilePath)(path));
33
- },
34
- };
35
- if (ts.sys.realpath) {
36
- const realpath = ts.sys.realpath;
37
- AstroSys.realpath = function (path) {
38
- if ((0, utils_1.isVirtualFilePath)(path)) {
39
- return realpath((0, utils_1.ensureRealFilePath)(path)) + '.tsx';
40
- }
41
- return realpath(path);
42
- };
43
- }
44
- return AstroSys;
45
- }
46
- exports.createAstroSys = createAstroSys;
@@ -1,2 +0,0 @@
1
- import type { TSXResult } from '@astrojs/compiler/types';
2
- export declare function astro2tsx(content: string, fileName: string): TSXResult;
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.astro2tsx = void 0;
4
- const sync_1 = require("@astrojs/compiler/sync");
5
- function astro2tsx(content, fileName) {
6
- try {
7
- const tsx = (0, sync_1.convertToTSX)(content, { filename: fileName });
8
- return tsx;
9
- }
10
- catch (e) {
11
- console.error(`There was an error transforming ${fileName} to TSX. An empty file will be returned instead. Please create an issue: https://github.com/withastro/language-tools/issues\nError: ${e}.`);
12
- return {
13
- code: '',
14
- map: {
15
- file: fileName,
16
- sources: [],
17
- sourcesContent: [],
18
- names: [],
19
- mappings: '',
20
- version: 0,
21
- },
22
- diagnostics: [],
23
- };
24
- }
25
- }
26
- exports.astro2tsx = astro2tsx;
@@ -1,17 +0,0 @@
1
- import type { CancellationToken } from 'vscode-languageserver';
2
- import { CodeAction, CodeActionContext, Range } from 'vscode-languageserver-types';
3
- import type { ConfigManager } from '../../../core/config';
4
- import { AstroDocument } from '../../../core/documents';
5
- import type { CodeActionsProvider } from '../../interfaces';
6
- import type { LanguageServiceManager } from '../LanguageServiceManager';
7
- export declare const sortImportKind: string;
8
- export declare class CodeActionsProviderImpl implements CodeActionsProvider {
9
- private languageServiceManager;
10
- private configManager;
11
- private ts;
12
- constructor(languageServiceManager: LanguageServiceManager, configManager: ConfigManager);
13
- getCodeActions(document: AstroDocument, range: Range, context: CodeActionContext, cancellationToken?: CancellationToken): Promise<CodeAction[]>;
14
- private getComponentQuickFix;
15
- private organizeSortImports;
16
- private fixIndentationOfImports;
17
- }
@@ -1,212 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CodeActionsProviderImpl = exports.sortImportKind = void 0;
4
- const vscode_languageserver_types_1 = require("vscode-languageserver-types");
5
- const documents_1 = require("../../../core/documents");
6
- const utils_1 = require("../../../utils");
7
- const utils_2 = require("../utils");
8
- const CompletionsProvider_1 = require("./CompletionsProvider");
9
- const utils_3 = require("./utils");
10
- // These are VS Code specific CodeActionKind so they're not in the language server protocol
11
- exports.sortImportKind = `${vscode_languageserver_types_1.CodeActionKind.Source}.sortImports`;
12
- class CodeActionsProviderImpl {
13
- constructor(languageServiceManager, configManager) {
14
- this.languageServiceManager = languageServiceManager;
15
- this.configManager = configManager;
16
- this.ts = languageServiceManager.docContext.ts;
17
- }
18
- async getCodeActions(document, range, context, cancellationToken) {
19
- const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
20
- const tsPreferences = await this.configManager.getTSPreferences(document);
21
- const formatOptions = await this.configManager.getTSFormatConfig(document);
22
- let result = [];
23
- if (cancellationToken?.isCancellationRequested) {
24
- return [];
25
- }
26
- if (context.only?.[0] === vscode_languageserver_types_1.CodeActionKind.SourceOrganizeImports) {
27
- return await this.organizeSortImports(document, false, cancellationToken);
28
- }
29
- // The difference between Sort Imports and Organize Imports is that Sort Imports won't do anything destructive.
30
- // For example, it won't remove unused imports whereas Organize Imports will
31
- if (context.only?.[0] === exports.sortImportKind) {
32
- return await this.organizeSortImports(document, true, cancellationToken);
33
- }
34
- if (context.only?.[0] === vscode_languageserver_types_1.CodeActionKind.Source) {
35
- return [
36
- ...(await this.organizeSortImports(document, true, cancellationToken)),
37
- ...(await this.organizeSortImports(document, false, cancellationToken)),
38
- ];
39
- }
40
- if (context.diagnostics.length && (!context.only || context.only.includes(vscode_languageserver_types_1.CodeActionKind.QuickFix))) {
41
- const errorCodes = context.diagnostics
42
- .map((diag) => Number(diag.code))
43
- // We currently cannot support quick fix for unreachable code properly due to the way our TSX output is structured
44
- .filter((code) => code !== 7027);
45
- const html = document.html;
46
- const node = html.findNodeAt(document.offsetAt(range.start));
47
- let codeFixes;
48
- let isInsideScript = false;
49
- if (node.tag === 'script') {
50
- const { snapshot: scriptTagSnapshot, filePath: scriptFilePath } = (0, utils_2.getScriptTagSnapshot)(tsDoc, document, node);
51
- const start = scriptTagSnapshot.offsetAt(scriptTagSnapshot.getGeneratedPosition(range.start));
52
- const end = scriptTagSnapshot.offsetAt(scriptTagSnapshot.getGeneratedPosition(range.end));
53
- codeFixes = lang.getCodeFixesAtPosition(scriptFilePath, start, end, errorCodes, formatOptions, tsPreferences);
54
- codeFixes = codeFixes.map((fix) => ({
55
- ...fix,
56
- changes: mapScriptTagFixToOriginal(fix.changes, scriptTagSnapshot),
57
- }));
58
- isInsideScript = true;
59
- }
60
- else {
61
- const start = tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.start));
62
- const end = tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.end));
63
- codeFixes = errorCodes.includes(2304)
64
- ? this.getComponentQuickFix(start, end, lang, tsDoc.filePath, formatOptions, tsPreferences)
65
- : undefined;
66
- codeFixes =
67
- codeFixes ??
68
- lang.getCodeFixesAtPosition(tsDoc.filePath, start, end, errorCodes, formatOptions, tsPreferences);
69
- }
70
- const codeActions = codeFixes.map((fix) => codeFixToCodeAction(fix, context.diagnostics, context.only ? vscode_languageserver_types_1.CodeActionKind.QuickFix : vscode_languageserver_types_1.CodeActionKind.Empty, isInsideScript, this.ts));
71
- result.push(...codeActions);
72
- }
73
- return result;
74
- function codeFixToCodeAction(codeFix, diagnostics, kind, isInsideScript, ts) {
75
- const documentChanges = codeFix.changes.map((change) => {
76
- return vscode_languageserver_types_1.TextDocumentEdit.create(vscode_languageserver_types_1.OptionalVersionedTextDocumentIdentifier.create(document.getURL(), null), change.textChanges.map((edit) => {
77
- let originalRange = (0, documents_1.mapRangeToOriginal)(tsDoc, (0, utils_2.convertRange)(tsDoc, edit.span));
78
- // Inside scripts, we don't need to restrain the insertion of code inside a specific zone as it will be
79
- // restricted to the area of the script tag by default
80
- if (!isInsideScript) {
81
- if (codeFix.fixName === 'import') {
82
- return (0, CompletionsProvider_1.codeActionChangeToTextEdit)(document, tsDoc, false, edit, ts);
83
- }
84
- if (codeFix.fixName === 'fixMissingFunctionDeclaration') {
85
- originalRange = (0, utils_2.checkEndOfFileCodeInsert)(originalRange, document);
86
- }
87
- }
88
- else {
89
- // Make sure new imports are not added on the file line of the script tag
90
- if (codeFix.fixName === 'import') {
91
- const existingLine = (0, documents_1.getLineAtPosition)(document.positionAt(edit.span.start), document.getText());
92
- const isNewImport = !existingLine.trim().startsWith('import');
93
- if (!(edit.newText.startsWith('\n') || edit.newText.startsWith('\r\n')) && isNewImport) {
94
- edit.newText = ts.sys.newLine + edit.newText;
95
- }
96
- }
97
- }
98
- return vscode_languageserver_types_1.TextEdit.replace(originalRange, edit.newText);
99
- }));
100
- });
101
- const codeAction = vscode_languageserver_types_1.CodeAction.create(codeFix.description, {
102
- documentChanges,
103
- }, kind);
104
- codeAction.diagnostics = diagnostics;
105
- return codeAction;
106
- }
107
- function mapScriptTagFixToOriginal(changes, scriptTagSnapshot) {
108
- return changes.map((change) => {
109
- change.textChanges.map((edit) => {
110
- edit.span.start = (0, documents_1.mapScriptSpanStartToSnapshot)(edit.span, scriptTagSnapshot, tsDoc);
111
- return edit;
112
- });
113
- return change;
114
- });
115
- }
116
- }
117
- getComponentQuickFix(start, end, lang, filePath, formatOptions, tsPreferences) {
118
- const sourceFile = lang.getProgram()?.getSourceFile(filePath);
119
- if (!sourceFile) {
120
- return;
121
- }
122
- const node = (0, utils_3.findContainingNode)(sourceFile, {
123
- start,
124
- length: end - start,
125
- }, (n) => this.ts.isJsxClosingElement(n) || this.ts.isJsxOpeningLikeElement(n));
126
- if (!node) {
127
- return;
128
- }
129
- const tagName = node.tagName;
130
- // Unlike quick fixes, completions will be able to find the component, so let's use those to get it
131
- const completion = lang.getCompletionsAtPosition(filePath, tagName.getEnd(), tsPreferences, formatOptions);
132
- if (!completion) {
133
- return;
134
- }
135
- const name = tagName.getText();
136
- const suffixedName = name + '__AstroComponent_';
137
- const toFix = (c) => lang.getCompletionEntryDetails(filePath, end, c.name, {}, c.source, {}, c.data)?.codeActions?.map((a) => ({
138
- ...a,
139
- description: (0, utils_2.removeAstroComponentSuffix)(a.description),
140
- fixName: 'import',
141
- })) ?? [];
142
- return completion.entries.filter((c) => c.name === name || c.name === suffixedName).flatMap(toFix);
143
- }
144
- async organizeSortImports(document, skipDestructiveCodeActions = false, cancellationToken) {
145
- const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
146
- const filePath = tsDoc.filePath;
147
- if (cancellationToken?.isCancellationRequested) {
148
- return [];
149
- }
150
- let changes = [];
151
- if (document.astroMeta.frontmatter.state === 'closed') {
152
- changes.push(...lang.organizeImports({ fileName: filePath, type: 'file', skipDestructiveCodeActions }, {}, {}));
153
- }
154
- document.scriptTags.forEach((scriptTag) => {
155
- const { filePath: scriptFilePath, snapshot: scriptTagSnapshot } = (0, utils_2.getScriptTagSnapshot)(tsDoc, document, scriptTag.container);
156
- const edits = lang.organizeImports({ fileName: scriptFilePath, type: 'file', skipDestructiveCodeActions }, {}, {});
157
- edits.forEach((edit) => {
158
- edit.fileName = tsDoc.filePath;
159
- edit.textChanges = edit.textChanges
160
- .map((change) => {
161
- change.span.start = (0, documents_1.mapScriptSpanStartToSnapshot)(change.span, scriptTagSnapshot, tsDoc);
162
- // If the result ending position is unmapped, it usually means the ending position has a newline
163
- // inside the virtual part of the script tag, so let's just make it a character shorter
164
- const range = (0, documents_1.mapRangeToOriginal)(tsDoc, (0, utils_2.convertRange)(tsDoc, change.span));
165
- if (range.end.character === 0 && range.end.line === 0) {
166
- change.span.length -= 1;
167
- }
168
- return change;
169
- })
170
- // Since our last line is a (virtual) export, organize imports will try to rewrite it, so let's only take
171
- // changes that actually happens inside the script tag
172
- .filter((change) => {
173
- return (scriptTagSnapshot.isInGenerated(document.positionAt(change.span.start)) &&
174
- !change.newText.includes('export { }'));
175
- });
176
- return edit;
177
- });
178
- changes.push(...edits);
179
- });
180
- const documentChanges = changes.map((change) => {
181
- return vscode_languageserver_types_1.TextDocumentEdit.create(vscode_languageserver_types_1.OptionalVersionedTextDocumentIdentifier.create(document.url, null), change.textChanges.map((edit) => {
182
- const range = (0, documents_1.mapRangeToOriginal)(tsDoc, (0, utils_2.convertRange)(tsDoc, edit.span));
183
- if (document.offsetAt(range.start) < (document.astroMeta.content.firstNonWhitespaceOffset ?? 0)) {
184
- if (document.offsetAt(range.end) > document.astroMeta.frontmatter.endOffset) {
185
- range.end = document.positionAt(document.astroMeta.frontmatter.endOffset);
186
- }
187
- }
188
- return vscode_languageserver_types_1.TextEdit.replace(range, this.fixIndentationOfImports(edit.newText, range, document));
189
- }));
190
- });
191
- return [
192
- vscode_languageserver_types_1.CodeAction.create(skipDestructiveCodeActions ? 'Sort Imports' : 'Organize Imports', {
193
- documentChanges,
194
- }, skipDestructiveCodeActions ? exports.sortImportKind : vscode_languageserver_types_1.CodeActionKind.SourceOrganizeImports),
195
- ];
196
- }
197
- // "Organize Imports" will have edits that delete all imports by return empty edits
198
- // and one edit which contains all the organized imports. Fix indentation
199
- // of that one by prepending all lines with the indentation of the first line.
200
- fixIndentationOfImports(edit, range, document) {
201
- if (!edit || range.start.character === 0) {
202
- return edit;
203
- }
204
- const existingLine = (0, documents_1.getLineAtPosition)(range.start, document.getText());
205
- const leadingChars = existingLine.substring(0, range.start.character);
206
- if (leadingChars.trim() !== '') {
207
- return edit;
208
- }
209
- return (0, utils_1.modifyLines)(edit, (line, idx) => (idx === 0 || !line ? line : leadingChars + line));
210
- }
211
- }
212
- exports.CodeActionsProviderImpl = CodeActionsProviderImpl;
@@ -1,31 +0,0 @@
1
- import type ts from 'typescript';
2
- import { CancellationToken, CompletionContext, Position, TextDocumentIdentifier, TextEdit } from 'vscode-languageserver';
3
- import type { ConfigManager } from '../../../core/config';
4
- import { AstroDocument } from '../../../core/documents';
5
- import type { AppCompletionItem, AppCompletionList, CompletionsProvider } from '../../interfaces';
6
- import type { LanguageServiceManager } from '../LanguageServiceManager';
7
- import type { DocumentSnapshot } from '../snapshots/DocumentSnapshot';
8
- export interface CompletionItemData extends TextDocumentIdentifier {
9
- filePath: string;
10
- offset: number;
11
- scriptTagIndex: number | undefined;
12
- originalItem: ts.CompletionEntry;
13
- }
14
- export declare class CompletionsProviderImpl implements CompletionsProvider<CompletionItemData> {
15
- private languageServiceManager;
16
- private configManager;
17
- private ts;
18
- constructor(languageServiceManager: LanguageServiceManager, configManager: ConfigManager);
19
- private readonly validTriggerCharacters;
20
- private isValidTriggerCharacter;
21
- private lastCompletion?;
22
- getCompletions(document: AstroDocument, position: Position, completionContext?: CompletionContext, cancellationToken?: CancellationToken): Promise<AppCompletionList<CompletionItemData> | null>;
23
- resolveCompletion(document: AstroDocument, item: AppCompletionItem<CompletionItemData>, cancellationToken?: CancellationToken): Promise<AppCompletionItem<CompletionItemData>>;
24
- private toCompletionItem;
25
- private getCompletionDocument;
26
- private canReuseLastCompletion;
27
- private getExistingImports;
28
- private isAstroComponentImport;
29
- private isValidCompletion;
30
- }
31
- export declare function codeActionChangeToTextEdit(document: AstroDocument, snapshot: DocumentSnapshot, isInsideScriptTag: boolean, change: ts.TextChange, ts: typeof import('typescript/lib/tsserverlibrary')): TextEdit;