@astrojs/language-server 0.18.1 → 0.19.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @astrojs/language-server
2
2
 
3
+ ## 0.19.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a97b9a4: Add support for Inlay Hints. Minimum VS Code version supported starting from this update is 1.67.0 (April 2022)
8
+
3
9
  ## 0.18.1
4
10
 
5
11
  ### Patch Changes
@@ -2,7 +2,7 @@ import { VSCodeEmmetConfig } from '@vscode/emmet-helper';
2
2
  import { LSConfig, LSCSSConfig, LSFormatConfig, LSHTMLConfig, LSTypescriptConfig } from './interfaces';
3
3
  import { Connection, FormattingOptions } from 'vscode-languageserver';
4
4
  import { TextDocument } from 'vscode-languageserver-textdocument';
5
- import { FormatCodeSettings, UserPreferences } from 'typescript';
5
+ import { FormatCodeSettings, InlayHintsOptions, UserPreferences } from 'typescript';
6
6
  export declare const defaultLSConfig: LSConfig;
7
7
  declare type DeepPartial<T> = T extends Record<string, unknown> ? {
8
8
  [P in keyof T]?: DeepPartial<T[P]>;
@@ -25,14 +25,18 @@ export declare class ConfigManager {
25
25
  getAstroFormatConfig(document: TextDocument): Promise<LSFormatConfig>;
26
26
  getTSFormatConfig(document: TextDocument, vscodeOptions?: FormattingOptions): Promise<FormatCodeSettings>;
27
27
  getTSPreferences(document: TextDocument): Promise<UserPreferences>;
28
+ getTSInlayHintsPreferences(document: TextDocument): Promise<InlayHintsOptions>;
28
29
  /**
29
30
  * Return true if a plugin and an optional feature is enabled
30
31
  */
31
32
  isEnabled(document: TextDocument, plugin: keyof LSConfig, feature?: keyof LSTypescriptConfig | keyof LSCSSConfig | keyof LSHTMLConfig): Promise<boolean>;
32
33
  /**
33
34
  * Updating the global config should only be done in cases where the client doesn't support `workspace/configuration`
34
- * or inside of tests
35
+ * or inside of tests.
36
+ *
37
+ * The `outsideAstro` parameter can be set to true to change configurations in the global scope.
38
+ * For example, to change TypeScript settings
35
39
  */
36
- updateGlobalConfig(config: DeepPartial<LSConfig>): void;
40
+ updateGlobalConfig(config: DeepPartial<LSConfig> | any, outsideAstro?: boolean): void;
37
41
  }
38
42
  export {};
@@ -124,6 +124,20 @@ class ConfigManager {
124
124
  includeCompletionsWithInsertText: true,
125
125
  };
126
126
  }
127
+ async getTSInlayHintsPreferences(document) {
128
+ const config = (await this.getConfig('typescript', document.uri)) ?? {};
129
+ const tsPreferences = this.getTSPreferences(document);
130
+ return {
131
+ ...tsPreferences,
132
+ includeInlayParameterNameHints: getInlayParameterNameHintsPreference(config),
133
+ includeInlayParameterNameHintsWhenArgumentMatchesName: !(config.inlayHints?.parameterNames?.suppressWhenArgumentMatchesName ?? true),
134
+ includeInlayFunctionParameterTypeHints: config.inlayHints?.parameterTypes?.enabled ?? false,
135
+ includeInlayVariableTypeHints: config.inlayHints?.variableTypes?.enabled ?? false,
136
+ includeInlayPropertyDeclarationTypeHints: config.inlayHints?.propertyDeclarationTypes?.enabled ?? false,
137
+ includeInlayFunctionLikeReturnTypeHints: config.inlayHints?.functionLikeReturnTypes?.enabled ?? false,
138
+ includeInlayEnumMemberValueHints: config.inlayHints?.enumMemberValues?.enabled ?? false,
139
+ };
140
+ }
127
141
  /**
128
142
  * Return true if a plugin and an optional feature is enabled
129
143
  */
@@ -133,10 +147,18 @@ class ConfigManager {
133
147
  }
134
148
  /**
135
149
  * Updating the global config should only be done in cases where the client doesn't support `workspace/configuration`
136
- * or inside of tests
150
+ * or inside of tests.
151
+ *
152
+ * The `outsideAstro` parameter can be set to true to change configurations in the global scope.
153
+ * For example, to change TypeScript settings
137
154
  */
