Skip to content

Commit e91b696

Browse files
committed
refactor: move Features class to configuration.ts
1 parent 478a096 commit e91b696

File tree

5 files changed

+83
-94
lines changed

5 files changed

+83
-94
lines changed

src/configuration.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,3 +906,81 @@ export async function openConfigFileCommand() {
906906
export async function refreshConfigCommand(config: Configuration) {
907907
await config.refreshConfig();
908908
};
909+
910+
function version(ver: string): number {
911+
/*
912+
* Search SemVer string in form
913+
*
914+
* MAJOR.MINOR.PATCH
915+
*
916+
* where PATCH may be missing.
917+
*
918+
* We use regex because of suffixes that can be inside version string.
919+
* Like: '1.90.0-insiders' or '1.89.2-prerelease'.
920+
* So just split by '.' is not enough.
921+
*/
922+
const parse = /(\d+)\.(\d+)(\.(\d+))?/.exec(ver);
923+
924+
if (!parse?.length) {
925+
throw new Error(`Invalid SemVer string: ${ver}`);
926+
}
927+
928+
let result = 0;
929+
930+
/* X.Y.Z - 1, 2, 4 indexes in regex */
931+
result += parseInt(parse[1]) * 1000000;
932+
result += parseInt(parse[2]) * 1000;
933+
934+
if (parse[4]) {
935+
result += parseInt(parse[4]);
936+
}
937+
938+
if (Number.isNaN(result)) {
939+
throw new Error(`Invalid SemVer string: ${ver}. Result version number is NaN`);
940+
}
941+
942+
return result;
943+
}
944+
945+
/*
946+
* Various feature flags related to VS Code
947+
* functionality, that depends on API
948+
*/
949+
export class Features {
950+
static versionAtLeast(ver: string) {
951+
return version(ver) <= version(vscode.version);
952+
}
953+
static debugFocus: boolean | undefined = undefined;
954+
static debugFocusEnabled() {
955+
/*
956+
* Easily track debugger actions (breakpoints etc) and
957+
* selected call stack changes.
958+
*/
959+
return this.debugFocus ??= this.versionAtLeast('1.90.0');
960+
}
961+
962+
static arrayLengthFeature: boolean | undefined = undefined;
963+
static hasEvaluateArrayLength() {
964+
/*
965+
* Evaluate array length in debugger like `arrayPtr, length`.
966+
* This is only cppdbg feature.
967+
*/
968+
if (this.arrayLengthFeature === undefined) {
969+
const cppDbgExtension = vscode.extensions.getExtension('ms-vscode.cpptools');
970+
if (cppDbgExtension?.packageJSON.version) {
971+
const cppDbgVersion = version(cppDbgExtension.packageJSON.version);
972+
this.arrayLengthFeature = version('1.13.0') <= cppDbgVersion;
973+
} else {
974+
/* Safe default */
975+
this.arrayLengthFeature = false;
976+
}
977+
}
978+
return this.arrayLengthFeature;
979+
}
980+
981+
static hasLogOutputChannel() {
982+
/* used only during initialization - do not have to save it */
983+
return this.versionAtLeast('1.74.0');
984+
}
985+
}
986+

src/debugger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as dap from "./dap";
3-
import { Features } from './utils';
3+
import { Features } from './configuration';
44
import { PgVariablesViewProvider } from './variables';
55
import {EvaluationError} from './error';
66

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as path from 'path';
22
import * as vscode from 'vscode';
33

44
import * as utils from './utils';
5-
import { Features } from './utils';
65
import * as vars from './variables';
76
import * as dbg from './debugger';
87
import { Commands,
@@ -11,7 +10,8 @@ import { Commands,
1110
VsCodeSettings,
1211
openConfigFileCommand,
1312
refreshConfigCommand,
14-
setupConfiguration } from './configuration';
13+
setupConfiguration,
14+
Features } from './configuration';
1515
import { setupPgConfSupport } from './pgconf';
1616
import { PgindentDocumentFormatterProvider,
1717
setupFormatting } from './formatter';

src/logger.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { format } from 'util';
22
import * as vscode from 'vscode';
3-
import * as utils from './utils';
4-
import { ExtensionPrettyName, VsCodeSettings } from './configuration';
3+
import { ExtensionPrettyName, VsCodeSettings, Features } from './configuration';
54

65
interface ILogger {
76
debug: (message: string, args: unknown[]) => void;
@@ -141,7 +140,7 @@ export function initLogger(context: vscode.ExtensionContext) {
141140

142141
let outputChannel;
143142
let logger;
144-
if (utils.Features.hasLogOutputChannel()) {
143+
if (Features.hasLogOutputChannel()) {
145144
outputChannel = vscode.window.createOutputChannel(extName, {log: true});
146145
logger = new VsCodeLogger(outputChannel);
147146
} else {

src/utils.ts

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -362,91 +362,3 @@ export function getPgSrcFile(...paths: string[]) {
362362

363363
return path.join(...paths);
364364
}
365-
366-
/**
367-
* Return integer representation of SemVer version string
368-
*
369-
* @param ver Version string
370-
*/
371-
export function version(ver: string): number {
372-
/*
373-
* Search SemVer string in form
374-
*
375-
* MAJOR.MINOR.PATCH
376-
*
377-
* where PATCH may be missing.
378-
*
379-
* We use regex because of suffixes that can be inside version string.
380-
* Like: '1.90.0-insiders' or '1.89.2-prerelease'.
381-
* So just split by '.' is not enough.
382-
*/
383-
const parse = /(\d+)\.(\d+)(\.(\d+))?/.exec(ver);
384-
385-
if (!parse?.length) {
386-
throw new Error(`Invalid SemVer string: ${ver}`);
387-
}
388-
389-
let result = 0;
390-
391-
/* X.Y.Z - 1, 2, 4 indexes in regex */
392-
result += parseInt(parse[1]) * 1000000;
393-
result += parseInt(parse[2]) * 1000;
394-
395-
if (parse[4]) {
396-
result += parseInt(parse[4]);
397-
}
398-
399-
if (Number.isNaN(result)) {
400-
throw new Error(`Invalid SemVer string: ${ver}. Result version number is NaN`);
401-
}
402-
403-
return result;
404-
}
405-
406-
/*
407-
* Various feature flags related to VS Code
408-
* functionality, that depends on API
409-
*/
410-
let debugFocusEnabled: boolean | undefined = undefined;
411-
let hasArrayLengthFeature: boolean | undefined = undefined;
412-
let hasLogOutputChannel: boolean | undefined = undefined;
413-
414-
export class Features {
415-
static versionAtLeast(ver: string) {
416-
return version(ver) <= version(vscode.version);
417-
}
418-
419-
static debugFocusEnabled() {
420-
/*
421-
* Easily track debugger actions (breakpoints etc) and
422-
* selected call stack changes
423-
*/
424-
if (debugFocusEnabled === undefined) {
425-
debugFocusEnabled = this.versionAtLeast('1.90.0');
426-
}
427-
return debugFocusEnabled;
428-
}
429-
430-
static hasEvaluateArrayLength() {
431-
/* Evaluate array length in debugger like `arrayPtr, length' */
432-
if (hasArrayLengthFeature === undefined) {
433-
const cppDbgExtension = vscode.extensions.getExtension('ms-vscode.cpptools');
434-
if (cppDbgExtension?.packageJSON.version) {
435-
const cppDbgVersion = version(cppDbgExtension.packageJSON.version);
436-
hasArrayLengthFeature = version('1.13.0') <= cppDbgVersion;
437-
} else {
438-
/* Safe default */
439-
hasArrayLengthFeature = false;
440-
}
441-
}
442-
return hasArrayLengthFeature;
443-
}
444-
445-
static hasLogOutputChannel() {
446-
if (hasLogOutputChannel === undefined) {
447-
hasLogOutputChannel = this.versionAtLeast('1.74.0');
448-
}
449-
450-
return hasLogOutputChannel;
451-
}
452-
}

0 commit comments

Comments
 (0)