Skip to content

Commit d9f7f3b

Browse files
committed
refactor for later reuse of pname extraction
Signed-off-by: Jens Reinecke <[email protected]>
1 parent 23b48bf commit d9f7f3b

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/debug-configuration/subproviders/base-configuration-provider.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as vscode from 'vscode';
1818
import { ExtendedGDBTargetConfiguration, GDBTargetConfiguration } from '../gdbtarget-configuration';
1919
import { CbuildRunReader } from '../../cbuild-run';
2020
import { logger } from '../../logger';
21+
import { extractPname } from '../../utils';
2122

2223
export abstract class BaseConfigurationProvider implements vscode.DebugConfigurationProvider {
2324
protected _cbuildRunReader?: CbuildRunReader;
@@ -75,22 +76,12 @@ export abstract class BaseConfigurationProvider implements vscode.DebugConfigura
7576
extDebugConfig.definitionPath = svdFilePaths[0];
7677
}
7778

78-
protected extractPname(debugConfiguration: GDBTargetConfiguration): string | undefined {
79+
protected extractPnameFromDebugConfig(debugConfiguration: GDBTargetConfiguration): string | undefined {
7980
const pnames = this.cbuildRunReader.getPnames();
8081
if (!pnames.length) {
8182
return undefined;
8283
}
83-
// Config names in debugger templates are pretty free-form. Hence, can't do a lot
84-
// of format validation without reading debugger templates. Only check if name
85-
// begins with valid pname string, and if string is part of processor list.
86-
const configName = debugConfiguration.name.trim();
87-
const pnameRegexp = /[-_A-Za-z0-9]+\s+.+/;
88-
if (!pnameRegexp.test(configName)) {
89-
// Not the right format, Pname is 'RestrictedString' in PDSC format.
90-
return undefined;
91-
}
92-
const pname = configName.slice(0, configName.indexOf(' '));
93-
return pnames.includes(pname) ? pname : undefined;
84+
return extractPname(debugConfiguration.name, pnames);
9485
}
9586

9687
protected abstract resolveServerParameters(debugConfiguration: GDBTargetConfiguration): Promise<GDBTargetConfiguration>;
@@ -101,7 +92,7 @@ export abstract class BaseConfigurationProvider implements vscode.DebugConfigura
10192
_token?: vscode.CancellationToken
10293
): Promise<vscode.DebugConfiguration | null | undefined> {
10394
await this.parseCbuildRunFile(debugConfiguration);
104-
this.resolveSvdFile(debugConfiguration, this.extractPname(debugConfiguration));
95+
this.resolveSvdFile(debugConfiguration, this.extractPnameFromDebugConfig(debugConfiguration));
10596
return this.resolveServerParameters(debugConfiguration);
10697
}
10798

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ export const getCmsisPackRootPath = (): string => {
3232

3333
return cmsisPackRootDefault;
3434
};
35+
36+
export const extractPname = (configString: string, pnames?: string[]): string | undefined => {
37+
const trimmedString = configString.trim();
38+
// Config names in debugger templates are pretty free-form. Hence, can't do a lot
39+
// of format validation without reading debugger templates. Only check if name
40+
// begins with valid pname string, and if string is part of processor list.
41+
const pnameRegexp = /[-_A-Za-z0-9]+\s+.+/;
42+
if (!pnameRegexp.test(trimmedString)) {
43+
// Not the right format, Pname is 'RestrictedString' in PDSC format.
44+
return undefined;
45+
}
46+
const pname = trimmedString.slice(0, trimmedString.indexOf(' '));
47+
if (!pnames || pnames.includes(pname)) {
48+
return pname;
49+
}
50+
return undefined;
51+
};

0 commit comments

Comments
 (0)