Skip to content

Commit

Permalink
feat: add support for new features from TypeScript 4.8 (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Sep 4, 2022
1 parent 9faa37f commit 7e88db3
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 100 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ The `preferences` object is an object specifying preferences for the internal `t

```ts
interface UserPreferences {
/**
* Glob patterns of files to exclude from auto imports. Requires using TypeScript 4.8 or newer in the workspace.
* Relative paths are resolved relative to the workspace root.
* @since 4.8.2
*/
autoImportFileExcludePatterns: [],
disableSuggestions: boolean;
quotePreference: "auto" | "double" | "single";
/**
Expand Down Expand Up @@ -172,6 +178,12 @@ interface UserPreferences {
includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
includeInlayFunctionParameterTypeHints: boolean,
includeInlayVariableTypeHints: boolean;
/**
* When disabled then type hints on variables whose name is identical to the type name won't be shown. Requires using TypeScript 4.8+ in the workspace.
* @since 4.8.2
* @default false
*/
includeInlayVariableTypeHintsWhenTypeMatchesName: boolean;
includeInlayPropertyDeclarationTypeHints: boolean;
includeInlayFunctionLikeReturnTypeHints: boolean;
includeInlayEnumMemberValueHints: boolean;
Expand Down Expand Up @@ -238,6 +250,7 @@ Some of the preferences can be controlled through the `workspace/didChangeConfig
[language].inlayHints.includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
[language].inlayHints.includeInlayPropertyDeclarationTypeHints: boolean;
[language].inlayHints.includeInlayVariableTypeHints: boolean;
[language].inlayHints.includeInlayVariableTypeHintsWhenTypeMatchesName: boolean;
/**
* Complete functions with their parameter signature.
* @default false
Expand Down Expand Up @@ -412,6 +425,7 @@ export interface InlayHintsOptions extends UserPreferences {
includeInlayParameterNameHintsWhenArgumentMatchesName: boolean;
includeInlayFunctionParameterTypeHints: boolean;
includeInlayVariableTypeHints: boolean;
includeInlayVariableTypeHintsWhenTypeMatchesName: boolean;
includeInlayPropertyDeclarationTypeHints: boolean;
includeInlayFunctionLikeReturnTypeHints: boolean;
includeInlayEnumMemberValueHints: boolean;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@
"@types/node": "^16.11.52",
"@types/semver": "^7.3.12",
"@types/which": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^5.33.1",
"@typescript-eslint/parser": "^5.33.1",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"chai": "^4.3.6",
"concurrently": "^7.3.0",
"cross-env": "^7.0.3",
"eslint": "^8.22.0",
"eslint": "^8.23.0",
"husky": "4.x",
"mocha": "^10.0.0",
"rimraf": "^3.0.2",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
"typescript": "^4.8.2"
}
}
47 changes: 36 additions & 11 deletions src/configuration-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deepmerge from 'deepmerge';
import path from 'node:path';
import type * as lsp from 'vscode-languageserver';
import type tsp from 'typescript/lib/protocol.d.js';
import { LspDocuments } from './document.js';
Expand All @@ -11,6 +12,7 @@ const DEFAULT_TSSERVER_PREFERENCES: Required<tsp.UserPreferences> = {
allowIncompleteCompletions: true,
allowRenameOfImportPath: true,
allowTextChangesInNewFiles: true,
autoImportFileExcludePatterns: [],
disableSuggestions: false,
displayPartsForJSDoc: true,
generateReturnInDocTemplate: true,
Expand All @@ -30,6 +32,7 @@ const DEFAULT_TSSERVER_PREFERENCES: Required<tsp.UserPreferences> = {
includeInlayParameterNameHintsWhenArgumentMatchesName: false,
includeInlayPropertyDeclarationTypeHints: false,
includeInlayVariableTypeHints: false,
includeInlayVariableTypeHintsWhenTypeMatchesName: false,
includePackageJsonAutoImports: 'auto',
jsxAttributeCompletionStyle: 'auto',
lazyConfiguredProjectsFromExternalProject: false,
Expand All @@ -51,15 +54,19 @@ export interface WorkspaceConfigurationLanguageOptions {
inlayHints?: TypeScriptInlayHintsPreferences;
}

export interface TypeScriptInlayHintsPreferences {
includeInlayParameterNameHints?: 'none' | 'literals' | 'all';
includeInlayParameterNameHintsWhenArgumentMatchesName?: boolean;
includeInlayFunctionParameterTypeHints?: boolean;
includeInlayVariableTypeHints?: boolean;
includeInlayPropertyDeclarationTypeHints?: boolean;
includeInlayFunctionLikeReturnTypeHints?: boolean;
includeInlayEnumMemberValueHints?: boolean;
}
/* eslint-disable @typescript-eslint/indent */
export type TypeScriptInlayHintsPreferences = Pick<
tsp.UserPreferences,
'includeInlayParameterNameHints' |
'includeInlayParameterNameHintsWhenArgumentMatchesName' |
'includeInlayFunctionParameterTypeHints' |
'includeInlayVariableTypeHints' |
'includeInlayVariableTypeHintsWhenTypeMatchesName' |
'includeInlayPropertyDeclarationTypeHints' |
'includeInlayFunctionLikeReturnTypeHints' |
'includeInlayEnumMemberValueHints'
>;
/* eslint-enable @typescript-eslint/indent */

interface WorkspaceConfigurationDiagnosticsOptions {
ignoredCodes?: number[];
Expand All @@ -84,7 +91,7 @@ export class ConfigurationManager {
this.workspaceConfiguration = configuration;
}

public async setAndConfigureTspClient(client: TspClient, hostInfo?: TypeScriptInitializationOptions['hostInfo']): Promise<void> {
public async setAndConfigureTspClient(workspaceFolder: string | undefined, client: TspClient, hostInfo?: TypeScriptInitializationOptions['hostInfo']): Promise<void> {
this.tspClient = client;
const formatOptions: tsp.FormatCodeSettings = {
// We can use \n here since the editor should normalize later on to its line endings.
Expand All @@ -93,7 +100,10 @@ export class ConfigurationManager {
const args: tsp.ConfigureRequestArguments = {
...hostInfo ? { hostInfo } : {},
formatOptions,
preferences: this.tsPreferences,
preferences: {
...this.tsPreferences,
autoImportFileExcludePatterns: this.getAutoImportFileExcludePatternsPreference(workspaceFolder),
},
};
await this.tspClient?.request(CommandTypes.Configure, args);
}
Expand Down Expand Up @@ -155,4 +165,19 @@ export class ConfigurationManager {
const languageId = document?.languageId.startsWith('typescript') ? 'typescript' : 'javascript';
return this.workspaceConfiguration[languageId] || {};
}

private getAutoImportFileExcludePatternsPreference(workspaceFolder: string | undefined): string[] | undefined {
if (!workspaceFolder || this.tsPreferences.autoImportFileExcludePatterns.length === 0) {
return;
}
return this.tsPreferences.autoImportFileExcludePatterns.map(p => {
// Normalization rules: https://github.com/microsoft/TypeScript/pull/49578
const slashNormalized = p.replace(/\\/g, '/');
const isRelative = /^\.\.?($|\/)/.test(slashNormalized);
return path.posix.isAbsolute(p) ? p :
p.startsWith('*') ? '/' + slashNormalized :
isRelative ? path.posix.join(workspaceFolder, p) :
'/**/' + slashNormalized;
});
}
}
4 changes: 2 additions & 2 deletions src/features/inlay-hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export class TypeScriptInlayHintsProvider {
function areInlayHintsEnabledForFile(configurationManager: ConfigurationManager, filename: string) {
const preferences = configurationManager.getPreferences(filename);

// Doesn't need to include `includeInlayVariableTypeHintsWhenTypeMatchesName` as it depends
// on `includeInlayVariableTypeHints` being enabled.
// Doesn't need to include `includeInlayVariableTypeHintsWhenTypeMatchesName` and
// `includeInlayVariableTypeHintsWhenTypeMatchesName` as those depend on other preferences being enabled.
return preferences.includeInlayParameterNameHints === 'literals' ||
preferences.includeInlayParameterNameHints === 'all' ||
preferences.includeInlayEnumMemberValueHints ||
Expand Down
2 changes: 1 addition & 1 deletion src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class LspServer {
this.typeScriptAutoFixProvider = new TypeScriptAutoFixProvider(this.tspClient);

await Promise.all([
this.configurationManager.setAndConfigureTspClient(this._tspClient, hostInfo),
this.configurationManager.setAndConfigureTspClient(this.workspaceRoot, this._tspClient, hostInfo),
this.tspClient.request(CommandTypes.CompilerOptionsForInferredProjects, {
options: {
module: tsp.ModuleKind.CommonJS,
Expand Down

0 comments on commit 7e88db3

Please sign in to comment.