Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
51 changes: 37 additions & 14 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { DataBinding } from './dataBinding';
import { cachedEditorConfigSettings, getEditorConfigSettings } from './editorConfig';
import { CppSourceStr, clients, configPrefix, updateLanguageConfigurations, usesCrashHandler, watchForCrashes } from './extension';
import { LocalizeStringParams, getLocaleId, getLocalizedString } from './localization';
import { PersistentFolderState, PersistentWorkspaceState } from './persistentState';
import { PersistentFolderState, PersistentState, PersistentWorkspaceState } from './persistentState';
import { createProtocolFilter } from './protocolFilter';
import * as refs from './references';
import { CppSettings, OtherSettings, SettingsParams, WorkspaceFolderSettingsParams } from './settings';
Expand Down Expand Up @@ -277,6 +277,11 @@ interface IntelliSenseDiagnostic {
relatedInformation?: IntelliSenseDiagnosticRelatedInformation[];
}

interface TextDocumentLanguageInformation {
uri: string;
languageId: string;
}

interface RefactorDiagnostic {
range: Range;
code?: number;
Expand Down Expand Up @@ -472,6 +477,7 @@ interface SetTemporaryTextDocumentLanguageParams {
uri: string;
isC: boolean;
isCuda: boolean;
isPersistent: boolean;
}

enum CodeAnalysisScope {
Expand Down Expand Up @@ -610,7 +616,6 @@ const RequestCustomConfig: NotificationType<string> = new NotificationType<strin
const PublishRefactorDiagnosticsNotification: NotificationType<PublishRefactorDiagnosticsParams> = new NotificationType<PublishRefactorDiagnosticsParams>('cpptools/publishRefactorDiagnostics');
const ShowMessageWindowNotification: NotificationType<ShowMessageWindowParams> = new NotificationType<ShowMessageWindowParams>('cpptools/showMessageWindow');
const ShowWarningNotification: NotificationType<ShowWarningParams> = new NotificationType<ShowWarningParams>('cpptools/showWarning');
const ReportTextDocumentLanguage: NotificationType<string> = new NotificationType<string>('cpptools/reportTextDocumentLanguage');
const IntelliSenseSetupNotification: NotificationType<IntelliSenseSetup> = new NotificationType<IntelliSenseSetup>('cpptools/IntelliSenseSetup');
const SetTemporaryTextDocumentLanguageNotification: NotificationType<SetTemporaryTextDocumentLanguageParams> = new NotificationType<SetTemporaryTextDocumentLanguageParams>('cpptools/setTemporaryTextDocumentLanguage');
const ReportCodeAnalysisProcessedNotification: NotificationType<number> = new NotificationType<number>('cpptools/reportCodeAnalysisProcessed');
Expand Down Expand Up @@ -1758,9 +1763,19 @@ export class DefaultClient implements Client {
}
}

public onDidOpenTextDocument(document: vscode.TextDocument): void {
public async onDidOpenTextDocument(document: vscode.TextDocument): Promise<void> {

if (document.uri.scheme === "file") {
console.log(document.languageId);
const uri: string = document.uri.toString();
const textDocumentLanguagePersistentState: PersistentState<TextDocumentLanguageInformation | undefined> = new PersistentState<TextDocumentLanguageInformation | undefined>("CPP.textDocumentLanguage", undefined);
const persistentLanguage: string | undefined = textDocumentLanguagePersistentState.Value?.languageId;
const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri;

if (persistentFile === uri && persistentLanguage) {
await vscode.languages.setTextDocumentLanguage(document, persistentLanguage);
}

openFileVersions.set(uri, document.version);
void SessionState.buildAndDebugIsSourceFile.set(util.isCppOrCFile(document.uri));
void SessionState.buildAndDebugIsFolderOpen.set(util.isFolderOpen(document.uri));
Expand All @@ -1771,6 +1786,14 @@ export class DefaultClient implements Client {

public onDidCloseTextDocument(document: vscode.TextDocument): void {
const uri: string = document.uri.toString();
const textDocumentLanguagePersistentState: PersistentState<TextDocumentLanguageInformation | undefined> = new PersistentState<TextDocumentLanguageInformation | undefined>("CPP.textDocumentLanguage", undefined);
const persistentFile: string | undefined = textDocumentLanguagePersistentState.Value?.uri;
const persistentLanguage: string | undefined = textDocumentLanguagePersistentState.Value?.languageId;
console.log(document.languageId);
// If the file being closed has changed its language from the one we have stored, clear the stored language.
if (persistentFile === uri && persistentLanguage !== document.languageId) {
textDocumentLanguagePersistentState.Value = undefined;
}
if (this.semanticTokensProvider) {
this.semanticTokensProvider.removeFile(uri);
}
Expand Down Expand Up @@ -2365,7 +2388,6 @@ export class DefaultClient implements Client {
RegisterCodeAnalysisNotifications(this.languageClient);
this.languageClient.onNotification(ShowMessageWindowNotification, showMessageWindow);
this.languageClient.onNotification(ShowWarningNotification, showWarning);
this.languageClient.onNotification(ReportTextDocumentLanguage, (e) => this.setTextDocumentLanguage(e));
this.languageClient.onNotification(IntelliSenseSetupNotification, (e) => this.logIntelliSenseSetupTime(e));
this.languageClient.onNotification(SetTemporaryTextDocumentLanguageNotification, (e) => void this.setTemporaryTextDocumentLanguage(e));
this.languageClient.onNotification(ReportCodeAnalysisProcessedNotification, (e) => this.updateCodeAnalysisProcessed(e));
Expand Down Expand Up @@ -2428,21 +2450,22 @@ export class DefaultClient implements Client {
clients.timeTelemetryCollector.setUpdateRangeTime(realUri);
}

private setTextDocumentLanguage(languageStr: string): void {
const cppSettings: CppSettings = new CppSettings();
if (cppSettings.autoAddFileAssociations) {
const is_c: boolean = languageStr.startsWith("c;");
const is_cuda: boolean = languageStr.startsWith("cu;");
languageStr = languageStr.substring(is_c ? 2 : is_cuda ? 3 : 1);
this.addFileAssociations(languageStr, is_c ? "c" : is_cuda ? "cuda-cpp" : "cpp");
}
}

private async setTemporaryTextDocumentLanguage(params: SetTemporaryTextDocumentLanguageParams): Promise<void> {
const languageId: string = params.isC ? "c" : params.isCuda ? "cuda-cpp" : "cpp";
const uri: vscode.Uri = vscode.Uri.parse(params.uri);

const client: Client = clients.getClientFor(uri);
const document: vscode.TextDocument | undefined = client.TrackedDocuments.get(params.uri);
if (params.isPersistent) {
const textDocumentLanguagePersistentState: PersistentState<TextDocumentLanguageInformation | undefined> = new PersistentState<TextDocumentLanguageInformation | undefined>("CPP.textDocumentLanguage", undefined);
textDocumentLanguagePersistentState.Value = undefined;
const doc: vscode.TextDocument | undefined = await vscode.workspace.openTextDocument(params.uri);
await vscode.languages.setTextDocumentLanguage(doc, languageId);
console.log(textDocumentLanguagePersistentState.Value);
if (!textDocumentLanguagePersistentState.Value) {
textDocumentLanguagePersistentState.Value = {uri: doc.uri.toString(), languageId: languageId};
}
}
if (!!document && document.languageId !== languageId) {
if (document.languageId === "cpp" && languageId === "c") {
handleChangedFromCppToC(document);
Expand Down
1 change: 0 additions & 1 deletion Extension/src/LanguageServer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ export class CppSettings extends Settings {
public get autocomplete(): string { return this.getAsString("autocomplete"); }
public get autocompleteAddParentheses(): boolean { return this.getAsBoolean("autocompleteAddParentheses"); }
public get loggingLevel(): string { return this.getAsString("loggingLevel"); }
public get autoAddFileAssociations(): boolean { return this.getAsBoolean("autoAddFileAssociations"); }
public get workspaceParsingPriority(): string { return this.getAsString("workspaceParsingPriority"); }
public get workspaceSymbols(): string { return this.getAsString("workspaceSymbols"); }
public get exclusionPolicy(): string { return this.getAsString("exclusionPolicy"); }
Expand Down
6 changes: 1 addition & 5 deletions Extension/src/LanguageServer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as vscode from 'vscode';
import { Range } from 'vscode-languageclient';
import { SessionState } from '../sessionState';
import { Location, TextEdit } from './commonTypes';
import { CppSettings } from './settings';

export function makeLspRange(vscRange: vscode.Range): Range {
return {
Expand Down Expand Up @@ -37,10 +36,7 @@ export function rangeEquals(range1: vscode.Range | Range, range2: vscode.Range |
// Check this before attempting to switch a document from C to C++.
export function shouldChangeFromCToCpp(document: vscode.TextDocument): boolean {
if (document.fileName.endsWith(".C") || document.fileName.endsWith(".H")) {
const cppSettings: CppSettings = new CppSettings();
if (cppSettings.autoAddFileAssociations) {
return !docsChangedFromCppToC.has(document.fileName);
}
return !docsChangedFromCppToC.has(document.fileName);
// We could potentially add a new setting to enable switching to cpp even when files.associations isn't changed.
}
return false;
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
The C/C++ extension adds language support for C/C++ to Visual Studio Code, including [editing (IntelliSense)](https://code.visualstudio.com/docs/cpp/cpp-ide) and [debugging](https://code.visualstudio.com/docs/cpp/cpp-debug) features.

## Pre-requisites

C++ is a compiled language meaning your program's source code must be translated (compiled) before it can be run on your computer. VS Code is first and foremost an editor, and relies on command-line tools to do much of the development workflow. The C/C++ extension **does not include a C++ compiler or debugger**. You will need to install these tools or use those already installed on your computer.
* C++ compiler pre-installed
* C++ debugger pre-installed

* C++ compiler pre-installed
* C++ debugger pre-installed

<br/>

Here is a list of compilers and architectures per platform officially supported by the extension. These are reflected by the available [IntelliSense modes](https://code.visualstudio.com/docs/cpp/configure-intellisense-crosscompilation#_intellisense-mode) from the extension's IntelliSense configuration. Note that support for other compilers may be limited.

Platform | Compilers | Architectures
:--- | :--- | :---
:--- | :--- | :---
Windows | MSVC, Clang, GCC | x64, x86, arm64, arm
Linux | Clang, GCC | x64, x86, arm64, arm
macOS | Clang, GCC | x64, x86, arm64
Expand All @@ -26,17 +28,20 @@ For more information about installing the required tools or setting up the exten
<br/>

## Overview and tutorials

* [C/C++ extension overview](https://code.visualstudio.com/docs/languages/cpp)
* [Introductory Videos](https://code.visualstudio.com/docs/cpp/introvideos-cpp)

C/C++ extension tutorials per compiler and platform

* [Microsoft C++ compiler (MSVC) on Windows](https://code.visualstudio.com/docs/cpp/config-msvc)
* [GCC and Mingw-w64 on Windows](https://code.visualstudio.com/docs/cpp/config-mingw)
* [GCC on Windows Subsystem for Linux (WSL)](https://code.visualstudio.com/docs/cpp/config-wsl)
* [GCC on Linux](https://code.visualstudio.com/docs/cpp/config-linux)
* [Clang on macOS](https://code.visualstudio.com/docs/cpp/config-clang-mac)

## Quick links

* [Editing features (IntelliSense)](https://code.visualstudio.com/docs/cpp/cpp-ide)
* [IntelliSense configuration](https://code.visualstudio.com/docs/cpp/customize-default-settings-cpp)
* [Enhanced colorization](https://code.visualstudio.com/docs/cpp/colorization-cpp)
Expand Down Expand Up @@ -71,12 +76,12 @@ Contributions are always welcome. Please see our [contributing guide](CONTRIBUTI

## Microsoft Open Source Code of Conduct

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact opencode@microsoft.com with any additional questions or comments.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact <opencode@microsoft.com> with any additional questions or comments.

## Data Collection

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry via the same setting provided by Visual Studio Code: `"telemetry.enableTelemetry"`. Our privacy statement is located [here](https://go.microsoft.com/fwlink/?LinkID=824704). You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.

## Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.
4 changes: 3 additions & 1 deletion launch.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
The documentation for debug configuration has moved to https://code.visualstudio.com/docs/cpp/launch-json-reference.
# Debug Configuration

The documentation for debug configuration has moved to <https://code.visualstudio.com/docs/cpp/launch-json-reference>.