138
- updateGlobalConfig(config) {
139
- this.globalConfig.astro = (0, lodash_1.merge)({}, exports.defaultLSConfig, this.globalConfig.astro, config);
155
+ updateGlobalConfig(config, outsideAstro) {
156
+ if (outsideAstro) {
157
+ this.globalConfig = (0, lodash_1.merge)({}, this.globalConfig, config);
158
+ }
159
+ else {
160
+ this.globalConfig.astro = (0, lodash_1.merge)({}, exports.defaultLSConfig, this.globalConfig.astro, config);
161
+ }
140
162
  }
141
163
  }
142
164
  exports.ConfigManager = ConfigManager;
@@ -174,3 +196,15 @@ function getImportModuleSpecifierEndingPreference(config) {
174
196
  return 'auto';
175
197
  }
176
198
  }
199
+ function getInlayParameterNameHintsPreference(config) {
200
+ switch (config.inlayHints?.parameterNames?.enabled) {
201
+ case 'none':
202
+ return 'none';
203
+ case 'literals':
204
+ return 'literals';
205
+ case 'all':
206
+ return 'all';
207
+ default:
208
+ return undefined;
209
+ }
210
+ }
@@ -1,4 +1,4 @@
1
- import { CancellationToken, Color, ColorInformation, ColorPresentation, CompletionContext, CompletionItem, CompletionList, DefinitionLink, Diagnostic, FoldingRange, Hover, Position, Range, Location, SignatureHelp, SignatureHelpContext, TextDocumentContentChangeEvent, TextDocumentIdentifier, WorkspaceEdit, SymbolInformation, SemanticTokens, CodeActionContext, CodeAction, FormattingOptions, TextEdit } from 'vscode-languageserver';
1
+ import { CancellationToken, Color, ColorInformation, ColorPresentation, CompletionContext, CompletionItem, CompletionList, DefinitionLink, Diagnostic, FoldingRange, Hover, Position, Range, Location, SignatureHelp, SignatureHelpContext, TextDocumentContentChangeEvent, TextDocumentIdentifier, WorkspaceEdit, SymbolInformation, SemanticTokens, CodeActionContext, CodeAction, InlayHint, FormattingOptions, TextEdit } from 'vscode-languageserver';
2
2
  import type { AppCompletionItem, Plugin } from './interfaces';
3
3
  import { DocumentManager } from '../core/documents/DocumentManager';
