Skip to content

Commit 4b706fc

Browse files
author
Andy
authored
Add 'disableSuggestions' to UserPreferences (#23283)
* Add 'disableSuggestions' to UserPreferences * Make mergeMapLikes return a new object * Avoid additional clone * mergeMapLikes -> object spread
1 parent 9c0671d commit 4b706fc

File tree

10 files changed

+76
-21
lines changed

10 files changed

+76
-21
lines changed

src/compiler/core.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1268,10 +1268,7 @@ namespace ts {
12681268
});
12691269
}
12701270

1271-
export function assign<T1 extends MapLike<{}>, T2, T3>(t: T1, arg1: T2, arg2: T3): T1 & T2 & T3;
1272-
export function assign<T1 extends MapLike<{}>, T2>(t: T1, arg1: T2): T1 & T2;
1273-
export function assign<T1 extends MapLike<{}>>(t: T1, ...args: any[]): any;
1274-
export function assign<T1 extends MapLike<{}>>(t: T1, ...args: any[]) {
1271+
export function assign<T extends object>(t: T, ...args: T[]) {
12751272
for (const arg of args) {
12761273
for (const p in arg) {
12771274
if (hasProperty(arg, p)) {

src/harness/unittests/tsserverProjectSystem.ts

+51
Original file line numberDiff line numberDiff line change
@@ -4184,6 +4184,57 @@ namespace ts.projectSystem {
41844184
session.clearMessages();
41854185
});
41864186

4187+
it("disable suggestion diagnostics", () => {
4188+
const file: FileOrFolder = {
4189+
path: "/a.js",
4190+
content: 'require("b")',
4191+
};
4192+
4193+
const host = createServerHost([file]);
4194+
const session = createSession(host, { canUseEvents: true });
4195+
const service = session.getProjectService();
4196+
4197+
session.executeCommandSeq<protocol.OpenRequest>({
4198+
command: server.CommandNames.Open,
4199+
arguments: { file: file.path, fileContent: file.content },
4200+
});
4201+
4202+
session.executeCommandSeq<protocol.ConfigureRequest>({
4203+
command: server.CommandNames.Configure,
4204+
arguments: {
4205+
preferences: { disableSuggestions: true }
4206+
},
4207+
});
4208+
4209+
checkNumberOfProjects(service, { inferredProjects: 1 });
4210+
session.clearMessages();
4211+
const expectedSequenceId = session.getNextSeq();
4212+
host.checkTimeoutQueueLengthAndRun(2);
4213+
4214+
checkProjectUpdatedInBackgroundEvent(session, [file.path]);
4215+
session.clearMessages();
4216+
4217+
session.executeCommandSeq<protocol.GeterrRequest>({
4218+
command: server.CommandNames.Geterr,
4219+
arguments: {
4220+
delay: 0,
4221+
files: [file.path],
4222+
}
4223+
});
4224+
4225+
host.checkTimeoutQueueLengthAndRun(1);
4226+
4227+
checkErrorMessage(session, "syntaxDiag", { file: file.path, diagnostics: [] }, /*isMostRecent*/ true);
4228+
session.clearMessages();
4229+
4230+
host.runQueuedImmediateCallbacks(1);
4231+
4232+
checkErrorMessage(session, "semanticDiag", { file: file.path, diagnostics: [] });
4233+
// No suggestion event, we're done.
4234+
checkCompleteEvent(session, 2, expectedSequenceId);
4235+
session.clearMessages();
4236+
});
4237+
41874238
it("suppressed diagnostic events", () => {
41884239
const file: FileOrFolder = {
41894240
path: "/a.ts",

src/server/editorServices.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1842,11 +1842,11 @@ namespace ts.server {
18421842
this.logger.info(`Host information ${args.hostInfo}`);
18431843
}
18441844
if (args.formatOptions) {
1845-
mergeMapLikes(this.hostConfiguration.formatCodeOptions, convertFormatOptions(args.formatOptions));
1845+
this.hostConfiguration.formatCodeOptions = { ...this.hostConfiguration.formatCodeOptions, ...convertFormatOptions(args.formatOptions) };
18461846
this.logger.info("Format host information updated");
18471847
}
18481848
if (args.preferences) {
1849-
mergeMapLikes(this.hostConfiguration.preferences, args.preferences);
1849+
this.hostConfiguration.preferences = { ...this.hostConfiguration.preferences, ...args.preferences };
18501850
}
18511851
if (args.extraFileExtensions) {
18521852
this.hostConfiguration.extraFileExtensions = args.extraFileExtensions;

src/server/protocol.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,7 @@ namespace ts.server.protocol {
26402640
}
26412641

26422642
export interface UserPreferences {
2643+
readonly disableSuggestions?: boolean;
26432644
readonly quotePreference?: "double" | "single";
26442645
/**
26452646
* If enabled, TypeScript will search through all external modules' exports and add them to the completions list.

src/server/scriptInfo.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,18 @@ namespace ts.server {
397397
if (formatSettings) {
398398
if (!this.formatSettings) {
399399
this.formatSettings = getDefaultFormatCodeSettings(this.host);
400+
assign(this.formatSettings, formatSettings);
401+
}
402+
else {
403+
this.formatSettings = { ...this.formatSettings, ...formatSettings };
400404
}
401-
mergeMapLikes(this.formatSettings, formatSettings);
402405
}
403406

404407
if (preferences) {
405408
if (!this.preferences) {
406-
this.preferences = clone(defaultPreferences);
409+
this.preferences = defaultPreferences;
407410
}
408-
mergeMapLikes(this.preferences, preferences);
411+
this.preferences = { ...this.preferences, ...preferences };
409412
}
410413
}
411414

src/server/session.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,20 @@ namespace ts.server {
528528
return;
529529
}
530530

531-
next.immediate(() => {
532-
this.suggestionCheck(fileName, project);
531+
const goNext = () => {
533532
if (checkList.length > index) {
534533
next.delay(followMs, checkOne);
535534
}
536-
});
535+
};
536+
if (this.getPreferences(fileName).disableSuggestions) {
537+
goNext();
538+
}
539+
else {
540+
next.immediate(() => {
541+
this.suggestionCheck(fileName, project);
542+
goNext();
543+
});
544+
}
537545
});
538546
};
539547

src/server/utilities.ts

-8
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ namespace ts.server {
8383
};
8484
}
8585

86-
export function mergeMapLikes<T extends object>(target: T, source: Partial<T>): void {
87-
for (const key in source) {
88-
if (hasProperty(source, key)) {
89-
target[key] = source[key];
90-
}
91-
}
92-
}
93-
9486
export type NormalizedPath = string & { __normalizedPathTag: any };
9587

9688
export function toNormalizedPath(fileName: string): NormalizedPath {

src/services/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ namespace ts {
228228
}
229229

230230
export interface UserPreferences {
231+
readonly disableSuggestions?: boolean;
231232
readonly quotePreference?: "double" | "single";
232233
readonly includeCompletionsForModuleExports?: boolean;
233234
readonly includeCompletionsWithInsertText?: boolean;

tests/baselines/reference/api/tsserverlibrary.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4116,6 +4116,7 @@ declare namespace ts {
41164116
installPackage?(options: InstallPackageOptions): Promise<ApplyCodeActionCommandResult>;
41174117
}
41184118
interface UserPreferences {
4119+
readonly disableSuggestions?: boolean;
41194120
readonly quotePreference?: "double" | "single";
41204121
readonly includeCompletionsForModuleExports?: boolean;
41214122
readonly includeCompletionsWithInsertText?: boolean;
@@ -5041,7 +5042,6 @@ declare namespace ts.server {
50415042
function ThrowProjectDoesNotContainDocument(fileName: string, project: Project): never;
50425043
}
50435044
function getDefaultFormatCodeSettings(host: ServerHost): FormatCodeSettings;
5044-
function mergeMapLikes<T extends object>(target: T, source: Partial<T>): void;
50455045
type NormalizedPath = string & {
50465046
__normalizedPathTag: any;
50475047
};
@@ -7157,6 +7157,7 @@ declare namespace ts.server.protocol {
71577157
insertSpaceBeforeTypeAnnotation?: boolean;
71587158
}
71597159
interface UserPreferences {
7160+
readonly disableSuggestions?: boolean;
71607161
readonly quotePreference?: "double" | "single";
71617162
/**
71627163
* If enabled, TypeScript will search through all external modules' exports and add them to the completions list.

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4369,6 +4369,7 @@ declare namespace ts {
43694369
installPackage?(options: InstallPackageOptions): Promise<ApplyCodeActionCommandResult>;
43704370
}
43714371
interface UserPreferences {
4372+
readonly disableSuggestions?: boolean;
43724373
readonly quotePreference?: "double" | "single";
43734374
readonly includeCompletionsForModuleExports?: boolean;
43744375
readonly includeCompletionsWithInsertText?: boolean;

0 commit comments

Comments
 (0)