Skip to content

Commit

Permalink
add git intellisense
Browse files Browse the repository at this point in the history
  • Loading branch information
yysun committed Feb 14, 2012
1 parent 611b65e commit ab53929
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 37 deletions.
15 changes: 9 additions & 6 deletions GitUI/GitViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,20 @@ private GitViewModel()
internal void Open(string directory)
{
workingDirectory = directory;
if (Directory.Exists(workingDirectory))
{
fileSystemWatcher = new FileSystemWatcher(workingDirectory);

fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Changed);
fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Changed);
tracker = new GitFileStatusTracker(directory);
if (tracker.HasGitRepository) directory = tracker.GitWorkingDirectory;

if (Directory.Exists(directory))
{
fileSystemWatcher = new FileSystemWatcher(directory);
fileSystemWatcher.IncludeSubdirectories = true;
//fileSystemWatcher.Created += new FileSystemEventHandler(fileSystemWatcher_Changed);
//fileSystemWatcher.Deleted += new FileSystemEventHandler(fileSystemWatcher_Changed);
//fileSystemWatcher.Renamed += new FileSystemEventHandler(fileSystemWatcher_Changed);
fileSystemWatcher.Changed += new FileSystemEventHandler(fileSystemWatcher_Changed);
fileSystemWatcher.EnableRaisingEvents = true;
}
tracker = new GitFileStatusTracker(directory);
}

internal static void OpenGitBash()
Expand Down
6 changes: 4 additions & 2 deletions GitUI/UI/GitConsole.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<RichTextBox HorizontalAlignment="Stretch" Name="richTextBox1" VerticalAlignment="Stretch"
PreviewKeyDown="richTextBox1_PreviewKeyDown" AcceptsReturn="False" AcceptsTab="False"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" GotFocus="richTextBox1_GotFocus">
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible">
<FlowDocument></FlowDocument>
</RichTextBox>
<ListBox Height="100" HorizontalAlignment="Left" Name="lstOptions" VerticalAlignment="Top" Width="120" Visibility="Collapsed"
MouseDoubleClick="lstOptions_MouseDoubleClick" PreviewKeyDown="lstOptions_PreviewKeyDown" />
</Grid>
</UserControl>
171 changes: 142 additions & 29 deletions GitUI/UI/GitConsole.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using GitScc.DataServices;

namespace GitUI.UI
{
Expand Down Expand Up @@ -36,20 +37,38 @@ public string WorkingDirectory
if (string.Compare(workingDirectory, value) != 0)
{
workingDirectory = value;
prompt = workingDirectory + ">";
prompt = string.Format("{0} ({1})\r\n>", workingDirectory,
GitViewModel.Current.Tracker.CurrentBranch);
this.richTextBox1.Document.Blocks.Clear();
WritePrompt();
}
}
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{

}

#region keydown event
private void richTextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
var command = new TextRange(richTextBox1.CaretPosition.GetLineStartPosition(0),
richTextBox1.CaretPosition).Text;
command = command.Substring(command.IndexOf(">") + 1).Trim();
ShowOptions(command);
return;
}
else if (e.Key == Key.Tab || e.Key == Key.Down)
{
if (lstOptions.Visibility == Visibility.Visible)
{
lstOptions.Focus();
return;
}
}
else
{
lstOptions.Visibility = Visibility.Collapsed;
}

if (e.Key == Key.Enter)
{
var command = new TextRange(richTextBox1.CaretPosition.GetLineStartPosition(0),
Expand Down Expand Up @@ -81,9 +100,13 @@ private void richTextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
else
{
var text = new TextRange(richTextBox1.CaretPosition, richTextBox1.CaretPosition.DocumentEnd).Text;
if (text.Contains(">")) e.Handled = true;
if (text.Contains(">")) //e.Handled = true;
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
}
}
}
#endregion

#region run command and command history

private void GetCommand(int idx)
{
Expand All @@ -97,30 +120,13 @@ private void GetCommand(int idx)
}
}

private void ChangePrompt(string command)
{
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
var range = this.richTextBox1.Selection;
range.Select(
richTextBox1.CaretPosition.GetLineStartPosition(0).GetPositionAtOffset(prompt.Length + 1, LogicalDirection.Forward),
richTextBox1.CaretPosition.GetLineStartPosition(1) ?? this.richTextBox1.CaretPosition.DocumentEnd);
range.Text = command;
this.richTextBox1.ScrollToEnd();
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
}