4
4
  interface PluginHostConfig {
@@ -25,6 +25,7 @@ export declare class PluginHost {
25
25
  getDefinitions(textDocument: TextDocumentIdentifier, position: Position): Promise<DefinitionLink[] | Location[]>;
26
26
  rename(textDocument: TextDocumentIdentifier, position: Position, newName: string): Promise<WorkspaceEdit | null>;
27
27
  getDocumentColors(textDocument: TextDocumentIdentifier): Promise<ColorInformation[]>;
28
+ getInlayHints(textDocument: TextDocumentIdentifier, range: Range, cancellationToken: CancellationToken): Promise<InlayHint[]>;
28
29
  getColorPresentations(textDocument: TextDocumentIdentifier, range: Range, color: Color): Promise<ColorPresentation[]>;
29
30
  getSignatureHelp(textDocument: TextDocumentIdentifier, position: Position, context: SignatureHelpContext | undefined, cancellationToken: CancellationToken): Promise<SignatureHelp | null>;
30
31
  onWatchFileChanges(onWatchFileChangesParams: any[]): void;
@@ -112,6 +112,10 @@ class PluginHost {
112
112
  const document = this.getDocument(textDocument.uri);
113
113
  return (0, lodash_1.flatten)(await this.execute('getDocumentColors', [document], ExecuteMode.Collect));
114
114
  }
115
+ async getInlayHints(textDocument, range, cancellationToken) {
116
+ const document = this.getDocument(textDocument.uri);
117
+ return (0, lodash_1.flatten)(await this.execute('getInlayHints', [document, range], ExecuteMode.FirstNonNull));
118
+ }
115
119
  async getColorPresentations(textDocument, range, color) {
116
120
  const document = this.getDocument(textDocument.uri);
117
121
  return (0, lodash_1.flatten)(await this.execute('getColorPresentations', [document, range, color], ExecuteMode.Collect));
@@ -1,4 +1,4 @@
1
- import { CodeAction, CodeActionContext, Color, ColorInformation, ColorPresentation, CompletionContext, CompletionItem, CompletionList, DefinitionLink, Diagnostic, FileChangeType, FoldingRange, FormattingOptions, Hover, LinkedEditingRanges, Position, Range, ReferenceContext, SelectionRange, SemanticTokens, SignatureHelp, SignatureHelpContext, SymbolInformation, TextDocumentContentChangeEvent, TextDocumentIdentifier, TextEdit, WorkspaceEdit } from 'vscode-languageserver';
1
+ import { CodeAction, CodeActionContext, Color, ColorInformation, ColorPresentation, CompletionContext, CompletionItem, CompletionList, DefinitionLink, Diagnostic, FileChangeType, FoldingRange, FormattingOptions, Hover, InlayHint, LinkedEditingRanges, Position, Range, ReferenceContext, SelectionRange, SemanticTokens, SignatureHelp, SignatureHelpContext, SymbolInformation, TextDocumentContentChangeEvent, TextDocumentIdentifier, TextEdit, WorkspaceEdit } from 'vscode-languageserver';
2
2
  import { TextDocument } from 'vscode-languageserver-textdocument';
3
3
  export declare type Resolvable<T> = T | Promise<T>;
4
4
  export interface AppCompletionItem<T extends TextDocumentIdentifier = any> extends CompletionItem {
@@ -52,6 +52,9 @@ export interface FileRename {
52
52
  export interface UpdateImportsProvider {
53
53
  updateImports(fileRename: FileRename): Resolvable<WorkspaceEdit | null>;
54
54
  }
55
+ export interface InlayHintsProvider {
56
+ getInlayHints(document: TextDocument, range: Range): Resolvable<InlayHint[]>;
57
+ }
55
58
  export interface RenameProvider {
56
59
  rename(document: TextDocument, position: Position, newName: string): Resolvable<WorkspaceEdit | null>;
57
60
  prepareRename(document: TextDocument, position: Position): Resolvable<Range | null>;
@@ -81,7 +84,7 @@ export interface OnWatchFileChangesProvider {
81
84
  export interface UpdateNonAstroFile {
82
85
  updateNonAstroFile(fileName: string, changes: TextDocumentContentChangeEvent[]): void;
83
86
  }
84
- declare type ProviderBase = DiagnosticsProvider & HoverProvider & CompletionsProvider & DefinitionsProvider & FormattingProvider & FoldingRangesProvider & TagCompleteProvider & DocumentColorsProvider & ColorPresentationsProvider & DocumentSymbolsProvider & UpdateImportsProvider & CodeActionsProvider & FindReferencesProvider & RenameProvider & SignatureHelpProvider & SemanticTokensProvider & SelectionRangeProvider & OnWatchFileChangesProvider & LinkedEditingRangesProvider & UpdateNonAstroFile;
87
+ declare type ProviderBase = DiagnosticsProvider & HoverProvider & CompletionsProvider & DefinitionsProvider & FormattingProvider & FoldingRangesProvider & TagCompleteProvider & DocumentColorsProvider & ColorPresentationsProvider & DocumentSymbolsProvider & UpdateImportsProvider & CodeActionsProvider & FindReferencesProvider & RenameProvider & SignatureHelpProvider & SemanticTokensProvider & SelectionRangeProvider & OnWatchFileChangesProvider & LinkedEditingRangesProvider & InlayHintsProvider & UpdateNonAstroFile;
85
88
  export declare type LSProvider = ProviderBase;
86
89
  export declare type Plugin = Partial<ProviderBase> & {
87
90
  __name: string;
@@ -1,4 +1,4 @@
1
- import { CancellationToken, CodeAction, CodeActionContext, CompletionContext, DefinitionLink, Diagnostic, FoldingRange, FormattingOptions, Hover, Position, Range, SemanticTokens, SignatureHelp, SignatureHelpContext, SymbolInformation, TextDocumentContentChangeEvent, TextEdit, WorkspaceEdit } from 'vscode-languageserver';
1
+ import { CancellationToken, CodeAction, CodeActionContext, CompletionContext, DefinitionLink, Diagnostic, FoldingRange, FormattingOptions, Hover, InlayHint, Position, Range, SemanticTokens, SignatureHelp, SignatureHelpContext, SymbolInformation, TextDocumentContentChangeEvent, TextEdit, WorkspaceEdit } from 'vscode-languageserver';
2
2
  import { ConfigManager } from '../../core/config';
3
3
  import { AstroDocument, DocumentManager } from '../../core/documents';
4
4
  import { AppCompletionItem, AppCompletionList, OnWatchFileChangesParam, Plugin } from '../interfaces';
@@ -14,6 +14,7 @@ export declare class TypeScriptPlugin implements Plugin {
14
14
  private readonly signatureHelpProvider;
15
15
  private readonly diagnosticsProvider;
16
16
  private readonly documentSymbolsProvider;
17
+ private readonly inlayHintsProvider;
17
18
  private readonly semanticTokensProvider;
18
19
  private readonly foldingRangesProvider;
19
20
  private readonly formattingProvider;
@@ -27,6 +28,7 @@ export declare class TypeScriptPlugin implements Plugin {
27
28
  getCodeActions(document: AstroDocument, range: Range, context: CodeActionContext, cancellationToken?: CancellationToken): Promise<CodeAction[]>;
28
29
  getCompletions(document: AstroDocument, position: Position, completionContext?: CompletionContext, cancellationToken?: CancellationToken): Promise<AppCompletionList<CompletionItemData> | null>;
29
30
  resolveCompletion(document: AstroDocument, completionItem: AppCompletionItem<CompletionItemData>, cancellationToken?: CancellationToken): Promise<AppCompletionItem<CompletionItemData>>;
31
+ getInlayHints(document: AstroDocument, range: Range): Promise<InlayHint[]>;
30
32
  getDefinitions(document: AstroDocument, position: Position): Promise<DefinitionLink[]>;
31
33
  getDiagnostics(document: AstroDocument, cancellationToken?: CancellationToken): Promise<Diagnostic[]>;
32
34
  onWatchFileChanges(onWatchFileChangesParas: OnWatchFileChangesParam[]): Promise<void>;
@@ -17,6 +17,7 @@ const SemanticTokenProvider_1 = require("./features/SemanticTokenProvider");
17
17
  const FoldingRangesProvider_1 = require("./features/FoldingRangesProvider");
18
18
  const CodeActionsProvider_1 = require("./features/CodeActionsProvider");
19
19
  const DefinitionsProvider_1 = require("./features/DefinitionsProvider");
20
+ const InlayHintsProvider_1 = require("./features/InlayHintsProvider");
20
21
  const FormattingProvider_1 = require("./features/FormattingProvider");
21
22
  class TypeScriptPlugin {
22
23
  constructor(docManager, configManager, workspaceUris) {
@@ -31,6 +32,7 @@ class TypeScriptPlugin {
31
32
  this.diagnosticsProvider = new DiagnosticsProvider_1.DiagnosticsProviderImpl(this.languageServiceManager);
32
33
  this.documentSymbolsProvider = new DocumentSymbolsProvider_1.DocumentSymbolsProviderImpl(this.languageServiceManager);
33
34
  this.semanticTokensProvider = new SemanticTokenProvider_1.SemanticTokensProviderImpl(this.languageServiceManager);
35
+ this.inlayHintsProvider = new InlayHintsProvider_1.InlayHintsProviderImpl(this.languageServiceManager, this.configManager);
34
36
  this.foldingRangesProvider = new FoldingRangesProvider_1.FoldingRangesProviderImpl(this.languageServiceManager);
35
37
  this.formattingProvider = new FormattingProvider_1.FormattingProviderImpl(this.languageServiceManager, this.configManager);
36
38
  }
@@ -98,6 +100,9 @@ class TypeScriptPlugin {
98
100
  async resolveCompletion(document, completionItem, cancellationToken) {
99
101
  return this.completionProvider.resolveCompletion(document, completionItem, cancellationToken);
100
102
  }
103
+ async getInlayHints(document, range) {
104
+ return this.inlayHintsProvider.getInlayHints(document, range);
105
+ }
101
106
  async getDefinitions(document, position) {
102
107
  return this.definitionsProvider.getDefinitions(document, position);
103
108
  }
@@ -0,0 +1,12 @@
1
+ import { InlayHint } from 'vscode-languageserver';
2
+ import { AstroDocument } from '../../../core/documents';
3
+ import { InlayHintsProvider } from '../../interfaces';
4
+ import { LanguageServiceManager } from '../LanguageServiceManager';
5
+ import { Range } from 'vscode-languageserver-types';
6
+ import { ConfigManager } from '../../../core/config';
7
+ export declare class InlayHintsProviderImpl implements InlayHintsProvider {
8
+ private languageServiceManager;
9
+ private configManager;
10
+ constructor(languageServiceManager: LanguageServiceManager, configManager: ConfigManager);
11
+ getInlayHints(document: AstroDocument, range: Range): Promise<InlayHint[]>;
12
+ }
@@ -0,0 +1,36 @@
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.InlayHintsProviderImpl = void 0;
7
+ const vscode_languageserver_1 = require("vscode-languageserver");
8
+ const utils_1 = require("../utils");
9
+ const vscode_languageserver_types_1 = require("vscode-languageserver-types");
10
+ const typescript_1 = __importDefault(require("typescript"));
11
+ class InlayHintsProviderImpl {
12
+ constructor(languageServiceManager, configManager) {
13
+ this.languageServiceManager = languageServiceManager;
14
+ this.configManager = configManager;
15
+ }
16
+ async getInlayHints(document, range) {
17
+ const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
18
+ const filePath = (0, utils_1.toVirtualAstroFilePath)(tsDoc.filePath);
19
+ const fragment = await tsDoc.createFragment();
20
+ const start = fragment.offsetAt(fragment.getGeneratedPosition(range.start));
21
+ const end = fragment.offsetAt(fragment.getGeneratedPosition(range.end));
22
+ const tsPreferences = await this.configManager.getTSInlayHintsPreferences(document);
23
+ const inlayHints = lang.provideInlayHints(filePath, { start, length: end - start }, tsPreferences);
24
+ return inlayHints.map((hint) => {
25
+ const result = vscode_languageserver_1.InlayHint.create(fragment.getOriginalPosition(fragment.positionAt(hint.position)), hint.text, hint.kind === typescript_1.default.InlayHintKind.Type
26
+ ? vscode_languageserver_types_1.InlayHintKind.Type
27
+ : hint.kind === typescript_1.default.InlayHintKind.Parameter
28
+ ? vscode_languageserver_types_1.InlayHintKind.Parameter
29
+ : undefined);
30
+ result.paddingLeft = hint.whitespaceBefore;
31
+ result.paddingRight = hint.whitespaceAfter;
32
+ return result;
33
+ });
34
+ }
35
+ }
36
+ exports.InlayHintsProviderImpl = InlayHintsProviderImpl;
package/dist/server.js CHANGED
@@ -134,6 +134,7 @@ function startLanguageServer(connection) {
134
134
  range: true,
135
135
  full: true,
136
136
  },
137
+ inlayHintProvider: true,
137
138
  signatureHelpProvider: {
138
139
  triggerCharacters: ['(', ',', '<'],
139
140
  retriggerCharacters: [')'],
@@ -197,6 +198,7 @@ function startLanguageServer(connection) {
197
198
  connection.onDocumentFormatting((params) => pluginHost.formatDocument(params.textDocument, params.options));
198
199
  connection.onDocumentColor((params) => pluginHost.getDocumentColors(params.textDocument));
199
200
  connection.onColorPresentation((params) => pluginHost.getColorPresentations(params.textDocument, params.range, params.color));
201
+ connection.onRequest(vscode_languageserver_1.InlayHintRequest.type, (params, cancellationToken) => pluginHost.getInlayHints(params.textDocument, params.range, cancellationToken));
200
202
  connection.onRequest(TagCloseRequest, (evt) => pluginHost.doTagComplete(evt.textDocument, evt.position));
201
203
  connection.onSignatureHelp((evt, cancellationToken) => pluginHost.getSignatureHelp(evt.textDocument, evt.position, evt.context, cancellationToken));
202
204
  connection.onRenameRequest((evt) => pluginHost.rename(evt.textDocument, evt.position, evt.newName));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/language-server",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "author": "withastro",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -24,14 +24,14 @@
24
24
  "@vscode/emmet-helper": "^2.8.4",
25
25
  "lodash": "^4.17.21",
26
26
  "source-map": "^0.7.3",
27
- "typescript": "~4.6.2",
28
- "vscode-css-languageservice": "^5.1.13",
29
- "vscode-html-languageservice": "^4.2.5",
30
- "vscode-languageserver": "7.0.0",
31
- "vscode-languageserver-protocol": "^3.16.0",
32
- "vscode-languageserver-textdocument": "^1.0.1",
33
- "vscode-languageserver-types": "^3.16.0",
34
- "vscode-uri": "^3.0.2"
27
+ "typescript": "~4.6.4",
28
+ "vscode-css-languageservice": "^6.0.1",
29
+ "vscode-html-languageservice": "^5.0.0",
30
+ "vscode-languageserver": "^8.0.1",
31
+ "vscode-languageserver-protocol": "^3.17.1",
32
+ "vscode-languageserver-textdocument": "^1.0.4",
33
+ "vscode-languageserver-types": "^3.17.1",
34
+ "vscode-uri": "^3.0.3"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/chai": "^4.3.0",