Skip to content

Commit

Permalink
Seanmcm/handle file changed (#4216)
Browse files Browse the repository at this point in the history
* Fix for #4211 and #3567: add handle file changed support. 
* Add back files.associations handling.
  • Loading branch information
sean-mcmanus authored Sep 10, 2019
1 parent 0d9ecd0 commit 17f2bcc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
37 changes: 36 additions & 1 deletion Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const GetCodeActionsRequest: RequestType<GetCodeActionsRequestParams, CodeAction
// Notifications to the server
const DidOpenNotification: NotificationType<DidOpenTextDocumentParams, void> = new NotificationType<DidOpenTextDocumentParams, void>('textDocument/didOpen');
const FileCreatedNotification: NotificationType<FileChangedParams, void> = new NotificationType<FileChangedParams, void>('cpptools/fileCreated');
const FileChangedNotification: NotificationType<FileChangedParams, void> = new NotificationType<FileChangedParams, void>('cpptools/fileChanged');
const FileDeletedNotification: NotificationType<FileChangedParams, void> = new NotificationType<FileChangedParams, void>('cpptools/fileDeleted');
const ResetDatabaseNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/resetDatabase');
const PauseParsingNotification: NotificationType<void, void> = new NotificationType<void, void>('cpptools/pauseParsing');
Expand Down Expand Up @@ -234,6 +235,7 @@ const ReportReferencesProgressNotification: NotificationType<refs.ReportReferenc
const RequestCustomConfig: NotificationType<string, void> = new NotificationType<string, void>('cpptools/requestCustomConfig');
const PublishDiagnosticsNotification: NotificationType<PublishDiagnosticsParams, void> = new NotificationType<PublishDiagnosticsParams, void>('cpptools/publishDiagnostics');
const ShowMessageWindowNotification: NotificationType<ShowMessageWindowParams, void> = new NotificationType<ShowMessageWindowParams, void>('cpptools/showMessageWindow');
const ReportTextDocumentLanguage: NotificationType<string, void> = new NotificationType<string, void>('cpptools/reportTextDocumentLanguage');

let failureMessageShown: boolean = false;

Expand Down Expand Up @@ -1182,9 +1184,21 @@ export class DefaultClient implements Client {
this.languageClient.onNotification(RequestCustomConfig, (e) => this.handleRequestCustomConfig(e));
this.languageClient.onNotification(PublishDiagnosticsNotification, (e) => this.publishDiagnostics(e));
this.languageClient.onNotification(ShowMessageWindowNotification, (e) => this.showMessageWindow(e));
this.languageClient.onNotification(ReportTextDocumentLanguage, (e) => this.setTextDocumentLanguage(e));
this.setupOutputHandlers();
}

private setTextDocumentLanguage(languageStr: string): void {
let cppSettings: CppSettings = new CppSettings(this.RootUri);
if (cppSettings.autoAddFileAssociations) {
const is_c: boolean = languageStr.startsWith("c;");
languageStr = languageStr.substr(is_c ? 2 : 1);
this.addFileAssociations(languageStr, is_c);
}
}

private associations_for_did_change: Set<string>;

/**
* listen for file created/deleted events under the ${workspaceFolder} folder
*/
Expand All @@ -1196,13 +1210,34 @@ export class DefaultClient implements Client {
this.rootPathFileWatcher = vscode.workspace.createFileSystemWatcher(
"**/*",
false /*ignoreCreateEvents*/,
true /*ignoreChangeEvents*/,
false /*ignoreChangeEvents*/,
false /*ignoreDeleteEvents*/);

this.rootPathFileWatcher.onDidCreate((uri) => {
this.languageClient.sendNotification(FileCreatedNotification, { uri: uri.toString() });
});

// TODO: Handle new associations without a reload.
this.associations_for_did_change = new Set<string>(["c", "i", "cpp", "cc", "cxx", "c++", "cp", "hpp", "hh", "hxx", "h++", "hp", "h", "ii", "ino", "inl", "ipp", "tcc", "idl"]);
let settings: OtherSettings = new OtherSettings(this.RootUri);
let assocs: any = settings.filesAssociations;
for (let assoc in assocs) {
let dotIndex: number = assoc.lastIndexOf('.');
if (dotIndex !== -1) {
let ext: string = assoc.substr(dotIndex + 1);
this.associations_for_did_change.add(ext);
}
}
this.rootPathFileWatcher.onDidChange((uri) => {
let dotIndex: number = uri.fsPath.lastIndexOf('.');
if (dotIndex !== -1) {
let ext: string = uri.fsPath.substr(dotIndex + 1);
if (this.associations_for_did_change.has(ext)) {
this.languageClient.sendNotification(FileChangedNotification, { uri: uri.toString() });
}
}
});

this.rootPathFileWatcher.onDidDelete((uri) => {
this.languageClient.sendNotification(FileDeletedNotification, { uri: uri.toString() });
});
Expand Down
12 changes: 8 additions & 4 deletions Extension/src/LanguageServer/protocolFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as path from 'path';
import { Middleware } from 'vscode-languageclient';
import { ClientCollection } from './clientCollection';
import { Client } from './client';
import { CppSettings } from './settings';

export function createProtocolFilter(me: Client, clients: ClientCollection): Middleware {
// Disabling lint for invoke handlers
Expand All @@ -26,10 +27,13 @@ export function createProtocolFilter(me: Client, clients: ClientCollection): Mid
me.TrackedDocuments.add(document);

// Work around vscode treating ".C" as c, by adding this file name to file associations as cpp
if (document.uri.path.endsWith(".C")) {
let fileName: string = path.basename(document.uri.fsPath);
let mappingString: string = fileName + "@" + document.uri.fsPath;
me.addFileAssociations(mappingString, false);
if (document.uri.path.endsWith(".C") && document.languageId === "c") {
let cppSettings: CppSettings = new CppSettings(me.RootUri);
if (cppSettings.autoAddFileAssociations) {
const fileName: string = path.basename(document.uri.fsPath);
const mappingString: string = fileName + "@" + document.uri.fsPath;
me.addFileAssociations(mappingString, false);
}
}

me.provideCustomConfiguration(document.uri, null);
Expand Down

0 comments on commit 17f2bcc

Please sign in to comment.