Skip to content

Commit 52e9083

Browse files
committed
Add brace matching support
1 parent 7d959a2 commit 52e9083

File tree

6 files changed

+91
-5
lines changed

6 files changed

+91
-5
lines changed

Changes.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### Version 1.0.0.4
2+
3+
New features:
4+
* Brace matching function added (GotoBrace)
5+
6+
Bugs fixed:
7+
* When showing possible completions, truncate at newline
8+
* Prompt before showing a large number of completions
9+
110
### Version 1.0.0.3
211

312
Bugs fixed:

PSReadLine/Cmdlets.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,11 @@ public class SetKeyHandlerCommand : PSCmdlet
395395
"BackwardDeleteLine", "BackwardKillLine", "BackwardWord", "BeginningOfHistory", "BeginningOfLine",
396396
"CancelLine", "Complete", "DeleteChar", "DisableDemoMode", "EmacsBackwardWord",
397397
"EmacsForwardWord", "EnableDemoMode", "EndOfHistory", "EndOfLine", "ExchangePointAndMark",
398-
"ForwardChar", "ForwardDeleteLine", "ForwardWord", "HistorySearchBackward", "HistorySearchForward",
399-
"KillBackwardWord", "KillLine", "KillWord", "NextHistory", "Paste",
400-
"PossibleCompletions", "PreviousHistory", "Redo", "RevertLine", "SetKeyHandler",
401-
"SetMark", "TabCompleteNext", "TabCompletePrevious", "Undo", "Yank",
402-
"YankPop")]
398+
"ForwardChar", "ForwardDeleteLine", "ForwardWord", "GotoBrace", "HistorySearchBackward",
399+
"HistorySearchForward", "KillBackwardWord", "KillLine", "KillWord", "NextHistory",
400+
"Paste", "PossibleCompletions", "PreviousHistory", "Redo", "RevertLine",
401+
"SetKeyHandler", "SetMark", "TabCompleteNext", "TabCompletePrevious", "Undo",
402+
"Yank", "YankPop")]
403403
[Parameter(Position = 1, Mandatory = true, ParameterSetName = "Function")]
404404
public string Function { get; set; }
405405

PSReadLine/Keys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public class Keys
116116
public static ConsoleKeyInfo CtrlSpace = new ConsoleKeyInfo(' ', ConsoleKey.Spacebar, false, false, true);
117117
public static ConsoleKeyInfo AltLess = new ConsoleKeyInfo('<', ConsoleKey.OemComma, true, true, false);
118118
public static ConsoleKeyInfo AltGreater = new ConsoleKeyInfo('>', ConsoleKey.OemPeriod, true, true, false);
119+
public static ConsoleKeyInfo CtrlRBracket = new ConsoleKeyInfo((char)29, ConsoleKey.Oem6, false, false, true);
119120

120121
public static ConsoleKeyInfo AltA = new ConsoleKeyInfo((char)97, ConsoleKey.A, false, true, false);
121122
public static ConsoleKeyInfo AltB = new ConsoleKeyInfo((char)98, ConsoleKey.B, false, true, false);

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,7 @@
252252
<data name="DisplayAllPossibilities" xml:space="preserve">
253253
<value>Display all {0} possibilities? (y or n)</value>
254254
</data>
255+
<data name="GotoBraceDescription" xml:space="preserve">
256+
<value>Go to matching brace</value>
257+
</data>
255258
</root>

PSReadLine/ReadLine.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ static PSConsoleReadLine()
370370
{ Keys.CtrlZ, MakeKeyHandler(Undo, "Undo") },
371371
{ Keys.CtrlEnd, MakeKeyHandler(ForwardDeleteLine, "ForwardDeleteLine") },
372372
{ Keys.CtrlHome, MakeKeyHandler(BackwardDeleteLine, "BackwardDeleteLine") },
373+
{ Keys.CtrlRBracket, MakeKeyHandler(GotoBrace, "GotoBrace") },
373374
};
374375

375376
_emacsKeyMap = new Dictionary<ConsoleKeyInfo, KeyHandler>(new ConsoleKeyInfoComparer())
@@ -774,6 +775,69 @@ public static void EmacsBackwardWord(ConsoleKeyInfo? key = null, object arg = nu
774775
_singleton.BackwardWord(EditMode.Emacs);
775776
}
776777

778+
/// <summary>
779+
/// Go to the matching brace, paren, or square bracket
780+
/// </summary>
781+
public static void GotoBrace(ConsoleKeyInfo? key = null, object arg = null)
782+
{
783+
if (_singleton._current >= _singleton._buffer.Length)
784+
{
785+
Ding();
786+
return;
787+
}
788+
789+
var charAtPoint = _singleton._buffer[_singleton._current];
790+
791+
Token token = null;
792+
var index = 0;
793+
for (; index < _singleton._tokens.Length; index++)
794+
{
795+
token = _singleton._tokens[index];
796+
if (token.Extent.StartOffset == _singleton._current)
797+
break;
798+
}
799+
800+
TokenKind toMatch;
801+
int direction;
802+
switch (token.Kind)
803+
{
804+
case TokenKind.LParen: toMatch = TokenKind.RParen; direction = 1; break;
805+
case TokenKind.LCurly: toMatch = TokenKind.RCurly; direction = 1; break;
806+
case TokenKind.LBracket: toMatch = TokenKind.RBracket; direction = 1; break;
807+
808+
case TokenKind.RParen: toMatch = TokenKind.LParen; direction = -1; break;
809+
case TokenKind.RCurly: toMatch = TokenKind.LCurly; direction = -1; break;
810+
case TokenKind.RBracket: toMatch = TokenKind.LBracket; direction = -1; break;
811+
812+
default:
813+
// Nothing to match (don't match inside strings/comments)
814+
Ding();
815+
return;
816+
}
817+
818+
var matchCount = 0;
819+
var limit = (direction > 0) ? _singleton._tokens.Length - 1 : -1;
820+
for (; index != limit; index += direction)
821+
{
822+
var t = _singleton._tokens[index];
823+
if (t.Kind == token.Kind)
824+
{
825+
matchCount++;
826+
}
827+
else if (t.Kind == toMatch)
828+
{
829+
matchCount--;
830+
if (matchCount == 0)
831+
{
832+
_singleton._current = t.Extent.StartOffset;
833+
_singleton.PlaceCursor();
834+
return;
835+
}
836+
}
837+
}
838+
Ding();
839+
}
840+
777841
#endregion Movement
778842

779843
#region History

0 commit comments

Comments
 (0)