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
38 changes: 6 additions & 32 deletions qt-cpp/src/commands/natvis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';

import { getSelectedKit, IsQtKit } from '@cmd/register-qt-path';
import { createLogger, telemetry } from 'qt-lib';
import { EXTENSION_ID } from '@/constants';
import { QtVersionFromKit } from '@/util/util';
import { CppProjectType, getActiveProject } from '@/project';
import { coreAPI } from '@/extension';
import { getActiveProject } from '@/project';

const logger = createLogger('natvis');

Expand Down Expand Up @@ -54,35 +51,12 @@ export function registerNatvisCommand() {
logger.error('Cannot find an active project');
return '';
}
if (project.type === CppProjectType.Presets) {
const qtpaths = await project.getQtPaths();
if (!qtpaths) {
const error = 'Cannot find Qt Paths in the project presets';
throw new Error(error);
}
const qtInfo = coreAPI?.getQtInfoFromPath(qtpaths);
const majorVersion = qtInfo?.info?.get('QT_VERSION')?.split('.')[0];
if (!majorVersion) {
throw new Error('Cannot determine the major version');
}
return getNatvis(majorVersion);
} else {
// CppProjectType.Kit
const kit = await getSelectedKit();
if (!kit || !IsQtKit(kit)) {
const error = `${kit?.name ?? ''} is not a Qt kit`;
throw new Error(error);
}
const version = QtVersionFromKit(kit);
if (version) {
const majorVersion = version.split('.')[0];
if (!majorVersion) {
throw new Error('Cannot determine the major version');
}
return getNatvis(majorVersion);
}
// Ask for a kit if none is selected
const majorVersion = await project.getMajorQtVersion(false);
if (!majorVersion) {
throw new Error('Cannot determine the major version');
}
return undefined;
return getNatvis(majorVersion);
}
);
const natvis5Disposal = vscode.commands.registerCommand(
Expand Down
89 changes: 69 additions & 20 deletions qt-cpp/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import {
getActiveFolder,
getQtInsRoot,
getQtPathsExe,
getSelectedKit
getSelectedKit,
IsQtKit
} from '@cmd/register-qt-path';
import { analyzeKit, Kit as CMakeKit } from '@/kit-manager';
import * as cmakeFileApi from '@/cmake-file-api';
import { getMajorQtVersion, isValidAndString } from '@/util/util';
import { QtVersionFromKit, isValidAndString } from '@/util/util';
import { hasCMakeSources } from '@/util/cmake-sources';

const logger = createLogger('project');
Expand Down Expand Up @@ -369,21 +370,24 @@ export class CppProject implements Project {
}
}
}
// macOS framework names (e.g. QtCore) do not contain the major version
const majorVersion = host.isMacOS
? await this.getMajorQtVersion()
: undefined;
if (host.isMacOS && !majorVersion) {
logger.warn('Cannot determine the major Qt version for Qt modules.');
}
let changed = false;
for (const t of targetJsonFiles) {
let modules = CppProject.parseCmakeFileApi(t.filePath, buildType);
if (isEmpty(modules)) {
continue;
}
if (host.isMacOS && this._type === CppProjectType.Kit) {
const majorVersion = await getMajorQtVersion();
if (majorVersion) {
modules = modules.map((module) => {
module = module.replace('Qt', `Qt${majorVersion}`);
return module;
});
}
}
modules = modules.map((module) =>
CppProject.refineModuleName(module, majorVersion)
);
// Remove duplicates
modules = [...new Set(modules)];

const qtModules = modules.filter((module) => module.startsWith('Qt'));
if (qtModules.length > 0) {
Expand Down Expand Up @@ -428,6 +432,53 @@ export class CppProject implements Project {
await this.getStateManager().setModules(currentModules);
}
}
async getMajorQtVersion(silent = true) {
let version: string | undefined;
if (this._type === CppProjectType.Presets) {
const qtpaths = await this.getQtPaths();
if (qtpaths) {
version = coreAPI?.getQtInfoFromPath(qtpaths).info?.get('QT_VERSION');
}
} else {
// CppProjectType.Kit
const kit = await getSelectedKit(this.folder, silent);
if (kit && IsQtKit(kit)) {
version = QtVersionFromKit(kit);
}
}
return version?.split('.')[0];
}
private static refineModuleName(name: string, majorVersion?: string) {
let refinedName = name;
// Remove "lib" prefix
if (refinedName.startsWith('lib')) {
refinedName = refinedName.slice(3);
}
// Remove architecture suffix
const architectureSuffixes = [
'_arm64-v8a',
'_armeabi-v7a',
'_x86_64',
'_x86'
];
for (const suffix of architectureSuffixes) {
if (refinedName.endsWith(suffix)) {
refinedName = refinedName.slice(0, -suffix.length);
break;
}
}
// Qt6Xml_TP should be refined to Qt6Xml
// We should remove any trailing suffixes after an underscore
const underscoreIndex = refinedName.indexOf('_');
if (underscoreIndex !== -1) {
refinedName = refinedName.slice(0, underscoreIndex);
}
if (majorVersion) {
// Skip names that already have a version (Qt6Core), but not Qt3DCore
refinedName = refinedName.replace(/^Qt(?![56])/, `Qt${majorVersion}`);
}
return refinedName;
}
private static parseCmakeFileApi(file: string, buildType: string) {
try {
const fileContent = fs.readFileSync(file, 'utf8');
Expand All @@ -443,9 +494,6 @@ export class CppProject implements Project {
buildType
);
}

// Remove duplicates
frameworks = [...new Set(frameworks)];
return frameworks;
} catch (error) {
logger.info(
Expand Down Expand Up @@ -503,13 +551,14 @@ export class CppProject implements Project {
return fragment.includes('Qt6') || fragment.includes('Qt5');
});
const refinedFragments = filteredFragment.map((fragment) => {
// Remove the path and extension and get the name without lib prefix
// Remove the path and extension
const name = path.parse(fragment).name;
if (buildType === 'Debug' || buildType === 'RelWithDebInfo') {
// Check if the name contains 'd' at the end
if (name.endsWith('d')) {
return name.slice(0, -1);
}
// Remove debug suffix
if (
(buildType === 'Debug' || buildType === 'RelWithDebInfo') &&
name.endsWith('d')
) {
return name.slice(0, -1);
}
return name;
});
Expand Down
20 changes: 1 addition & 19 deletions qt-cpp/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

import * as path from 'path';

import {
getQtInsRoot,
getQtPathsExe,
getSelectedKit,
IsQtKit
} from '@/commands/register-qt-path';
import { getQtInsRoot, getQtPathsExe } from '@/commands/register-qt-path';
import { coreAPI } from '@/extension';
import { Kit } from '@/kit-manager';

Expand Down Expand Up @@ -42,16 +37,3 @@ export function QtVersionFromKit(kit: Kit) {
}
return undefined;
}

export async function getMajorQtVersion() {
const kit = await getSelectedKit();
if (!kit || !IsQtKit(kit)) {
return undefined;
}
const version = QtVersionFromKit(kit);
if (version) {
const majorVersion = version.split('.')[0];
return majorVersion;
}
return undefined;
}
Loading