Skip to content

Commit c3d6a76

Browse files
authored
Fix diagnostics issue after saving unsaved-but-named buffer (#1810)
Fixes #1797: Diagnostics requested after saving unsaved-but-named buffer
1 parent 60ba788 commit c3d6a76

4 files changed

Lines changed: 52 additions & 13 deletions

File tree

client/src/common/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,7 @@ export abstract class BaseLanguageClient implements FeatureClient<Middleware, La
17101710
const params = this.code2ProtocolConverter.asChangeTextDocumentParams(document);
17111711
// We await the send and not the delivery since it is more or less the same for
17121712
// notifications.
1713+
this._didChangeTextDocumentFeature!.aboutToSendNotification(document, DidChangeTextDocumentNotification.type, params);
17131714
await connection.sendNotification(DidChangeTextDocumentNotification.type, params);
17141715
this._didChangeTextDocumentFeature!.notificationSent(document, DidChangeTextDocumentNotification.type, params);
17151716
}

client/src/common/diagnostic.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ class DiagnosticRequestor implements Disposable {
533533
return { kind: vsdiag.DocumentDiagnosticReportKind.full, items: [] };
534534
}
535535
if (token.isCancellationRequested) {
536-
throw new CancellationError()
536+
throw new CancellationError();
537537
}
538538
if (result === undefined || result === null) {
539539
return { kind: vsdiag.DocumentDiagnosticReportKind.full, items: [] };
@@ -1025,9 +1025,11 @@ class DiagnosticFeatureProviderImpl implements DiagnosticProviderShape {
10251025
}));
10261026
}
10271027

