11using System ;
22using System . Collections . Generic ;
3-
3+ using System . Runtime . CompilerServices ;
4+
45namespace Microsoft . PowerShell
56{
67 public partial class PSConsoleReadLine
@@ -22,9 +23,17 @@ internal enum TextObjectSpan
2223 private TextObjectOperation _textObjectOperation = TextObjectOperation . None ;
2324 private TextObjectSpan _textObjectSpan = TextObjectSpan . None ;
2425
25- private readonly Dictionary < TextObjectOperation , Dictionary < TextObjectSpan , KeyHandler > > _textObjectHandlers = new ( )
26+ private readonly Dictionary < TextObjectOperation , Dictionary < TextObjectSpan , Dictionary < PSKeyInfo , KeyHandler > > > _textObjectHandlers = new ( )
2627 {
27- [ TextObjectOperation . Delete ] = new ( ) { [ TextObjectSpan . Inner ] = MakeKeyHandler ( ViDeleteInnerWord , "ViDeleteInnerWord" ) } ,
28+ [ TextObjectOperation . Delete ] = new ( )
29+ {
30+ [ TextObjectSpan . Inner ] = new ( )
31+ {
32+ [ Keys . DQuote ] = MakeKeyHandler ( ViDeleteInnerDQuote , "ViDeleteInnerDQuote" ) ,
33+ [ Keys . SQuote ] = MakeKeyHandler ( ViDeleteInnerSQuote , "ViDeleteInnerSQuote" ) ,
34+ [ Keys . W ] = MakeKeyHandler ( ViDeleteInnerWord , "ViDeleteInnerWord" ) ,
35+ }
36+ } ,
2837 } ;
2938
3039 private void ViChordDeleteTextObject ( ConsoleKeyInfo ? key = null , object arg = null )
@@ -75,8 +84,12 @@ private TextObjectSpan GetRequestedTextObjectSpan(ConsoleKeyInfo key)
7584
7685 private static void ViHandleTextObject ( ConsoleKeyInfo ? key = null , object arg = null )
7786 {
78- if ( ! _singleton . _textObjectHandlers . TryGetValue ( _singleton . _textObjectOperation , out var textObjectHandler ) ||
79- ! textObjectHandler . TryGetValue ( _singleton . _textObjectSpan , out var handler ) )
87+ System . Diagnostics . Debug . Assert ( key != null ) ;
88+ var keyInfo = PSKeyInfo . FromConsoleKeyInfo ( key . Value ) ;
89+
90+ if ( ! _singleton . _textObjectHandlers . TryGetValue ( _singleton . _textObjectOperation , out var textObjectSpanHandlers ) ||
91+ ! textObjectSpanHandlers . TryGetValue ( _singleton . _textObjectSpan , out var textObjectKeyHandlers ) ||
92+ ! textObjectKeyHandlers . TryGetValue ( keyInfo , out var handler ) )
8093 {
8194 ResetTextObjectState ( ) ;
8295 Ding ( ) ;
@@ -92,6 +105,77 @@ private static void ResetTextObjectState()
92105 _singleton . _textObjectSpan = TextObjectSpan . None ;
93106 }
94107
108+ private static void ViDeleteInnerSQuote ( ConsoleKeyInfo ? key = null , object arg = null )
109+ => ViDeleteInnerQuotes ( '\' ' , key , arg ) ;
110+ private static void ViDeleteInnerDQuote ( ConsoleKeyInfo ? key = null , object arg = null )
111+ => ViDeleteInnerQuotes ( '\" ' , key , arg ) ;
112+
113+ private static void ViDeleteInnerQuotes ( char delimiter , ConsoleKeyInfo ? key = null , object arg = null )
114+ {
115+ if ( ! TryGetArgAsInt ( arg , out var numericArg , 1 ) )
116+ {
117+ return ;
118+ }
119+
120+ if ( _singleton . _buffer . Length == 0 )
121+ {
122+ Ding ( ) ;
123+ return ;
124+ }
125+
126+ var start = _singleton . _buffer . ViFindBeginningOfQuotedTextObjectBoundary ( delimiter , _singleton . _current ) ;
127+ if ( start == - 1 )
128+ {
129+ Ding ( ) ;
130+ return ;
131+ }
132+
133+ // find the position of the ending delimiter by walking forward
134+ if ( start + 1 >= _singleton . _buffer . Length )
135+ {
136+ Ding ( ) ;
137+ return ;
138+ }
139+
140+ var end = - 1 ;
141+
142+ for ( var offset = start + 1 ; offset < _singleton . _buffer . Length ; offset ++ )
143+ {
144+ if ( _singleton . _buffer [ offset ] == delimiter )
145+ {
146+ end = offset ;
147+ break ;
148+ }
149+ if ( _singleton . _buffer [ offset ] == '\n ' )
150+ {
151+ break ;
152+ }
153+ }
154+
155+ if ( end == - 1 )
156+ {
157+ Ding ( ) ;
158+ return ;
159+ }
160+
161+ var position = start + 1 ;
162+
163+ // deleting multiple times
164+ // removes the surrounding quotes
165+
166+ if ( numericArg > 1 )
167+ {
168+ position = start ;
169+ // TODO:
170+ // make sure we can do that
171+ end = end + 1 ;
172+ }
173+
174+ _singleton . RemoveTextToViRegister ( position , end - position ) ;
175+ _singleton . AdjustCursorPosition ( position ) ;
176+ _singleton . Render ( ) ;
177+ }
178+
95179 private static void ViDeleteInnerWord ( ConsoleKeyInfo ? key = null , object arg = null )
96180 {
97181 var delimiters = _singleton . Options . WordDelimiters ;
0 commit comments