private void richTextBox1_GotFocus(object sender, RoutedEventArgs e)
{
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
}

private void RunCommand(string command)
{
var isGit = true;
command = command.Substring(command.IndexOf(">") + 1).Trim();

if (!string.IsNullOrWhiteSpace(command) &&
(commandHistory.Count==0 || commandHistory.Last()!=command))
if (!string.IsNullOrWhiteSpace(command) &&
(commandHistory.Count == 0 || commandHistory.Last() != command))
{
commandHistory.Add(command);
commandIdx = commandHistory.Count;
Expand All @@ -142,7 +148,7 @@ private void RunCommand(string command)
command = "/C " + command;
isGit = false;
}

ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = command;
startInfo.RedirectStandardError = true;
Expand Down Expand Up @@ -170,6 +176,18 @@ private void RunCommand(string command)
}
}

private void ChangePrompt(string command)
{
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
var range = this.richTextBox1.Selection;
range.Select(
richTextBox1.CaretPosition.GetLineStartPosition(0).GetPositionAtOffset(1, LogicalDirection.Forward),
richTextBox1.CaretPosition.GetLineStartPosition(1) ?? this.richTextBox1.CaretPosition.DocumentEnd);
range.Text = command;
this.richTextBox1.ScrollToEnd();
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
}

private void WritePrompt()
{
Paragraph para = new Paragraph();
Expand All @@ -178,7 +196,7 @@ private void WritePrompt()
para.LineHeight = 10;
para.Inlines.Add(new Run(prompt));
this.richTextBox1.Document.Blocks.Add(para);

this.richTextBox1.ScrollToEnd();
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
}
Expand All @@ -205,5 +223,100 @@ private bool ProcessInternalCommand(string command)
}
return false;
}
#endregion

#region intellisense

private void ShowOptions(string command)
{
var options = GetOptions(command);
if (options != null && options.Any())
{
Rect rect = this.richTextBox1.CaretPosition.GetCharacterRect(LogicalDirection.Forward);
double d = this.ActualHeight - (rect.Y + lstOptions.Height + 12);
double left = rect.X + 6;
double top = d > 0 ? rect.Y + 12 : rect.Y - lstOptions.Height;
left += this.Padding.Left;
top += this.Padding.Top;
lstOptions.SetCurrentValue(ListBox.MarginProperty, new Thickness(left, top, 0, 0));
lstOptions.ItemsSource = options;
lstOptions.Visibility = Visibility.Visible;
}
}

private void lstOptions_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
InsertText(lstOptions.SelectedValue as string);
}

private void lstOptions_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter || e.Key == Key.Tab || e.Key == Key.Space)
{
InsertText(lstOptions.SelectedValue as string);
e.Handled = true;
}
else if (e.Key == Key.Back || e.Key == Key.Escape)
{
this.richTextBox1.Focus();
this.lstOptions.Visibility = Visibility.Collapsed;
e.Handled = true;
}
}

private void InsertText(string text)
{
this.richTextBox1.Focus();
this.richTextBox1.CaretPosition.InsertTextInRun(text);
this.richTextBox1.CaretPosition = this.richTextBox1.CaretPosition.DocumentEnd;
this.lstOptions.Visibility = Visibility.Collapsed;
}

#endregion

#region git command intellisense
private IEnumerable<string> GetOptions(string command)
{
switch (command)
{
case "git":
return new string[] {
"add", "bisect", "branch", "checkout", "clone",
"commit", "diff", "fetch", "grep", "init",
"log", "merge", "mv", "pull", "push", "rebase",
"reset", "rm", "show", "status", "tag"
};

case "git checkout":
if (GitViewModel.Current.Tracker.HasGitRepository)
{
return GitViewModel.Current.Tracker.RepositoryGraph.Refs
.Where(r => r.Type == RefTypes.Branch)
.Select(r => r.Name);
}
break;

case "git branch -D":
case "git branch -d":
if (GitViewModel.Current.Tracker.HasGitRepository)
{
return GitViewModel.Current.Tracker.RepositoryGraph.Refs
.Where(r => r.Type == RefTypes.Branch)
.Select(r => r.Name);
}
break;

case "git tag -d":
if (GitViewModel.Current.Tracker.HasGitRepository)
{
return GitViewModel.Current.Tracker.RepositoryGraph.Refs
.Where(r => r.Type == RefTypes.Tag)
.Select(r => r.Name);
}
break;
}
return new string[] { };
}
#endregion
}
}

0 comments on commit ab53929

Please sign in to comment.