-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabstopreset.ps1
184 lines (163 loc) · 5.5 KB
/
tabstopreset.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
$psISE.Options.ShowOutlining = $true
$psISE.Options.ShowDefaultSnippets = $true
$psISE.Options.ShowIntellisenseInScriptPane = $true
$psISE.Options.ShowIntellisenseInConsolePane = $true
#
Set-Variable -Option AllScope -Name OptionSetter -Value (&{
$ClassName = 'IndentFixer'
$Namespace = 'ISEHijack'
Add-Type @"
internal class _Option<TValue>
{
public _Option(string key, TValue value)
{
Key = key;
Value = value;
}
public Object[] Params { get { return new object[] { Key, Value }; } }
public string Key { get; private set; }
public TValue Value { get; private set; }
}
internal static object[] Opt<TVal>(string key, TVal value)
{
return new _Option<TVal>(key, value).Params;
}
internal static readonly object[][] NewOpts = {
Opt<bool>("Adornments/HighlightCurrentLine/Enable", false),
Opt<int>("Tabs/TabSize", 4),
Opt<int>("Tabs/IndentSize", 4),
Opt<bool>("Tabs/ConvertTabsToSpaces", false),
Opt<bool>("TextView/UseVirtualSpace", false),
Opt<bool>("TextView/UseVisibleWhitespace", true)
};
internal void SetOpts()
{
foreach(object[] args in NewOpts) {
Setter.Invoke(Options, args);
}
}
public MethodInfo Setter { get; set; }
public object Options { get; set; }
public Dispatcher EditorDispatcher { get; set; }
public List<Action> Actions { get; private set; }
public ${ClassName}()
{
Actions = new List<Action>();
Actions.Add(SetOpts);
}
public void Dispatch(Dispatcher dispatcher)
{
DispatcherFrame frame = new DispatcherFrame();
dispatcher.BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(ExitFrames), frame);
Dispatcher.PushFrame(frame);
}
private object ExitFrames(object f){
DispatcherFrame frame = ((DispatcherFrame)f);
// foreach(Action action in Actions) {
// action.Invoke();
// }
// Actions.Clear();
foreach(object[] args in NewOpts) {
Setter.Invoke(Options, args);
}
frame.Continue = false;
return null;
}
"@ -Name $ClassName `
-NameSpace $Namespace `
-UsingNamespace System.Windows.Forms,System.Windows.Threading,System.Reflection,Microsoft.VisualStudio.Text.EditorOptions.Implementation,System.Collections.Generic `
-ReferencedAssemblies WindowsBase,System.Windows.Forms,Microsoft.PowerShell.Editor
return `
"${Namespace}.${ClassName}" |
Add-Member -Type NoteProperty -Name Namespace -Value $Namespace -Passthru |
Add-Member -Type NoteProperty -Name ClassName -Value $ClassName -Passthru
})
[System.Reflection.BindingFlags] $NonPublicFlags = [System.Reflection.BindingFlags] 'NonPublic,Instance'
filter Expand-Property
{
PARAM(
[Alias('Property')]
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory, Position=0)]
[String] $Name
)
$_ | Select-Object -ExpandProperty $Name | Write-Output
}
function Get-IsePrefs
{
PARAM(
[Parameter(Mandatory, Position=1)]
[ValidateNotNullOrEmpty()]
[string] $Key
)
$oEditor = $psISE.CurrentFile.Editor
$tEditor = $oEditor.GetType()
$tEditor.GetProperty('EditorOperations', $NonPublicFlags).GetMethod.Invoke($oEditor, $null).Options | `
Expand-Property GlobalOptions | `
Expand-Property SupportedOptions | `
Select-Object -Property Key,ValueType,DefaultValue | `
Write-Output
}
function Comment-Selection
{
$output = ''
$psISE.CurrentFile.Editor.SelectedText.Split("`n") | ForEach-Object -Process { $output += "# " + [string]$_ + "`n" }
$psISE.CurrentFile.Editor.InsertText($output)
}
function Fix-EditorIndentation
{
PARAM(
[Alias('ISEEditor')][ValidateNotNull()]
[Parameter(Mandatory, ValueFromPipeline, Position=1)]
[Microsoft.PowerShell.Host.ISE.ISEEditor]
$Editor
)
$EditorType = $Editor.GetType()
$SetterInstance = New-Object -TypeName $OptionSetter
$SetterInstance.Options = $EditorType.GetProperty('EditorOperations', $NonPublicFlags).GetMethod.Invoke($Editor, $null).Options
$Dispatcher = $EditorType.GetProperty('EditorViewHost', $NonPublicFlags).GetMethod.Invoke($Editor, $null).Dispatcher
$SetterInstance.Setter = $SetterInstance.Options.GetType().GetMethod('SetOptionValue', [type[]]@([string],[object]))
$SetterInstance.Dispatch($Dispatcher)
}
function Fix-IseIndentation
{
[Microsoft.Windows.PowerShell.Gui.Internal.MainWindow] $ISEWindow = (Get-Host | Select-Object -ExpandProperty PrivateData | Select-Object -ExpandProperty Window)
[Microsoft.Windows.PowerShell.Gui.Internal.RunspaceTabControl] $RSTabCtrl = $ISEWindow.Content[0].Children[2]
[Microsoft.PowerShell.Host.ISE.PowerShellTab] $PSTab = $RSTabCtrl.Items[0]
$PSTab.Files | Select-Object -ExpandProperty Editor | Fix-EditorIndentation
}
function Invoke-ISEFunction
{
PARAM(
[Parameter(Mandatory, Position=1)]
[Action] $ScriptBlock
)
$ISEWindow = $(Get-Host | Select-Object -ExpandProperty PrivateData | Select-Object -ExpandProperty Window)
return $ISEWindow.Dispatcher.Invoke($ScriptBlock)
}
function Fix-IndentationForAll
{
$psISE.PowerShellTabs | Select-Object -ExpandProperty 'Files' | Select-Object -ExpandProperty 'Editor' | Fix-EditorIndentation
}
# Startup
function New-Timer
{
PARAM(
[Alias('Handler')]
[Parameter(Position=0, Mandatory=$true)]
$Action,
[Double]
[Parameter(Mandatory=$false)]
$Interval = 50,
[Boolean]
[Parameter(Mandatory=$false)]
$AutoReset = $true
)
[System.Timers.Timer] $oTimer = New-Object -TypeName System.Timers.Timer($Interval)
$ElapsedEvent = Register-ObjectEvent -InputObject $oTimer -EventName 'Elapsed' -Action $Action
$oTimer.AutoReset = $AutoReset
return $oTimer
}
$(New-Timer {
Fix-IndentationForAll
} -Interval 100 -AutoReset $true).Start()