1028-
// When the document closes clear things up
1028+
// When the document closes clear things up. We do that right before the
1029+
// close notification is sent to the server so that it is not taken
1030+
// from the pull queue in case it is still there.
10291031
const closeFeature = client.getFeature(DidCloseTextDocumentNotification.method);
1030-
disposables.push(closeFeature.onNotificationSent((event) => {
1032+
disposables.push(closeFeature.onAboutToSendNotification((event) => {
10311033
this.cleanUpDocument(event.textDocument);
10321034
}));
10331035
disposables.push(notebookFeature.onCloseNotificationSent((event) => {

client/src/common/features.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,27 @@ interface CreateParamsSignature<E, P> {
260260
(data: E): RequestParam<P>;
261261
}
262262

263-
export interface NotificationSendEvent<P extends { textDocument: TextDocumentIdentifier }> {
263+
interface NotificationEvent<P extends { textDocument: TextDocumentIdentifier }> {
264264
textDocument: TextDocument;
265265
type: ProtocolNotificationType<P, TextDocumentRegistrationOptions>;
266266
params: RequestParam<P>;
267267
}
268268

269+
export interface AboutToSendNotificationEvent<P extends { textDocument: TextDocumentIdentifier }> extends NotificationEvent<P> {
270+
}
271+
272+
export interface NotificationSentEvent<P extends { textDocument: TextDocumentIdentifier }> extends NotificationEvent<P> {
273+
}
274+
275+
/**
276+
* @deprecated Use `NotificationSentEvent` instead.
277+
*/
278+
export interface NotificationSendEvent<P extends { textDocument: TextDocumentIdentifier }> extends NotificationSentEvent<P> {
279+
}
280+
269281
export interface NotifyingFeature<P extends { textDocument: TextDocumentIdentifier }> {
270-
onNotificationSent: VEvent<NotificationSendEvent<P>>;
282+
onAboutToSendNotification: VEvent<AboutToSendNotificationEvent<P>>;
283+
onNotificationSent: VEvent<NotificationSentEvent<P>>;
271284
}
272285

273286
/**
@@ -340,7 +353,8 @@ export abstract class TextDocumentEventFeature<P extends { textDocument: TextDoc
340353

341354
private _listener: Disposable | undefined;
342355
protected readonly _selectors: Map<string, VDocumentSelector>;
343-
private _onNotificationSent: EventEmitter<NotificationSendEvent<P>>;
356+
private _onAboutToSendNotification: EventEmitter<AboutToSendNotificationEvent<P>>;
357+
private _onNotificationSent: EventEmitter<NotificationSentEvent<P>>;
344358

345359
public static textDocumentFilter(selectors: IterableIterator<VDocumentSelector>, textDocument: TextDocument): boolean {
346360
for (const selector of selectors) {
@@ -365,7 +379,8 @@ export abstract class TextDocumentEventFeature<P extends { textDocument: TextDoc
365379
this._selectorFilter = selectorFilter;
366380

367381
this._selectors = new Map<string, VDocumentSelector>();
368-
this._onNotificationSent = new EventEmitter<NotificationSendEvent<P>>();
382+
this._onAboutToSendNotification = new EventEmitter<AboutToSendNotificationEvent<P>>();
383+
this._onNotificationSent = new EventEmitter<NotificationSentEvent<P>>();
369384
}
370385

371386
protected getStateInfo(): [IterableIterator<VDocumentSelector>, boolean] {
@@ -392,9 +407,11 @@ export abstract class TextDocumentEventFeature<P extends { textDocument: TextDoc
392407

393408
protected async callback(data: E): Promise<void> {
394409
const doSend = async (data: E): Promise<void> => {
410+
const textDocument = this.getTextDocument(data);
395411
const params = this._createParams(data);
412+
this.aboutToSendNotification(textDocument, this._type, params);
396413
await this._client.sendNotification(this._type, params);
397-
this.notificationSent(this.getTextDocument(data), this._type, params);
414+
this.notificationSent(textDocument, this._type, params);
398415
};
399416
if (this.matches(data)) {
400417
const middleware = this._middleware();
@@ -409,7 +426,15 @@ export abstract class TextDocumentEventFeature<P extends { textDocument: TextDoc
409426
return !this._selectorFilter || this._selectorFilter(this._selectors.values(), data);
410427
}
411428

412-
public get onNotificationSent(): VEvent<NotificationSendEvent<P>> {
429+
public get onAboutToSendNotification(): VEvent<AboutToSendNotificationEvent<P>> {
430+
return this._onAboutToSendNotification.event;
431+
}
432+
433+
protected aboutToSendNotification(textDocument: TextDocument, type: ProtocolNotificationType<P, TextDocumentRegistrationOptions>, params: RequestParam<P>): void {
434+
this._onAboutToSendNotification.fire({ textDocument, type, params });
435+
}
436+
437+
public get onNotificationSent(): VEvent<NotificationSentEvent<P>> {
413438
return this._onNotificationSent.event;
414439
}
415440

@@ -430,7 +455,7 @@ export abstract class TextDocumentEventFeature<P extends { textDocument: TextDoc
430455
public clear(): void {
431456
this._selectors.clear();
432457
this._onNotificationSent.dispose();
433-
this._onNotificationSent = new EventEmitter<NotificationSendEvent<P>>();
458+
this._onNotificationSent = new EventEmitter<NotificationSentEvent<P>>();
434459
if (this._listener) {
435460
this._listener.dispose();
436461
this._listener = undefined;

client/src/common/textSynchronization.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717

1818
import {
1919
FeatureClient, TextDocumentEventFeature, DynamicFeature, NextSignature, TextDocumentSendFeature, NotifyingFeature, ensure, RegistrationData, DynamicDocumentFeature,
20-
NotificationSendEvent
20+
NotificationSentEvent, AboutToSendNotificationEvent
2121
} from './features';
2222

2323
import * as UUID from './utils/uuid';
@@ -288,21 +288,27 @@ export class DidChangeTextDocumentFeature extends DynamicDocumentFeature<TextDoc
288288

289289
private _listener: Disposable | undefined;
290290
private readonly _changeData: Map<string, DidChangeTextDocumentData>;
291-
private readonly _onNotificationSent: EventEmitter<NotificationSendEvent<DidChangeTextDocumentParams>>;
291+
private readonly _onAboutToSendNotification: EventEmitter<AboutToSendNotificationEvent<DidChangeTextDocumentParams>>;
292+
private readonly _onNotificationSent: EventEmitter<NotificationSentEvent<DidChangeTextDocumentParams>>;
292293
private readonly _onPendingChangeAdded: EventEmitter<void>;
293294
private readonly _pendingTextDocumentChanges: Map<string, TextDocument>;
294295
private _syncKind: TextDocumentSyncKind;
295296

296297
constructor(client: FeatureClient<TextDocumentSynchronizationMiddleware>, pendingTextDocumentChanges: Map<string, TextDocument>) {
297298
super(client);
298299
this._changeData = new Map<string, DidChangeTextDocumentData>();
300+
this._onAboutToSendNotification = new EventEmitter();
299301
this._onNotificationSent = new EventEmitter();
300302
this._onPendingChangeAdded = new EventEmitter();
301303
this._pendingTextDocumentChanges = pendingTextDocumentChanges;
302304
this._syncKind = TextDocumentSyncKind.None;
303305
}
304306

305-
public get onNotificationSent(): Event<NotificationSendEvent<DidChangeTextDocumentParams>> {
307+
public get onAboutToSendNotification(): Event<AboutToSendNotificationEvent<DidChangeTextDocumentParams>> {
308+
return this._onAboutToSendNotification.event;
309+
}
310+
311+
public get onNotificationSent(): Event<NotificationSentEvent<DidChangeTextDocumentParams>> {
306312
return this._onNotificationSent.event;
307313
}
308314

@@ -375,6 +381,7 @@ export class DidChangeTextDocumentFeature extends DynamicDocumentFeature<TextDoc
375381
if (changeData.syncKind === TextDocumentSyncKind.Incremental) {
376382
const didChange = async (event: TextDocumentChangeEvent): Promise<void> => {
377383
const params = this._client.code2ProtocolConverter.asChangeTextDocumentParams(event, uri, version);
384+
this.aboutToSendNotification(event.document, DidChangeTextDocumentNotification.type, params);
378385
await this._client.sendNotification(DidChangeTextDocumentNotification.type, params);
379386
this.notificationSent(event.document, DidChangeTextDocumentNotification.type, params);
380387
};
@@ -395,6 +402,10 @@ export class DidChangeTextDocumentFeature extends DynamicDocumentFeature<TextDoc
395402
});
396403
}
397404

405+
public aboutToSendNotification(textDocument: TextDocument, type: ProtocolNotificationType<DidChangeTextDocumentParams, TextDocumentRegistrationOptions>, params: DidChangeTextDocumentParams): void {
406+
this._onAboutToSendNotification.fire({ textDocument, type, params });
407+
}
408+
398409
public notificationSent(textDocument: TextDocument, type: ProtocolNotificationType<DidChangeTextDocumentParams, TextDocumentRegistrationOptions>, params: DidChangeTextDocumentParams): void {
399410
this._onNotificationSent.fire({ textDocument, type, params });
400411
}

0 commit comments

Comments
 (0)