Skip to content

Commit 56fe520

Browse files
author
Arnold Trakhtenberg
authored
Release 2.2.2 (#33)
* fix caching of flag metadata and improve formatting of hover display * int * prepare 2.2.2 * fix typo
1 parent 01df781 commit 56fe520

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to the "launchdarkly" extension will be documented in this file.
44

5+
## [2.2.2] = 2020-02-24
6+
7+
### Changed
8+
9+
- Improved formatting and readability of hover display
10+
- Feature flag metadata used for hover is now cached
11+
512
## [2.2.1] - 2020-02-21
613

714
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "launchdarkly",
33
"displayName": "LaunchDarkly",
44
"description": "View LaunchDarkly feature flags in your editor.",
5-
"version": "2.2.1",
5+
"version": "2.2.2",
66
"publisher": "launchdarkly",
77
"engines": {
88
"vscode": "^1.34.0"

src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class LaunchDarklyAPI {
3939
const envParam = envKey ? '?env=' + envKey : '';
4040
const options = this.createOptions(`flags/${projectKey}/${flagKey + envParam}`);
4141
const data = await rp(options);
42-
return JSON.parse(data);
42+
return new Flag(JSON.parse(data));
4343
}
4444

4545
private createOptions(path: string) {

src/flagStore.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,21 @@ export class FlagStore {
122122
this.store.get(DATA_KIND, key, async (res: FlagConfiguration) => {
123123
if (!res) {
124124
resolve(null);
125+
return;
125126
}
126-
if (!flag || flag.environments[this.config.env].version < res.version) {
127+
128+
if (!flag || flag.environmentVersion(this.config.env) < res.version) {
127129
try {
128130
flag = await this.api.getFeatureFlag(this.config.project, key, this.config.env);
129131
this.flagMetadata[key] = flag;
130-
resolve({ flag, config: res });
131132
} catch (e) {
132133
console.error(`Could not retrieve feature flag metadata for ${key}: ${e}`);
133134
reject(e);
135+
return;
134136
}
135137
}
138+
139+
resolve({ flag, config: res });
136140
});
137141
});
138142
}

src/models.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export class Resource {
22
name: string;
33
key: string;
44
tags: Array<string>;
5+
_version: number;
56
}
67

78
export class Project extends Resource {
@@ -16,6 +17,18 @@ export class Environment extends Resource {
1617

1718
export class Flag extends Resource {
1819
environments: Map<String, Environment>;
20+
21+
constructor(init?: Partial<Flag>) {
22+
super();
23+
Object.assign(this, init);
24+
}
25+
26+
environmentVersion(env: string): number {
27+
if (!this.environments[env]) {
28+
return -1;
29+
}
30+
return this._version + this.environments[env].version;
31+
}
1932
}
2033

2134
export class FlagConfiguration {

src/providers.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Position,
1313
Range,
1414
TextDocument,
15+
MarkdownString,
1516
} from 'vscode';
1617
import * as url from 'url';
1718
import opn = require('opn');
@@ -113,13 +114,13 @@ class LaunchDarklyHoverProvider implements HoverProvider {
113114
public provideHover(document: TextDocument, position: Position): Thenable<Hover> {
114115
return new Promise(async (resolve, reject) => {
115116
if (this.config.enableHover) {
116-
let candidate = document.getText(document.getWordRangeAtPosition(position, FLAG_KEY_REGEX));
117+
const candidate = document.getText(document.getWordRangeAtPosition(position, FLAG_KEY_REGEX));
117118
try {
118-
let data =
119+
const data =
119120
(await this.flagStore.getFeatureFlag(candidate)) ||
120121
(await this.flagStore.getFeatureFlag(kebabCase(candidate)));
121122
if (data) {
122-
let hover = generateHoverString(data.flag, data.config);
123+
const hover = generateHoverString(data.flag, data.config);
123124
resolve(new Hover(hover));
124125
return;
125126
}
@@ -180,19 +181,31 @@ const openFlagInBrowser = async (config: Configuration, flagKey: string, flagSto
180181
};
181182

182183
export function generateHoverString(flag: Flag, c: FlagConfiguration) {
183-
return `**LaunchDarkly feature flag**\n
184-
Name: ${flag.name}
185-
Key: ${c.key}
186-
Enabled: ${c.on}
187-
Default variation: ${JSON.stringify(c.variations[c.fallthrough.variation])}
188-
Off variation: ${JSON.stringify(c.variations[c.offVariation])}
189-
${plural(c.prerequisites.length, 'prerequisite', 'prerequisites')}
190-
${plural(
191-
c.targets.reduce((acc, curr) => acc + curr.values.length, 0),
192-
'user target',
193-
'user targets',
194-
)}
195-
${plural(c.rules.length, 'rule', 'rules')}`;
184+
const fields = [
185+
['Name', flag.name],
186+
['Key', c.key],
187+
['Enabled', c.on],
188+
['Default variation', JSON.stringify(c.variations[c.fallthrough.variation], null, 2)],
189+
['Off variation', JSON.stringify(c.variations[c.offVariation], null, 2)],
190+
[plural(c.prerequisites.length, 'prerequisite', 'prerequisites')],
191+
[
192+
plural(
193+
c.targets.reduce((acc, curr) => acc + curr.values.length, 0),
194+
'user target',
195+
'user targets',
196+
),
197+
],
198+
[plural(c.rules.length, 'rule', 'rules')],
199+
];
200+
let hoverString = new MarkdownString(`**LaunchDarkly feature flag**`);
201+
fields.forEach(field => {
202+
hoverString = hoverString.appendText('\n' + `${field[0]}`);
203+
if (field.length == 2) {
204+
hoverString = hoverString.appendText(`: `);
205+
hoverString = hoverString.appendCodeblock(`${field[1]}`);
206+
}
207+
});
208+
return hoverString;
196209
}
197210

198211
function plural(count: number, singular: string, plural: string) {

test/providers.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import * as vscode from 'vscode';
55
import * as providers from '../src/providers';
66
import { Flag, FlagConfiguration } from '../src/models';
77

8-
const flag: Flag = {
8+
const flag = new Flag ({
99
name: "Test",
1010
key: "test",
1111
tags: [],
1212
environments: null,
13-
}
13+
_version: 1,
14+
});
1415

1516
const flagConfig: FlagConfiguration = {
1617
key: 'test',
@@ -31,8 +32,8 @@ let testPath = path.join(__dirname, '..', '..', 'test');
3132
suite('provider utils tests', () => {
3233
test('generateHoverString', () => {
3334
assert.equal(
34-
`**LaunchDarkly feature flag**\n\n\tName: Test\n\tKey: test\n\tEnabled: true\n\tDefault variation: "SomeVariation"\n\tOff variation: {"thisIsJson":"AnotherVariation"}\n\t1 prerequisite\n\t3 user targets\n\t0 rules`,
35-
providers.generateHoverString(flag, flagConfig),
35+
"**LaunchDarkly feature flag**\n\nName: \n```\nTest\n```\n\n\nKey: \n```\ntest\n```\n\n\nEnabled: \n```\ntrue\n```\n\n\nDefault variation: \n```\n\"SomeVariation\"\n```\n\n\nOff variation: \n```\n{\n \"thisIsJson\": \"AnotherVariation\"\n}\n```\n\n\n1 prerequisite\n\n3 user targets\n\n0 rules",
36+
providers.generateHoverString(flag, flagConfig).value,
3637
);
3738
});
3839

0 commit comments

Comments
 (0)