Skip to content

Commit 1206db1

Browse files
committed
Merge branch 'main' of https://github.com/microsoft/vscode-cpptools into dev/coleng/editorconfig_sections
2 parents c7cdbe8 + 9646722 commit 1206db1

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

Extension/src/LanguageServer/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,12 @@ export class DefaultClient implements Client {
12851285

12861286
// Listen for messages from the language server.
12871287
this.registerNotifications();
1288+
1289+
// If a file is already open when we activate, sometimes we don't get any notifications about visible
1290+
// or active text editors, visible ranges, or text selection. As a workaround, we trigger
1291+
// onDidChangeVisibleTextEditors here.
1292+
const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document));
1293+
await this.onDidChangeVisibleTextEditors(cppEditors);
12881294
}
12891295

12901296
// update all client configurations

Extension/src/LanguageServer/configurations.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class CppProperties {
138138
private configFileWatcherFallbackTime: Date = new Date(); // Used when file watching fails.
139139
private compileCommandsFile: vscode.Uri | undefined | null = undefined;
140140
private compileCommandsFileWatchers: fs.FSWatcher[] = [];
141-
private compileCommandsFileWatcherFallbackTime: Date = new Date(); // Used when file watching fails.
141+
private compileCommandsFileWatcherFallbackTime: Map<string, Date> = new Map<string, Date>(); // Used when file watching fails.
142142
private defaultCompilerPath: string | null = null;
143143
private knownCompilers?: KnownCompiler[];
144144
private defaultCStandard: string | null = null;
@@ -1093,6 +1093,10 @@ export class CppProperties {
10931093

10941094
if (configuration.compileCommands) {
10951095
configuration.compileCommands = this.resolvePath(configuration.compileCommands);
1096+
if (!this.compileCommandsFileWatcherFallbackTime.has(configuration.compileCommands)) {
1097+
// Start tracking the fallback time for a new path.
1098+
this.compileCommandsFileWatcherFallbackTime.set(configuration.compileCommands, new Date());
1099+
}
10961100
}
10971101

10981102
if (configuration.forcedInclude) {
@@ -1104,12 +1108,31 @@ export class CppProperties {
11041108
}
11051109
}
11061110

1111+
this.clearStaleCompileCommandsFileWatcherFallbackTimes();
11071112
this.updateCompileCommandsFileWatchers();
11081113
if (!this.configurationIncomplete) {
11091114
this.onConfigurationsChanged();
11101115
}
11111116
}
11121117

1118+
private clearStaleCompileCommandsFileWatcherFallbackTimes(): void {
1119+
// We need to keep track of relevant timestamps, so we cannot simply clear all entries.
1120+
// Instead, we clear entries that are no longer relevant.
1121+
const trackedCompileCommandsPaths: Set<string> = new Set();
1122+
this.configurationJson?.configurations.forEach((config: Configuration) => {
1123+
const path = this.resolvePath(config.compileCommands);
1124+
if (path.length > 0) {
1125+
trackedCompileCommandsPaths.add(path);
1126+
}
1127+
});
1128+
1129+
for (const path of this.compileCommandsFileWatcherFallbackTime.keys()) {
1130+
if (!trackedCompileCommandsPaths.has(path)) {
1131+
this.compileCommandsFileWatcherFallbackTime.delete(path);
1132+
}
1133+
}
1134+
}
1135+
11131136
private compileCommandsFileWatcherTimer?: NodeJS.Timeout;
11141137
private compileCommandsFileWatcherFiles: Set<string> = new Set<string>();
11151138

@@ -2310,14 +2333,18 @@ export class CppProperties {
23102333
fs.stat(compileCommandsFile, (err, stats) => {
23112334
if (err) {
23122335
if (err.code === "ENOENT" && this.compileCommandsFile) {
2336+
this.compileCommandsFileWatchers.forEach((watcher: fs.FSWatcher) => watcher.close());
23132337
this.compileCommandsFileWatchers = []; // reset file watchers
23142338
this.onCompileCommandsChanged(compileCommandsFile);
23152339
this.compileCommandsFile = null; // File deleted
23162340
}
2317-
} else if (stats.mtime > this.compileCommandsFileWatcherFallbackTime) {
2318-
this.compileCommandsFileWatcherFallbackTime = new Date();
2319-
this.onCompileCommandsChanged(compileCommandsFile);
2320-
this.compileCommandsFile = vscode.Uri.file(compileCommandsFile); // File created.
2341+
} else {
2342+
const compileCommandsLastChanged: Date | undefined = this.compileCommandsFileWatcherFallbackTime.get(compileCommandsFile);
2343+
if (compileCommandsLastChanged !== undefined && stats.mtime > compileCommandsLastChanged) {
2344+
this.compileCommandsFileWatcherFallbackTime.set(compileCommandsFile, new Date());
2345+
this.onCompileCommandsChanged(compileCommandsFile);
2346+
this.compileCommandsFile = vscode.Uri.file(compileCommandsFile); // File created.
2347+
}
23212348
}
23222349
});
23232350
}

Extension/src/LanguageServer/protocolFilter.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ import * as path from 'path';
88
import * as vscode from 'vscode';
99
import { Middleware } from 'vscode-languageclient';
1010
import * as util from '../common';
11-
import { logAndReturn } from '../Utility/Async/returns';
1211
import { Client } from './client';
1312
import { clients } from './extension';
1413
import { shouldChangeFromCToCpp } from './utils';
1514

1615
export const RequestCancelled: number = -32800;
1716
export const ServerCancelled: number = -32802;
1817

19-
let anyFileOpened: boolean = false;
20-
2118
export function createProtocolFilter(): Middleware {
2219
return {
2320
didOpen: async (document, sendMessage) => {
@@ -43,16 +40,7 @@ export function createProtocolFilter(): Middleware {
4340
// client.takeOwnership() will call client.TrackedDocuments.add() again, but that's ok. It's a Set.
4441
client.onDidOpenTextDocument(document);
4542
client.takeOwnership(document);
46-
void sendMessage(document).then(() => {
47-
// For a file already open when we activate, sometimes we don't get any notifications about visible
48-
// or active text editors, visible ranges, or text selection. As a workaround, we trigger
49-
// onDidChangeVisibleTextEditors here, only for the first file opened.
50-
if (!anyFileOpened) {
51-
anyFileOpened = true;
52-
const cppEditors: vscode.TextEditor[] = vscode.window.visibleTextEditors.filter(e => util.isCpp(e.document));
53-
client.onDidChangeVisibleTextEditors(cppEditors).catch(logAndReturn.undefined);
54-
}
55-
});
43+
void sendMessage(document);
5644
}
5745
}
5846
},

0 commit comments

Comments
 (0)