Skip to content

Commit ec6772c

Browse files
committed
refactor
1 parent 9f2727b commit ec6772c

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

src/server/session.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,8 @@ export class Session<TMessage = string> implements EventSender {
994994
private eventHandler: ProjectServiceEventHandler | undefined;
995995
private readonly noGetErrOnBackgroundUpdate?: boolean;
996996

997-
private diagnosticsTime: [number, number] | undefined;
997+
// Minimum number of lines for attempting to use region diagnostics for a file.
998+
protected regionDiagLineCountThreshold = 500;
998999

9991000
constructor(opts: SessionOptions) {
10001001
this.host = opts.host;
@@ -1260,53 +1261,61 @@ export class Session<TMessage = string> implements EventSender {
12601261
}
12611262

12621263
private semanticCheck(file: NormalizedPath, project: Project) {
1263-
this.diagnosticsTime = this.hrtime();
1264+
const diagnosticsStartTime = this.hrtime();
12641265
tracing?.push(tracing.Phase.Session, "semanticCheck", { file, configFilePath: (project as ConfiguredProject).canonicalConfigFilePath }); // undefined is fine if the cast fails
12651266
const diags = isDeclarationFileInJSOnlyNonConfiguredProject(project, file)
12661267
? emptyArray
12671268
: project.getLanguageService().getSemanticDiagnostics(file).filter(d => !!d.file);
1268-
this.sendDiagnosticsEvent(file, project, diags, "semanticDiag");
1269+
this.sendDiagnosticsEvent(file, project, diags, "semanticDiag", diagnosticsStartTime);
12691270
tracing?.pop();
12701271
}
12711272

12721273
private syntacticCheck(file: NormalizedPath, project: Project) {
1273-
this.diagnosticsTime = this.hrtime();
1274+
const diagnosticsStartTime = this.hrtime();
12741275
tracing?.push(tracing.Phase.Session, "syntacticCheck", { file, configFilePath: (project as ConfiguredProject).canonicalConfigFilePath }); // undefined is fine if the cast fails
1275-
this.sendDiagnosticsEvent(file, project, project.getLanguageService().getSyntacticDiagnostics(file), "syntaxDiag");
1276+
this.sendDiagnosticsEvent(file, project, project.getLanguageService().getSyntacticDiagnostics(file), "syntaxDiag", diagnosticsStartTime);
12761277
tracing?.pop();
12771278
}
12781279

12791280
private suggestionCheck(file: NormalizedPath, project: Project) {
1280-
this.diagnosticsTime = this.hrtime();
1281+
const diagnosticsStartTime = this.hrtime();
12811282
tracing?.push(tracing.Phase.Session, "suggestionCheck", { file, configFilePath: (project as ConfiguredProject).canonicalConfigFilePath }); // undefined is fine if the cast fails
1282-
this.sendDiagnosticsEvent(file, project, project.getLanguageService().getSuggestionDiagnostics(file), "suggestionDiag");
1283+
this.sendDiagnosticsEvent(file, project, project.getLanguageService().getSuggestionDiagnostics(file), "suggestionDiag", diagnosticsStartTime);
12831284
tracing?.pop();
12841285
}
12851286

12861287
private regionSemanticCheck(file: NormalizedPath, project: Project, ranges: TextRange[]): void {
1287-
this.diagnosticsTime = this.hrtime();
1288+
const diagnosticsStartTime = this.hrtime();
12881289
tracing?.push(tracing.Phase.Session, "regionSemanticCheck", { file, configFilePath: (project as ConfiguredProject).canonicalConfigFilePath }); // undefined is fine if the cast fails
12891290
let diagnosticsResult;
12901291
if (!this.shouldDoRegionCheck(file) || !(diagnosticsResult = project.getLanguageService().getRegionSemanticDiagnostics(file, ranges))) {
12911292
tracing?.pop();
12921293
return;
12931294
}
1294-
this.sendDiagnosticsEvent(file, project, diagnosticsResult.diagnostics, "regionSemanticDiag", diagnosticsResult.spans);
1295+
this.sendDiagnosticsEvent(file, project, diagnosticsResult.diagnostics, "regionSemanticDiag", diagnosticsStartTime, diagnosticsResult.spans);
12951296
tracing?.pop();
12961297
return;
12971298
}
12981299

1299-
// We should only do the region-based semantic check if we think it would be considerably faster than a whole-file semantic check
1300+
// We should only do the region-based semantic check if we think it would be
1301+
// considerably faster than a whole-file semantic check.
13001302
/** @internal */
13011303
protected shouldDoRegionCheck(file: NormalizedPath): boolean {
13021304
const lineCount = this.projectService.getScriptInfoForNormalizedPath(file)?.textStorage.getLineInfo().getLineCount();
1303-
return !!(lineCount && lineCount > 500);
1305+
return !!(lineCount && lineCount >= this.regionDiagLineCountThreshold);
13041306
}
13051307

1306-
private sendDiagnosticsEvent(file: NormalizedPath, project: Project, diagnostics: readonly Diagnostic[], kind: protocol.DiagnosticEventKind, spans?: TextSpan[]): void {
1308+
private sendDiagnosticsEvent(
1309+
file: NormalizedPath,
1310+
project: Project,
1311+
diagnostics: readonly Diagnostic[],
1312+
kind: protocol.DiagnosticEventKind,
1313+
diagnosticsStartTime: [number, number],
1314+
spans?: TextSpan[],
1315+
): void {
13071316
try {
13081317
const scriptInfo = Debug.checkDefined(project.getScriptInfo(file));
1309-
const duration = hrTimeToMilliseconds(this.hrtime(this.diagnosticsTime));
1318+
const duration = hrTimeToMilliseconds(this.hrtime(diagnosticsStartTime));
13101319

13111320
const body: protocol.DiagnosticEventBody = {
13121321
file,

src/testRunner/unittests/helpers/tsserver.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface TestSessionOptions extends ts.server.SessionOptions, TestTyping
7272
logger: LoggerWithInMemoryLogs;
7373
disableAutomaticTypingAcquisition?: boolean;
7474
useCancellationToken?: boolean | number;
75-
alwaysDoRegionDiagnostics?: boolean;
75+
regionDiagLineCountThreshold?: number;
7676
}
7777
export type TestSessionPartialOptionsAndHost = Partial<Omit<TestSessionOptions, "typingsInstaller" | "cancellationToken">> & Pick<TestSessionOptions, "host">;
7878
export type TestSessionConstructorOptions = TestServerHost | TestSessionPartialOptionsAndHost;
@@ -90,7 +90,6 @@ export class TestSession extends ts.server.Session {
9090
public override logger!: LoggerWithInMemoryLogs;
9191
public override readonly typingsInstaller!: TestTypingsInstallerAdapter;
9292
public serverCancellationToken: TestServerCancellationToken;
93-
private alwaysDoRegionDiagnostics: boolean;
9493

9594
constructor(optsOrHost: TestSessionConstructorOptions) {
9695
const opts = getTestSessionPartialOptionsAndHost(optsOrHost);
@@ -123,7 +122,9 @@ export class TestSession extends ts.server.Session {
123122
this,
124123
this.logger,
125124
);
126-
this.alwaysDoRegionDiagnostics = !!opts.alwaysDoRegionDiagnostics;
125+
if (opts.regionDiagLineCountThreshold !== undefined) {
126+
this.regionDiagLineCountThreshold = opts.regionDiagLineCountThreshold;
127+
}
127128
}
128129

129130
getProjectService() {
@@ -158,10 +159,6 @@ export class TestSession extends ts.server.Session {
158159
request.type = "request";
159160
return this.executeCommand(request);
160161
}
161-
162-
protected override shouldDoRegionCheck(file: ts.server.NormalizedPath): boolean {
163-
return this.alwaysDoRegionDiagnostics || super.shouldDoRegionCheck(file);
164-
}
165162
}
166163

167164
export function createSessionWithCustomEventHandler(

src/testRunner/unittests/tsserver/regionDiagnostics.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("unittests:: tsserver:: regionDiagnostics", () => {
2727
foo(10, 50);`,
2828
};
2929
const host = createServerHost([file1, libFile]);
30-
const session = new TestSession({ host, alwaysDoRegionDiagnostics: true });
30+
const session = new TestSession({ host, regionDiagLineCountThreshold: 0 });
3131

3232
openFilesForSession([file1], session);
3333

@@ -89,7 +89,7 @@ describe("unittests:: tsserver:: regionDiagnostics", () => {
8989

9090
const files = [file1, file2, file3, file4];
9191
const host = createServerHost([...files, libFile]);
92-
const session = new TestSession({ host, alwaysDoRegionDiagnostics: true });
92+
const session = new TestSession({ host, regionDiagLineCountThreshold: 0 });
9393

9494
openFilesForSession(files, session);
9595

@@ -148,7 +148,7 @@ describe("unittests:: tsserver:: regionDiagnostics", () => {
148148

149149
it("region has suggestion diagnostics", () => {
150150
const host = createServerHost([config, indexFile, otherFile, libFile]);
151-
const session = new TestSession({ host, alwaysDoRegionDiagnostics: true });
151+
const session = new TestSession({ host, regionDiagLineCountThreshold: 0 });
152152

153153
openFilesForSession([indexFile], session);
154154

@@ -177,7 +177,7 @@ describe("unittests:: tsserver:: regionDiagnostics", () => {
177177

178178
it("region does not have suggestion diagnostics", () => {
179179
const host = createServerHost([config, indexFile, otherFile, libFile]);
180-
const session = new TestSession({ host, alwaysDoRegionDiagnostics: true });
180+
const session = new TestSession({ host, regionDiagLineCountThreshold: 0 });
181181

182182
openFilesForSession([indexFile], session);
183183

@@ -218,7 +218,7 @@ describe("unittests:: tsserver:: regionDiagnostics", () => {
218218
foo(10, 50);`,
219219
};
220220
const host = createServerHost([file1, libFile]);
221-
const session = new TestSession({ host, alwaysDoRegionDiagnostics: false });
221+
const session = new TestSession({ host });
222222

223223
openFilesForSession([file1], session);
224224

@@ -250,7 +250,7 @@ describe("unittests:: tsserver:: regionDiagnostics", () => {
250250
foo(10, 50);`,
251251
};
252252
const host = createServerHost([file1, libFile]);
253-
const session = new TestSession({ host, alwaysDoRegionDiagnostics: true });
253+
const session = new TestSession({ host, regionDiagLineCountThreshold: 0 });
254254

255255
openFilesForSession([file1], session);
256256

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3455,7 +3455,7 @@ declare namespace ts {
34553455
private suppressDiagnosticEvents?;
34563456
private eventHandler;
34573457
private readonly noGetErrOnBackgroundUpdate?;
3458-
private diagnosticsTime;
3458+
protected regionDiagLineCountThreshold: number;
34593459
constructor(opts: SessionOptions);
34603460
private sendRequestCompletedEvent;
34613461
private addPerformanceData;

0 commit comments

Comments
 (0)