Skip to content

Commit

Permalink
update mode default resolution logic to overwrite, not merge (#35)
Browse files Browse the repository at this point in the history
Fixes #34. I used the lodash functions instead of object splatting so I
didn't have to deal with typing the function 😬.

Tested with the following config to ensure that modes were overridden,
but args / computed args were merged.

```toml
[header]
name = "Debug Key Bindings"
version = "1.0"

[[mode]]
name = "a"
default = true

[[mode]]
name = "b"

[[path]]
id = "actions"
name = "Actions"
default.mode = ["a", "b"]
default.args.to = "up"
default.args.select = false

[[bind]]
path = "actions"
key = "x"
name = "x"
command = "cursorMove"
args = { to = "down"}
mode = ["a"]

[[bind]]
path = "actions"
key = "x"
name = "x"
command = "cursorMove"
computedArgs = { select = true }
mode = ["b"]
```

---------

Co-authored-by: David Little <david.frank.little@gmail.com>
  • Loading branch information
bhainesva and haberdashPI authored Oct 2, 2024
1 parent b01929e commit 000f525
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Master Key",
"publisher": "haberdashPI",
"description": "Master your keybindings with documentation, discoverability, modal bindings, macros and expressive configuration",
"version": "0.3.6",
"version": "0.3.7",
"icon": "logo.png",
"repository": {
"url": "https://github.com/haberdashPi/vscode-master-key"
Expand Down
20 changes: 7 additions & 13 deletions src/web/keybindings/processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import {
pick,
isEqual,
omit,
mergeWith,
cloneDeep,
flatMap,
mapValues,
merge,
assignWith,
} from 'lodash';
import {reifyStrings, EvalContext} from '../expressions';
import {isSingleCommand, validateInput, IIndexed} from '../utils';
Expand Down Expand Up @@ -78,17 +78,11 @@ function mapByName(specs: ModeSpec[]) {
return modeSpecs;
}

function overwritePrefixesAndWhen(obj_: RawBindingItem, src_: RawBindingItem, key: string) {
if (key === 'prefixes' || key === 'when') {
if (src_ !== undefined) {
return src_;
} else {
return obj_;
}
} else {
// revert to default behavior
return;
function mergeArgs(obj_: RawBindingItem, src_: RawBindingItem, key: string) {
if (key === 'args' || key === 'computedArgs') {
return merge(obj_, src_);
}
return;
}

const runCommandsArgs = z.object({
Expand Down Expand Up @@ -157,7 +151,7 @@ function expandDefaultsDefinedAndForeach(
whens = cloneDeep(pathWhens[prefix]);
}
}
pathDefaults[path.id] = mergeWith(defaults, path.default, overwritePrefixesAndWhen);
pathDefaults[path.id] = assignWith(defaults, path.default, mergeArgs);
if (path.when) {
pathWhens[path.id] = whens.concat(path.when);
}
Expand All @@ -170,7 +164,7 @@ function expandDefaultsDefinedAndForeach(
problems.push(`The path '${item.path}' is undefined.`);
return undefined;
} else {
item = mergeWith(cloneDeep(itemDefault), item, overwritePrefixesAndWhen);
item = assignWith(cloneDeep(itemDefault), item, mergeArgs);
if (item.when) {
item.when = itemConcatWhen ? item.when.concat(itemConcatWhen) : item.when;
} else if (itemConcatWhen) {
Expand Down
64 changes: 64 additions & 0 deletions test/specs/config.ux.mts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,70 @@ describe('Configuration', () => {
expect(messages).toContainEqual(error);
});

it('Can overwrite default mode', async () => {
await setBindings(`
[header]
name = "Debug Key Bindings"
version = "1.0"
[[mode]]
name = "a"
default = true
[[mode]]
name = "b"
[[bind]]
name = "mode a"
key = "ctrl+["
command = "master-key.setMode"
args.value = "a"
[[bind]]
name = "mode b"
key = "ctrl+]"
command = "master-key.setMode"
args.value = "b"
[[path]]
id = "actions"
name = "Actions"
default.mode = ["a", "b"]
default.args.to = "up"
default.args.select = false
[[bind]]
path = "actions"
key = "ctrl+h"
name = "x"
command = "cursorMove"
args = { to = "down"}
mode = ["a"]
[[bind]]
path = "actions"
key = "ctrl+h"
name = "x"
command = "cursorMove"
computedArgs = { select = true }
mode = ["b"]
`);

editor = await setupEditor('A simple test\nwith two lines');
await editor.moveCursor(1, 1);

await movesCursorInEditor(
async () => await enterModalKeys(['ctrl', 'h']),
[1, 0],
editor
);

await enterModalKeys(['ctrl', ']']);
await waitForMode('b');
await enterModalKeys(['ctrl', 'h']);
expect(await editor.getSelectedText()).toEqual('A simple test\n');
});

after(async () => {
const workbench = await browser.getWorkbench();
await workbench.executeCommand('Clear Command History');
Expand Down
2 changes: 0 additions & 2 deletions wdio.conf.mts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ export const config: Options.Testrunner = {
workspacePath: __dirname,
vscodeArgs: {
profile: 'debug-profile',
'enable-features':
'ConversionMeasurement,AttributionReportingCrossAppWeb',
},
storagePath: __dirname + '/.wdio-vscode-service/storage/',
},
Expand Down

0 comments on commit 000f525

Please sign in to comment.