Skip to content

Commit bb9309b

Browse files
committed
[pwsh keymap] Add keymap conflicts analyzer PSReadline vs VSCode.
1 parent c396d86 commit bb9309b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Copyright (c) 2023 Matthias Wolf, Mawosoft.
2+
3+
<#
4+
.SYNOPSIS
5+
Gets conflicting keymappings for PSReadLine and VSCode.
6+
.OUTPUTS
7+
Objects containing the conflicting binding objects from both sources per first normalized chord.
8+
#>
9+
10+
using namespace System
11+
using namespace System.Collections.Generic
12+
13+
[CmdletBinding()]
14+
[OutputType([psobject])]
15+
param(
16+
# Paths to JSON files containing the key mappings.
17+
# The path for VSCode mappings is required, the path for PSReadLine is optional.
18+
# If the latter is not provided, the currently active mappings are used.
19+
[Parameter(Mandatory)]
20+
[ValidateNotNullOrEmpty()]
21+
[ValidateCount(1, 2)]
22+
[string[]]$Path
23+
)
24+
25+
. "$PSScriptRoot/KeyMapGerman.ps1"
26+
27+
$vscode = $null
28+
$psreadline = $null
29+
foreach ($_path in $Path) {
30+
$json = Get-Content $_path -Raw | ConvertFrom-Json
31+
if (-not $json) {
32+
throw "No content: $_path"
33+
}
34+
else {
35+
$p = $json[0].psobject.Properties
36+
if ($p['key'] -and $p['command']) {
37+
$vscode = $json
38+
}
39+
elseif ($p['Key'] -and $p['Function']) {
40+
$psreadline = $json
41+
}
42+
else {
43+
throw "Unknown file format: $_path"
44+
}
45+
}
46+
}
47+
48+
if (-not $vscode) {
49+
throw 'No VSCode key mappings provided.'
50+
}
51+
52+
if (-not $psreadline) {
53+
$psreadline = Get-PSReadLineKeyHandler
54+
}
55+
56+
$vscode.Where({
57+
-not $_.psobject.Properties['when'] -or
58+
$_.command.Contains('terminal', [StringComparison]::OrdinalIgnoreCase) -or
59+
$_.when.Contains('terminal', [StringComparison]::OrdinalIgnoreCase)
60+
}).ForEach({
61+
[Chord[]]$chords = [KeyMapGerman]::ParseChord($_.key, ' ', $true)
62+
[PSCustomObject]@{
63+
FirstNormalized = $chords[0].Normalized
64+
Source = 'VSCode'
65+
Item = $_
66+
}
67+
}) +
68+
$psreadline.ForEach({
69+
[Chord[]]$chords = [KeyMapGerman]::ParseChord($_.Key, ',', $true)
70+
[PSCustomObject]@{
71+
FirstNormalized = $chords[0].Normalized
72+
Source = 'PSReadLine'
73+
Item = $_
74+
}
75+
}) |
76+
Group-Object -Property FirstNormalized -CaseSensitive |
77+
ForEach-Object {
78+
if ($_.Count -gt 1) {
79+
$sources = $_.Group | Group-Object -Property Source -CaseSensitive
80+
if ($sources -is [array] -and $sources.Count -gt 1) {
81+
[PSCustomObject]@{
82+
FirstNormalized = $_.Name
83+
$sources[0].Name = $sources[0].Group | Select-Object -ExpandProperty Item
84+
$sources[1].Name = $sources[1].Group | Select-Object -ExpandProperty Item
85+
# If we want to force arrays:
86+
# $sources[0].Name = [object[]]($sources[0].Group | Select-Object -ExpandProperty Item)
87+
# $sources[1].Name = [object[]]($sources[1].Group | Select-Object -ExpandProperty Item)
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)